You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2020/09/04 15:49:51 UTC

[sling-whiteboard] branch master updated: ResourceConverter implementation, generic 'nicer json' content dump

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

bdelacretaz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git


The following commit(s) were added to refs/heads/master by this push:
     new cde1924  ResourceConverter implementation, generic 'nicer json' content dump
cde1924 is described below

commit cde1924c32307ab61b3ba4edf6df3deb956082d4
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Fri Sep 4 17:49:34 2020 +0200

    ResourceConverter implementation, generic 'nicer json' content dump
---
 .../hardcodedfirstshot/ContentProcessor.java       |  2 +-
 .../remotecontentapi/hardcodedfirstshot/P.java     |  5 ++
 .../resourceconverter/ResourceConverter.java       | 92 +++++++++++-----------
 3 files changed, 50 insertions(+), 49 deletions(-)

diff --git a/remote-content-api/src/main/java/org/apache/sling/remotecontentapi/hardcodedfirstshot/ContentProcessor.java b/remote-content-api/src/main/java/org/apache/sling/remotecontentapi/hardcodedfirstshot/ContentProcessor.java
index 31b2761..1bb114c 100644
--- a/remote-content-api/src/main/java/org/apache/sling/remotecontentapi/hardcodedfirstshot/ContentProcessor.java
+++ b/remote-content-api/src/main/java/org/apache/sling/remotecontentapi/hardcodedfirstshot/ContentProcessor.java
@@ -47,6 +47,6 @@ class ContentProcessor implements JsonProcessor {
 
     @Override
     public void process(PipelineContext pc) {
-        pc.content.addAll(new ResourceConverter(pc.resource, new ConverterContext(pc)).getJson());
+        new ResourceConverter(pc.resource, new ConverterContext(pc)).addTo(pc.content);
     }
 }
\ No newline at end of file
diff --git a/remote-content-api/src/main/java/org/apache/sling/remotecontentapi/hardcodedfirstshot/P.java b/remote-content-api/src/main/java/org/apache/sling/remotecontentapi/hardcodedfirstshot/P.java
index 1c1b2ad..f94abd1 100644
--- a/remote-content-api/src/main/java/org/apache/sling/remotecontentapi/hardcodedfirstshot/P.java
+++ b/remote-content-api/src/main/java/org/apache/sling/remotecontentapi/hardcodedfirstshot/P.java
@@ -77,6 +77,11 @@ public class P {
     }
 
     public static String convertName(String in) {
+        if("sling:resourceType".equals(in)) {
+            return "_componentType";
+        } else if("sling:resourceSuperType".equals(in)) {
+            return "_componentSuperType";
+        }
         return in.replace("sling:", "_");
     }
 
