You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cl...@apache.org on 2009/08/17 22:42:19 UTC

svn commit: r805130 - in /sling/trunk: bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/ bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/helpers/ launchpad/testing/src/test/java/org/apache/sling/launchpad/weba...

Author: clombart
Date: Mon Aug 17 20:42:19 2009
New Revision: 805130

URL: http://svn.apache.org/viewvc?rev=805130&view=rev
Log:
SLING-1088. From now, the JsonQueryServlet uses the JsonResourceWriter to render calendars in the same format as the JsonRendererServlet.

Modified:
    sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/JsonQueryServlet.java
    sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/helpers/JsonResourceWriter.java
    sling/trunk/launchpad/testing/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/JsonQueryServletTest.java

Modified: sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/JsonQueryServlet.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/JsonQueryServlet.java?rev=805130&r1=805129&r2=805130&view=diff
==============================================================================
--- sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/JsonQueryServlet.java (original)
+++ sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/JsonQueryServlet.java Mon Aug 17 20:42:19 2009
@@ -38,6 +38,7 @@
 import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
 import org.apache.sling.commons.json.JSONException;
 import org.apache.sling.commons.json.io.JSONWriter;
+import org.apache.sling.servlets.get.impl.helpers.JsonResourceWriter;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -82,7 +83,13 @@
 
     /** rep:exerpt */
     private static final String REP_EXCERPT = "rep:excerpt()";
+    
+    private final JsonResourceWriter itemWriter;
 
+    public JsonQueryServlet() {
+        itemWriter = new JsonResourceWriter(null);
+    }    
+    
     @Override
     protected void doGet(SlingHttpServletRequest req,
             SlingHttpServletResponse resp) throws IOException {
@@ -107,6 +114,7 @@
 
             Iterator<Map<String, Object>> result = resolver.queryResources(
                 statement, queryType);
+          
 
             if (req.getParameter(OFFSET) != null) {
                 long skip = Long.parseLong(req.getParameter(OFFSET));
@@ -143,6 +151,7 @@
             while (result.hasNext() && count != 0) {
                 Map<String, Object> row = result.next();
 
+                
                 w.object();
                 String path = row.get("jcr:path").toString();
 
@@ -156,10 +165,13 @@
                     if (colName.equals(REP_EXCERPT)) {
                         Object ev = row.get("rep:excerpt(" + exerptPath + ")");
                         strValue = (ev == null) ? "" : ev.toString();
+                        w.value(strValue);
+                        
                     } else {
-                        strValue = formatValue(row.get(colName));
+                        //strValue = formatValue(row.get(colName));
+                    	itemWriter.dumpValue(w, row.get(colName));
                     }
-                    w.value(strValue);
+                    //w.value(strValue);
                 }
 
                 // load properties and add it to the result set
@@ -169,6 +181,8 @@
                 }
 
                 w.endObject();
+                
+                
                 count--;
             }
             w.endArray();
@@ -184,55 +198,11 @@
         if (nodeRes == null) {
             return;
         }
-
-        // get the properties of the resource, nothing todo if none
-        ValueMap props = nodeRes.adaptTo(ValueMap.class);
-        if (props == null) {
-            return;
-        }
         
-        // get the actual properties now into the JSON output
-        for (String property : properties) {
-            Object value = props.get(property);
-            if (value != null) {
-                w.key(property);
-
-                if (value.getClass().isArray()) {
-
-                    w.array();
-                    for (int i = 0; i < Array.getLength(value); i++) {
-                        w.value(formatValue(Array.get(value, i)));
-                    }
-                    w.endArray();
-
-                } else {
-
-                    w.value(formatValue(value));
-
-                }
-            }
-        }
+        itemWriter.dumpProperties(nodeRes, w, properties);
+        
     }
 
-    private String formatValue(Object value) {
-        String strValue;
-        if (value instanceof InputStream) {
-            // binary value comes as a LazyInputStream
-            strValue = "[binary]";
-
-            // just to be clean, close the stream
-            try {
-                ((InputStream) value).close();
-            } catch (IOException ignore) {
-            }
-        } else if (value != null) {
-            strValue = value.toString();
-        } else {
-            strValue = "";
-        }
-
-        return strValue;
-    }
 
     /**
      * @param e
@@ -242,4 +212,6 @@
         log.warn("Error in QueryServlet: " + e.toString(), e);
         return new SlingException(e.toString(), e);
     }
+    
+    
 }

Modified: sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/helpers/JsonResourceWriter.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/helpers/JsonResourceWriter.java?rev=805130&r1=805129&r2=805130&view=diff
==============================================================================
--- sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/helpers/JsonResourceWriter.java (original)
+++ sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/helpers/JsonResourceWriter.java Mon Aug 17 20:42:19 2009
@@ -23,6 +23,7 @@
 import java.text.SimpleDateFormat;
 import java.util.Calendar;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 import java.util.Set;
@@ -35,6 +36,9 @@
 
 /**
  * Dumps JCR Items as JSON data. The dump methods are threadsafe.
+ * 
+ * Dump can be done on the Resource, property or value level. 
+ * 
  */
 public class JsonResourceWriter {
 
@@ -77,6 +81,7 @@
         jw.setTidy(tidy);
         dump(resource, jw, 0, maxRecursionLevels);
     }
