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/26 13:45:14 UTC

svn commit: r1806297 - in /sling/trunk/bundles/extensions/bundleresource/src: main/java/org/apache/sling/bundleresource/impl/ test/java/org/apache/sling/bundleresource/impl/

Author: cziegeler
Date: Sat Aug 26 13:45:14 2017
New Revision: 1806297

URL: http://svn.apache.org/viewvc?rev=1806297&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
    sling/trunk/bundles/extensions/bundleresource/src/test/java/org/apache/sling/bundleresource/impl/BundleResourceTest.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=1806297&r1=1806296&r2=1806297&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 Sat Aug 26 13:45:14 2017
@@ -21,12 +21,13 @@ import static org.apache.jackrabbit.JcrC
 
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.StringWriter;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 
 import javax.json.Json;
@@ -65,11 +66,15 @@ public class BundleResource extends Abst
 
     private final ValueMap valueMap;
 
+    private final Map<String, Map<String, Object>> subResources;
+
+    @SuppressWarnings("unchecked")
     public BundleResource(final ResourceResolver resourceResolver,
             final BundleResourceCache cache,
             final MappedPath mappedPath,
             final String resourcePath,
             final String propsPath,
+            final Map<String, Object> readProps,
             final boolean isFolder) {
 
         this.resourceResolver = resourceResolver;
@@ -103,15 +108,35 @@ public class BundleResource extends Abst
             }
         }
 
+        Map<String, Map<String, Object>> children = null;
+        if ( readProps != null ) {
+            for(final Map.Entry<String, Object> entry : readProps.entrySet()) {
+                if ( entry.getValue() instanceof Map ) {
+                    if ( children == null ) {
+                        children = new HashMap<>();
+                    }
+                    children.put(entry.getKey(), (Map<String, Object>)entry.getValue());
+                } else {
+                    properties.put(entry.getKey(), entry.getValue());
+                }
+            }
+        }
         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());
+                        final Object value = getValue(entry.getValue(), true);
                         if ( value != null ) {
-                            properties.put(entry.getKey(), value);
+                            if ( value instanceof Map ) {
+                                if ( children == null ) {
+                                    children = new HashMap<>();
+                                }
+                                children.put(entry.getKey(), (Map<String, Object>)value);
+                            } else {
+                                properties.put(entry.getKey(), value);
+                            }
                         }
                     }
                 }
@@ -121,9 +146,10 @@ public class BundleResource extends Abst
             }
 
         }
+        this.subResources = children;
     }
 
-    private static Object getValue(final JsonValue value) {
+    private static Object getValue(final JsonValue value, final boolean topLevel) {
         switch ( value.getValueType() ) {
             // type NULL -> return null
             case NULL : return null;
@@ -138,18 +164,27 @@ public class BundleResource extends Abst
                                return num.longValue();
                           }
                           return num.doubleValue();
-            // type ARRAY -> return JSON string
-            case ARRAY : final StringWriter writer = new StringWriter();
-                         Json.createWriter(writer).writeArray((JsonArray)value);
-                         return writer.toString();
-             // type OBJECT -> return JSON string
-             case OBJECT : final StringWriter mapWriter = new StringWriter();
-                           Json.createWriter(mapWriter).writeObject((JsonObject)value);
-                           return mapWriter.toString();
+            // type ARRAY -> return list and call this method for each value
+            case ARRAY : final List<Object> array = new ArrayList<>();
+                         for(final JsonValue x : ((JsonArray)value)) {
+                             array.add(getValue(x, false));
+                         }
+                         return array;
+            // type OBJECT -> return map
+            case OBJECT : final Map<String, Object> map = new HashMap<>();
+                          final JsonObject obj = (JsonObject)value;
+                          for(final Map.Entry<String, JsonValue> entry : obj.entrySet()) {
+                              map.put(entry.getKey(), getValue(entry.getValue(), false));
+                          }
+                          return map;
         }
         return null;
     }
 
