You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2017/09/09 13:03:10 UTC

svn commit: r1807892 - in /sling/trunk/bundles/extensions/bundleresource/src/main/java/org/apache/sling/bundleresource/impl: BundleResourceIterator.java BundleResourceProvider.java MappedPath.java

Author: cziegeler
Date: Sat Sep  9 13:03:10 2017
New Revision: 1807892

URL: http://svn.apache.org/viewvc?rev=1807892&view=rev
Log:
SLING-6878 : Bundle resource provider: support mounting of JSON files

Modified:
    sling/trunk/bundles/extensions/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceIterator.java
    sling/trunk/bundles/extensions/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceProvider.java
    sling/trunk/bundles/extensions/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/MappedPath.java

Modified: sling/trunk/bundles/extensions/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceIterator.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceIterator.java?rev=1807892&r1=1807891&r2=1807892&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceIterator.java (original)
+++ sling/trunk/bundles/extensions/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceIterator.java Sat Sep  9 13:03:10 2017
@@ -67,15 +67,15 @@ class BundleResourceIterator implements
     BundleResourceIterator(final BundleResource parent) {
 
         // trailing slash to enumerate children
-        String parentPath = parent.getPath().concat("/");
+        final String parentEntryPath = parent.getMappedPath().getEntryPath(parent.getPath().concat("/"));
+        this.prefixLength = parentEntryPath.length();
 
         this.resourceResolver = parent.getResourceResolver();
         this.cache = parent.getBundle();
         this.subResources = parent.getSubResources() != null ? new HashMap<>(parent.getSubResources()) : null;
         this.mappedPath = parent.getMappedPath();
 
-        this.entries = getFilteredEntries(mappedPath.getEntryPath(parentPath));
-        this.prefixLength = parentPath.length();
+        this.entries = getFilteredEntries(parentEntryPath);
 
         this.nextResult = (entries != null) ? seek() : null;
     }
@@ -87,19 +87,20 @@ class BundleResourceIterator implements
         if (!parentPath.endsWith("/")) {
             parentPath = parentPath.concat("/");
         }
+        final String parentEntryPath = mappedPath.getEntryPath(parentPath);
+        this.prefixLength = parentEntryPath.length();
 
         this.resourceResolver = resourceResolver;
         this.cache = bundle;
         this.subResources = null;
         this.mappedPath = mappedPath;
-        this.entries = getFilteredEntries(parentPath);
-        this.prefixLength = parentPath.length();
+        this.entries = getFilteredEntries(parentEntryPath);
 
         this.nextResult = (entries != null) ? seek() : null;
     }
 
