You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by an...@apache.org on 2021/04/07 00:44:32 UTC

[sling-whiteboard] branch master updated: Added a fix for reference children

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

andysch 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 8fd9825  Added a fix for reference children
8fd9825 is described below

commit 8fd9825729e7361e65288069361390e9571a28d0
Author: Andreas Schaefer <sc...@me.com>
AuthorDate: Tue Apr 6 17:44:18 2021 -0700

    Added a fix for reference children
---
 .../core/DeclarativeDynamicDecoratorService.java   | 60 +++++++++++++++++++++
 .../DeclarativeDynamicResourceProviderHandler.java | 18 +++++--
 ...larativeDynamicResourceProviderHandlerTest.java | 61 ++++++++++++++++++++--
 .../resources/ddr-reference/ddr-apps-settings.json | 28 ++++++++++
 .../resources/ddr-reference/ddr-conf-settings.json | 33 ++++++++++++
 .../SLING-CONTENT/apps/ddr-static/ref.json         | 10 +++-
 6 files changed, 200 insertions(+), 10 deletions(-)

diff --git a/org.apache.sling.ddr/core/src/main/java/org/apache/sling/ddr/core/DeclarativeDynamicDecoratorService.java b/org.apache.sling.ddr/core/src/main/java/org/apache/sling/ddr/core/DeclarativeDynamicDecoratorService.java
new file mode 100644
index 0000000..739c4c7
--- /dev/null
+++ b/org.apache.sling.ddr/core/src/main/java/org/apache/sling/ddr/core/DeclarativeDynamicDecoratorService.java
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package org.apache.sling.ddr.core;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceDecorator;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.metatype.annotations.AttributeDefinition;
+import org.osgi.service.metatype.annotations.Designate;
+import org.osgi.service.metatype.annotations.ObjectClassDefinition;
+
+import javax.servlet.http.HttpServletRequest;
+
+//AS TODO: We might not need it
+
+//@Component(
+//    service = ResourceDecorator.class,
+//    immediate = true,
+//    name="DDR Decorator"
+//)
+//@Designate(ocd = DeclarativeDynamicDecoratorService.Configuration.class, factory = false)
+public class DeclarativeDynamicDecoratorService
+    implements ResourceDecorator
+{
+    @ObjectClassDefinition(
+        name = "Declarative Dynamic Component Resource Manager",
+        description = "Configuration of the Dynamic Component Resource Manager")
+    public @interface Configuration {
+        @AttributeDefinition(
+            name = "Path Mappings",
+            description = "")
+        String[] path_mapping() default "/apps/ddr-dynamic";
+    }
+
+    @Override
+    public @Nullable Resource decorate(@NotNull Resource resource) {
+        return resource;
+    }
+
+    @Override
+    public @Nullable Resource decorate(@NotNull Resource resource, @NotNull HttpServletRequest request) {
+        return this.decorate(resource);
+    }
+}
diff --git a/org.apache.sling.ddr/core/src/main/java/org/apache/sling/ddr/core/DeclarativeDynamicResourceProviderHandler.java b/org.apache.sling.ddr/core/src/main/java/org/apache/sling/ddr/core/DeclarativeDynamicResourceProviderHandler.java
index 6e805bf..ed73040 100644
--- a/org.apache.sling.ddr/core/src/main/java/org/apache/sling/ddr/core/DeclarativeDynamicResourceProviderHandler.java
+++ b/org.apache.sling.ddr/core/src/main/java/org/apache/sling/ddr/core/DeclarativeDynamicResourceProviderHandler.java
@@ -276,7 +276,11 @@ public class DeclarativeDynamicResourceProviderHandler
         List<Reference> childrenList = new ArrayList<>();
         childrenMappings.put(resourcePath, childrenList);
         String targetPath = providerRootPath + SLASH + postfix;
