Author: fmeschbe
Date: Mon Mar 28 13:19:54 2011
New Revision: 1086221

URL: http://svn.apache.org/viewvc?rev=1086221&view=rev
Log:
SLING-2035 Properly handle :name request parameter and replacement of existing 
content

Modified:
    
sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/ImportOperation.java

Modified: 
sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/ImportOperation.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/ImportOperation.java?rev=1086221&r1=1086220&r2=1086221&view=diff
==============================================================================
--- 
sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/ImportOperation.java
 (original)
+++ 
sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/ImportOperation.java
 Mon Mar 28 13:19:54 2011
@@ -114,18 +114,33 @@ public class ImportOperation extends Abs
                basePath = basePath.substring(0, basePath.length() - 1);
         }
 
-        String contentRootName;
+        // default to creating content
+        response.setCreateRequest(true);
+
+        final String targetName;
         //check if a name was posted to use as the name of the imported root 
node
-        if (request.getParameter(SlingPostConstants.RP_NODE_NAME) != null ||
-                       
request.getParameter(SlingPostConstants.RP_NODE_NAME_HINT) != null) {
-                       String nodePath = generateName(request, basePath);
-                       String name = 
nodePath.substring(nodePath.lastIndexOf('/') + 1);
-               contentRootName = name + "." + contentType;
+        if (request.getParameter(SlingPostConstants.RP_NODE_NAME) != null) {
+            // exact name
+            targetName = request.getParameter(SlingPostConstants.RP_NODE_NAME);
+            if (targetName.length() > 0 && node.hasNode(targetName) && 
!replace) {
+                response.setStatus(HttpServletResponse.SC_PRECONDITION_FAILED,
+                    "Cannot import " + path + "/" + targetName
+                        + ": node exists");
+                return;
+            }
+
+            // node exists to be overwritten
+            response.setCreateRequest(false);
+        } else if (request.getParameter(SlingPostConstants.RP_NODE_NAME_HINT) 
!= null) {
+            // node name hint only
+            String nodePath = generateName(request, basePath);
+            String name = nodePath.substring(nodePath.lastIndexOf('/') + 1);
+            targetName = name;
         } else {
-               //no name posted, so the import won't create a root node
-               contentRootName = "." + contentType;
+            // no name posted, so the import won't create a root node
+            targetName = "";
         }
-        response.setCreateRequest(true);
+        final String contentRootName = targetName + "." + contentType;
 
         try {
             InputStream contentStream = null;


Reply via email to