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/02/29 19:51:35 UTC

svn commit: r632409 - in /incubator/sling/trunk: launchpad/launchpad-webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/ sling/ujax/src/main/java/org/apache/sling/ujax/

Author: cziegeler
Date: Fri Feb 29 10:51:30 2008
New Revision: 632409

URL: http://svn.apache.org/viewvc?rev=632409&view=rev
Log:
SLING-298: Ignore FormEncoding parameter during POST processing
SLING-297: Log exception during POST processing
SLING-295: First version for specifying the node type using the "ujax:" prefix.

Modified:
    incubator/sling/trunk/launchpad/launchpad-webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/CreateNodeTest.java
    incubator/sling/trunk/sling/ujax/src/main/java/org/apache/sling/ujax/UjaxPostProcessor.java
    incubator/sling/trunk/sling/ujax/src/main/java/org/apache/sling/ujax/UjaxPostServlet.java

Modified: incubator/sling/trunk/launchpad/launchpad-webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/CreateNodeTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/launchpad/launchpad-webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/CreateNodeTest.java?rev=632409&r1=632408&r2=632409&view=diff
==============================================================================
--- incubator/sling/trunk/launchpad/launchpad-webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/CreateNodeTest.java (original)
+++ incubator/sling/trunk/launchpad/launchpad-webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/CreateNodeTest.java Fri Feb 29 10:51:30 2008
@@ -24,23 +24,23 @@
 
 /** Test creating a Node using the MicroslingIntegrationTestClient */
 public class CreateNodeTest extends HttpTestBase {
-    
+
     public void testCreateNode() throws IOException {
         final String url = HTTP_BASE_URL + "/CreateNodeTest_1_" + System.currentTimeMillis();
-        
+
         // add some properties to the node
         final Map<String,String> props = new HashMap<String,String>();
         props.put("name1","value1");
         props.put("name2","value2");
-        
-        // POST and get URL of created node 
+
+        // POST and get URL of created node
         String urlOfNewNode = null;
         try {
             urlOfNewNode = testClient.createNode(url, props);
         } catch(IOException ioe) {
             fail("createNode failed: " + ioe);
         }
-        
+
         // get and check URL of created node
         final GetMethod get = new GetMethod(urlOfNewNode);
         final int status = httpClient.executeMethod(get);
@@ -48,28 +48,28 @@
         final String responseBodyStr = get.getResponseBodyAsString();
         assertTrue(responseBodyStr.contains("value1"));
         assertTrue(responseBodyStr.contains("value2"));
-        
+
         // test default txt and html renderings
         getContent(urlOfNewNode, CONTENT_TYPE_PLAIN);
         getContent(urlOfNewNode + ".txt", CONTENT_TYPE_PLAIN);
         getContent(urlOfNewNode + ".html", CONTENT_TYPE_HTML);
         getContent(urlOfNewNode + ".json", CONTENT_TYPE_JSON);
-        
+
         // And extensions for which we have no renderer fail
         assertHttpStatus(urlOfNewNode + ".xml", 500);
         assertHttpStatus(urlOfNewNode + ".pdf", 500);
         assertHttpStatus(urlOfNewNode + ".someWeirdExtension", 500);
     }
-    
+
     public void testCreateNodeMultipart() throws IOException {
         final String url = HTTP_BASE_URL + "/CreateNodeTest_2_" + System.currentTimeMillis();
-        
+
         // add some properties to the node
         final Map<String,String> props = new HashMap<String,String>();
         props.put("name1","value1B");
         props.put("name2","value2B");
-        
-        // POST and get URL of created node 
+
+        // POST and get URL of created node
         String urlOfNewNode = null;
         try {
             urlOfNewNode = testClient.createNode(url, props, null, true);
@@ -85,4 +85,50 @@
         assertTrue(responseBodyStr.contains("value1B"));
         assertTrue(responseBodyStr.contains("value2B"));
    }
+
+    public void testCreateNodeWithNodeType() throws IOException {
+        final String url = HTTP_BASE_URL + "/CreateNodeTest_3_" + System.currentTimeMillis();
+
+        // add node type param
+        final Map<String,String> props = new HashMap<String,String>();
+        props.put("ujax:nodeType","nt:unstructured");
+        props.put("name1","value1B");
+
+        // POST and get URL of created node
+        String urlOfNewNode = null;
+        try {
+            urlOfNewNode = testClient.createNode(url, props);
+        } catch(IOException ioe) {
+            fail("createNode failed: " + ioe);
+        }
+
+        // get and check URL of created node
+        final GetMethod get = new GetMethod(urlOfNewNode);
+        final int status = httpClient.executeMethod(get);
+        assertEquals(urlOfNewNode + " must be accessible after createNode",200,status);
+        final String responseBodyStr = get.getResponseBodyAsString();
+        assertTrue(responseBodyStr.contains("nt:unstructured"));
+    }
+
+    /** Disabled - See SLING-299
+    public void testCreateEmptyNode() throws IOException {
+        final String url = HTTP_BASE_URL + "/CreateNodeTest_4_" + System.currentTimeMillis();
+
+        // add node type param
+        final Map<String,String> props = new HashMap<String,String>();
+
+        // POST and get URL of created node
+        String urlOfNewNode = null;
+        try {
+            urlOfNewNode = testClient.createNode(url, props);
+        } catch(IOException ioe) {
+            fail("createNode failed: " + ioe);
+        }
+
+        // get and check URL of created node
+        final GetMethod get = new GetMethod(urlOfNewNode);
+        final int status = httpClient.executeMethod(get);
+        assertEquals(urlOfNewNode + " must be accessible after createNode",200,status);
+    }
+    */
 }

Modified: incubator/sling/trunk/sling/ujax/src/main/java/org/apache/sling/ujax/UjaxPostProcessor.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/ujax/src/main/java/org/apache/sling/ujax/UjaxPostProcessor.java?rev=632409&r1=632408&r2=632409&view=diff
==============================================================================
--- incubator/sling/trunk/sling/ujax/src/main/java/org/apache/sling/ujax/UjaxPostProcessor.java (original)
+++ incubator/sling/trunk/sling/ujax/src/main/java/org/apache/sling/ujax/UjaxPostProcessor.java Fri Feb 29 10:51:30 2008
@@ -299,6 +299,7 @@
                 session.save();
             }
         } catch (Exception e) {
+            log.error("Exception during response processing.", e);
             error = e;
         } finally {
             try {
@@ -436,7 +437,7 @@
             if (session.itemExists(currentPath)) {
                 throw new ServletException("Collision in generated node names for path=" + currentPath);
             }
-            deepGetOrCreateNode(null, currentPath);
+            deepGetOrCreateNode(null, currentPath, true);
         }
     }
 
