jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r1179541 - in /jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/server/remoting/davex: JcrRemotingServlet.java JsonWriter.java
Date Thu, 06 Oct 2011 09:01:33 GMT
Author: jukka
Date: Thu Oct  6 09:01:32 2011
New Revision: 1179541

URL: http://svn.apache.org/viewvc?rev=1179541&view=rev
Log:
JCR-3005: Make it possible to get multiple nodes in one call via davex

Collect all results to a Map before serializing the JSON result inside the JsonWriter class

Modified:
    jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/server/remoting/davex/JcrRemotingServlet.java
    jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/server/remoting/davex/JsonWriter.java

Modified: jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/server/remoting/davex/JcrRemotingServlet.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/server/remoting/davex/JcrRemotingServlet.java?rev=1179541&r1=1179540&r2=1179541&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/server/remoting/davex/JcrRemotingServlet.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/server/remoting/davex/JcrRemotingServlet.java
Thu Oct  6 09:01:32 2011
@@ -41,6 +41,7 @@ import org.slf4j.LoggerFactory;
 
 import javax.jcr.Item;
 import javax.jcr.Node;
+import javax.jcr.Repository;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 import javax.jcr.Workspace;
@@ -54,7 +55,9 @@ import javax.servlet.http.HttpServletRes
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.HashMap;
 import java.util.Iterator;
+import java.util.Map;
 
 /**
  * <code>JcrRemotingServlet</code> is an extended version of the
@@ -366,33 +369,6 @@ public abstract class JcrRemotingServlet
         }
     }
 
-    private boolean getSingle(Session session, String path, DavResourceLocator locator, PrintWriter
webdavWriter, boolean isFirst, boolean withPath) throws IOException, RepositoryException {
-        try {
-            Item item = session.getItem(path);
-            JsonWriter writer = new JsonWriter(webdavWriter);
-            if (item.isNode()) {
-                if (!isFirst) {
-                    webdavWriter.write(',');
-                }
-                if (withPath) {
-                    webdavWriter.write("\"" + path + "\":");
-                }
-                int depth = ((WrappingLocator) locator).getDepth();
-                if (depth < BatchReadConfig.DEPTH_INFINITE) {
-                    depth = getDepth((Node) item);
-                }
-                writer.write((Node) item, depth);
-
-                return true;
-            } else {
-                return false;
-            }
-        } catch (PathNotFoundException ex) {
-            return false;
-        } 
-    }
-
-    
     private boolean canHandle(int methodCode, WebdavRequest request, DavResource davResource)
{
         DavResourceLocator locator = davResource.getLocator();
         switch (methodCode) {
@@ -581,29 +557,29 @@ public abstract class JcrRemotingServlet
     }
 
     protected void getMultiple(
-            Session session, String[] getArgs, DavResourceLocator locator,
+            Session session, String[] paths, DavResourceLocator locator,
             WebdavResponse webdavResponse)
             throws IOException, RepositoryException {
-        webdavResponse.setContentType("text/plain;charset=utf-8");
-        webdavResponse.setStatus(DavServletResponse.SC_OK);
-        PrintWriter webdavWriter = webdavResponse.getWriter();
-        Boolean isFirst = true;
-        webdavWriter.write("{\"nodes\": {");
-        String path = locator.getRepositoryPath();
-        if (getSingle(session, path, locator, webdavWriter, isFirst, true)) {
-            isFirst = false;
-        }
-
-        for (String getArg : getArgs) {
+        // Collect all requested nodes
+        Map<String, Node> nodes = new HashMap<String, Node>();
+        Node node = session.getNode(locator.getRepositoryPath());
+        nodes.put(node.getPath(), node);
+        for (String path : paths) {
             try {
-                if (!getArg.equals(path) && getSingle(session, getArg, locator, webdavWriter,
isFirst, true)) {
-                    isFirst = false;
-                }
-            } catch (RepositoryException ex) {
-
+                nodes.put(path, session.getNode(path));
+            } catch (PathNotFoundException ignore) {
+                // skip a missing node
             }
         }
-        webdavWriter.write("}}");
+
+        int depth = ((WrappingLocator) locator).getDepth();
+        if (depth < BatchReadConfig.DEPTH_INFINITE) {
+            depth = getDepth(node);
+        }
+
+        webdavResponse.setContentType("text/plain;charset=utf-8");
+        webdavResponse.setStatus(DavServletResponse.SC_OK);
+        new JsonWriter(webdavResponse.getWriter()).write(nodes, depth);
     }
 
     //--------------------------------------------------------------------------

Modified: jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/server/remoting/davex/JsonWriter.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/server/remoting/davex/JsonWriter.java?rev=1179541&r1=1179540&r2=1179541&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/server/remoting/davex/JsonWriter.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/server/remoting/davex/JsonWriter.java
Thu Oct  6 09:01:32 2011
@@ -25,6 +25,7 @@ import javax.jcr.RepositoryException;
 import javax.jcr.Value;
 import java.io.Writer;
 import java.io.IOException;
+import java.util.Map;
 
 import org.apache.jackrabbit.JcrConstants;
 import org.apache.jackrabbit.commons.json.JsonUtil;
@@ -68,6 +69,25 @@ class JsonWriter {
         write(node, 0, maxLevels);
     }
 
+    void write(Map<String, Node> nodes, int maxLevels)
+            throws RepositoryException, IOException {
+        writer.write('{');
+        writeKey(writer, "nodes");
+        writer.write('{');
+        boolean first = true;
+        for (Map.Entry<String, Node> entry : nodes.entrySet()) {
+            if (first) {
+                first = false;
+            } else {
+                writer.write(',');
+            }
+            writeKey(writer, entry.getKey());
+            write(entry.getValue(), maxLevels);
+        }
+        writer.write('}');
+        writer.write('}');
+    }
+
     private void write(Node node, int currentLevel, int maxLevels)
             throws RepositoryException, IOException {
         // start of node info



Mime
View raw message