You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:47:19 UTC

[sling-org-apache-sling-jcr-contentloader] 16/36: SLING-1165 Last modifie date reported by the filesystem is now used, instead of current date

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.jcr.contentloader-2.0.6
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-jcr-contentloader.git

commit a3acd00a8e3d30b9c4a5a0f2cdd44f40b5bb3f11
Author: Vidar Skauge Ramdal <vr...@apache.org>
AuthorDate: Wed Oct 28 10:40:23 2009 +0000

    SLING-1165 Last modifie date reported by the filesystem is now used, instead of current date
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/jcr/contentloader@830495 13f79535-47bb-0310-9956-ffa450edef68
---
 .../contentloader/internal/readers/XmlReader.java    | 15 +++++++++++++--
 .../internal/readers/XmlReaderTest.java              | 12 ++++++++++++
 src/test/resources/reader/datefallbacksample.xml     | 20 ++++++++++++++++++++
 3 files changed, 45 insertions(+), 2 deletions(-)

diff --git a/src/main/java/org/apache/sling/jcr/contentloader/internal/readers/XmlReader.java b/src/main/java/org/apache/sling/jcr/contentloader/internal/readers/XmlReader.java
index a4a525a..3c28ef3 100644
--- a/src/main/java/org/apache/sling/jcr/contentloader/internal/readers/XmlReader.java
+++ b/src/main/java/org/apache/sling/jcr/contentloader/internal/readers/XmlReader.java
@@ -24,8 +24,10 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.PipedInputStream;
 import java.io.PipedOutputStream;
+import java.io.File;
 import java.net.URL;
 import java.net.MalformedURLException;
+import java.net.URISyntaxException;
 import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.HashMap;
@@ -498,7 +500,8 @@ public class XmlReader implements ContentReader {
      * &lt;nt:file src="../../image.png" mimeType="image/png" lastModified="1977-06-01T07:00:00+0100" /&gt;
      * </pre>
      * The date format for <code>lastModified</code> is <code>yyyy-MM-dd'T'HH:mm:ssZ</code>.
-     * The <code>lastModified</code> attribute is optional. If missing, it will be set to the current time.
+     * The <code>lastModified</code> attribute is optional. If missing, the last modified date reported by the
+     * filesystem will be used.
      */
     protected static final class FileDescription {
 
@@ -535,7 +538,15 @@ public class XmlReader implements ContentReader {
             String[] parts = url.getPath().split("/");
             String name = parts[parts.length - 1];
             InputStream stream = url.openStream();
-            creator.createFileAndResourceNode(name, stream, mimeType, lastModified != null ? lastModified : Calendar.getInstance().getTimeInMillis());
+            if (lastModified == null) {
+                try {
+                    lastModified = new File(url.toURI()).lastModified();
+                } catch (Throwable ignore) {
+                    // Could not get lastModified from file system, so we'll use current date
+                    lastModified = Calendar.getInstance().getTimeInMillis();
+                }
+            }
+            creator.createFileAndResourceNode(name, stream, mimeType, lastModified);
             closeStream(stream);
             creator.finishNode();
             creator.finishNode();
diff --git a/src/test/java/org/apache/sling/jcr/contentloader/internal/readers/XmlReaderTest.java b/src/test/java/org/apache/sling/jcr/contentloader/internal/readers/XmlReaderTest.java
index 2bc5ab2..2ef7482 100644
--- a/src/test/java/org/apache/sling/jcr/contentloader/internal/readers/XmlReaderTest.java
+++ b/src/test/java/org/apache/sling/jcr/contentloader/internal/readers/XmlReaderTest.java
@@ -69,6 +69,18 @@ public class XmlReaderTest extends TestCase {
 
     }
 
+    public void testUseOSLastModified() throws RepositoryException, IOException {
+        File input = new File("src/test/resources/reader/datefallbacksample.xml");
+        final URL testdata = input.toURI().toURL();
+        reader.parse(testdata, creator);
+        File file = new File("src/test/resources/reader/testfile.txt");
+        long originalLastModified = file.lastModified();
+        assertEquals("Did not create expected number of files", 1, creator.filesCreated.size());
+        MockContentCreator.FileDescription fileDescription = creator.filesCreated.get(0);
+        assertEquals("Did not pick up last modified date from file", originalLastModified, fileDescription.lastModified);
+
+    }
+
     protected void setUp() throws Exception {
         super.setUp();
         reader = new XmlReader();
diff --git a/src/test/resources/reader/datefallbacksample.xml b/src/test/resources/reader/datefallbacksample.xml
new file mode 100644
index 0000000..3784a0e
--- /dev/null
+++ b/src/test/resources/reader/datefallbacksample.xml
@@ -0,0 +1,20 @@
+<node xmlns:nt="http://www.jcp.org/jcr/nt/1.0">
+    <name>nodeName</name>
+    <primaryNodeType>type</primaryNodeType>
+    <mixinNodeTypes>
+        <mixinNodeType>mixtype1</mixinNodeType>
+        <mixinNodeType>mixtype2</mixinNodeType>
+    </mixinNodeTypes>
+    <properties>
+        <property>
+            <name>propName</name>
+            <value>propValue</value>
+            <type>String</type>
+        </property>
+        <!-- more properties -->
+    </properties>
+    <nodes>
+        <!-- child nodes -->
+        <nt:file src="testfile.txt" mimeType="application/test"/>
+    </nodes>
+</node>

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.