jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r1354284 - in /jackrabbit/oak/trunk/oak-http: pom.xml src/main/java/org/apache/jackrabbit/oak/http/OakServlet.java
Date Tue, 26 Jun 2012 23:05:11 GMT
Author: jukka
Date: Tue Jun 26 23:05:10 2012
New Revision: 1354284

URL: http://svn.apache.org/viewvc?rev=1354284&view=rev
Log:
OAK-104: HTTP bindings for Oak

Add simple content modification support

Modified:
    jackrabbit/oak/trunk/oak-http/pom.xml
    jackrabbit/oak/trunk/oak-http/src/main/java/org/apache/jackrabbit/oak/http/OakServlet.java

Modified: jackrabbit/oak/trunk/oak-http/pom.xml
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-http/pom.xml?rev=1354284&r1=1354283&r2=1354284&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-http/pom.xml (original)
+++ jackrabbit/oak/trunk/oak-http/pom.xml Tue Jun 26 23:05:10 2012
@@ -92,6 +92,11 @@
       <version>2.0.0</version>
     </dependency>
     <dependency>
+      <groupId>com.fasterxml.jackson.core</groupId>
+      <artifactId>jackson-databind</artifactId>
+      <version>2.0.0</version>
+    </dependency>
+    <dependency>
       <groupId>com.fasterxml.jackson.dataformat</groupId>
       <artifactId>jackson-dataformat-smile</artifactId>
       <version>2.0.2</version>

Modified: jackrabbit/oak/trunk/oak-http/src/main/java/org/apache/jackrabbit/oak/http/OakServlet.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-http/src/main/java/org/apache/jackrabbit/oak/http/OakServlet.java?rev=1354284&r1=1354283&r2=1354284&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-http/src/main/java/org/apache/jackrabbit/oak/http/OakServlet.java
(original)
+++ jackrabbit/oak/trunk/oak-http/src/main/java/org/apache/jackrabbit/oak/http/OakServlet.java
Tue Jun 26 23:05:10 2012
@@ -19,6 +19,8 @@ package org.apache.jackrabbit.oak.http;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.Iterator;
+import java.util.Map.Entry;
 
 import javax.jcr.GuestCredentials;
 import javax.jcr.NoSuchWorkspaceException;
@@ -29,16 +31,22 @@ import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import org.apache.jackrabbit.oak.api.CommitFailedException;
 import org.apache.jackrabbit.oak.api.ContentRepository;
 import org.apache.jackrabbit.oak.api.ContentSession;
 import org.apache.jackrabbit.oak.api.CoreValue;
+import org.apache.jackrabbit.oak.api.CoreValueFactory;
 import org.apache.jackrabbit.oak.api.PropertyState;
 import org.apache.jackrabbit.oak.api.Root;
 import org.apache.jackrabbit.oak.api.Tree;
+import org.apache.jackrabbit.oak.core.DefaultConflictHandler;
+import org.apache.jackrabbit.oak.plugins.memory.MemoryValueFactory;
 
 import com.fasterxml.jackson.core.JsonFactory;
 import com.fasterxml.jackson.core.JsonGenerationException;
 import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.dataformat.smile.SmileFactory;
 
 public class OakServlet extends HttpServlet {
@@ -62,13 +70,9 @@ public class OakServlet extends HttpServ
             try {
                 Root root = session.getCurrentRoot();
                 Tree tree = root.getTree(request.getPathInfo());
-                if (tree != null) {
-                    JsonGenerator generator = getRenderer(request, response);
-                    int depth = getDepth(request);
-                    render(tree, depth, generator);
-                } else {
-                    response.sendError(HttpServletResponse.SC_NOT_FOUND);
-                }
+                request.setAttribute("root", root);
+                request.setAttribute("tree", tree);
+                super.service(request, response);
             } finally {
                 session.close();
             }
@@ -79,6 +83,93 @@ public class OakServlet extends HttpServ
         }
     }
 
+    @Override
+    protected void doGet(
+            HttpServletRequest request, HttpServletResponse response)
+            throws ServletException, IOException {
+        Tree tree = (Tree) request.getAttribute("tree");
+        render(tree, getDepth(request), getRenderer(request, response));
+    }
+
+    @Override
+    protected void doPost(
+            HttpServletRequest request, HttpServletResponse response)
+            throws ServletException, IOException {
+        try {
+            Root root = (Root) request.getAttribute("root");
+            Tree tree = (Tree) request.getAttribute("tree");
+            ObjectMapper mapper = new ObjectMapper();
+            JsonNode node = mapper.readTree(request.getInputStream());
+            if (node.isObject()) {
+                post(node, tree);
+                root.commit(DefaultConflictHandler.OURS);
+                doGet(request, response);
+            } else {
+                response.sendError(HttpServletResponse.SC_BAD_REQUEST);
+            }
+        } catch (CommitFailedException e) {
+            throw new ServletException(e);
+        }
+    }
+
+    private void post(JsonNode node, Tree tree) {
+        Iterator<Entry<String, JsonNode>> iterator = node.fields();
+        while (iterator.hasNext()) {
+            Entry<String, JsonNode> entry = iterator.next();
+            String name = entry.getKey();
+            JsonNode value = entry.getValue();
+            if (value.isObject()) {
+                if (tree.hasProperty(name)) {
+                    tree.removeProperty(name);
+                }
+                Tree child = tree.getChild(name);
+                if (child == null) {
+                    child = tree.addChild(name);
+                }
+                post(value, child);
+            } else {
+                if (tree.hasChild(name)) {
+                    tree.removeChild(name);
+                }
+                CoreValueFactory vf = MemoryValueFactory.INSTANCE;
+                if (value.isNull()) {
+                    tree.removeProperty(name);
+                } else if (value.isBoolean()) {
+                    tree.setProperty(name, vf.createValue(value.asBoolean()));
+                } else if (value.isLong()) {
+                    tree.setProperty(name, vf.createValue(value.asLong()));
+                } else if (value.isDouble()) {
+                    tree.setProperty(name, vf.createValue(value.asDouble()));
+                } else if (value.isBigDecimal()) {
+                    tree.setProperty(name, vf.createValue(value.decimalValue()));
+                } else {
+                    tree.setProperty(name, vf.createValue(value.asText()));
+                }
+            }
+        }
+    }
+
+    @Override
+    protected void doDelete(
+            HttpServletRequest request, HttpServletResponse response)
+            throws ServletException, IOException {
+        try {
+            Root root = (Root) request.getAttribute("root");
+            Tree tree = (Tree) request.getAttribute("tree");
+            Tree parent = tree.getParent();
+            if (parent != null) {
+                parent.removeChild(tree.getName());
+                root.commit(DefaultConflictHandler.OURS);
+                response.sendError(HttpServletResponse.SC_OK);
+            } else {
+                // Can't remove the root node
+                response.sendError(HttpServletResponse.SC_FORBIDDEN);
+            }
+        } catch (CommitFailedException e) {
+            throw new ServletException(e);
+        }
+    }
+
     private int getDepth(HttpServletRequest request) {
         String d = request.getParameter("depth");
         if (d == null) {



Mime
View raw message