incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From enor...@apache.org
Subject svn commit: r1150269 - in /sling/trunk: bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/ bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/ launchpad/integration-tests/src/main/java/or...
Date Sun, 24 Jul 2011 03:20:01 GMT
Author: enorman
Date: Sun Jul 24 03:19:59 2011
New Revision: 1150269

URL: http://svn.apache.org/viewvc?rev=1150269&view=rev
Log:
SLING-2143 SlingPostServlet ImportOperation should honor the _charset_ parameter

Added:
    sling/trunk/launchpad/integration-tests/src/main/resources/integration-test/servlets/post/testimport_utf8.json
Modified:
    sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/SlingIntegrationTestClient.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/testing/pom.xml

Modified: sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/SlingIntegrationTestClient.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/SlingIntegrationTestClient.java?rev=1150269&r1=1150268&r2=1150269&view=diff
==============================================================================
--- sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/SlingIntegrationTestClient.java
(original)
+++ sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/SlingIntegrationTestClient.java
Sun Jul 24 03:19:59 2011
@@ -169,6 +169,7 @@ public class SlingIntegrationTestClient 
                 final Part [] parts = partList.toArray(new Part[partList.size()]);
                 post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
             } else {
+            	post.getParams().setContentCharset("UTF-8");
                 for(NameValuePair e : nodeProperties) {
                     post.addParameter(e.getName(),e.getValue());
                 }

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=1150269&r1=1150268&r2=1150269&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
Sun Jul 24 03:19:59 2011
@@ -16,7 +16,6 @@
  */
 package org.apache.sling.servlets.post.impl.operations;
 
-import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.List;
@@ -59,6 +58,14 @@ public class ImportOperation extends Abs
         this.contentImporter = importer;
     }
 
+    private String getRequestParamAsString(SlingHttpServletRequest request, String key) {
+    	RequestParameter requestParameter = request.getRequestParameter(key);
+    	if (requestParameter == null) {
+    		return null;
+    	}
+    	return requestParameter.getString();
+    }
+    
     @Override
     protected void doRun(SlingHttpServletRequest request, PostResponse response, final List<Modification>
changes)
             throws RepositoryException {
@@ -95,7 +102,7 @@ public class ImportOperation extends Abs
             return;
         }
 
-        String contentType = request.getParameter(SlingPostConstants.RP_CONTENT_TYPE);
+        String contentType = getRequestParamAsString(request, SlingPostConstants.RP_CONTENT_TYPE);
         if (contentType == null) {
             response.setStatus(HttpServletResponse.SC_PRECONDITION_FAILED,
             "Required :contentType parameter is missing");
@@ -103,9 +110,9 @@ public class ImportOperation extends Abs
         }
 
         //import options passed as request parameters.
-        final boolean replace = "true".equalsIgnoreCase(request.getParameter(SlingPostConstants.RP_REPLACE));
-        final boolean replaceProperties = "true".equalsIgnoreCase(request.getParameter(SlingPostConstants.RP_REPLACE_PROPERTIES));
-        final boolean checkin = "true".equalsIgnoreCase(request.getParameter(SlingPostConstants.RP_CHECKIN));
+        final boolean replace = "true".equalsIgnoreCase(getRequestParamAsString(request,
SlingPostConstants.RP_REPLACE));
+        final boolean replaceProperties = "true".equalsIgnoreCase(getRequestParamAsString(request,
SlingPostConstants.RP_REPLACE_PROPERTIES));
+        final boolean checkin = "true".equalsIgnoreCase(getRequestParamAsString(request,
SlingPostConstants.RP_CHECKIN));
 
         String basePath = getItemPath(request);
         basePath = removeAndValidateWorkspace(basePath, request.getResourceResolver().adaptTo(Session.class));
