You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by fm...@apache.org on 2007/11/08 00:34:29 UTC

svn commit: r592955 - in /incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource: SyntheticResource.java internal/helper/JcrNodeResource.java

Author: fmeschbe
Date: Wed Nov  7 15:34:28 2007
New Revision: 592955

URL: http://svn.apache.org/viewvc?rev=592955&view=rev
Log:
SLING-99 Implement Resource.getInputStream method and try to set the
streaming resource properties for nt:file nodes

Modified:
    incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/SyntheticResource.java
    incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/JcrNodeResource.java

Modified: incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/SyntheticResource.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/SyntheticResource.java?rev=592955&r1=592954&r2=592955&view=diff
==============================================================================
--- incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/SyntheticResource.java (original)
+++ incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/SyntheticResource.java Wed Nov  7 15:34:28 2007
@@ -18,6 +18,8 @@
  */
 package org.apache.sling.jcr.resource;
 
+import java.io.InputStream;
+
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceMetadata;
 
@@ -88,4 +90,7 @@
         return object;
     }
 
+    public InputStream getInputStream() {
+        return null;
+    }
 }

Modified: incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/JcrNodeResource.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/JcrNodeResource.java?rev=592955&r1=592954&r2=592955&view=diff
==============================================================================
--- incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/JcrNodeResource.java (original)
+++ incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/JcrNodeResource.java Wed Nov  7 15:34:28 2007
@@ -16,8 +16,22 @@
  */
 package org.apache.sling.jcr.resource.internal.helper;
 
+import static org.apache.jackrabbit.JcrConstants.JCR_CONTENT;
+import static org.apache.jackrabbit.JcrConstants.JCR_CREATED;
+import static org.apache.jackrabbit.JcrConstants.JCR_DATA;
+import static org.apache.jackrabbit.JcrConstants.JCR_ENCODING;
+import static org.apache.jackrabbit.JcrConstants.JCR_LASTMODIFIED;
+import static org.apache.jackrabbit.JcrConstants.JCR_MIMETYPE;
+import static org.apache.jackrabbit.JcrConstants.NT_FILE;
+import static org.apache.sling.api.resource.ResourceMetadata.CHARACTER_ENCODING;
+import static org.apache.sling.api.resource.ResourceMetadata.CONTENT_TYPE;
+import static org.apache.sling.api.resource.ResourceMetadata.CREATION_TIME;
+import static org.apache.sling.api.resource.ResourceMetadata.MODIFICATION_TIME;
 import static org.apache.sling.jcr.resource.JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY;
 
+import java.io.IOException;
+import java.io.InputStream;
+
 import javax.jcr.Node;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
@@ -31,6 +45,9 @@
 
     private static final Object UNDEFINED = "undefined";
 
+    /** The relative path name of the data property of an nt:file node */
+    private static final String FILE_DATA_PROP = JCR_CONTENT + "/" + JCR_DATA;
+
     private final JcrResourceManager cMgr;
 
     private final Node node;
@@ -52,6 +69,9 @@
         this.path = node.getPath();
         metadata = new ResourceMetadata();
         resourceType = getResourceTypeForNode(node);
+
+        // check for nt:file metadata
+        setMetaData(node, metadata);
     }
 
     public JcrNodeResource(JcrResourceManager cMgr, Session s, String path,
@@ -62,6 +82,9 @@
         metadata = new ResourceMetadata();
         resourceType = getResourceTypeForNode(node);
         objectType = type;
+
+        // check for nt:file metadata
+        setMetaData(node, metadata);
     }
 
     public JcrNodeResource(JcrResourceManager cMgr, Node node)
@@ -72,6 +95,9 @@
         metadata = new ResourceMetadata();
         metadata.put(ResourceMetadata.RESOLUTION_PATH, path);
         resourceType = getResourceTypeForNode(node);
+
+        // check for nt:file metadata
+        setMetaData(node, metadata);
     }
 
     public String toString() {
@@ -104,6 +130,31 @@
     }
 
     /**
+     * Returns a stream to the <em>jcr:content/jcr:data</em> property if the
+     * {@link #getRawData() raw data} is an <em>nt:file</em> node. Otherwise
+     * returns <code>null</code>.
+     */
+    public InputStream getInputStream() throws IOException {
+        // implement this for nt:file only
+        if (!(getRawData() instanceof Node)) {
+            return null;
+        }
+
+        try {
+            Node node = (Node) getRawData();
+            if (node.isNodeType(NT_FILE) && node.hasProperty(FILE_DATA_PROP)) {
+                return node.getProperty(FILE_DATA_PROP).getStream();
+            }
+        } catch (RepositoryException re) {
+            throw (IOException) new IOException("Cannot get InputStream for "
+                + getURI()).initCause(re);
+        }
+
+        // fallback to non-streamable resource
+        return null;
+    }
+
+    /**
      * Compute the resource type of the given node, using either the
      * SLING_RESOURCE_TYPE_PROPERTY, or the node's primary type, if the property
      * is not set
@@ -122,4 +173,34 @@
 
         return result;
     }
+
+    private static void setMetaData(Node node, ResourceMetadata metadata) {
+        try {
+            if (node.isNodeType(NT_FILE)) {
+                metadata.put(CREATION_TIME,
+                    node.getProperty(JCR_CREATED).getLong());
+
+                if (node.hasNode(JCR_CONTENT)) {
+                    Node content = node.getNode(JCR_CONTENT);
+                    if (content.hasProperty(JCR_MIMETYPE)) {
+                        metadata.put(CONTENT_TYPE, content.getProperty(
+                            JCR_MIMETYPE).getString());
+                    }
+
+                    if (content.hasProperty(JCR_ENCODING)) {
+                        metadata.put(CHARACTER_ENCODING, content.getProperty(
+                            JCR_ENCODING).getString());
+                    }
+
+                    if (content.hasProperty(JCR_LASTMODIFIED)) {
+                        metadata.put(MODIFICATION_TIME, content.getProperty(
+                            JCR_LASTMODIFIED).getLong());
+                    }
+                }
+            }
+        } catch (RepositoryException re) {
+            // TODO: should log
+        }
+    }
+
 }