You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2008/01/16 10:34:05 UTC

svn commit: r612394 - in /incubator/sling/trunk/sling/servlet-resolver: pom.xml src/main/java/org/apache/sling/servlet/resolver/defaults/DefaultServlet.java src/main/java/org/apache/sling/servlet/resolver/helper/JsonItemWriter.java

Author: cziegeler
Date: Wed Jan 16 01:34:03 2008
New Revision: 612394

URL: http://svn.apache.org/viewvc?rev=612394&view=rev
Log:
Interim solution for streaming json representation. The max recursion level can be set using a request paramter, default is 1.

Added:
    incubator/sling/trunk/sling/servlet-resolver/src/main/java/org/apache/sling/servlet/resolver/helper/JsonItemWriter.java   (with props)
Modified:
    incubator/sling/trunk/sling/servlet-resolver/pom.xml
    incubator/sling/trunk/sling/servlet-resolver/src/main/java/org/apache/sling/servlet/resolver/defaults/DefaultServlet.java

Modified: incubator/sling/trunk/sling/servlet-resolver/pom.xml
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/servlet-resolver/pom.xml?rev=612394&r1=612393&r2=612394&view=diff
==============================================================================
--- incubator/sling/trunk/sling/servlet-resolver/pom.xml (original)
+++ incubator/sling/trunk/sling/servlet-resolver/pom.xml Wed Jan 16 01:34:03 2008
@@ -79,7 +79,9 @@
                             org.apache.sling.api.servlets,
                             org.apache.sling.core.servlets,
                             org.osgi.framework,
-                            org.osgi.service.component, org.slf4j
+                            org.osgi.service.component, org.slf4j,
+                            javax.jcr, javax.jcr.nodetype, org.apache.sling.commons.json,
+                            org.apache.sling.commons.json.io
                         </Import-Package>
                     </instructions>
                 </configuration>
@@ -99,6 +101,11 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.core</artifactId>
+            <version>2.0.0-incubator-SNAPSHOT</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.commons.json</artifactId>
             <version>2.0.0-incubator-SNAPSHOT</version>
         </dependency>
         <dependency>

Modified: incubator/sling/trunk/sling/servlet-resolver/src/main/java/org/apache/sling/servlet/resolver/defaults/DefaultServlet.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/servlet-resolver/src/main/java/org/apache/sling/servlet/resolver/defaults/DefaultServlet.java?rev=612394&r1=612393&r2=612394&view=diff
==============================================================================
--- incubator/sling/trunk/sling/servlet-resolver/src/main/java/org/apache/sling/servlet/resolver/defaults/DefaultServlet.java (original)
+++ incubator/sling/trunk/sling/servlet-resolver/src/main/java/org/apache/sling/servlet/resolver/defaults/DefaultServlet.java Wed Jan 16 01:34:03 2008
@@ -28,6 +28,8 @@
 import java.util.Properties;
 import java.util.TreeMap;
 
+import javax.jcr.Node;
+import javax.jcr.RepositoryException;
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.commons.beanutils.BeanMap;
@@ -36,6 +38,8 @@
 import org.apache.sling.api.resource.NonExistingResource;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
