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 2021/06/10 15:34:18 UTC

[sling-whiteboard] branch master updated: DefaultContentGenerator

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 cf45fb6  DefaultContentGenerator
cf45fb6 is described below

commit cf45fb656a91f9dbb6f548e50b91e940d503cde4
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Thu Jun 10 17:33:58 2021 +0200

    DefaultContentGenerator
---
 remote-content-api/sample-graphql-api/README.md    | 25 +++++++--
 .../remotecontent/contentmodel/Backstage.java      |  7 ++-
 .../contentmodel/ContentGenerator.java             |  1 +
 .../sling/remotecontent/contentmodel/Document.java | 18 +------
 .../contentmodel/UnstructuredContent.java          |  8 +--
 .../samples/graphql/DefaultContentGenerator.java   | 61 ++++++++++++++++++++++
 .../samples/graphql/DocumentDataFetcher.java       |  7 ++-
 .../samples/graphql/DocumentsDataFetcher.java      |  7 ++-
 .../graphql/DummyContentGeneratorSupplier.java     | 41 ---------------
 .../samples/graphql/FolderDataFetcher.java         |  7 ++-
 .../samples/graphql/FoldersDataFetcher.java        |  7 ++-
 11 files changed, 116 insertions(+), 73 deletions(-)

diff --git a/remote-content-api/sample-graphql-api/README.md b/remote-content-api/sample-graphql-api/README.md
index 8d622b8..7adc603 100644
--- a/remote-content-api/sample-graphql-api/README.md
+++ b/remote-content-api/sample-graphql-api/README.md
@@ -37,6 +37,7 @@ we don't really care about the details of these node types besides their names.
 This prototype is evolving, some of these examples might be out of date. See the
 [Schema for the API plane N](src/main/resources/schemas/default/N.GQLschema.jsp) for possible Queries and Mutations.
 
+    # This one works at http://localhost:8080/content.N.json
     {
       folders(limit: 55, after: "L2NvbnRlbnQvYXJ0aWNsZXMvbXVzaWM=") {
         pageInfo {
@@ -54,6 +55,7 @@ This prototype is evolving, some of these examples might be out of date. See the
       }
     }
 
+    # Works well at http://localhost:8080/content.N.json
     {
       folder {
         path
@@ -100,7 +102,6 @@ This prototype is evolving, some of these examples might be out of date. See the
             href
             rel
           }
-          etc
         }
         backstage {
           authoring
@@ -118,8 +119,18 @@ This prototype is evolving, some of these examples might be out of date. See the
             path
             header {
               parent
-              resourceType 
-              resourceSuperType 
+              resourceType
+              resourceSuperType
+            }
+            body {
+              source
+              content
+            }
+            backstage {
+              authoring {
+                source
+                content
+              }
             }
           }
         }
