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/10/22 16:08:04 UTC

[sling-whiteboard] branch master updated: Saving jsonifier work-in-progress to keep it around but that should really use MapOfMaps, see TODO in JsonRenderer

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 82700d3  Saving jsonifier work-in-progress to keep it around but that should really use MapOfMaps, see TODO in JsonRenderer
82700d3 is described below

commit 82700d360af5ae1597f7a9b282ade63048d1f1c0
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Thu Oct 22 18:05:03 2020 +0200

    Saving jsonifier work-in-progress to keep it around but that should really use MapOfMaps, see TODO in JsonRenderer
---
 .../apache/sling/jsonifier/api/JsonRenderer.java   | 45 ++++++++++++++
 .../sling/jsonifier/api/JsonRendererSelector.java  | 29 +++++++++
 .../sling/jsonifier/impl/DefaultJsonRenderer.java  | 47 +++++++++++++++
 .../jsonifier/impl/JsonRendererSelectorImpl.java   | 37 ++++++++++++
 .../sling/jsonifier/servlet/RendererServlet.java   | 70 ++++++++++++++++++++++
 .../resources/SLING-INF/initial-content/ROOT.json  |  2 +-
 6 files changed, 229 insertions(+), 1 deletion(-)

diff --git a/remote-content-api/src/main/java/org/apache/sling/jsonifier/api/JsonRenderer.java b/remote-content-api/src/main/java/org/apache/sling/jsonifier/api/JsonRenderer.java
new file mode 100644
index 0000000..10b8cf3
--- /dev/null
+++ b/remote-content-api/src/main/java/org/apache/sling/jsonifier/api/JsonRenderer.java
@@ -0,0 +1,45 @@
+/*
+ * 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.jsonifier.api;
+
+import javax.json.JsonObjectBuilder;
+
+import org.apache.sling.api.resource.Resource;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+public interface JsonRenderer {
+    enum Mode {
+        CONTENT,
+        NAVIGATION
+    };
+
+    String SP_RESOURCE_TYPES = "sling.json.resourceTypes";
+    String SP_SELECTORS = "sling.json.selectors";
+
+    /** Render supplied Resource to JSON according to supplied selectors
+     *  TODO the target should be a MapOfMaps that can be used for both
+     *  JSON serialization and as part of a GraphQL query response
+    */
+    void render(@NotNull JsonObjectBuilder target, @NotNull Resource r, Mode mode, @Nullable String [] selectors);
+
+    /** Decide whether to recurse into supplied child resource */
+    boolean recurseInto(Resource child, Mode mode);
+}
\ No newline at end of file
diff --git a/remote-content-api/src/main/java/org/apache/sling/jsonifier/api/JsonRendererSelector.java b/remote-content-api/src/main/java/org/apache/sling/jsonifier/api/JsonRendererSelector.java
new file mode 100644
index 0000000..7921e14
--- /dev/null
+++ b/remote-content-api/src/main/java/org/apache/sling/jsonifier/api/JsonRendererSelector.java
@@ -0,0 +1,29 @@
+/*
+ * 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.jsonifier.api;
+
+import org.apache.sling.api.resource.Resource;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+public interface JsonRendererSelector {
+    /** @return a renderer for the supplied Resource  */
+    @NotNull JsonRenderer select(@NotNull Resource r, @Nullable String [] selectors);
+}
\ No newline at end of file
diff --git a/remote-content-api/src/main/java/org/apache/sling/jsonifier/impl/DefaultJsonRenderer.java b/remote-content-api/src/main/java/org/apache/sling/jsonifier/impl/DefaultJsonRenderer.java
new file mode 100644
index 0000000..a076c51
--- /dev/null
+++ b/remote-content-api/src/main/java/org/apache/sling/jsonifier/impl/DefaultJsonRenderer.java
@@ -0,0 +1,47 @@
+/*
+ * 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.jsonifier.impl;
+
+import javax.json.JsonObjectBuilder;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.jsonifier.api.JsonRenderer;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import org.osgi.service.component.annotations.Component;
+
+@Component(
+    service = JsonRenderer.class,
+    property = {
+        JsonRenderer.SP_RESOURCE_TYPES + "=" + JsonRendererSelectorImpl.DEFAULT_RESOURCE_TYPE,
+    })
+class DefaultJsonRenderer implements JsonRenderer {
+
+    @Override
+    public void render(@NotNull JsonObjectBuilder target, @NotNull Resource r, Mode mode,
+        @Nullable String[] selectors) {
+        target.add("source", getClass().getName());
+    }
+
+    @Override
+    public boolean recurseInto(Resource child, Mode mode) {
+        return true;
+    }
+}
\ No newline at end of file
diff --git a/remote-content-api/src/main/java/org/apache/sling/jsonifier/impl/JsonRendererSelectorImpl.java b/remote-content-api/src/main/java/org/apache/sling/jsonifier/impl/JsonRendererSelectorImpl.java
new file mode 100644
index 0000000..e181726
--- /dev/null
+++ b/remote-content-api/src/main/java/org/apache/sling/jsonifier/impl/JsonRendererSelectorImpl.java
@@ -0,0 +1,37 @@
+/*
+ * 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.jsonifier.impl;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.jsonifier.api.JsonRendererSelector;
+import org.apache.sling.jsonifier.api.JsonRenderer;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import org.osgi.service.component.annotations.Component;
+
+@Component(service = JsonRendererSelector.class)
+public class JsonRendererSelectorImpl implements JsonRendererSelector {
+    public static final String DEFAULT_RESOURCE_TYPE = "sling/servlet/default";
+
+    /** @return a ResourceJsonifier for the supplied Resource  */
+    public @NotNull JsonRenderer select(@NotNull Resource r, @Nullable String [] selectors) {
+        return new DefaultJsonRenderer();
+    }
+}
\ No newline at end of file
diff --git a/remote-content-api/src/main/java/org/apache/sling/jsonifier/servlet/RendererServlet.java b/remote-content-api/src/main/java/org/apache/sling/jsonifier/servlet/RendererServlet.java
new file mode 100644
index 0000000..6257e5c
--- /dev/null
+++ b/remote-content-api/src/main/java/org/apache/sling/jsonifier/servlet/RendererServlet.java
@@ -0,0 +1,70 @@
+/*
+ * 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.jsonifier.servlet;
+
+import java.io.IOException;
+
+import javax.json.Json;
+import javax.json.JsonObjectBuilder;
+import javax.servlet.Servlet;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
+import org.apache.sling.jsonifier.api.JsonRendererSelector;
+import org.apache.sling.jsonifier.api.JsonRenderer;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
+
+/** Render the current Resource to JSON */
+@Component(service = Servlet.class,
+    property = {
+            "sling.servlet.resourceTypes=sling/servlet/default",
+            "sling.servlet.prefix:Integer=-1",
+
+            "sling.servlet.methods=GET",
+            "sling.servlet.methods=HEAD",
+            "sling.servlet.selectors=s:jr",
+            "sling.servlet.extension=json",
+    })
+public class RendererServlet extends SlingSafeMethodsServlet {
+    private static final long serialVersionUID = 1L;
+
+    @Reference
+    private transient JsonRendererSelector renderers;
+
+    @Override
+    public void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
+        final JsonObjectBuilder result = Json.createObjectBuilder();
+
+        final Resource resource = request.getResource();
+        final String [] selectors = request.getRequestPathInfo().getSelectors();
+
+        final JsonRenderer renderer = renderers.select(
+            request.getResource(), 
+            request.getRequestPathInfo().getSelectors());
+        renderer.render(result, resource, JsonRenderer.Mode.CONTENT, selectors);
+
+        response.setCharacterEncoding("UTF-8");
+        response.setContentType("application/json");
+        response.getWriter().write(result.build().toString());
+    }
+}
\ No newline at end of file
diff --git a/remote-content-api/src/main/resources/SLING-INF/initial-content/ROOT.json b/remote-content-api/src/main/resources/SLING-INF/initial-content/ROOT.json
index 456502e..52efecc 100644
--- a/remote-content-api/src/main/resources/SLING-INF/initial-content/ROOT.json
+++ b/remote-content-api/src/main/resources/SLING-INF/initial-content/ROOT.json
@@ -1,4 +1,4 @@
 {
     "sling:resourceType" : "sling:redirect",
-    "sling:target" : "/content.s:t5.json"
+    "sling:target" : "/content.s:jr.json"
 }
\ No newline at end of file