+    Map<String, Map<String, Object>> getSubResources() {
+        return this.subResources;
+    }
+
     @Override
     public String getPath() {
         return path;

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=1806297&r1=1806296&r2=1806297&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 Aug 26 13:45:14 2017
@@ -20,12 +20,14 @@ package org.apache.sling.bundleresource.
 
 import java.util.HashSet;
 import java.util.Iterator;
+import java.util.Map;
 import java.util.NoSuchElementException;
 import java.util.Set;
 import java.util.TreeSet;
 
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.ResourceUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -43,7 +45,7 @@ class BundleResourceIterator implements
     private final ResourceResolver resourceResolver;
 
     /** Bundle providing the entry resources */
-    private final BundleResourceCache bundle;
+    private final BundleResourceCache cache;
 
     private final MappedPath mappedPath;
 
@@ -56,6 +58,8 @@ class BundleResourceIterator implements
     /** The prefetched next iterator entry, null at the end of iterating */
     private Resource nextResult;
 
+    private final Map<String, Map<String, Object>> subResources;
+
     /**
      * Creates an instance using the given parent bundle resource.
      */
@@ -65,7 +69,8 @@ class BundleResourceIterator implements
         String parentPath = parent.getPath().concat("/");
 
         this.resourceResolver = parent.getResourceResolver();
-        this.bundle = parent.getBundle();
+        this.cache = parent.getBundle();
+        this.subResources = parent.getSubResources();
         this.mappedPath = parent.getMappedPath();
 
         this.entries = getFilteredEntries(mappedPath.getEntryPath(parentPath));
@@ -83,7 +88,8 @@ class BundleResourceIterator implements
         }
 
         this.resourceResolver = resourceResolver;
-        this.bundle = bundle;
+        this.cache = bundle;
+        this.subResources = null;
         this.mappedPath = mappedPath;
         this.entries = getFilteredEntries(parentPath);
         this.prefixLength = parentPath.length();
@@ -92,8 +98,13 @@ class BundleResourceIterator implements
     }
 
     private Iterator<String> getFilteredEntries(final String parentPath) {
-        final Set<String> bundleEntries = new TreeSet<>(bundle.getEntryPaths(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() ) {
@@ -156,9 +167,11 @@ class BundleResourceIterator implements
                 if ( mappedPath.getJSONPropertiesExtension() != null ) {
                     propsPath = entry.concat(mappedPath.getJSONPropertiesExtension());
                 }
-                return new BundleResource(resourceResolver, bundle, mappedPath,
-                        isFolder ? entry.substring(0, entry.length()-1): entry,
+                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);
             }
 

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=1806297&r1=1806296&r2=1806297&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 Aug 26 13:45:14 2017
@@ -125,6 +125,7 @@ public class BundleResourceProvider exte
                             mappedPath,
                             path,
                             propsPath,
+                            null,
                             isFolder);
                 }
             }

Modified: sling/trunk/bundles/extensions/bundleresource/src/test/java/org/apache/sling/bundleresource/impl/BundleResourceTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/bundleresource/src/test/java/org/apache/sling/bundleresource/impl/BundleResourceTest.java?rev=1806297&r1=1806296&r2=1806297&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/bundleresource/src/test/java/org/apache/sling/bundleresource/impl/BundleResourceTest.java (original)
+++ sling/trunk/bundles/extensions/bundleresource/src/test/java/org/apache/sling/bundleresource/impl/BundleResourceTest.java Sat Aug 26 13:45:14 2017
@@ -66,7 +66,7 @@ public class BundleResourceTest {
         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, false);
+                new MappedPath("/libs/foo", null, null), "/libs/foo/test.json", null, null, false);
         assertEquals(JcrConstants.NT_FILE, rsrc.getResourceType());
         assertNull(rsrc.getResourceSuperType());
         final ValueMap vm = rsrc.getValueMap();
@@ -77,7 +77,7 @@ public class BundleResourceTest {
         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", false);
+                new MappedPath("/libs/foo", null, "json"), "/libs/foo/test", "/libs/foo/test.json", null, false);
         assertEquals(JcrConstants.NT_FILE, rsrc.getResourceType());
         assertNull(rsrc.getResourceSuperType());
         final ValueMap vm = rsrc.getValueMap();