-    private Iterator<String> getFilteredEntries(final String parentPath) {
-        final Set<String> bundleEntries = new TreeSet<>(cache.getEntryPaths(parentPath));
+    private Iterator<String> getFilteredEntries(final String parentEntryPath) {
+        final Set<String> bundleEntries = new TreeSet<>(cache.getEntryPaths(parentEntryPath));
         if ( this.mappedPath.getJSONPropertiesExtension() != null ) {
             final Set<String> add = new HashSet<>();
             final Iterator<String> iter = bundleEntries.iterator();
@@ -113,7 +114,7 @@ class BundleResourceIterator implements
             bundleEntries.addAll(add);
             if ( subResources != null ) {
                 for(final String name : subResources.keySet()) {
-                    final String fullPath = parentPath.concat(name);
+                    final String fullPath = parentEntryPath.concat(name);
                     if ( !bundleEntries.contains(fullPath) ) {
                         bundleEntries.add(fullPath);
                     } else {
@@ -138,7 +139,7 @@ class BundleResourceIterator implements
             throw new NoSuchElementException();
         }
 
-        Resource result = nextResult;
+        final Resource result = nextResult;
         nextResult = seek();
         return result;
     }
@@ -160,18 +161,19 @@ class BundleResourceIterator implements
         while (entries.hasNext()) {
             String entry = entries.next();
 
-            // require leading slash
+            // require leading slash (sanity check, should always be the case)
             if (!entry.startsWith("/")) {
                 entry = "/".concat(entry);
             }
 
+            // another sanity check if the prefix is correct
             int slash = entry.indexOf('/', prefixLength);
             if (slash < 0 || slash == entry.length() - 1) {
                 log.debug("seek: Using entry {}", entry);
                 final boolean isFolder = entry.endsWith("/");
                 final String entryPath = isFolder ? entry.substring(0, entry.length()-1) : entry;
                 return new BundleResource(resourceResolver, cache, mappedPath,
-                        entryPath,
+                        mappedPath.getResourcePath(entryPath),
                         this.subResources != null ? this.subResources.get(ResourceUtil.getName(entryPath)) : null,
                         isFolder);
             }

Modified: sling/trunk/bundles/extensions/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceProvider.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceProvider.java?rev=1807892&r1=1807891&r2=1807892&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceProvider.java (original)
+++ sling/trunk/bundles/extensions/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceProvider.java Sat Sep  9 13:03:10 2017
@@ -85,12 +85,12 @@ public class BundleResourceProvider exte
      */
     @Override
     public Resource getResource(final ResolveContext<Object> ctx,
-            final String path,
+            final String resourcePath,
             final ResourceContext resourceContext,
             final Resource parent) {
-        final MappedPath mappedPath = getMappedPath(path);
+        final MappedPath mappedPath = getMappedPath(resourcePath);
         if (mappedPath != null) {
-            final String entryPath = mappedPath.getEntryPath(path);
+            final String entryPath = mappedPath.getEntryPath(resourcePath);
 
             // first try, whether the bundle has an entry with a trailing slash
             // which would be a folder. In this case we check whether the
@@ -120,7 +120,7 @@ public class BundleResourceProvider exte
                     return new BundleResource(ctx.getResourceResolver(),
                             cache,
                             mappedPath,
-                            path,
+                            resourcePath,
                             null,
                             isFolder);
                 }
@@ -129,16 +129,19 @@ public class BundleResourceProvider exte
             // the bundle does not contain the path
             // if JSON is enabled check for any parent
             if ( this.root.getJSONPropertiesExtension() != null ) {
-                String resourcePath = ResourceUtil.getParent(path);
-                while ( resourcePath != null ) {
-                    final Resource rsrc = getResource(ctx, resourcePath, resourceContext, null);
+                String parentPath = ResourceUtil.getParent(resourcePath);
+                while ( parentPath != null ) {
+                    final Resource rsrc = getResource(ctx, parentPath, resourceContext, null);
                     if (rsrc != null ) {
-                        final Resource childResource = ((BundleResource)rsrc).getChildResource(path.substring(resourcePath.length() + 1));
+                        final Resource childResource = ((BundleResource)rsrc).getChildResource(resourcePath.substring(parentPath.length() + 1));
                         if ( childResource != null ) {
                             return childResource;
                         }
                     }
-                    resourcePath = ResourceUtil.getParent(resourcePath);
+                    parentPath = ResourceUtil.getParent(parentPath);
+                    if ( parentPath != null && this.getMappedPath(parentPath) == null ) {
+                        parentPath = null;
+                    }
                 }
             }
         }

Modified: sling/trunk/bundles/extensions/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/MappedPath.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/MappedPath.java?rev=1807892&r1=1807891&r2=1807892&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/MappedPath.java (original)
+++ sling/trunk/bundles/extensions/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/MappedPath.java Sat Sep  9 13:03:10 2017
@@ -102,6 +102,18 @@ class MappedPath {
         return null;
     }
 
+    String getResourcePath(final String entryPath) {
+        if ( entryRootPrefix == null ) {
+            return entryPath;
+        }
+        if ( entryPath.startsWith(entryRootPrefix) ) {
+            return resourceRootPrefix.concat(entryPath.substring(entryRootPrefix.length()));
+        } else if ( entryPath.equals(entryRoot)) {
+            return resourceRoot;
+        }
+        return null;
+    }
+
     String getResourceRoot() {
         return resourceRoot;
     }