Commits

petermr  committed 143c57c

fixed updown

  • Participants
  • Parent commits 9d59d1d

Comments (0)

Files changed (3)

       <version>0.1-SNAPSHOT</version>
     </dependency>
       <dependency>
+            <groupId>joda-time</groupId>
+            <artifactId>joda-time</artifactId>
+            <version>1.6</version>
+      </dependency>   
+      <dependency>
             <groupId>org.restlet.jee</groupId>
             <artifactId>org.restlet</artifactId>
             <version>${restlet.version}</version>

File src/main/java/org/xmlcml/cml/rest/UpDownLoader.java

 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Collections;
-import java.util.Date;
 import java.util.List;
 import java.util.Map;
 
 
 import org.apache.commons.io.FileUtils;
 import org.apache.log4j.Logger;
+import org.joda.time.DateTime;
 import org.restlet.Request;
 import org.restlet.Response;
 import org.restlet.data.Form;
 import org.xmlcml.cml.html.HtmlTable;
 import org.xmlcml.cml.html.HtmlTd;
 import org.xmlcml.cml.html.HtmlTr;
+import org.xmlcml.euclid.JodaDate;
 import org.xmlcml.euclid.Util;
 
 public class UpDownLoader {
 	private String lastHeaderLastModified;
 	private String lastHeaderContentType;
 	private String lastHeaderContentLength;
-	private Date lastHeaderResponseDate;
+	private DateTime lastHeaderResponseDate;
 
 	public UpDownLoader() {
 		setBaseUrl(HTTP_GREENCHAIN_CH_CAM_AC_UK_QUIXOTE);
 			org.restlet.Client restletClient = new org.restlet.Client(Protocol.HTTP);
 			Request request = new Request(Method.HEAD, url);
 			lastHeaderResponse = restletClient.handle(request);
-			lastHeaderResponseDate = lastHeaderResponse.getDate();
+			lastHeaderResponseDate = JodaDate.parseJavaDate(lastHeaderResponse.getDate());
 			lastHeaderAttributes = lastHeaderResponse.getAttributes();
 			lastHeaderForm = (lastHeaderAttributes.keySet().size() == 1) ?
 					(Form) lastHeaderAttributes.get(lastHeaderAttributes.keySet().iterator().next()) : null;
 		return lastHeaderContentLength;
 	}
 
-	public Date getDate() {
+	public DateTime getDate() {
 		return lastHeaderResponseDate;
 	}
 
-	public Date getLastModifiedDate() {
-		return (lastHeaderLastModified == null) ? null : new Date(lastHeaderLastModified);
+	public DateTime getLastModifiedDate() {
+		return (lastHeaderLastModified == null) ? null : JodaDate.parseDate(lastHeaderLastModified);
 	}
 
 	public Map<String, Object> getLastHeaderAttributes() {
 		System.out.println(" args (mandatory): [-download|upload] -url <url> -dir <dir>");
 	}
 
+	public static boolean isFileNewer(File file, DateTime now) {
+		return FileUtils.isFileNewer(file, now.getMillis()-1000);
+	}
+
 }

File src/test/java/org/xmlcml/cml/rest/UpDownLoaderTest.java

 
 
 import java.io.File;
-
-import java.util.Date;
 import java.util.List;
 
 import junit.framework.Assert;
 
 import org.apache.commons.io.FileUtils;
-import org.junit.Ignore;
+import org.joda.time.DateTime;
 import org.junit.Test;
 import org.restlet.Response;
 import org.xmlcml.cml.rest.UpDownLoader.DirectoryType;
 
 	@Test
 	public void testDownloadFile() throws Exception {
-		Date now = new Date();
+		DateTime now = new DateTime();
 		UpDownLoader downloader = new UpDownLoader();
 		String topUrl = "http://greenchain.ch.cam.ac.uk/patents/quixote/pablo1/aproxcall.log";
 		String topDirName = "target/quixote/files/";
 		Assert.assertEquals("name", new File(new File(topDirName), "aproxcall.log").getAbsolutePath(), file.getAbsolutePath());
 		Assert.assertTrue("exists", file.exists());
 		Assert.assertEquals("size", 545405, FileUtils.readFileToByteArray(file).length);
-		Assert.assertTrue("is newer than "+now, FileUtils.isFileNewer(file, now));
+		Assert.assertTrue("is newer than "+now, UpDownLoader.isFileNewer(file, now));
 	}
 
 	@Test
 	public void testDownloadFile1() throws Exception {
-		Date now = new Date();
+		DateTime now = new DateTime();
 		UpDownLoader downloader = new UpDownLoader();
 		String topUrl = "http://greenchain.ch.cam.ac.uk/patents/quixote/pablo1/aproxcall.log";
 		File topDir = new File("target/quixote/files");
 		downloader.downloadFile(topUrl, file);
 		Assert.assertTrue("exists", file.exists());
 		Assert.assertEquals("size", 545405, FileUtils.readFileToByteArray(file).length);
-		Assert.assertTrue("is newer than "+now, FileUtils.isFileNewer(file, now));
+		Assert.assertTrue("is newer than "+now, UpDownLoader.isFileNewer(file, now));
 	}
 
 	@Test
 	public void testDownloadDirectory() throws Exception {
-		Date now = new Date();
+		DateTime now = new DateTime();
 		UpDownLoader downloader = new UpDownLoader();
 		String topUrl = "http://greenchain.ch.cam.ac.uk/patents/quixote/pablo1/";
 		File topDir = new File("target/quixote");
 		Assert.assertTrue("exists", file0.exists());
 		Assert.assertEquals("file name", "aproxcall.log", file0.getName());
 		Assert.assertEquals("file size", 545405, FileUtils.readFileToByteArray(file0).length);
-		Assert.assertTrue("is newer than "+now, FileUtils.isFileNewer(file0, now));
+		Assert.assertTrue("is newer than "+now, UpDownLoader.isFileNewer(file0, now));
 	}
 
 	@Test
 	public void testDownloadDirectoryChunks() throws Exception {
-		Date now = new Date();
+		DateTime now = new DateTime();
 		UpDownLoader downloader = new UpDownLoader();
 		String topUrl = "http://greenchain.ch.cam.ac.uk/patents/quixote/pablo1/";
 		File topDir = new File("target/quixote/chunks");
 		Assert.assertTrue("exists", file0.exists());
 		Assert.assertEquals("file name", "aproxcall.log", file0.getName());
 		Assert.assertEquals("file size", 999, FileUtils.readFileToByteArray(file0).length);
-		Assert.assertTrue("is newer than "+now, FileUtils.isFileNewer(file0, now));
+		Assert.assertTrue("is newer than "+now, UpDownLoader.isFileNewer(file0, now));
 	}
 
 	@Test
 	
 	@Test
 	public void testDownloadNonRestDirectoryChunks() throws Exception {
-		Date now = new Date();
+		DateTime now = new DateTime();
 		UpDownLoader downloader = new UpDownLoader();
 		String topUrl = "http://neptuno.unizar.es/files/public/datasets/isaias_gaussian/";
 		File topDir = new File("target/quixote/nonrestchunks");
 		Assert.assertTrue("exists", file0.exists());
 		Assert.assertEquals("file name", "aproxcall.log", file0.getName());
 		Assert.assertEquals("file size", 999, FileUtils.readFileToByteArray(file0).length);
-		Assert.assertTrue("is newer than "+now, FileUtils.isFileNewer(file0, now));
+		Assert.assertTrue("is newer than "+now, UpDownLoader.isFileNewer(file0, now));
 	}
 
 }