@@ -159,10 +170,16 @@ This prototype is evolving, some of these examples might be out of date. See the
         where [sling:resourceType] = 'wknd/components/carousel'
         and isdescendantnode(R, '/content/wknd/us/en')
       """) {
+        pageInfo {
+          endCursor
+        }
         edges {
           node {
             path
+            body {
+              content
+            }
           }
         }
       }
-    }
+    }
\ No newline at end of file
diff --git a/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/contentmodel/Backstage.java b/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/contentmodel/Backstage.java
index 54bf2ea..becf0e7 100644
--- a/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/contentmodel/Backstage.java
+++ b/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/contentmodel/Backstage.java
@@ -32,10 +32,9 @@ public class Backstage {
     private final UnstructuredContent etc;
 
     public Backstage(Resource resource, Supplier<ContentGenerator> contentGeneratorSupplier) {
-        final String source = "TODO indicate source";
-        authoring = new UnstructuredContent(resource, "authoring", source, contentGeneratorSupplier);
-        publishing = new UnstructuredContent(resource, "publishing", source, contentGeneratorSupplier);
-        etc = new UnstructuredContent(resource, "etc", source, contentGeneratorSupplier);
+        authoring = new UnstructuredContent(resource, "authoring", contentGeneratorSupplier);
+        publishing = new UnstructuredContent(resource, "publishing", contentGeneratorSupplier);
+        etc = new UnstructuredContent(resource, "etc", contentGeneratorSupplier);
     }
 
     public UnstructuredContent getAuthoring() {
diff --git a/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/contentmodel/ContentGenerator.java b/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/contentmodel/ContentGenerator.java
index b09d729..610ec63 100644
--- a/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/contentmodel/ContentGenerator.java
+++ b/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/contentmodel/ContentGenerator.java
@@ -22,5 +22,6 @@ package org.apache.sling.remotecontent.contentmodel;
 import org.apache.sling.api.resource.Resource;
 
 public interface ContentGenerator {
+    String getSourceInfo(Resource r, String name);
     Object getContent(Resource r, String name);
 }
\ No newline at end of file
diff --git a/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/contentmodel/Document.java b/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/contentmodel/Document.java
index c91e7e8..156d768 100644
--- a/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/contentmodel/Document.java
+++ b/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/contentmodel/Document.java
@@ -30,26 +30,10 @@ public class Document extends ContentItem {
 
     public Document(Resource r, Supplier<ContentGenerator> contentGeneratorSupplier) {
         super(r, contentGeneratorSupplier);
-        body = new UnstructuredContent(resource, "body", "No source yet", contentGeneratorSupplier);
+        body = new UnstructuredContent(resource, "body", contentGeneratorSupplier);
     }
 
     public UnstructuredContent getBody() {
-        /*
-        @Reference(target="(" + DocumentTree.TARGET_TYPE + "=map)")
-        private DocumentTree mappingTarget;
-
-        @Reference
-        private DocumentAggregator documentAggregator;
-        
-        // Use the aggregator to build the body
-        if(mappingTarget != null) {
-            final DocumentTree.DocumentNode body = mappingTarget.newDocumentNode();
-            final DocumentAggregator.Options opt = new DocumentAggregator.Options(false, new UrlBuilderStub());
-            documentAggregator.aggregate(r, body, opt);
-            body.close();
-            data.put("body", body.adaptTo(Map.class));
-        }
-        */
         return body;
     }
 
diff --git a/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/contentmodel/UnstructuredContent.java b/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/contentmodel/UnstructuredContent.java
index 28086df..7a3c164 100644
--- a/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/contentmodel/UnstructuredContent.java
+++ b/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/contentmodel/UnstructuredContent.java
@@ -29,14 +29,13 @@ import org.apache.sling.api.resource.Resource;
 public class UnstructuredContent {
     private final Resource resource;
     private final String name;
-    private final String source;
+    private String source;
     private Object content;
     private final Supplier<ContentGenerator> contentGeneratorSupplier;
 
-    public UnstructuredContent(Resource resource, String name, String source, Supplier<ContentGenerator> contentGeneratorSupplier) {
+    public UnstructuredContent(Resource resource, String name, Supplier<ContentGenerator> contentGeneratorSupplier) {
         this.resource = resource;
         this.name = name;
-        this.source = source;
         this.contentGeneratorSupplier = contentGeneratorSupplier;
     }
 
@@ -45,6 +44,9 @@ public class UnstructuredContent {
     }
 
     public String getSource() {
+        if(source == null) {
+            source = contentGeneratorSupplier.get().getSourceInfo(resource, name);
+        }
         return source;
     }
 
diff --git a/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/samples/graphql/DefaultContentGenerator.java b/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/samples/graphql/DefaultContentGenerator.java
new file mode 100644
index 0000000..fc473ad
--- /dev/null
+++ b/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/samples/graphql/DefaultContentGenerator.java
@@ -0,0 +1,61 @@
+/*
+ * 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.remotecontent.samples.graphql;
+
+import java.util.Collections;
+import java.util.Map;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.documentaggregator.api.DocumentAggregator;
+import org.apache.sling.documentaggregator.api.DocumentTree;
+import org.apache.sling.remotecontent.contentmodel.ContentGenerator;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
+
+@Component(service = ContentGenerator.class)
+public class DefaultContentGenerator implements ContentGenerator {
+
+    public static final String BODY = "body";
+
+    @Reference(target="(" + DocumentTree.TARGET_TYPE + "=map)")
+    private DocumentTree mappingTarget;
+
+    @Reference
+    private DocumentAggregator documentAggregator;
+
+    @Override
+    public String getSourceInfo(Resource r, String name) {
+        return String.format("%s for '%s'", getClass().getName(), name);
+    }
+
+    @Override
+    public Object getContent(Resource r, String name) {
+        if(!BODY.equals(name)) {
+            return Collections.singletonMap("nocontent", String.format("No content available for '%s'", name));
+        }
+
+        final DocumentTree.DocumentNode body = mappingTarget.newDocumentNode();
+        final DocumentAggregator.Options opt = new DocumentAggregator.Options(false, new UrlBuilderStub());
+        documentAggregator.aggregate(r, body, opt);
+        body.close();
+        return body.adaptTo(Map.class);
+    }
+
+}
\ No newline at end of file
diff --git a/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/samples/graphql/DocumentDataFetcher.java b/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/samples/graphql/DocumentDataFetcher.java
index 18c3cca..da13618 100644
--- a/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/samples/graphql/DocumentDataFetcher.java
+++ b/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/samples/graphql/DocumentDataFetcher.java
@@ -21,16 +21,21 @@ package org.apache.sling.remotecontent.samples.graphql;
 
 import org.apache.sling.graphql.api.SlingDataFetcher;
 import org.apache.sling.graphql.api.SlingDataFetcherEnvironment;
+import org.apache.sling.remotecontent.contentmodel.ContentGenerator;
 import org.apache.sling.remotecontent.contentmodel.Document;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
 
 @Component(service = SlingDataFetcher.class, property = {"name=samples/document"})
 public class DocumentDataFetcher implements SlingDataFetcher<Document> {
 
+    @Reference
+    private ContentGenerator contentGenerator;
+    
     @Override
     public @Nullable Document get(@NotNull SlingDataFetcherEnvironment e) throws Exception {
-        return new Document(new FetcherContext(e, false).currentResource, new DummyContentGeneratorSupplier());
+        return new Document(new FetcherContext(e, false).currentResource, () -> contentGenerator);
     }   
 }
\ No newline at end of file
diff --git a/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/samples/graphql/DocumentsDataFetcher.java b/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/samples/graphql/DocumentsDataFetcher.java
index fb3069c..590d925 100644
--- a/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/samples/graphql/DocumentsDataFetcher.java
+++ b/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/samples/graphql/DocumentsDataFetcher.java
@@ -26,14 +26,19 @@ import org.apache.sling.graphql.api.SlingDataFetcher;
 import org.apache.sling.graphql.api.SlingDataFetcherEnvironment;
 import org.apache.sling.graphql.api.pagination.Connection;
 import org.apache.sling.graphql.helpers.GenericConnection;
+import org.apache.sling.remotecontent.contentmodel.ContentGenerator;
 import org.apache.sling.remotecontent.contentmodel.Document;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
 
 @Component(service = SlingDataFetcher.class, property = {"name=samples/documents"})
 public class DocumentsDataFetcher implements SlingDataFetcher<Connection<Document>> {
 
+    @Reference
+    private ContentGenerator contentGenerator;
+
     @Override
     public @Nullable Connection<Document> get(@NotNull SlingDataFetcherEnvironment e) throws Exception {
         final FetcherContext ctx = new FetcherContext(e, true);
@@ -49,7 +54,7 @@ public class DocumentsDataFetcher implements SlingDataFetcher<Connection<Documen
         final String query = e.getArgument("query");
 
         final Iterator<Resource> resultIterator = ctx.currentResource.getResourceResolver().findResources(query, lang);
-        final Iterator<Document> it = new ConvertingIterator<>(resultIterator, r -> new Document(r, new DummyContentGeneratorSupplier()));
+        final Iterator<Document> it = new ConvertingIterator<>(resultIterator, r -> new Document(r, () -> contentGenerator));
         return new GenericConnection.Builder<>(it, Document::getPath)
             .withStartAfter(ctx.afterCursor)
             .withLimit(ctx.limit)
diff --git a/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/samples/graphql/DummyContentGeneratorSupplier.java b/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/samples/graphql/DummyContentGeneratorSupplier.java
deleted file mode 100644
index 3f911a2..0000000
--- a/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/samples/graphql/DummyContentGeneratorSupplier.java
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * 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.remotecontent.samples.graphql;
-
-import java.util.HashMap;
-import java.util.Map;
-import java.util.function.Supplier;
-
-import org.apache.sling.api.resource.Resource;
-import org.apache.sling.remotecontent.contentmodel.ContentGenerator;
-
-public class DummyContentGeneratorSupplier implements Supplier<ContentGenerator> {
-
-    @Override
-    public ContentGenerator get() {
-        return (Resource r, String name) -> {
-            final Map<String, Object> result = new HashMap<>();
-            result.put(
-                "dummy-content", 
-                String.format("This is the %s dummy content for resource %s", name, r.getPath()));
-            return result;
-        };
-    }
-}
\ No newline at end of file
diff --git a/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/samples/graphql/FolderDataFetcher.java b/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/samples/graphql/FolderDataFetcher.java
index 35967aa..d35e808 100644
--- a/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/samples/graphql/FolderDataFetcher.java
+++ b/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/samples/graphql/FolderDataFetcher.java
@@ -21,16 +21,21 @@ package org.apache.sling.remotecontent.samples.graphql;
 
 import org.apache.sling.graphql.api.SlingDataFetcher;
 import org.apache.sling.graphql.api.SlingDataFetcherEnvironment;
+import org.apache.sling.remotecontent.contentmodel.ContentGenerator;
 import org.apache.sling.remotecontent.contentmodel.Folder;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
 
 @Component(service = SlingDataFetcher.class, property = {"name=samples/folder"})
 public class FolderDataFetcher implements SlingDataFetcher<Object> {
 
+    @Reference
+    private ContentGenerator contentGenerator;
+
     @Override
     public @Nullable Object get(@NotNull SlingDataFetcherEnvironment e) throws Exception {
-        return new Folder(new FetcherContext(e, false).currentResource, new DummyContentGeneratorSupplier());
+        return new Folder(new FetcherContext(e, false).currentResource, () -> contentGenerator);
     }   
 }
\ No newline at end of file
diff --git a/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/samples/graphql/FoldersDataFetcher.java b/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/samples/graphql/FoldersDataFetcher.java
index 74bafb9..2a8bf12 100644
--- a/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/samples/graphql/FoldersDataFetcher.java
+++ b/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/samples/graphql/FoldersDataFetcher.java
@@ -26,14 +26,19 @@ import org.apache.sling.graphql.api.SlingDataFetcher;
 import org.apache.sling.graphql.api.SlingDataFetcherEnvironment;
 import org.apache.sling.graphql.api.pagination.Connection;
 import org.apache.sling.graphql.helpers.GenericConnection;
+import org.apache.sling.remotecontent.contentmodel.ContentGenerator;
 import org.apache.sling.remotecontent.contentmodel.Folder;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
 
 @Component(service = SlingDataFetcher.class, property = {"name=samples/folders"})
 public class FoldersDataFetcher implements SlingDataFetcher<Connection<Folder>> {
 
+    @Reference
+    private ContentGenerator contentGenerator;
+
     @Override
     public @Nullable Connection<Folder> get(@NotNull SlingDataFetcherEnvironment e) throws Exception {
         final FetcherContext ctx = new FetcherContext(e, true);
@@ -43,7 +48,7 @@ public class FoldersDataFetcher implements SlingDataFetcher<Connection<Folder>>
             ctx.currentResource.getPath());
 
         final Iterator<Resource> resultIterator = ctx.currentResource.getResourceResolver().findResources(xpathQuery, "xpath");
-        final Iterator<Folder> it = new ConvertingIterator<>(resultIterator, r -> new Folder(r, new DummyContentGeneratorSupplier()));
+        final Iterator<Folder> it = new ConvertingIterator<>(resultIterator, r -> new Folder(r, () -> contentGenerator));
         return new GenericConnection.Builder<>(it, Folder::getPath)
             .withStartAfter(ctx.afterCursor)
             .withLimit(ctx.limit)