+    
 
     /** Dump given resource in JSON, optionally recursing into its objects */
     protected void dump(Resource resource, JSONWriter w,
@@ -134,6 +139,66 @@
         w.endObject();
     }
     
+    /** Dump only a subset of the resource properties */
+    public void dumpProperties(Resource resource, JSONWriter w,
+            List<String> properties)
+            throws JSONException {
+
+        final ValueMap valueMap = resource.adaptTo(ValueMap.class);
+
+        @SuppressWarnings("unchecked")
+        final Map propertyMap = (valueMap != null)
+                ? valueMap
+                : resource.adaptTo(Map.class);
+
+        if (propertyMap == null) {
+        	
+        	//TODO : not sure if we have to do something in this case ?
+        	return; 
+
+        } else {
+
+            @SuppressWarnings("unchecked")
+            final Iterator<Map.Entry> props = propertyMap.entrySet().iterator();
+
+            // the node's actual properties
+            while (props.hasNext()) {
+                @SuppressWarnings("unchecked")
+                final Map.Entry prop = props.next();
+
+                if (propertyNamesToIgnore != null
+                    && propertyNamesToIgnore.contains(prop.getKey())) {
+                    continue;
+                }
+
+                if (properties.contains(prop.getKey().toString()))
+                    writeProperty(w, valueMap, prop.getKey().toString(),
+                                  prop.getValue());
+            }
+        }
+    }        
+    
+    /** Dump only a value in the correct format */
+    public void dumpValue(JSONWriter w, Object value)
+    throws JSONException {
+        if ( value instanceof InputStream ) {
+            // input stream is already handled
+            w.value(0);
+        } else if ( value instanceof Calendar ) {
+            w.value(format((Calendar)value));
+        } else if ( value instanceof Boolean ) {
+            w.value(((Boolean)value).booleanValue());
+        } else if ( value instanceof Long ) {
+            w.value(((Long)value).longValue());
+        } else if ( value instanceof Integer ) {
+            w.value(((Integer)value).longValue());
+        } else if ( value instanceof Double ) {
+            w.value(((Double)value).doubleValue());
+        } else {
+            w.value(value.toString());
+        }
+    }
+    
     /** Dump a single node */
     protected void dumpSingleResource(Resource n, JSONWriter w,
             int currentRecursionLevel, int maxRecursionLevels)
@@ -226,25 +291,6 @@
         w.value(length);
     }
 
-    protected void dumpValue(JSONWriter w, Object value)
-    throws JSONException {
-        if ( value instanceof InputStream ) {
-            // input stream is already handled
-            w.value(0);
-        } else if ( value instanceof Calendar ) {
-            w.value(format((Calendar)value));
-        } else if ( value instanceof Boolean ) {
-            w.value(((Boolean)value).booleanValue());
-        } else if ( value instanceof Long ) {
-            w.value(((Long)value).longValue());
-        } else if ( value instanceof Integer ) {
-            w.value(((Integer)value).longValue());
-        } else if ( value instanceof Double ) {
-            w.value(((Double)value).doubleValue());
-        } else {
-            w.value(value.toString());
-        }
-    }
 
     public static synchronized String format(Calendar date) {
         if (calendarFormat == null) {

Modified: sling/trunk/launchpad/testing/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/JsonQueryServletTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/testing/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/JsonQueryServletTest.java?rev=805130&r1=805129&r2=805130&view=diff
==============================================================================
--- sling/trunk/launchpad/testing/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/JsonQueryServletTest.java (original)
+++ sling/trunk/launchpad/testing/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/JsonQueryServletTest.java Mon Aug 17 20:42:19 2009
@@ -46,6 +46,8 @@
         for(char folder = 'A'; folder <= 'E'; folder++) {
             final Map<String, String> props = new HashMap<String, String> ();
             props.put("creator", getClass().getSimpleName());
+            props.put("date", "2008-04-13T17:55:00"); 
+            props.put("date@TypeHint", "Date");
             props.put("text", "folder " + folder);
             final String subfolderUrl = testClient.createNode(testFolderUrl + "/folder" + folder, props);
             for(int i=0; i < 5; i++) {
@@ -137,5 +139,13 @@
         assertJavascript("folder B node 3", json, "out.print(data[0].text)");
         assertJavascript("ok", json, "if(data[0].creator) out.print('ok')");
         assertJavascript(getClass().getSimpleName(), json, "out.print(data[0].creator)");
+        
+        params.add(new NameValuePair("property", "date"));
+        json = getContent(url, CONTENT_TYPE_JSON, params);
+        assertJavascript("1.0", json, counterCode);
+        assertJavascript("ok", json, "if(data[0].date) out.print('ok')");
+        assertJavascript("Sun Apr 13 2008 17:55:00 GMT+0200", json, "out.print(data[0].date)");
+        
+        
     }
 }