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/08/27 10:36:47 UTC

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

Author: cziegeler
Date: Sun Aug 27 10:36:47 2017
New Revision: 1806348

URL: http://svn.apache.org/viewvc?rev=1806348&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/BundleResource.java
    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

Modified: sling/trunk/bundles/extensions/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResource.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResource.java?rev=1806348&r1=1806347&r2=1806348&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResource.java (original)
+++ sling/trunk/bundles/extensions/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResource.java Sun Aug 27 10:36:47 2017
@@ -149,6 +149,29 @@ public class BundleResource extends Abst
         this.subResources = children;
     }
 
+    Resource getChildResource(String path) {
+        Resource result = null;
+        Map<String, Map<String, Object>> resources = this.subResources;
+        for(String segment : path.split("/")) {
+            if ( resources != null ) {
+                path = path.concat("/").concat(segment);
+                final Map<String, Object> props = resources.get(segment);
+                if ( props != null ) {
+                    result = new BundleResource(this.resourceResolver, this.cache, this.mappedPath,
+                            path, path.concat(this.mappedPath.getJSONPropertiesExtension()), props, false);
+                    resources = ((BundleResource)result).subResources;
+                } else {
+                    result = null;
+                    break;
+                }
+            } else {
+                result = null;
+                break;
+            }
+        }
+        return result;
+    }
+
     private static Object getValue(final JsonValue value, final boolean topLevel) {
         switch ( value.getValueType() ) {
             // type NULL -> return null

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=1806348&r1=1806347&r2=1806348&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 Sun Aug 27 10:36:47 2017
@@ -18,6 +18,7 @@
  */
 package org.apache.sling.bundleresource.impl;
 
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Map;
@@ -70,7 +71,7 @@ class BundleResourceIterator implements
 
         this.resourceResolver = parent.getResourceResolver();
         this.cache = parent.getBundle();
-        this.subResources = parent.getSubResources();
+        this.subResources = parent.getSubResources() != null ? new HashMap<>(parent.getSubResources()) : null;
         this.mappedPath = parent.getMappedPath();
 
         this.entries = getFilteredEntries(mappedPath.getEntryPath(parentPath));
@@ -100,11 +101,6 @@ class BundleResourceIterator implements
     private Iterator<String> getFilteredEntries(final String parentPath) {
         final Set<String> bundleEntries = new TreeSet<>(cache.getEntryPaths(parentPath));
         if ( this.mappedPath.getJSONPropertiesExtension() != null ) {
-            if ( subResources != null ) {
-                for(final String name : subResources.keySet()) {
-                    bundleEntries.add(parentPath.concat(name));
-                }
-            }
             final Set<String> add = new HashSet<>();
             final Iterator<String> iter = bundleEntries.iterator();
             while ( iter.hasNext() ) {
@@ -115,6 +111,16 @@ class BundleResourceIterator implements
                 }
             }
             bundleEntries.addAll(add);
+            if ( subResources != null ) {
+                for(final String name : subResources.keySet()) {
+                    final String fullPath = parentPath.concat(name);
+                    if ( !bundleEntries.contains(fullPath) ) {
+                        bundleEntries.add(fullPath);
+                    } else {
+                        subResources.remove(name);
+                    }
+                }
+            }
         }
         return (bundleEntries.isEmpty() ? null : bundleEntries.iterator());
     }

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=1806348&r1=1806347&r2=1806348&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 Sun Aug 27 10:36:47 2017
@@ -24,6 +24,7 @@ import java.util.Hashtable;
 import java.util.Iterator;
 
 import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceUtil;
 import org.apache.sling.spi.resource.provider.ResolveContext;
 import org.apache.sling.spi.resource.provider.ResourceContext;
 import org.apache.sling.spi.resource.provider.ResourceProvider;
@@ -131,6 +132,20 @@ 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);
+                    if (rsrc != null ) {
+                        final Resource childResource = ((BundleResource)rsrc).getChildResource(path.substring(resourcePath.length() + 1));
+                        if ( childResource != null ) {
+                            return childResource;
+                        }
+                    }
+                    resourcePath = ResourceUtil.getParent(resourcePath);
+                }
+            }
         }
 
         return null;