SLING-6878 : Bundle resource provider: support mounting of JSON files

git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1807776 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/sling/bundleresource/impl/BundleResource.java b/src/main/java/org/apache/sling/bundleresource/impl/BundleResource.java
index bf00328..cbac4b3 100644
--- a/src/main/java/org/apache/sling/bundleresource/impl/BundleResource.java
+++ b/src/main/java/org/apache/sling/bundleresource/impl/BundleResource.java
@@ -73,7 +73,6 @@
             final BundleResourceCache cache,
             final MappedPath mappedPath,
             final String resourcePath,
-            final String propsPath,
             final Map<String, Object> readProps,
             final boolean isFolder) {
 
@@ -121,30 +120,33 @@
                 }
             }
         }
-        if ( propsPath != null ) {
-            try {
-                final URL url = this.cache.getEntry(mappedPath.getEntryPath(propsPath));
-                if (url != null) {
-                    final JsonObject obj = Json.createReader(url.openStream()).readObject();
-                    for(final Map.Entry<String, JsonValue> entry : obj.entrySet()) {
-                        final Object value = getValue(entry.getValue(), true);
-                        if ( value != null ) {
-                            if ( value instanceof Map ) {
-                                if ( children == null ) {
-                                    children = new HashMap<>();
+        if ( this.mappedPath.getJSONPropertiesExtension() != null ) {
+            final String propsPath = mappedPath.getEntryPath(resourcePath.concat(this.mappedPath.getJSONPropertiesExtension()));
+            if ( propsPath != null ) {
+
+                try {
+                    final URL url = this.cache.getEntry(propsPath);
+                    if (url != null) {
+                        final JsonObject obj = Json.createReader(url.openStream()).readObject();
+                        for(final Map.Entry<String, JsonValue> entry : obj.entrySet()) {
+                            final Object value = getValue(entry.getValue(), true);
+                            if ( value != null ) {
+                                if ( value instanceof Map ) {
+                                    if ( children == null ) {
+                                        children = new HashMap<>();
+                                    }
+                                    children.put(entry.getKey(), (Map<String, Object>)value);
+                                } else {
+                                    properties.put(entry.getKey(), value);
                                 }
-                                children.put(entry.getKey(), (Map<String, Object>)value);
-                            } else {
-                                properties.put(entry.getKey(), value);
                             }
                         }
                     }
+                } catch (final IOException ioe) {
+                    log.error(
+                            "getInputStream: Cannot get input stream for " + propsPath, ioe);
                 }
-            } catch (final IOException ioe) {
-                log.error(
-                        "getInputStream: Cannot get input stream for " + mappedPath.getEntryPath(propsPath), ioe);
             }
-
         }
         this.subResources = children;
     }
@@ -158,7 +160,7 @@
                 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);
+                            path, props, false);
                     resources = ((BundleResource)result).subResources;
                 } else {
                     result = null;
diff --git a/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceIterator.java b/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceIterator.java
index 32a8f4f..d8401a5 100644
--- a/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceIterator.java
+++ b/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceIterator.java
@@ -169,14 +169,9 @@
             if (slash < 0 || slash == entry.length() - 1) {
                 log.debug("seek: Using entry {}", entry);
                 final boolean isFolder = entry.endsWith("/");
-                String propsPath = null;
-                if ( mappedPath.getJSONPropertiesExtension() != null ) {
-                    propsPath = entry.concat(mappedPath.getJSONPropertiesExtension());
-                }
                 final String entryPath = isFolder ? entry.substring(0, entry.length()-1) : entry;
                 return new BundleResource(resourceResolver, cache, mappedPath,
                         entryPath,
-                        propsPath,
                         this.subResources != null ? this.subResources.get(ResourceUtil.getName(entryPath)) : null,
                         isFolder);
             }
diff --git a/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceProvider.java b/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceProvider.java
index b905ba6..2e27295 100644
--- a/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceProvider.java
+++ b/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceProvider.java
@@ -117,15 +117,10 @@
                 if ( this.root.getJSONPropertiesExtension() == null
                      || !entryPath.endsWith(this.root.getJSONPropertiesExtension()) ) {
 
-                    String propsPath = null;
-                    if ( this.root.getJSONPropertiesExtension() != null ) {
-                        propsPath = entryPath.concat(this.root.getJSONPropertiesExtension());
-                    }
                     return new BundleResource(ctx.getResourceResolver(),
                             cache,
                             mappedPath,
                             path,
-                            propsPath,
                             null,
                             isFolder);
                 }
diff --git a/src/test/java/org/apache/sling/bundleresource/impl/BundleResourceTest.java b/src/test/java/org/apache/sling/bundleresource/impl/BundleResourceTest.java
index 9191418..7202fab 100644
--- a/src/test/java/org/apache/sling/bundleresource/impl/BundleResourceTest.java
+++ b/src/test/java/org/apache/sling/bundleresource/impl/BundleResourceTest.java
@@ -72,7 +72,7 @@
         final BundleResourceCache cache = getBundleResourceCache();
         when(cache.getEntry("/libs/foo/test.json")).thenReturn(new URL("file:/libs/foo/test.json"));
         final BundleResource rsrc = new BundleResource(null, cache,
-                new MappedPath("/libs/foo", null, null), "/libs/foo/test.json", null, null, false);
+                new MappedPath("/libs/foo", null, null), "/libs/foo/test.json", null, false);
         assertEquals(JcrConstants.NT_FILE, rsrc.getResourceType());
         assertNull(rsrc.getResourceSuperType());
         final ValueMap vm = rsrc.getValueMap();
@@ -83,7 +83,7 @@
         final BundleResourceCache cache = getBundleResourceCache();
         addContent(cache, "/libs/foo/test.json", Collections.singletonMap("test", (Object)"foo"));
         final BundleResource rsrc = new BundleResource(null, cache,
-                new MappedPath("/libs/foo", null, "json"), "/libs/foo/test", "/libs/foo/test.json", null, false);
+                new MappedPath("/libs/foo", null, "json"), "/libs/foo/test", null, false);
         assertEquals(JcrConstants.NT_FILE, rsrc.getResourceType());
         assertNull(rsrc.getResourceSuperType());
         final ValueMap vm = rsrc.getValueMap();