+import org.apache.sling.commons.json.JSONException;
+import org.apache.sling.servlet.resolver.helper.JsonItemWriter;
 
 /**
  * The <code>DefaultServlet</code> is a very simple default resource handler.
@@ -46,6 +50,10 @@
  */
 public class DefaultServlet extends SlingSafeMethodsServlet {
 
+    /** This optional request parameter sets the recursion level
+     *  (into chldren) when dumping a node */
+    public static final String PARAM_RECURSION_LEVEL = "maxlevels";
+
     @Override
     protected void doGet(SlingHttpServletRequest request,
             SlingHttpServletResponse response) throws IOException {
@@ -67,7 +75,7 @@
         } else if ("properties".equals(extension)) {
             this.renderContentProperties(resource, response);
         } else if ("json".equals(extension)) {
-            this.renderContentJson(resource, response);
+            this.renderContentJson(resource, request, response);
         } else {
             // default rendering as plain text
             this.renderContentText(resource, response);
@@ -166,58 +174,31 @@
     }
 
     private void renderContentJson(Resource resource,
+            SlingHttpServletRequest request,
             SlingHttpServletResponse response) throws IOException {
-
-        Map<Object, Object> contentMap = new TreeMap<Object, Object>(
-            this.asMap(resource));
-
-        response.setContentType("text/x-json; charset=UTF-8");
-        PrintWriter pw = response.getWriter();
-
-        pw.println("{");
-
-        boolean notFirst = false;
-        for (Map.Entry<Object, Object> entry : contentMap.entrySet()) {
-
-            if (notFirst) {
-                pw.println(',');
-            } else {
-                notFirst = true;
-            }
-
-            pw.print("  \"" + entry.getKey() + "\": ");
-
-            if (entry.getValue() instanceof Collection) {
-                pw.println("[");
-                Collection<?> coll = (Collection<?>) entry.getValue();
-                for (Iterator<?> ci = coll.iterator(); ci.hasNext();) {
-                    pw.print("    ");
-                    this.printObjectJson(pw, ci.next());
-                    if (ci.hasNext()) {
-                        pw.println(',');
-                    }
-                }
-                pw.println();
-                pw.print("  ]");
-
-            } else {
-                this.printObjectJson(pw, entry.getValue());
+        // how many levels deep?
+        int maxRecursionLevels = 1;
+        if (request.getRequestPathInfo().getSelectors().length > 0) {
+            try {
+                maxRecursionLevels = Integer.parseInt(request.getRequestPathInfo().getSelectors()[0]);
+            } catch(Exception e) {
+                // TODO ignore
             }
         }
 
-        pw.println();
-        pw.println("}");
-
-    }
-
-    private void printObjectJson(PrintWriter pw, Object object) {
-        boolean quote = !((object instanceof Boolean) || (object instanceof Number));
-        if (quote) {
-            pw.print('"');
-        }
-        pw.print(object);
-        if (quote) {
-            pw.print('"');
+        response.setContentType("text/x-json");
+        response.setCharacterEncoding("UTF-8");
+        final PrintWriter pw = response.getWriter();
+        final JsonItemWriter itemWriter = new JsonItemWriter(null);
+        try {
+            final Node node =resource.adaptTo(Node.class);
+            if ( node != null ) {
+                itemWriter.dump(node, pw, maxRecursionLevels);
+            }
+        } catch(JSONException je) {
+            throw new IOException(je.getMessage());
+        } catch(RepositoryException re) {
+            throw new IOException(re.getMessage());
         }
     }
 

Added: incubator/sling/trunk/sling/servlet-resolver/src/main/java/org/apache/sling/servlet/resolver/helper/JsonItemWriter.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/servlet-resolver/src/main/java/org/apache/sling/servlet/resolver/helper/JsonItemWriter.java?rev=612394&view=auto
==============================================================================
--- incubator/sling/trunk/sling/servlet-resolver/src/main/java/org/apache/sling/servlet/resolver/helper/JsonItemWriter.java (added)
+++ incubator/sling/trunk/sling/servlet-resolver/src/main/java/org/apache/sling/servlet/resolver/helper/JsonItemWriter.java Wed Jan 16 01:34:03 2008
@@ -0,0 +1,161 @@
+/*
+ * 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.servlet.resolver.helper;
+
+import java.io.Writer;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Locale;
+import java.util.Set;
+
+import javax.jcr.Node;
+import javax.jcr.NodeIterator;
+import javax.jcr.Property;
+import javax.jcr.PropertyIterator;
+import javax.jcr.PropertyType;
+import javax.jcr.RepositoryException;
+import javax.jcr.Value;
+import javax.jcr.ValueFormatException;
+
+import org.apache.sling.commons.json.JSONException;
+import org.apache.sling.commons.json.io.JSONWriter;
+
+/** Dumps JCR Items as JSON data. The dump methods
+ *  are threadsafe.
+ */
+public class JsonItemWriter {
+    final Set<String> propertyNamesToIgnore;
+
+    /** Create a JsonItemWriter
+     *  @param propertyNamesToIgnore if not null, a property having a name from this
+     *  set of values is ignored.
+     *  TODO we should use a filtering interface to make the selection of which Nodes
+     *  and Properties to dump more flexible.
+     */
+    public JsonItemWriter(Set<String> propertyNamesToIgnore) {
+        this.propertyNamesToIgnore = propertyNamesToIgnore;
+    }
+
+    /** Dump all Nodes of given NodeIterator in JSON
+     * @throws JSONException */
+    public void dump(NodeIterator it, Writer out) throws RepositoryException, JSONException {
+        final JSONWriter w = new JSONWriter(out);
+        w.array();
+        while (it.hasNext()) {
+            dumpSingleNode(it.nextNode(), w, 1, 0);
+        }
+        w.endArray();
+    }
+
+    /** Dump given node in JSON, optionally recursing into its child nodes */
+    public void dump(Node node, Writer w, int maxRecursionLevels) throws RepositoryException, JSONException {
+        dump(node, new JSONWriter(w), 0, maxRecursionLevels);
+    }
+
+    /** Dump given property in JSON */
+    public void dump(Property p, Writer w) throws JSONException, ValueFormatException, RepositoryException {
+        final JSONWriter jw = new JSONWriter(w);
+        jw.object();
+        writeProperty(jw, 0, p);
+        jw.endObject();
+    }
+
+    /** Dump given node in JSON, optionally recursing into its child nodes */
+    protected void dump(Node node, JSONWriter w, int currentRecursionLevel, int maxRecursionLevels)
+    throws RepositoryException, JSONException {
+
+        w.object();
+        PropertyIterator props = node.getProperties();
+
+        // the node's actual properties
+        while (props.hasNext()) {
+            Property prop = props.nextProperty();
+
+            if (propertyNamesToIgnore!=null && propertyNamesToIgnore.contains(prop.getName())) {
+                continue;
+            }
+
+            if (!prop.getDefinition().isMultiple()) {
+                writeProperty(w, currentRecursionLevel, prop);
+            } else {
+                w.array();
+                for(Value v : prop.getValues()) {
+                    w.value(convertValue(v));
+                }
+                w.endArray();
+            }
+        }
+
+        // the child nodes
+        if(recursionLevelActive(currentRecursionLevel, maxRecursionLevels)) {
+            final NodeIterator children = node.getNodes();
+            while(children.hasNext()) {
+                final Node n = children.nextNode();
+                dumpSingleNode(n, w, currentRecursionLevel, maxRecursionLevels);
+            }
+        }
+
+        w.endObject();
+    }
+
+    /** Dump a single node */
+    protected void dumpSingleNode(Node n, JSONWriter w, int currentRecursionLevel, int maxRecursionLevels)
+    throws RepositoryException, JSONException {
+        if (recursionLevelActive(currentRecursionLevel, maxRecursionLevels)) {
+            w.key(n.getName());
+            dump(n, w, currentRecursionLevel + 1, maxRecursionLevels);
+        }
+    }
+
+    /** true if the current recursion level is active */
+    protected boolean recursionLevelActive(int currentRecursionLevel, int maxRecursionLevels) {
+        return maxRecursionLevels < 0 || currentRecursionLevel < maxRecursionLevels;
+    }
+
+    /**
+     * Write a single property
+     */
+    protected void writeProperty(JSONWriter w, int indent, Property p)
+    throws ValueFormatException, RepositoryException, JSONException {
+        if(p.getType() == PropertyType.BINARY) {
+            // TODO for now we mark binary properties with an initial star in their name
+            // (star is not allowed as a JCR property name)
+            // in the name, and the value should be the size of the binary data
+            w.key("*" + p.getName());
+
+        } else {
+            w.key(p.getName());
+        }
+
+        w.value(convertValue(p.getValue()));
+    }
+
+    /** Convert a Value for JSON output */
+    protected Object convertValue(Value v) throws ValueFormatException, IllegalStateException, RepositoryException {
+        if(v.getType() == PropertyType.BINARY) {
+            // TODO return the binary size
+            return new Integer(0);
+
+        } else if(v.getType() == PropertyType.DATE) {
+            final DateFormat fmt = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'Z", Locale.US);
+            return fmt.format(v.getDate().getTime());
+
+        } else {
+            return v.getString();
+        }
+    }
+}

Propchange: incubator/sling/trunk/sling/servlet-resolver/src/main/java/org/apache/sling/servlet/resolver/helper/JsonItemWriter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/sling/servlet-resolver/src/main/java/org/apache/sling/servlet/resolver/helper/JsonItemWriter.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision url