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:58:44 UTC

[sling-org-apache-sling-resourcemerger] 04/24: SLING-2986 : Fix path handling, especially when the root node is requested

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

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

commit e9da669a50e98453adebce1484c3134972b815d9
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Wed Jan 29 14:28:08 2014 +0000

    SLING-2986 : Fix path handling, especially when the root node is requested
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/contrib/extensions/resourcemerger@1562454 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            | 21 ------------
 .../sling/resourcemerger/impl/MergedResource.java  | 37 ++++++++--------------
 .../impl/MergedResourceProvider.java               | 37 ++++++++++++++--------
 .../impl/MergedResourceProviderFactory.java        |  4 ++-
 4 files changed, 40 insertions(+), 59 deletions(-)

diff --git a/pom.xml b/pom.xml
index 6985712..aeceef7 100644
--- a/pom.xml
+++ b/pom.xml
@@ -83,26 +83,5 @@
             <version>2.2.0</version>
             <scope>provided</scope>
         </dependency>
-
-        <dependency>
-            <groupId>org.apache.commons</groupId>
-            <artifactId>commons-lang3</artifactId>
-            <version>3.0.1</version>
-            <scope>provided</scope>
-        </dependency>
-
-        <!-- Test dependencies -->
-        <dependency>
-            <groupId>org.apache.sling</groupId>
-            <artifactId>org.apache.sling.commons.testing</artifactId>
-            <version>2.0.14</version>
-            <scope>test</scope>
-        </dependency>
-        <dependency>
-            <groupId>javax.jcr</groupId>
-            <artifactId>jcr</artifactId>
-            <version>2.0</version>
-            <scope>test</scope>
-        </dependency>
     </dependencies>
 </project>
diff --git a/src/main/java/org/apache/sling/resourcemerger/impl/MergedResource.java b/src/main/java/org/apache/sling/resourcemerger/impl/MergedResource.java
index b2b69cd..a250e63 100644
--- a/src/main/java/org/apache/sling/resourcemerger/impl/MergedResource.java
+++ b/src/main/java/org/apache/sling/resourcemerger/impl/MergedResource.java
@@ -19,14 +19,12 @@
 package org.apache.sling.resourcemerger.impl;
 
 import java.util.ArrayList;
-import java.util.Iterator;
 import java.util.List;
 
 import org.apache.sling.api.resource.AbstractResource;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceMetadata;
 import org.apache.sling.api.resource.ResourceResolver;
