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 2008/02/28 14:32:29 UTC

svn commit: r631967 - in /incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper: bundle/BundleResource.java jcr/JcrNodeResource.java jcr/JcrPropertyResource.java

Author: fmeschbe
Date: Thu Feb 28 05:32:26 2008
New Revision: 631967

URL: http://svn.apache.org/viewvc?rev=631967&view=rev
Log:
SLING-288 Add support for the sling.contentLenght property

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

Modified: incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/bundle/BundleResource.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/bundle/BundleResource.java?rev=631967&r1=631966&r2=631967&view=diff
==============================================================================
--- incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/bundle/BundleResource.java (original)
+++ incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/bundle/BundleResource.java Thu Feb 28 05:32:26 2008
@@ -83,15 +83,29 @@
     public BundleResource(ResourceResolver resourceResolver, Bundle bundle, String path) {
         this.resourceResolver = resourceResolver;
         this.bundle = bundle;
-        this.path = path.endsWith("/")
-                ? path.substring(0, path.length() - 1)
-                : path;
-        this.resourceType = path.endsWith("/") ? NT_FOLDER : NT_FILE;
 
         metadata = new ResourceMetadata();
         metadata.setResolutionPath(path);
         metadata.setCreationTime(bundle.getLastModified());
         metadata.setModificationTime(bundle.getLastModified());
+
+        if (path.endsWith("/")) {
+            
+            this.path = path.substring(0, path.length() - 1);
+            this.resourceType = NT_FOLDER;
+            
+        } else {
+            
+            this.path = path;
+            this.resourceType = NT_FILE;
+
+            try {
+                URL url = bundle.getEntry(path);
+                metadata.setContentLength(url.openConnection().getContentLength());
+            } catch (Exception e) {
+                // don't care, we just have no content length
+            }
+        }
     }
 
     public String getPath() {

Modified: incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResource.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResource.java?rev=631967&r1=631966&r2=631967&view=diff
==============================================================================
--- incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResource.java (original)
+++ incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResource.java Thu Feb 28 05:32:26 2008
@@ -98,9 +98,11 @@
     }
 
     /**
-     * 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>.
+     * Returns a stream to the <em>jcr:data</em> property if the
+     * {@link #getNode() node} is an <em>nt:file</em> or <em>nt:resource</em>
+     * node. Otherwise returns <code>null</code>.
+     * If a valid stream can be returned, this method also sets the
+     * content length resource metadata.
      */
     private InputStream getInputStream() {
         // implement this for nt:file only
@@ -112,22 +114,39 @@
                         ? node.getNode(JCR_CONTENT)
                         : node;
 
+                Property data;
+                
                 // if the node has a jcr:data property, use that property
                 if (content.hasProperty(JCR_DATA)) {
-                    return content.getProperty(JCR_DATA).getStream();
-                }
+                    data = content.getProperty(JCR_DATA);
+
+                } else {
 
-                // otherwise try to follow default item trail
-                try {
-                    Item item = content.getPrimaryItem();
-                    while (item.isNode()) {
-                        item = ((Node) item).getPrimaryItem();
+                    // otherwise try to follow default item trail
+                    try {
+                        Item item = content.getPrimaryItem();
+                        while (item.isNode()) {
+                            item = ((Node) item).getPrimaryItem();
+                        }
+                        data = ((Property) item);
+                    } catch (ItemNotFoundException infe) {
+                        // we don't actually care, but log for completeness
+                        log.debug("getInputStream: No primary items for "
+                            + toString(), infe);
+                        data = null;
                     }
-                    return ((Property) item).getStream();
-                } catch (ItemNotFoundException infe) {
-                    // we don't actually care, but log for completeness
-                    log.debug("getInputStream: No primary items for "
-                        + toString(), infe);
+                }
+
+                if (data != null) {
+                    // we set the content length only if the input stream is
+                    // fetched. otherwise the repository needs to load the
+                    // binary property which could cause performance loss
+                    // for all resources that do need to provide the stream
+                    long length = data.getLength();
+                    InputStream stream =  data.getStream();
+                    
+                    getResourceMetadata().setContentLength(length);
+                    return stream;
                 }
 
             } catch (RepositoryException re) {

Modified: incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrPropertyResource.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrPropertyResource.java?rev=631967&r1=631966&r2=631967&view=diff
==============================================================================
--- incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrPropertyResource.java (original)
+++ incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrPropertyResource.java Thu Feb 28 05:32:26 2008
@@ -18,6 +18,7 @@
  */
 package org.apache.sling.jcr.resource.internal.helper.jcr;
 
+import java.io.InputStream;
 import java.util.Calendar;
 
 import javax.jcr.Item;
@@ -27,12 +28,15 @@
 import javax.jcr.Value;
 import javax.jcr.ValueFormatException;
 
-import org.apache.sling.api.resource.Resource;
-import org.apache.sling.api.resource.ResourceMetadata;
 import org.apache.sling.api.resource.ResourceResolver;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class JcrPropertyResource extends JcrItemResource {
 
+    /** default log */
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    
     private final Property property;
 
     private final String resourceType;
@@ -73,23 +77,48 @@
                 return (AdapterType) getProperty().getValue();
             } else if (type == Node.class) {
                 return (AdapterType) getProperty().getNode();
+            } else if (type == InputStream.class) {
+                return (AdapterType) getInputStream();
             }
         } catch (ValueFormatException vfe) {
-            // TODO: log
+            log.info("adaptTo: Problem accessing the property value of "
+                + getPath(), vfe);
         } catch (RepositoryException re) {
-            // TODO: log
+            log.info("adaptTo: Problem accessing the property " + getPath(), re);
         }
 
         // try to use adapter factories
         return super.adaptTo(type);
     }
 
-    public Property getProperty() {
-        return property;
-    }
-
     public String toString() {
         return getClass().getSimpleName() + ", type=" + getResourceType()
             + ", path=" + getPath();
+    }
+
+    private Property getProperty() {
+        return property;
+    }
+
+    private InputStream getInputStream() {
+        Property prop = getProperty();
+        
+        try {
+            // we set the content length only if the input stream is
+            // fetched. otherwise the repository needs to load the
+            // binary property which could cause performance loss
+            // for all resources that do need to provide the stream
+            long length = prop.getLength();
+            InputStream stream =  prop.getStream();
+            
+            getResourceMetadata().setContentLength(length);
+            return stream;
+        } catch (RepositoryException re) {
+            log.error("getInputStream: Problem accessing the property "
+                + getPath() + " stream", re);
+        }
+        
+        // fall back to none in case of an error
+        return null;
     }
 }