Author: enorman
Date: Fri Jul  2 23:18:18 2010
New Revision: 960134

URL: http://svn.apache.org/viewvc?rev=960134&view=rev
Log:
SLING-1172 Allow uploading JSON files to create content structures
Fixed the import case where no name is provided

Added:
    
sling/trunk/launchpad/integration-tests/src/main/resources/integration-test/servlets/post/importresults.json
Modified:
    
sling/trunk/bundles/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/DefaultContentCreator.java
    
sling/trunk/bundles/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/DefaultContentImporter.java
    
sling/trunk/bundles/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/readers/JsonReader.java
    
sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/ImportOperation.java
    
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/post/PostServletImportTest.java
    
sling/trunk/launchpad/integration-tests/src/main/resources/integration-test/servlets/post/testimport2.json

Modified: 
sling/trunk/bundles/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/DefaultContentCreator.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/DefaultContentCreator.java?rev=960134&r1=960133&r2=960134&view=diff
==============================================================================
--- 
sling/trunk/bundles/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/DefaultContentCreator.java
 (original)
+++ 
sling/trunk/bundles/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/DefaultContentCreator.java
 Fri Jul  2 23:18:18 2010
@@ -239,6 +239,7 @@ public class DefaultContentCreator imple
                            String[] mixinNodeTypes)
     throws RepositoryException {
         final Node parentNode = this.parentNodeStack.peek();
+        boolean isParentImport = (name == null && isParentNodeImport);
         if ( name == null ) {
             if ( this.parentNodeStack.size() > 1 ) {
                 throw new RepositoryException("Node needs to have a name.");
@@ -247,7 +248,7 @@ public class DefaultContentCreator imple
         }
 
         // if we are in parent node import mode, we don't create the root top 
level node!
-        if ( !isParentNodeImport || this.parentNodeStack.size() > 1 ) {
+        if ( !isParentImport || this.parentNodeStack.size() > 1 ) {
             // if node already exists but should be overwritten, delete it
             if (!this.ignoreOverwriteFlag && this.configuration.isOverwrite() 
&& parentNode.hasNode(name)) {
                 parentNode.getNode(name).remove();

Modified: 
sling/trunk/bundles/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/DefaultContentImporter.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/DefaultContentImporter.java?rev=960134&r1=960133&r2=960134&view=diff
==============================================================================
--- 
sling/trunk/bundles/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/DefaultContentImporter.java
 (original)
+++ 
sling/trunk/bundles/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/DefaultContentImporter.java
 Fri Jul  2 23:18:18 2010
@@ -120,7 +120,10 @@ public class DefaultContentImporter exte
     private String toPlainName(DefaultContentCreator contentCreator, String 
name) {
         final String providerExt = 
contentCreator.getImportProviderExtension(name);
         if (providerExt != null) {
-            return name.substring(0, name.length() - providerExt.length());
+               if (name.length() == providerExt.length()) {
+                       return null; //no name is provided
+               }
+               return name.substring(0, name.length() - providerExt.length());
         }
         return name;
     }

Modified: 
sling/trunk/bundles/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/readers/JsonReader.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/readers/JsonReader.java?rev=960134&r1=960133&r2=960134&view=diff
==============================================================================
--- 
sling/trunk/bundles/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/readers/JsonReader.java
 (original)
+++ 
sling/trunk/bundles/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/readers/JsonReader.java
 Fri Jul  2 23:18:18 2010
@@ -105,7 +105,6 @@ public class JsonReader implements Conte
         ignoredNames.add("jcr:successors");
         ignoredNames.add("jcr:checkedOut");
         ignoredNames.add("jcr:created");
-        ignoredNames.add(":name");
     }
 
     private static final Set<String> ignoredPrincipalPropertyNames = new 
HashSet<String>();
@@ -157,8 +156,7 @@ public class JsonReader implements Conte
             }
 
             JSONObject json = new JSONObject(jsonString);
-            String optionalName = json.optString(":name", null);
-            this.createNode(optionalName, json, contentCreator);
+            this.createNode(null, json, contentCreator);
         } catch (JSONException je) {
             throw (IOException) new IOException(je.getMessage()).initCause(je);
         }

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=960134&r1=960133&r2=960134&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
 Fri Jul  2 23:18:18 2010
@@ -109,8 +109,17 @@ public class ImportOperation extends Abs
                //remove the trailing slash
                basePath = basePath.substring(0, basePath.length() - 1);
         }
-               String name = generateName(request, basePath);
-        String contentRootName = name + "." + contentType;
+        
+        String contentRootName;
+        //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 name = generateName(request, basePath);
+               contentRootName = name + "." + contentType;
+        } else {
+               //no name posted, so the import won't create a root node
+               contentRootName = "." + contentType;
+        }
         response.setCreateRequest(true);
         
         try {

Modified: 
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/post/PostServletImportTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/post/PostServletImportTest.java?rev=960134&r1=960133&r2=960134&view=diff
==============================================================================
--- 
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/post/PostServletImportTest.java
 (original)
+++ 
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/post/PostServletImportTest.java
 Fri Jul  2 23:18:18 2010
@@ -81,9 +81,6 @@ public class PostServletImportTest exten
        Iterator<String> keys = expectedJson.keys();
        while (keys.hasNext()) {
                String key = keys.next();
-                if ( ":name".equals(key) ) {
-                    continue;
-                }
                
                Object object = expectedJson.get(key);
                Object object2 = actualJson.get(key);
@@ -146,8 +143,8 @@ public class PostServletImportTest exten
                assertNull(jsonObj.optString("propTest", null)); //test 
property should be gone.
 
                //assert the imported content is there.
-        String jsonContent = 
(String)getStreamAsString(getClass().getResourceAsStream("/integration-test/servlets/post/testimport.json"));
-               assertExpectedJSON(new JSONObject(jsonContent), jsonObj);
+        String expectedJsonContent = 
(String)getStreamAsString(getClass().getResourceAsStream("/integration-test/servlets/post/importresults.json"));
+               assertExpectedJSON(new JSONObject(expectedJsonContent), 
jsonObj);
     }
 
     /**
@@ -235,8 +232,8 @@ public class PostServletImportTest exten
                assertNotNull(jsonObj);
 
                //assert the imported content is there.
-        String jsonContent = 
(String)getStreamAsString(getClass().getResourceAsStream("/integration-test/servlets/post/testimport.json"));
-               assertExpectedJSON(new JSONObject(jsonContent), jsonObj);
+        String expectedJsonContent = 
(String)getStreamAsString(getClass().getResourceAsStream("/integration-test/servlets/post/importresults.json"));
+               assertExpectedJSON(new JSONObject(expectedJsonContent), 
jsonObj);
     }
 
     /**
@@ -268,8 +265,8 @@ public class PostServletImportTest exten
                assertNotNull(jsonObj);
 
                //assert the imported content is there.
-        String jsonContent = 
(String)getStreamAsString(getClass().getResourceAsStream("/integration-test/servlets/post/testimport2.json"));
-               assertExpectedJSON(new JSONObject(jsonContent), jsonObj);
+        String expectedJsonContent = 
(String)getStreamAsString(getClass().getResourceAsStream("/integration-test/servlets/post/importresults.json"));
+               assertExpectedJSON(new JSONObject(expectedJsonContent), 
jsonObj);
     }
     
     /**
@@ -300,7 +297,8 @@ public class PostServletImportTest exten
                assertNotNull(jsonObj);
 
                //assert the imported content is there.
-               assertExpectedJSON(new JSONObject(jsonContent), jsonObj);
+        String expectedJsonContent = 
(String)getStreamAsString(getClass().getResourceAsStream("/integration-test/servlets/post/importresults.json"));
+               assertExpectedJSON(new JSONObject(expectedJsonContent), 
jsonObj);
     }
 
     /**
@@ -332,7 +330,8 @@ public class PostServletImportTest exten
                assertNotNull(jsonObj);
 
                //assert the imported content is there.
-               assertExpectedJSON(new JSONObject(jsonContent), jsonObj);
+        String expectedJsonContent = 
(String)getStreamAsString(getClass().getResourceAsStream("/integration-test/servlets/post/importresults.json"));
+               assertExpectedJSON(new JSONObject(expectedJsonContent), 
jsonObj);
     }
     
     public void testImportXMLFromFile() throws IOException, JSONException {
@@ -360,8 +359,8 @@ public class PostServletImportTest exten
                assertNotNull(jsonObj);
 
                //assert the imported content is there.
-        String jsonContent = 
(String)getStreamAsString(getClass().getResourceAsStream("/integration-test/servlets/post/testimport.json"));
-               assertExpectedJSON(new JSONObject(jsonContent), jsonObj);
+        String expectedJsonContent = 
(String)getStreamAsString(getClass().getResourceAsStream("/integration-test/servlets/post/importresults.json"));
+               assertExpectedJSON(new JSONObject(expectedJsonContent), 
jsonObj);
     }
 
     public void testImportXMLFromFileWithoutOptionalName() throws IOException, 
JSONException {
@@ -390,8 +389,8 @@ public class PostServletImportTest exten
                assertNotNull(jsonObj);
 
                //assert the imported content is there.
-        String jsonContent = 
(String)getStreamAsString(getClass().getResourceAsStream("/integration-test/servlets/post/testimport.json"));
-               assertExpectedJSON(new JSONObject(jsonContent), jsonObj);
+        String expectedJsonContent = 
(String)getStreamAsString(getClass().getResourceAsStream("/integration-test/servlets/post/importresults.json"));
+               assertExpectedJSON(new JSONObject(expectedJsonContent), 
jsonObj);
     }
     
     public void testImportXMLFromRequestParam() throws IOException, 
JSONException {
@@ -419,8 +418,8 @@ public class PostServletImportTest exten
                assertNotNull(jsonObj);
 
                //assert the imported content is there.
-        String jsonContent = 
(String)getStreamAsString(getClass().getResourceAsStream("/integration-test/servlets/post/testimport.json"));
-               assertExpectedJSON(new JSONObject(jsonContent), jsonObj);
+        String expectedJsonContent = 
(String)getStreamAsString(getClass().getResourceAsStream("/integration-test/servlets/post/importresults.json"));
+               assertExpectedJSON(new JSONObject(expectedJsonContent), 
jsonObj);
     }
 
     public void testImportXMLFromRequestParamWithoutOptionalName() throws 
IOException, JSONException {
@@ -449,8 +448,8 @@ public class PostServletImportTest exten
                assertNotNull(jsonObj);
 
                //assert the imported content is there.
-        String jsonContent = 
(String)getStreamAsString(getClass().getResourceAsStream("/integration-test/servlets/post/testimport.json"));
-               assertExpectedJSON(new JSONObject(jsonContent), jsonObj);
+        String expectedJsonContent = 
(String)getStreamAsString(getClass().getResourceAsStream("/integration-test/servlets/post/importresults.json"));
+               assertExpectedJSON(new JSONObject(expectedJsonContent), 
jsonObj);
     }
  
     
@@ -479,8 +478,8 @@ public class PostServletImportTest exten
                assertNotNull(jsonObj);
 
                //assert the imported content is there.
-        String jsonContent = 
(String)getStreamAsString(getClass().getResourceAsStream("/integration-test/servlets/post/testimportzip.json"));
-               assertExpectedJSON(new JSONObject(jsonContent), jsonObj);
+        String expectedJsonContent = 
(String)getStreamAsString(getClass().getResourceAsStream("/integration-test/servlets/post/testimportzip.json"));
+               assertExpectedJSON(new JSONObject(expectedJsonContent), 
jsonObj);
     }
 
     public void testImportJarFromFile() throws IOException, JSONException {
@@ -508,8 +507,8 @@ public class PostServletImportTest exten
                assertNotNull(jsonObj);
 
                //assert the imported content is there.
-        String jsonContent = 
(String)getStreamAsString(getClass().getResourceAsStream("/integration-test/servlets/post/testimportzip.json"));
-               assertExpectedJSON(new JSONObject(jsonContent), jsonObj);
+        String expectedJsonContent = 
(String)getStreamAsString(getClass().getResourceAsStream("/integration-test/servlets/post/testimportzip.json"));
+               assertExpectedJSON(new JSONObject(expectedJsonContent), 
jsonObj);
     }
  
     
@@ -538,8 +537,8 @@ public class PostServletImportTest exten
                assertNotNull(jsonObj);
 
                //assert the imported content is there.
-        String jsonContent = 
(String)getStreamAsString(getClass().getResourceAsStream("/integration-test/servlets/post/testimport.json"));
-               assertExpectedJSON(new JSONObject(jsonContent), jsonObj);
+        String expectedJsonContent = 
(String)getStreamAsString(getClass().getResourceAsStream("/integration-test/servlets/post/importresults.json"));
+               assertExpectedJSON(new JSONObject(expectedJsonContent), 
jsonObj);
     }
     
 }

Added: 
sling/trunk/launchpad/integration-tests/src/main/resources/integration-test/servlets/post/importresults.json
URL: 
http://svn.apache.org/viewvc/sling/trunk/launchpad/integration-tests/src/main/resources/integration-test/servlets/post/importresults.json?rev=960134&view=auto
==============================================================================
--- 
sling/trunk/launchpad/integration-tests/src/main/resources/integration-test/servlets/post/importresults.json
 (added)
+++ 
sling/trunk/launchpad/integration-tests/src/main/resources/integration-test/servlets/post/importresults.json
 Fri Jul  2 23:18:18 2010
@@ -0,0 +1,11 @@
+{
+       "jcr:primaryType": "nt:unstructured",
+       "jcr:mixinTypes": [
+               "mix:referenceable"
+       ],
+       "propOne" : "propOneValue",
+       
+       "childOne" : {
+               "childPropOne" : true
+       }
+}
\ No newline at end of file

Modified: 
sling/trunk/launchpad/integration-tests/src/main/resources/integration-test/servlets/post/testimport2.json
URL: 
http://svn.apache.org/viewvc/sling/trunk/launchpad/integration-tests/src/main/resources/integration-test/servlets/post/testimport2.json?rev=960134&r1=960133&r2=960134&view=diff
==============================================================================
--- 
sling/trunk/launchpad/integration-tests/src/main/resources/integration-test/servlets/post/testimport2.json
 (original)
+++ 
sling/trunk/launchpad/integration-tests/src/main/resources/integration-test/servlets/post/testimport2.json
 Fri Jul  2 23:18:18 2010
@@ -1,12 +1,13 @@
 {
-    ":name" : "nodeName",
-       "jcr:primaryType": "nt:unstructured",
-       "jcr:mixinTypes": [
-               "mix:referenceable"
-       ],
-       "propOne" : "propOneValue",
-       
-       "childOne" : {
-               "childPropOne" : true
+       "nodeName": {
+               "jcr:primaryType": "nt:unstructured",
+               "jcr:mixinTypes": [
+                       "mix:referenceable"
+               ],
+               "propOne" : "propOneValue",
+               
+               "childOne" : {
+                       "childPropOne" : true
+               }
        }
-}
+}
\ No newline at end of file


Reply via email to