diff --git a/remote-content-api/src/main/java/org/apache/sling/remotecontentapi/resourceconverter/ResourceConverter.java b/remote-content-api/src/main/java/org/apache/sling/remotecontentapi/resourceconverter/ResourceConverter.java
index 466cb33..3811681 100644
--- a/remote-content-api/src/main/java/org/apache/sling/remotecontentapi/resourceconverter/ResourceConverter.java
+++ b/remote-content-api/src/main/java/org/apache/sling/remotecontentapi/resourceconverter/ResourceConverter.java
@@ -29,6 +29,8 @@ import org.apache.sling.api.resource.ValueMap;
 import org.apache.sling.remotecontentapi.hardcodedfirstshot.P;
 
 public class ResourceConverter {
+    public static final String SLING_RESOURCE_TYPE = "sling:resourceType";
+    
     public static interface Context {
         /** Return the full URL to access the given path */
         String getUrlForPath(String path, boolean includeApiSelectorsAndExtension);
@@ -40,60 +42,56 @@ public class ResourceConverter {
     private final Context context;
     private final Resource resource;
 
-    static interface ResourceProcessor {
-        void process(JsonObjectBuilder json, Resource r);
-    }
-
-    static interface PropertyProcessor {
-        void process(JsonObjectBuilder json, String key, Object value);
-    }
-
-    static class DefaultResourceProcessor implements ResourceProcessor {
-        private final Resource contentRoot;
+    static class ResourceProcessor {
         private final Context context;
 
-        DefaultResourceProcessor(Context context, Resource contentRoot) {
-            this.contentRoot = contentRoot;
+        ResourceProcessor(Context context) {
             this.context = context;
         }
 
-        public void process(JsonObjectBuilder json, Resource r) {
-            final JsonObjectBuilder b = Json.createObjectBuilder();
-            final ValueMap vm = r.adaptTo(ValueMap.class);
+        private void addProperties(JsonObjectBuilder json, Resource r, ValueMap vm) {
             if(vm != null) {
-                final PropertyProcessor pp = new DefaultPropertyProcessor();
+                // Add an _id to all components which have a resource type
+                if(vm.containsKey(SLING_RESOURCE_TYPE)) {
+                    json.add("_id", r.getPath());
+                }
+
+                final PropertyProcessor pp = new PropertyProcessor();
                 for(Map.Entry<String, Object> entry : vm.entrySet()) {
                     pp.process(json, entry.getKey(), entry.getValue());
                 }
             }
+        }
 
-            // Special treatment for nt:file nodes
-            if(isNodeType(vm, "nt:file")) {
-                json.add("url", context.getUrlForPath(r.getPath(), false));
-                return;
-            }
-    
-            if(r.getPath().equals(contentRoot.getPath())) {
-                // At the root, recurse only in the jcr:content child
-                final Resource content = r.getChild("jcr:content");
-                if(content != null) {
-                    process(b, content);
-                }
-            } else if(r.hasChildren()) {
-                for(Resource child : r.getChildren()) {
-                    process(b, child);
+        private void addChild(JsonObjectBuilder parent, JsonObjectBuilder childJson, Resource r, ValueMap vm) {
+            if(context.getRelativePath(r).isEmpty()) {
+                // at content root, do not add intermediate element
+                parent.addAll(childJson);
+            } else {
+                if("jcr:content".equals(r.getName())) {
+                    parent.add("_composite", childJson);
+                } else {
+                    parent.add(r.getName(), childJson);
                 }
             }
+        }
 
-            if(r.getName().equals("jcr:content")) {
-                json.add("_components", b.build());
-            } else if(vm!=null && vm.containsKey("sling:resourceType")) {
-                final JsonObjectBuilder comp = Json.createObjectBuilder();
-                comp.add("_name", r.getName());
-                comp.addAll(b);
-                json.add("_component", comp);
+        public void addTo(JsonObjectBuilder parentJson, Resource r) {
+            final ValueMap vm = r.adaptTo(ValueMap.class);
+
+            if(isNodeType(vm, "nt:file")) {
+                // nt:file nodes: emit just a link
+                parentJson.add("file", Json.createObjectBuilder()
+                    .add("url", context.getUrlForPath(r.getPath(), false))
+                );
             } else {
-                json.add(r.getName(), b.build());
+                // general node: add a child JSON node with its name, properties and child nodes
+                final JsonObjectBuilder childJson = Json.createObjectBuilder();
+                addProperties(childJson, r, vm);
+                for(Resource child : r.getChildren()) {
+                    addTo(childJson, child);
+                }
+                addChild(parentJson, childJson, r, vm);
             }
         }
     }
@@ -102,7 +100,7 @@ public class ResourceConverter {
         return vm == null ? false : nodeType.equals(vm.get("jcr:primaryType", String.class));
     }
 
-    static class DefaultPropertyProcessor implements PropertyProcessor {
+    static class PropertyProcessor {
         public void process(JsonObjectBuilder json, String key, Object value) {
             if(value != null) {
                 final String newName = processName(key);
@@ -116,11 +114,11 @@ public class ResourceConverter {
             if(!propertyName.contains(":")) {
                 return propertyName;
             } else if(propertyName.equals("jcr:title")) {
-                return "title";
+                return "_title";
             } else if(propertyName.equals("jcr:description")) {
-                return "description";
-            } else if(propertyName.equals("sling:resourceType")) {
-                return "_resourceType";
+                return "_description";
+            } else if(propertyName.equals(SLING_RESOURCE_TYPE)) {
+                return "_componentType";
             } else {
                 return null;
             }
@@ -132,9 +130,7 @@ public class ResourceConverter {
         resource = r;
     }
 
-    public JsonObjectBuilder getJson() {
-        final JsonObjectBuilder b = Json.createObjectBuilder();
-        new DefaultResourceProcessor(context, resource).process(b, resource);
-        return b;
+    public void addTo(JsonObjectBuilder content) {
+        new ResourceProcessor(context).addTo(content, resource);
     }
 }
\ No newline at end of file