@@ -462,6 +463,10 @@
             if (paramName.endsWith(UjaxPostServlet.DEFAULT_VALUE_SUFFIX)) {
                 continue;
             }
+            // skip FormEncoding parameter
+            if ( paramName.equals("FormEncoding") ) {
+                continue;
+            }
             // skip parameters that do not start with the save prefix
             if(!paramName.startsWith(getSavePrefix())) {
                 continue;
@@ -493,7 +498,7 @@
             }
             // create property helper and get parent node
             RequestProperty prop = new RequestProperty(this, propertyName, values);
-            Node parent = deepGetOrCreateNode(currentPath, prop.getParentPath());
+            Node parent = deepGetOrCreateNode(currentPath, prop.getParentPath(), true);
 
             // call handler
             if (prop.isFileUpload()) {
@@ -510,14 +515,15 @@
      *
      * @param parent path to the parent node, may be null if path is absolute
      * @param path path to node that needs to be deep-created
+     * @param isCreate is this a node creation
      * @return node at path
      * @throws RepositoryException if an error occurs
      * @throws IllegalArgumentException if the path is relative and parent
      *         is <code>null</code>
      */
-    private Node deepGetOrCreateNode(String parent, String path)
+    private Node deepGetOrCreateNode(String parent, String path, boolean isCreate)
             throws RepositoryException {
-        if(log.isDebugEnabled()) {
+        if (log.isDebugEnabled()) {
             log.debug("Deep-creating Node '{}/{}'", parent, path);
         }
         if (path.equals("")) {
@@ -540,11 +546,30 @@
         String[] pathelems = path.substring(1).split("/");
         Node node = session.getRootNode();
 
-        for (String name: pathelems) {
+        for(int i=0; i<pathelems.length; i++) {
+            final String name = pathelems[i];
             if (node.hasNode(name)) {
                 node = node.getNode(name);
             } else {
-                node = node.addNode(name);
+                boolean isLast = (i == pathelems.length - 1);
+                if ( isLast && isCreate ) {
+                    // check for node type
+                    final String nodeType = request.getParameter(UjaxPostServlet.RP_NODE_TYPE);
+                    if ( nodeType != null ) {
+                        node = node.addNode(name, nodeType);
+                    } else {
+                        node = node.addNode(name);
+                    }
+                    // check for mixin types
+                    final String[] mixinTypes = request.getParameterValues(UjaxPostServlet.RP_MIXIN_TYPES);
+                    if ( mixinTypes != null ) {
+                        for(int m=0; m<mixinTypes.length; m++) {
+                            node.addMixin(mixinTypes[m]);
+                        }
+                    }
+                } else {
+                    node = node.addNode(name);
+                }
                 changeLog.onCreated(node.getPath());
             }
         }
@@ -589,7 +614,7 @@
         final String orderCode = request.getParameter(UjaxPostServlet.RP_ORDER);
         if  (orderCode!=null) {
             if (UjaxPostServlet.ORDER_ZERO.equals(orderCode)) {
-                final Node n = deepGetOrCreateNode(null, currentPath);
+                final Node n = deepGetOrCreateNode(null, currentPath, false);
                 final Node parent = n.getParent();
                 final String beforename=parent.getNodes().nextNode().getName();
                 parent.orderBefore(n.getName(), beforename);

Modified: incubator/sling/trunk/sling/ujax/src/main/java/org/apache/sling/ujax/UjaxPostServlet.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/ujax/src/main/java/org/apache/sling/ujax/UjaxPostServlet.java?rev=632409&r1=632408&r2=632409&view=diff
==============================================================================
--- incubator/sling/trunk/sling/ujax/src/main/java/org/apache/sling/ujax/UjaxPostServlet.java (original)
+++ incubator/sling/trunk/sling/ujax/src/main/java/org/apache/sling/ujax/UjaxPostServlet.java Fri Feb 29 10:51:30 2008
@@ -150,6 +150,18 @@
     public static final String DEFAULT_VALUE_SUFFIX = "@DefaultValue";
 
     /**
+     * Optional request parameter: if provided and a new node is created it
+     * specifies the node type of the newly created node.
+     */
+    public static final String RP_NODE_TYPE = RP_PREFIX + "nodeType";
+
+    /**
+     * Optional request parameter: if provided and a new node is created it
+     * specifies the additional mixin types of the newly created node.
+     */
+    public static final String RP_MIXIN_TYPES = RP_PREFIX + "mixinTypes";
+
+    /**
      * utility class for generating node names
      */
     private final NodeNameGenerator nodeNameGenerator = new NodeNameGenerator();
@@ -172,7 +184,7 @@
     protected void doPost(SlingHttpServletRequest request,
                           SlingHttpServletResponse response)
             throws ServletException, IOException {
-
+            
         // create a post processor and process changes
         UjaxPostProcessor p = createPostProcessor(request);
         p.run();