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:31:36 UTC

svn commit: r592952 - /incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/resource/JcrNodeResource.java

Author: fmeschbe
Date: Wed Nov  7 15:31:33 2007
New Revision: 592952

URL: http://svn.apache.org/viewvc?rev=592952&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/microsling/microsling-core/src/main/java/org/apache/sling/microsling/resource/JcrNodeResource.java

Modified: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/resource/JcrNodeResource.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/resource/JcrNodeResource.java?rev=592952&r1=592951&r2=592952&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/resource/JcrNodeResource.java (original)
+++ incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/resource/JcrNodeResource.java Wed Nov  7 15:31:33 2007
@@ -17,6 +17,22 @@
 package org.apache.sling.microsling.resource;
 
 
+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.api.resource.ResourceMetadata.RESOLUTION_PATH;
+
+import java.io.IOException;
+import java.io.InputStream;
+
 import javax.jcr.Node;
 import javax.jcr.RepositoryException;
 
@@ -36,19 +52,28 @@
      */
     public static final String SLING_RESOURCE_TYPE_PROPERTY = "slingResourceType";
 
+    /** The relative path name of the data property of an nt:file node */
+    private static final String FILE_DATA_PROP = JCR_CONTENT + "/" + JCR_DATA;
+
     JcrNodeResource(javax.jcr.Session s,String path) throws RepositoryException {
         node = (Node)s.getItem(path);
         this.path = node.getPath();
         metadata = new ResourceMetadata();
         resourceType = getResourceTypeForNode(node);
+
+        // check for nt:file metadata
+        setMetaData(node, metadata);
     }
 
     public JcrNodeResource(Node node) throws RepositoryException {
         this.node = node;
         this.path = node.getPath();
         metadata = new ResourceMetadata();
-        metadata.put(ResourceMetadata.RESOLUTION_PATH, path);
+        metadata.put(RESOLUTION_PATH, path);
         resourceType = getResourceTypeForNode(node);
+
+        // check for nt:file metadata
+        setMetaData(node, metadata);
     }
 
     public void setObject(Object object) {
@@ -80,6 +105,31 @@
         return metadata;
     }
 
+    /**
+     * 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
      */
@@ -95,5 +145,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
+        }
     }
 }