[dna-commits] DNA SVN: r1030 - in trunk/extensions: dna-web-jcr-rest-war/src/test/java/org/jboss/dna/web/jcr/rest and 1 other directory.

dna-commits at lists.jboss.org dna-commits at lists.jboss.org
Wed Jun 10 15:56:54 EDT 2009


Author: bcarothers
Date: 2009-06-10 15:56:53 -0400 (Wed, 10 Jun 2009)
New Revision: 1030

Modified:
   trunk/extensions/dna-web-jcr-rest-war/src/test/java/org/jboss/dna/web/jcr/rest/JcrResourcesTest.java
   trunk/extensions/dna-web-jcr-rest/src/main/java/org/jboss/dna/web/jcr/rest/JcrResources.java
Log:
DNA-447 Make REST PUT Method Support add/removeMixin Like POST Method

Added check to JcrResources.setPropertyOnNode to handle the special case of the jcr:mixinTypes property and a corresponding test case.

Also added missing node.save() call for PUT handler.


Modified: trunk/extensions/dna-web-jcr-rest/src/main/java/org/jboss/dna/web/jcr/rest/JcrResources.java
===================================================================
--- trunk/extensions/dna-web-jcr-rest/src/main/java/org/jboss/dna/web/jcr/rest/JcrResources.java	2009-06-10 15:01:21 UTC (rev 1029)
+++ trunk/extensions/dna-web-jcr-rest/src/main/java/org/jboss/dna/web/jcr/rest/JcrResources.java	2009-06-10 19:56:53 UTC (rev 1030)
@@ -25,10 +25,13 @@
 
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import javax.jcr.Item;
 import javax.jcr.Node;
 import javax.jcr.NodeIterator;
@@ -38,6 +41,7 @@
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 import javax.jcr.Value;
+import javax.jcr.nodetype.NodeType;
 import javax.jcr.nodetype.PropertyDefinition;
 import javax.servlet.http.HttpServletRequest;
 import javax.ws.rs.Consumes;
@@ -122,17 +126,17 @@
      * @return an active session with the given workspace in the named repository
      * @throws RepositoryException if any other error occurs
      */
