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/03 13:58:24 UTC

[sling-whiteboard] 01/02: Rough, hardcoded experiment with more complex content structures

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

commit cc8bd9e133b9456143490e5e9367fb635c31d13c
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Thu Sep 3 13:34:40 2020 +0200

    Rough, hardcoded experiment with more complex content structures
---
 .../hardcodedfirstshot/ContentProcessor.java       | 82 ++++++++++++++++++++--
 .../hardcodedfirstshot/PipelineContext.java        | 13 +++-
 2 files changed, 88 insertions(+), 7 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 92fc063..b145517 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
@@ -19,16 +19,90 @@
 
 package org.apache.sling.remotecontentapi.hardcodedfirstshot;
 
+import javax.json.Json;
+import javax.json.JsonObjectBuilder;
+
+import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ValueMap;
 
 class ContentProcessor implements JsonProcessor {
+    private static final int MAX_RECURSION = 99;
+    private static final String JCR_CONTENT = "jcr:content";
+
     @Override
     public void process(PipelineContext pc) {
-        final ValueMap vm = pc.resource.adaptTo(ValueMap.class);
-        for(String key : vm.keySet()) {
-            if(!P.ignoreProperty(key) && !P.isMetadata(key)) {
-                P.maybeAdd(pc.content, key, P.convertName(key), vm);
+        processResource(pc, pc.content, pc.resource, true, MAX_RECURSION);
+    }
+
+    private boolean isNodeType(ValueMap vm, String nodeType) {
+        return vm == null ? false : nodeType.equals(vm.get("jcr:primaryType", String.class));
+    }
+
+    private boolean ignoreResource(Resource r) {
+        final String name = r.getName();
+        if(name.startsWith("cq:")) {
+            return !name.equals("cq:tags");
+        }
+        final ValueMap vm = r.adaptTo(ValueMap.class);
+        if(isNodeType(vm, "cq:Page")) {
+                return true;
+        }
+        return false;
+    }
+
+    private String convertPropertyName(String name) {
+        if(!name.contains(":")) {
+            return name;
+        } else if(name.equals("jcr:title")) {
+            return "title";
+        } else if(name.equals("jcr:description")) {
+            return "description";
+        } else if(name.equals("sling:resourceType")) {
+            return "_resourceType";
+        } else {
+            return null;
+        }
+    }
+
+    private void processResource(PipelineContext pc, JsonObjectBuilder json, Resource r, boolean contentRootMode, int maxRecursion) {
+        if(maxRecursion <= 0) {
+            return;
+        }
+        final ValueMap vm = r.adaptTo(ValueMap.class);
+
+        if(isNodeType(vm, "nt:file")) {
+            json.add("url", pc.pathToUrlNoJsonExtension(r.getPath()));
+            return;
+        }
+        
+        if(vm != null) {
+            for(String key : vm.keySet()) {
+                final String newName = convertPropertyName(key);
+                if(newName != null) {
+                    P.maybeAdd(json, key, newName, vm);
+                }
             }
         }
+
+        if(contentRootMode) {
+            final Resource content = r.getChild(JCR_CONTENT);
+            if(content != null) {
+                json.addAll(visitContentResource(pc, content, maxRecursion - 1));
+            }
+        } else if(r.hasChildren()) {
+            final JsonObjectBuilder b = Json.createObjectBuilder();
+            for(Resource child : r.getChildren()) {
+                if(!ignoreResource(child)) {
+                    b.add(child.getName(), visitContentResource(pc, child, maxRecursion - 1));
+                }
+            }
+            json.addAll(b);
+        }
+    }
+
+    private JsonObjectBuilder visitContentResource(PipelineContext pc, Resource r, int maxRecursion) {
+        final JsonObjectBuilder b = Json.createObjectBuilder();
+        processResource(pc, b, r, false, maxRecursion - 1);
+        return b;
     }
 }
\ No newline at end of file
diff --git a/remote-content-api/src/main/java/org/apache/sling/remotecontentapi/hardcodedfirstshot/PipelineContext.java b/remote-content-api/src/main/java/org/apache/sling/remotecontentapi/hardcodedfirstshot/PipelineContext.java
index 8d2893b..7ae342d 100644
--- a/remote-content-api/src/main/java/org/apache/sling/remotecontentapi/hardcodedfirstshot/PipelineContext.java
+++ b/remote-content-api/src/main/java/org/apache/sling/remotecontentapi/hardcodedfirstshot/PipelineContext.java
@@ -66,13 +66,20 @@ public class PipelineContext {
         return b.build();
     }
 
-    String pathToUrl(String path) {
+    String pathToUrlNoJsonExtension(String path) {
         return String.format(
-            "%s://%s:%d%s.%s.%s",
+            "%s://%s:%d%s",
             request.getScheme(),
             request.getServerName(),
             request.getServerPort(),
-            path,
+            path
+        );
+    }
+
+    String pathToUrl(String path) {
+        return String.format(
+            "%s.%s.%s",
+            pathToUrlNoJsonExtension(path),
             request.getRequestPathInfo().getSelectorString(),
             request.getRequestPathInfo().getExtension()
         );