@@ -119,9 +126,9 @@ public class ImportOperation extends Abs
 
         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) {
+        if (getRequestParamAsString(request, SlingPostConstants.RP_NODE_NAME) != null) {
             // exact name
-            targetName = request.getParameter(SlingPostConstants.RP_NODE_NAME);
+            targetName = getRequestParamAsString(request, SlingPostConstants.RP_NODE_NAME);
             if (targetName.length() > 0 && node.hasNode(targetName)) {
                 if (replace) {
                     response.setCreateRequest(false);
@@ -133,7 +140,7 @@ public class ImportOperation extends Abs
                     return;
                 }
             }
-        } else if (request.getParameter(SlingPostConstants.RP_NODE_NAME_HINT) != null) {
+        } else if (getRequestParamAsString(request, SlingPostConstants.RP_NODE_NAME_HINT)
!= null) {
             // node name hint only
             String nodePath = generateName(request, basePath);
             String name = nodePath.substring(nodePath.lastIndexOf('/') + 1);
@@ -146,9 +153,9 @@ public class ImportOperation extends Abs
 
         try {
             InputStream contentStream = null;
-            String content = request.getParameter(SlingPostConstants.RP_CONTENT);
-            if (content != null) {
-                contentStream = new ByteArrayInputStream(content.getBytes());
+        	RequestParameter contentParameter = request.getRequestParameter(SlingPostConstants.RP_CONTENT);
+            if (contentParameter != null) {
+                contentStream = contentParameter.getInputStream();
             } else {
                 RequestParameter contentFile = request.getRequestParameter(SlingPostConstants.RP_CONTENT_FILE);
                 if (contentFile != null) {

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=1150269&r1=1150268&r2=1150269&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
Sun Jul 24 03:19:59 2011
@@ -20,6 +20,7 @@ import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.InputStreamReader;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -68,6 +69,17 @@ public class PostServletImportTest exten
         }
         return content.toString();
     }
+
+	static String getStreamAsString(InputStream is, String charset) throws IOException {
+		InputStreamReader reader = new InputStreamReader(is, charset);
+        final StringBuilder content = new StringBuilder();
+        final char [] buffer = new char[16384];
+        int n = 0;
+        while( (n = reader.read(buffer, 0, buffer.length)) > 0) {
+            content.append(new String(buffer, 0, n));
+        }
+        return content.toString();
+    }
     
     private File getTestFile(InputStream inputStream) throws IOException {
     	File tempFile = File.createTempFile("file-to-upload", null, new File("target"));
@@ -696,5 +708,37 @@ public class PostServletImportTest exten
 		assertPostStatus(postUrl + SlingPostConstants.DEFAULT_CREATE_SUFFIX, HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
postParams, null);
     }
 
-    
+    /**
+     * SLING-2143: test import where json is in a UTF-8 charset
+     */
+    public void testImportJSONWithUTF8Content() throws IOException, JSONException {
+        final String testPath = TEST_BASE_PATH;
+        Map<String, String> props = new HashMap<String, String>();
+        String testNode = testClient.createNode(HTTP_BASE_URL + testPath, props);
+        urlsToDelete.add(testNode);
+
+        props.clear();
+        props.put(SlingPostConstants.RP_OPERATION,
+        		SlingPostConstants.OPERATION_IMPORT);
+        
+        String testNodeName = "testNode_" + String.valueOf(random.nextInt());
+        props.put(SlingPostConstants.RP_NODE_NAME_HINT, testNodeName);
+        String jsonContent = (String)getStreamAsString(getClass().getResourceAsStream("/integration-test/servlets/post/testimport_utf8.json"),
"UTF-8");
+        props.put(SlingPostConstants.RP_CONTENT, jsonContent);
+        props.put(SlingPostConstants.RP_CONTENT_TYPE, "json");
+        props.put(SlingPostConstants.RP_REDIRECT_TO, SERVLET_CONTEXT + testPath + "/*");
+
+        String importedNodeUrl = testClient.createNode(HTTP_BASE_URL + testPath, props);
+
+        // assert content at new location
+        String content = getContent(importedNodeUrl + ".json", CONTENT_TYPE_JSON);
+
+		JSONObject jsonObj = new JSONObject(content);
+		assertNotNull(jsonObj);
+
+		//assert the imported content is there.
+        String expectedJsonContent = (String)getStreamAsString(getClass().getResourceAsStream("/integration-test/servlets/post/testimport_utf8.json"),
"UTF-8");
+		assertExpectedJSON(new JSONObject(expectedJsonContent), jsonObj);
+    }
+
 }

Added: sling/trunk/launchpad/integration-tests/src/main/resources/integration-test/servlets/post/testimport_utf8.json
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/integration-tests/src/main/resources/integration-test/servlets/post/testimport_utf8.json?rev=1150269&view=auto
==============================================================================
--- sling/trunk/launchpad/integration-tests/src/main/resources/integration-test/servlets/post/testimport_utf8.json
(added)
+++ sling/trunk/launchpad/integration-tests/src/main/resources/integration-test/servlets/post/testimport_utf8.json
Sun Jul 24 03:19:59 2011
@@ -0,0 +1,4 @@
+{
+    'latin':'øµå', 
+    'chinese':'玄牛'
+}
\ No newline at end of file

Modified: sling/trunk/launchpad/testing/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/testing/pom.xml?rev=1150269&r1=1150268&r2=1150269&view=diff
==============================================================================
--- sling/trunk/launchpad/testing/pom.xml (original)
+++ sling/trunk/launchpad/testing/pom.xml Sun Jul 24 03:19:59 2011
@@ -504,7 +504,7 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.commons.testing</artifactId>
-            <version>2.0.8</version>
+            <version>2.0.9-SNAPSHOT</version>
             <scope>test</scope>
             <exclusions>
                 <exclusion>



Mime
View raw message