-        Resource provider = resourceResolver.getResource(targetPath);
+        Reference ref = mappings.get(resourcePath);
+        Resource provider = ref == null || !ref.isRef() ?
+            resourceResolver.getResource(targetPath) :
+            resourceResolver.getResource(ref.getReference());
+            ;
         log.info("Provider, Path: '{}', Resource: '{}'", targetPath, provider);
         if (provider != null) {
             Iterator<Resource> i = provider.listChildren();
@@ -316,8 +320,14 @@ public class DeclarativeDynamicResourceProviderHandler
                     if(!handled) {
                         // Not a reference
                         log.info("Add Path: '{}' to children list", child.getPath());
-                        childrenList.add(new Reference(child.getPath()));
-                        mappings.put(targetRootPath + SLASH + (postfix.isEmpty() ? "" : postfix + SLASH) + child.getName(), new Reference(child.getPath()));
+                        Reference newRef;
+                        if(ref != null && ref.isRef()) {
+                            newRef = new Reference(ref.getSource() + SLASH + child.getName(), child.getPath());
+                        } else {
+                            newRef = new Reference(child.getPath());
+                        }
+                        childrenList.add(newRef);
+                        mappings.put(targetRootPath + SLASH + (postfix.isEmpty() ? "" : postfix + SLASH) + child.getName(), newRef);
                         if(returnChildren) {
                             answer.add(
                                 createSyntheticFromResource(
@@ -345,7 +355,7 @@ public class DeclarativeDynamicResourceProviderHandler
         public Reference(String source, String reference) {
             this.source = source;
             this.reference = reference;
-            this.ref = reference == null || this.reference.equals(this.source);
+            this.ref = reference != null && !this.reference.equals(this.source);
         }
 
         public String getSource() {
diff --git a/org.apache.sling.ddr/core/src/test/java/org/apache/sling/ddr/core/DeclarativeDynamicResourceProviderHandlerTest.java b/org.apache.sling.ddr/core/src/test/java/org/apache/sling/ddr/core/DeclarativeDynamicResourceProviderHandlerTest.java
index ee3bdcc..5cc7493 100644
--- a/org.apache.sling.ddr/core/src/test/java/org/apache/sling/ddr/core/DeclarativeDynamicResourceProviderHandlerTest.java
+++ b/org.apache.sling.ddr/core/src/test/java/org/apache/sling/ddr/core/DeclarativeDynamicResourceProviderHandlerTest.java
@@ -91,11 +91,6 @@ public class DeclarativeDynamicResourceProviderHandlerTest {
         catch (RepositoryException ex) {
             throw new RuntimeException("Unable to register namespaces.", ex);
         }
-
-        log.info("Before Loading Test Resources");
-        context.load().json("/ddr-filter/ddr-conf-settings.json", "/conf");
-        context.load().json("/ddr-filter/ddr-apps-settings.json", "/apps");
-        log.info("After Loading Test Resources");
     }
 
     @Test
@@ -108,6 +103,11 @@ public class DeclarativeDynamicResourceProviderHandlerTest {
         final String testPropertyKey = "jcr:title";
         final String testPropertyValue = "Test-1";
 
+        log.info("Before Loading Test Resources");
+        context.load().json("/ddr-filter/ddr-conf-settings.json", "/conf");
+        context.load().json("/ddr-filter/ddr-apps-settings.json", "/apps");
+        log.info("After Loading Test Resources");
+
         Resource dynamicParent = resourceResolver.getResource(dynamicResourceRoot);
         declarativeDynamicResourceProviderHandler.registerService(
             context.bundleContext().getBundle(), dynamicResourceRoot, confResourceRoot,
@@ -147,6 +147,11 @@ public class DeclarativeDynamicResourceProviderHandlerTest {
         final String testPropertyKey = "jcr:title";
         final String testPropertyValue = "Test-1";
 
+        log.info("Before Loading Test Resources");
+        context.load().json("/ddr-filter/ddr-conf-settings.json", "/conf");
+        context.load().json("/ddr-filter/ddr-apps-settings.json", "/apps");
+        log.info("After Loading Test Resources");
+
         Resource dynamicParent = resourceResolver.getResource(dynamicResourceRoot);
 
         declarativeDynamicResourceProviderHandler.registerService(
@@ -177,4 +182,50 @@ public class DeclarativeDynamicResourceProviderHandlerTest {
         assertNotNull("Title Property not found", title);
         assertEquals("Title Property wrong", testPropertyValue, title);
     }
+
+    @Test
+    public void testListReferences() throws Exception {
+        String resourceName = "test1";
+        String confResourceRoot = "/conf/testReference/settings/dynamic";
+        String dynamicResourceRoot = "/apps/dynamicReference";
+        final String testPropertyKey = "jcr:title";
+        final String testPropertyValue = "Test-1";
+
+        context.load().json("/ddr-reference/ddr-conf-settings.json", "/conf");
+        context.load().json("/ddr-reference/ddr-apps-settings.json", "/apps");
+
+        Resource dynamicParent = resourceResolver.getResource(dynamicResourceRoot);
+
+        declarativeDynamicResourceProviderHandler.registerService(
+            context.bundleContext().getBundle(), dynamicResourceRoot, confResourceRoot,
+            resourceResolverFactory, null, null,
+            Arrays.asList("sling:ddrRef")
+        );
+
+        Resource noRef = checkAndGetResource(dynamicParent, "noRef");
+        Resource refNoChild = checkAndGetResource(dynamicParent, "refNoChild");
+        Resource refWithChild = checkAndGetResource(dynamicParent, "refWithChild");
+        Resource refChild = checkAndGetResource(refWithChild, "child");
+        log.info("Ref Child: '{}'", refChild);
+        Resource refGrandchild = checkAndGetResource(refChild, "grandChild");
+        log.info("Ref Grandchild: '{}'", refGrandchild);
+    }
+
+    private Resource checkAndGetResource(Resource parent, String expectedChildName) {
+        Resource answer = null;
+        // List all the children and make sure that only one is returned
+        Iterator<Resource> i = declarativeDynamicResourceProviderHandler.listChildren(
+            resolveContext, parent
+        );
+        assertNotNull("No Iterator returned", i);
+        while(i.hasNext()) {
+            Resource child = i.next();
+            if(child.getName().equals(expectedChildName)) {
+                answer = child;
+                break;
+            }
+        }
+        assertNotNull("Child: '" + expectedChildName + "' not found", answer);
+        return answer;
+    }
 }
diff --git a/org.apache.sling.ddr/core/src/test/resources/ddr-reference/ddr-apps-settings.json b/org.apache.sling.ddr/core/src/test/resources/ddr-reference/ddr-apps-settings.json
new file mode 100644
index 0000000..f57fd00
--- /dev/null
+++ b/org.apache.sling.ddr/core/src/test/resources/ddr-reference/ddr-apps-settings.json
@@ -0,0 +1,28 @@
+{
+  "sling:resourceType": "sling:Folder",
+  "dynamicReference": {
+    "sling:resourceType": "sling:Folder"
+  },
+  "references": {
+    "sling:resourceType": "sling:Folder",
+    "noChild": {
+      "sling:resourceType": "sling:Folder",
+      "jcr:description": "no child reference"
+    },
+    "withChild": {
+      "sling:resourceType": "sling:Folder",
+      "jcr:title": "With-Child-1",
+      "jcr:description": "with child reference",
+      "child": {
+        "sling:resourceType": "sling:Folder",
+        "jcr:title": "Child-1",
+        "jcr:description": "child reference",
+        "grandChild": {
+          "sling:resourceType": "sling:Folder",
+          "jcr:title": "Grandchild-1",
+          "jcr:description": "grandchild reference"
+        }
+      }
+    }
+  }
+}
\ No newline at end of file
diff --git a/org.apache.sling.ddr/core/src/test/resources/ddr-reference/ddr-conf-settings.json b/org.apache.sling.ddr/core/src/test/resources/ddr-reference/ddr-conf-settings.json
new file mode 100644
index 0000000..ee2843a
--- /dev/null
+++ b/org.apache.sling.ddr/core/src/test/resources/ddr-reference/ddr-conf-settings.json
@@ -0,0 +1,33 @@
+{
+  "sling:resourceType": "sling:Folder",
+  "testReference": {
+    "sling:resourceType": "sling:Folder",
+    "settings": {
+      "sling:resourceType": "sling:Folder",
+      "dynamic": {
+        "sling:resourceType": "sling:DDR",
+        "sling:ddrTarget": "/apps/dynamicReference",
+        "noRef": {
+          "jcr:primaryType": "sling:Folder",
+          "sling:resourceType": "test/noRef",
+          "sling:resourceSuperType": "test/static",
+          "jcr:title": "No-Ref-1"
+        },
+        "refNoChild": {
+          "jcr:primaryType": "sling:Folder",
+          "sling:resourceType": "test/refNoChild",
+          "sling:resourceSuperType": "test/static",
+          "jcr:title": "Ref-No-Child-1",
+          "sling:ddrRef": "/apps/references/noChild"
+        },
+        "refWithChild": {
+          "jcr:primaryType": "sling:Folder",
+          "sling:resourceType": "test/refWithChild",
+          "sling:resourceSuperType": "test/static",
+          "jcr:title": "Ref-With-Child-1",
+          "sling:ddrRef": "/apps/references/withChild"
+        }
+      }
+    }
+  }
+}
\ No newline at end of file
diff --git a/org.apache.sling.ddr/sample.installation/src/main/resources/SLING-CONTENT/apps/ddr-static/ref.json b/org.apache.sling.ddr/sample.installation/src/main/resources/SLING-CONTENT/apps/ddr-static/ref.json
index e0fc720..242e347 100644
--- a/org.apache.sling.ddr/sample.installation/src/main/resources/SLING-CONTENT/apps/ddr-static/ref.json
+++ b/org.apache.sling.ddr/sample.installation/src/main/resources/SLING-CONTENT/apps/ddr-static/ref.json
@@ -1,4 +1,12 @@
 {
   "jcr:primaryType": "sling:Folder",
-  "jcr:title": "ref-1"
+  "jcr:title": "ref-1",
+  "child": {
+    "jcr:primaryType": "sling:Folder",
+    "jcr:title": "Ref Child",
+    "grandChild": {
+      "jcr:primaryType": "sling:Folder",
+      "jcr:title": "Ref Grandchild"
+    }
+  }
 }
\ No newline at end of file