-import org.apache.sling.api.resource.ResourceUtil;
 import org.apache.sling.api.resource.ValueMap;
 
 /**
@@ -35,10 +33,12 @@ import org.apache.sling.api.resource.ValueMap;
 public class MergedResource extends AbstractResource {
 
     private final ResourceResolver resolver;
-    private final String mergeRootPath;
+    private final String path;
     private final String relativePath;
     private final List<String> mappedResources = new ArrayList<String>();
 
+    private final ResourceMetadata metadata = new ResourceMetadata();
+
     /**
      * Constructor
      *
@@ -49,9 +49,7 @@ public class MergedResource extends AbstractResource {
     MergedResource(final ResourceResolver resolver,
                    final String mergeRootPath,
                    final String relativePath) {
-        this.resolver = resolver;
-        this.mergeRootPath = mergeRootPath;
-        this.relativePath = relativePath;
+        this(resolver, mergeRootPath, relativePath, null);
     }
 
     /**
@@ -67,9 +65,14 @@ public class MergedResource extends AbstractResource {
                    final String relativePath,
                    final List<String> mappedResources) {
         this.resolver = resolver;
-        this.mergeRootPath = mergeRootPath;
-        this.relativePath = relativePath;
-        this.mappedResources.addAll(mappedResources);
+        this.path = (relativePath.length() == 0 ? mergeRootPath : mergeRootPath + "/" + relativePath);
+        this.relativePath = (relativePath.length() == 0 ? "/" : relativePath);
+        if ( mappedResources != null ) {
+            this.mappedResources.addAll(mappedResources);
+        }
+        metadata.put(ResourceMetadata.RESOLUTION_PATH, getPath());
+        metadata.put("sling.mergedResource", true);
+        metadata.put("sling.mappedResources", mappedResources.toArray(new String[mappedResources.size()]));
     }
 
 
@@ -100,22 +103,14 @@ public class MergedResource extends AbstractResource {
      * {@inheritDoc}
      */
     public String getPath() {
-        return ResourceUtil.normalize(mergeRootPath + "/" + relativePath);
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public Iterator<Resource> listChildren() {
-        return resolver.listChildren(this);
+        return this.path;
     }
 
     /**
      * {@inheritDoc}
      */
     public String getResourceType() {
-        return relativePath;
+        return this.relativePath;
     }
 
     /**
@@ -130,10 +125,6 @@ public class MergedResource extends AbstractResource {
      * {@inheritDoc}
      */
     public ResourceMetadata getResourceMetadata() {
-        ResourceMetadata metadata = new ResourceMetadata();
-        metadata.put(ResourceMetadata.RESOLUTION_PATH, getPath());
-        metadata.put("sling.mergedResource", true);
-        metadata.put("sling.mappedResources", mappedResources.toArray(new String[mappedResources.size()]));
         return metadata;
     }
 
diff --git a/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourceProvider.java b/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourceProvider.java
index 8a57510..f63e9dc 100644
--- a/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourceProvider.java
+++ b/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourceProvider.java
@@ -25,7 +25,6 @@ import java.util.List;
 
 import javax.servlet.http.HttpServletRequest;
 
-import org.apache.commons.lang3.StringUtils;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceProvider;
 import org.apache.sling.api.resource.ResourceResolver;
@@ -55,24 +54,26 @@ public class MergedResourceProvider implements ResourceProvider {
      * {@inheritDoc}
      */
     public Resource getResource(final ResourceResolver resolver, final String path) {
-        List<String> mappedResources = new ArrayList<String>();
 
         if (resolver.getSearchPath() != null) {
             final String relativePath = getRelativePath(path);
 
-            // Loop over provided base paths
-            for (final String basePath : resolver.getSearchPath()) {
-                // Try to get the corresponding physical resource for this base path
-                final Resource baseRes = resolver.getResource(ResourceUtil.normalize(basePath + "/" + relativePath));
-                if (baseRes != null) {
-                    // Physical resource exists, add it to the list of mapped resources
-                    mappedResources.add(0, baseRes.getPath());
+            if ( relativePath != null ) {
+                final List<String> mappedResources = new ArrayList<String>();
+                // Loop over provided base paths
+                for (final String basePath : resolver.getSearchPath()) {
+                    // Try to get the corresponding physical resource for this base path
+                    final Resource baseRes = resolver.getResource(ResourceUtil.normalize(basePath + "/" + relativePath));
+                    if (baseRes != null) {
+                        // Physical resource exists, add it to the list of mapped resources
+                        mappedResources.add(0, baseRes.getPath());
+                    }
                 }
-            }
 
-            if (!mappedResources.isEmpty()) {
-                // Create a new merged resource based on the list of mapped physical resources
-                return new MergedResource(resolver, mergeRootPath, relativePath, mappedResources);
+                if (!mappedResources.isEmpty()) {
+                    // Create a new merged resource based on the list of mapped physical resources
+                    return new MergedResource(resolver, mergeRootPath, relativePath, mappedResources);
+                }
             }
         }
 
@@ -179,7 +180,15 @@ public class MergedResourceProvider implements ResourceProvider {
      * @return Relative path
      */
     private String getRelativePath(String path) {
-        return StringUtils.removeStart(path, mergeRootPath);
+        if ( path.startsWith(mergeRootPath) ) {
+            path = path.substring(mergeRootPath.length());
+            if ( path.length() == 0 ) {
+                return path;
+            } else if ( path.charAt(0) == '/' ) {
+                return path.substring(1);
+            }
+        }
+        return null;
     }
 
 }
diff --git a/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourceProviderFactory.java b/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourceProviderFactory.java
index 4fd955c..9883414 100644
--- a/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourceProviderFactory.java
+++ b/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourceProviderFactory.java
@@ -68,6 +68,8 @@ public class MergedResourceProviderFactory implements ResourceProviderFactory {
     @Activate
     protected void configure(final Map<String, Object> properties) {
         mergeRootPath = PropertiesUtil.toString(properties.get(ResourceProvider.ROOTS), DEFAULT_ROOT);
+        if ( mergeRootPath.endsWith("/") ) {
+            mergeRootPath = mergeRootPath.substring(mergeRootPath.length() - 1);
+        }
     }
-
 }

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