-    private Session getSession( HttpServletRequest request, 
+    private Session getSession( HttpServletRequest request,
                                 String rawRepositoryName,
                                 String rawWorkspaceName ) throws NotFoundException, RepositoryException {
         assert request != null;
-        assert request.getUserPrincipal() != null: "Request must be authorized";
+        assert request.getUserPrincipal() != null : "Request must be authorized";
 
         // Sanity check
         if (request.getUserPrincipal() == null) {
             throw new UnauthorizedException("Client is not authorized");
         }
-        
+
         return RepositoryFactory.getSession(request, repositoryNameFor(rawRepositoryName), workspaceNameFor(rawWorkspaceName));
     }
 
@@ -177,7 +181,7 @@
     public Map<String, WorkspaceEntry> getWorkspaces( @Context HttpServletRequest request,
                                                       @PathParam( "repositoryName" ) String rawRepositoryName )
         throws RepositoryException, IOException {
-        
+
         assert request != null;
         assert rawRepositoryName != null;
 
@@ -185,7 +189,7 @@
 
         Session session = getSession(request, rawRepositoryName, null);
         rawRepositoryName = URL_ENCODER.encode(rawRepositoryName);
-        
+
         for (String name : session.getWorkspace().getAccessibleWorkspaceNames()) {
             if (name.trim().length() == 0) {
                 name = EMPTY_WORKSPACE_NAME;
@@ -221,7 +225,7 @@
     @GET
     @Path( "/{repositoryName}/{workspaceName}/items{path:.*}" )
     @Produces( "application/json" )
-    public String getItem( @Context HttpServletRequest request, 
+    public String getItem( @Context HttpServletRequest request,
                            @PathParam( "repositoryName" ) String rawRepositoryName,
                            @PathParam( "workspaceName" ) String rawWorkspaceName,
                            @PathParam( "path" ) String path,
@@ -464,18 +468,46 @@
     private void setPropertyOnNode( Node node,
                                     String propName,
                                     Object value ) throws RepositoryException, JSONException {
+        String[] values;
         if (value instanceof JSONArray) {
             JSONArray jsonValues = (JSONArray)value;
-            String[] values = new String[jsonValues.length()];
+            values = new String[jsonValues.length()];
 
             for (int i = 0; i < values.length; i++) {
                 values[i] = jsonValues.getString(i);
             }
-            node.setProperty(propName, values);
         } else {
-            node.setProperty(propName, (String)value);
+            values = new String[] { (String)value };
         }
 
+        if (propName.equals(JcrResources.MIXIN_TYPES_PROPERTY)) {
+            Set<String> toBeMixins = new HashSet<String>(Arrays.asList(values));
+            Set<String> asIsMixins = new HashSet<String>();
+            
+            for (NodeType nodeType : node.getMixinNodeTypes()) {
+                asIsMixins.add(nodeType.getName());
+            }
+            
+            Set<String> mixinsToAdd = new HashSet<String>(toBeMixins);
+            mixinsToAdd.removeAll(asIsMixins);
+            asIsMixins.removeAll(toBeMixins);
+            
+            for (String nodeType : mixinsToAdd) {
+                node.addMixin(nodeType);
+            }
+
+            for (String nodeType : asIsMixins) {
+                node.removeMixin(nodeType);
+            }
+        } else {
+            if (values.length == 1) {
+                node.setProperty(propName, values[0]);
+                
+            }
+            else {
+                node.setProperty(propName, values);
+            }
+        }
     }
 
     /**
@@ -581,7 +613,7 @@
 
             setPropertyOnNode(node, property.getName(), properties.get("value"));
         }
-
+        node.save();
         return jsonFor(node, 0).toString();
     }
 

Modified: trunk/extensions/dna-web-jcr-rest-war/src/test/java/org/jboss/dna/web/jcr/rest/JcrResourcesTest.java
===================================================================
--- trunk/extensions/dna-web-jcr-rest-war/src/test/java/org/jboss/dna/web/jcr/rest/JcrResourcesTest.java	2009-06-10 15:01:21 UTC (rev 1029)
+++ trunk/extensions/dna-web-jcr-rest-war/src/test/java/org/jboss/dna/web/jcr/rest/JcrResourcesTest.java	2009-06-10 19:56:53 UTC (rev 1030)
@@ -825,4 +825,74 @@
         connection.disconnect();
     
     }
+
+    @Test
+    public void shouldBeAbleToAddAndRemoveMixinTypes() throws Exception {
+
+        URL postUrl = new URL(SERVER_URL + "/dna%3arepository/default/items/nodeWithNoMixins");
+        HttpURLConnection connection = (HttpURLConnection)postUrl.openConnection();
+
+        connection.setDoOutput(true);
+        connection.setRequestMethod("POST");
+        connection.setRequestProperty("Content-Type", MediaType.APPLICATION_JSON);
+
+        String payload = "{ \"properties\": {\"jcr:primaryType\": \"nt:unstructured\" }}";
+        connection.getOutputStream().write(payload.getBytes());
+        
+        assertThat(connection.getResponseCode(), is(HttpURLConnection.HTTP_CREATED));
+        connection.disconnect();
+
+        connection = (HttpURLConnection)postUrl.openConnection();
+
+        connection.setDoOutput(true);
+        connection.setRequestMethod("PUT");
+        connection.setRequestProperty("Content-Type", MediaType.APPLICATION_JSON);
+
+        payload = "{\"jcr:mixinTypes\": \"mix:referenceable\"}";
+        connection.getOutputStream().write(payload.getBytes());
+        
+        JSONObject body = new JSONObject(getResponseFor(connection));
+        assertThat(body.length(), is(1));
+
+        JSONObject properties = body.getJSONObject("properties");
+        assertThat(properties, is(notNullValue()));
+
+        assertThat(properties.length(), is(3));
+        assertThat(properties.getString("jcr:primaryType"), is("nt:unstructured"));
+        JSONArray mixinTypes = properties.getJSONArray("jcr:mixinTypes");
+        assertThat(mixinTypes, is(notNullValue()));
+        assertThat(mixinTypes.length(), is(1));
+        assertThat(mixinTypes.getString(0), is("mix:referenceable"));
+        assertThat(properties.getString("jcr:uuid"), is(notNullValue()));
+        
+        assertThat(connection.getResponseCode(), is(HttpURLConnection.HTTP_OK));
+        connection.disconnect();
+
+        connection = (HttpURLConnection)postUrl.openConnection();
+
+        connection.setDoOutput(true);
+        connection.setRequestMethod("PUT");
+        connection.setRequestProperty("Content-Type", MediaType.APPLICATION_JSON);
+
+        payload = "{\"jcr:mixinTypes\": []}";
+        connection.getOutputStream().write(payload.getBytes());
+        
+        body = new JSONObject(getResponseFor(connection));
+        assertThat(body.length(), is(1));
+
+        properties = body.getJSONObject("properties");
+        assertThat(properties, is(notNullValue()));
+        assertThat(properties.length(), is(2));
+        assertThat(properties.getString("jcr:primaryType"), is("nt:unstructured"));
+
+        // removeMixin doesn't currently null out this value
+        mixinTypes = properties.getJSONArray("jcr:mixinTypes");
+        assertThat(mixinTypes, is(notNullValue()));
+        assertThat(mixinTypes.length(), is(0));
+
+        assertThat(connection.getResponseCode(), is(HttpURLConnection.HTTP_OK));
+        connection.disconnect();
+
+    }
+
 }




More information about the dna-commits mailing list