incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r742907 - in /incubator/sling/trunk: commons/testing/src/main/java/org/apache/sling/commons/testing/integration/ launchpad/testing/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/ servlets/post/src/main/java/org/apache/sling...
Date Tue, 10 Feb 2009 09:23:18 GMT
Author: fmeschbe
Date: Tue Feb 10 09:23:10 2009
New Revision: 742907

URL: http://svn.apache.org/viewvc?rev=742907&view=rev
Log:
SLING-854 Apply patch by Eric Norman (Thanks!)

Modified:
    incubator/sling/trunk/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/SlingIntegrationTestClient.java
    incubator/sling/trunk/launchpad/testing/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/UploadFileTest.java
    incubator/sling/trunk/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/helper/SlingFileUploadHandler.java

Modified: incubator/sling/trunk/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/SlingIntegrationTestClient.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/SlingIntegrationTestClient.java?rev=742907&r1=742906&r2=742907&view=diff
==============================================================================
--- incubator/sling/trunk/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/SlingIntegrationTestClient.java
(original)
+++ incubator/sling/trunk/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/SlingIntegrationTestClient.java
Tue Feb 10 09:23:10 2009
@@ -190,4 +190,29 @@
             throw new HttpStatusCodeException(200, status, "POST", url);
         }
     }
+
+    /** Upload multiple files to file node structures */
+    public void uploadToFileNodes(String url, File[] localFiles, String[] fieldNames, String[]
typeHints)
+        throws IOException {
+
+    	List<Part> partsList = new ArrayList<Part>();
+    	for (int i=0; i < localFiles.length; i++) {
+            Part filePart = new FilePart(fieldNames[i], localFiles[i]);
+            partsList.add(filePart);
+            if (typeHints != null) {
+            	Part typeHintPart = new StringPart(fieldNames[i] + "@TypeHint", typeHints[i]);
+            	partsList.add(typeHintPart);
+            }
+		}
+    	
+        final Part[] parts = partsList.toArray(new Part[partsList.size()]);
+        final PostMethod post = new PostMethod(url);
+        post.setFollowRedirects(false);
+        post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
+
+        final int status = httpClient.executeMethod(post);
+        if(status!=200) { // fmeschbe: The default sling status is 200, not 302
+            throw new HttpStatusCodeException(200, status, "POST", url);
+        }
+    }
 }

Modified: incubator/sling/trunk/launchpad/testing/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/UploadFileTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/launchpad/testing/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/UploadFileTest.java?rev=742907&r1=742906&r2=742907&view=diff
==============================================================================
--- incubator/sling/trunk/launchpad/testing/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/UploadFileTest.java
(original)
+++ incubator/sling/trunk/launchpad/testing/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/UploadFileTest.java
Tue Feb 10 09:23:10 2009
@@ -19,6 +19,7 @@
 import java.io.File;
 import java.io.IOException;
 
+import org.apache.commons.httpclient.HttpException;
 import org.apache.commons.httpclient.methods.GetMethod;
 import org.apache.sling.commons.testing.integration.HttpTestBase;
 
@@ -147,4 +148,68 @@
         assertTrue("checking primary type", json.contains("\"jcr:primaryType\":\"nt:file\""));
     }
 
+    
+    public void testMultiFileUpload() throws IOException {
+        String folderPath = "/UploadFileTest_1_" + System.currentTimeMillis();
+        final String url = HTTP_BASE_URL + folderPath;
+
+        // create new node
+        String urlOfNewNode = null;
+        try {
+            urlOfNewNode = testClient.createNode(url, null);
+        } catch(IOException ioe) {
+            fail("createNode failed: " + ioe);
+        }
+
+        // upload local file
+        File [] localFiles = new File[] {
+        	new File(TEST_FILE),
+        	new File(TEST_FILE),
+        	new File(TEST_FILE)
+        };
+        String [] fieldNames = new String [] {
+        		"./file1.txt",
+        		"./file2.txt",
+        		"./*"        		
+        };
+        testClient.uploadToFileNodes(urlOfNewNode, localFiles, fieldNames, null);
+
+        // get and check URL of created file1
+        String urlOfFileNode = urlOfNewNode + "/file1.txt";
+        checkUploadedFileState(urlOfFileNode);    	
+
+        // get and check URL of created file2
+        String urlOfFileNode2 = urlOfNewNode + "/file2.txt";
+        checkUploadedFileState(urlOfFileNode2);    	
+
+        // get and check URL of created file3
+        String urlOfFileNode3 = urlOfNewNode + "/file-to-upload.txt";
+        checkUploadedFileState(urlOfFileNode3);    	
+    }
+
+	private void checkUploadedFileState(String urlOfFileNode) throws IOException,
+			HttpException {
+		final GetMethod get = new GetMethod(urlOfFileNode);
+        final int status = httpClient.executeMethod(get);
+        assertEquals(urlOfFileNode + " must be accessible after createNode",200,status);
+
+        /*
+        We should check the data, but nt:resources are not handled yet
+        // compare data with local file (just length)
+        final byte[] data = get.getResponseBody();
+        assertEquals("size of file must be same", localFile.length(), data.length);
+        */
+        String data = get.getResponseBodyAsString();
+        assertTrue("checking for content", data.contains("http://www.apache.org/licenses/LICENSE-2.0"));
+
+        // download structure
+        String json = getContent(urlOfFileNode + ".json", CONTENT_TYPE_JSON);
+        // just check for some strings
+        assertTrue("checking primary type", json.contains("\"jcr:primaryType\":\"nt:file\""));
+
+        String content_json = getContent(urlOfFileNode + "/jcr:content.json", CONTENT_TYPE_JSON);
+        // just check for some strings
+        assertTrue("checking primary type", content_json.contains("\"jcr:primaryType\":\"nt:resource\""));
+        assertTrue("checking mime type", content_json.contains("\"jcr:mimeType\":\"text/plain\""));
+	}
 }
\ No newline at end of file

Modified: incubator/sling/trunk/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/helper/SlingFileUploadHandler.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/helper/SlingFileUploadHandler.java?rev=742907&r1=742906&r2=742907&view=diff
==============================================================================
--- incubator/sling/trunk/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/helper/SlingFileUploadHandler.java
(original)
+++ incubator/sling/trunk/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/helper/SlingFileUploadHandler.java
Tue Feb 10 09:23:10 2009
@@ -108,99 +108,104 @@
      */
     public void setFile(Node parent, RequestProperty prop, List<Modification> changes)
             throws RepositoryException {
-        RequestParameter value = prop.getValues()[0];
-        assert !value.isFormField();
+    	RequestParameter[] values = prop.getValues();
+    	for (RequestParameter requestParameter : values) {
+        	RequestParameter value = requestParameter;
+
+        	// ignore if a plain form field or empty
+            if (value.isFormField() || value.getSize() <= 0) {
+                continue;
+            }
 
-        // ignore if empty
-        if (value.getSize() <= 0) {
-            return;
-        }
-
-        // get node name
-        String name = prop.getName();
-        if (name.equals("*")) {
-            name = value.getFileName();
-            // strip of possible path (some browsers include the entire path)
-            name = name.substring(name.lastIndexOf('/') + 1);
-            name = name.substring(name.lastIndexOf('\\') + 1);
-        }
-        name = Text.escapeIllegalJcrChars(name);
-
-        // check type hint. if the type is ok and extends from nt:file,
-        // create an nt:file with that type. if it's invalid, drop it and let
-        // the parent node type decide.
-        boolean createNtFile = parent.isNodeType(NT_FOLDER);
-        String typeHint = prop.getTypeHint();
-        if (typeHint != null) {
-            try {
-                NodeTypeManager ntMgr = parent.getSession().getWorkspace().getNodeTypeManager();
-                NodeType nt = ntMgr.getNodeType(typeHint);
-                createNtFile = nt.isNodeType(NT_FILE);
-            } catch (RepositoryException e) {
-                // assuming type not valid.
-                typeHint = null;
-            }
-        }
-
-        // also create an nt:file if the name contains an extension
-        // the rationale is that if the file name is "important" we want
-        // an nt:file, and an image name with an extension is probably "important"
-        if(!createNtFile && name.indexOf('.') > 0) {
-            createNtFile = true;
-        }
-
-        // set empty type
-        if (typeHint == null) {
-            typeHint = createNtFile ? NT_FILE : NT_RESOURCE;
-        }
-
-        // remove node
-        if (parent.hasNode(name)) {
-            parent.getNode(name).remove();
-        }
-
-        // create nt:file node if needed
-        if (createNtFile) {
-            // create nt:file
-            parent = parent.addNode(name, typeHint);
-            changes.add(Modification.onCreated(parent.getPath()));
-            name = JCR_CONTENT;
-            typeHint = NT_RESOURCE;
-        }
-
-        // create resource node
-        Node res = parent.addNode(name, typeHint);
-        changes.add(Modification.onCreated(res.getPath()));
-
-        // get content type
-        String contentType = value.getContentType();
-        if (contentType != null) {
-            int idx = contentType.indexOf(';');
-            if (idx > 0) {
-                contentType = contentType.substring(0, idx);
-            }
-        }
-        if (contentType == null || contentType.equals("application/octet-stream")) {
-            // try to find a better content type
-            contentType = this.servletContext.getMimeType(value.getFileName());
+            // get node name
+            String name = prop.getName();
+            if (name.equals("*")) {
+                name = value.getFileName();
+                // strip of possible path (some browsers include the entire path)
+                name = name.substring(name.lastIndexOf('/') + 1);
+                name = name.substring(name.lastIndexOf('\\') + 1);
+            }
+            name = Text.escapeIllegalJcrChars(name);
+
+            // check type hint. if the type is ok and extends from nt:file,
+            // create an nt:file with that type. if it's invalid, drop it and let
+            // the parent node type decide.
+            boolean createNtFile = parent.isNodeType(NT_FOLDER);
+            String typeHint = prop.getTypeHint();
+            if (typeHint != null) {
+                try {
+                    NodeTypeManager ntMgr = parent.getSession().getWorkspace().getNodeTypeManager();
+                    NodeType nt = ntMgr.getNodeType(typeHint);
+                    createNtFile = nt.isNodeType(NT_FILE);
+                } catch (RepositoryException e) {
+                    // assuming type not valid.
+                    typeHint = null;
+                }
+            }
+
+            // also create an nt:file if the name contains an extension
+            // the rationale is that if the file name is "important" we want
+            // an nt:file, and an image name with an extension is probably "important"
+            if(!createNtFile && name.indexOf('.') > 0) {
+                createNtFile = true;
+            }
+
+            // set empty type
+            if (typeHint == null) {
+                typeHint = createNtFile ? NT_FILE : NT_RESOURCE;
+            }
+
+            // remove node
+            if (parent.hasNode(name)) {
+                parent.getNode(name).remove();
+            }
+
+            // create nt:file node if needed
+            Node resParent;
+            if (createNtFile) {
+                // create nt:file
+                resParent = parent.addNode(name, typeHint);
+                changes.add(Modification.onCreated(resParent.getPath()));
+                name = JCR_CONTENT;
+                typeHint = NT_RESOURCE;
+            } else {
+            	resParent = parent;
+            }
+
+            // create resource node
+            Node res = resParent.addNode(name, typeHint);
+            changes.add(Modification.onCreated(res.getPath()));
+
+            // get content type
+            String contentType = value.getContentType();
+            if (contentType != null) {
+                int idx = contentType.indexOf(';');
+                if (idx > 0) {
+                    contentType = contentType.substring(0, idx);
+                }
+            }
             if (contentType == null || contentType.equals("application/octet-stream")) {
-                contentType = "application/octet-stream";
+                // try to find a better content type
+                contentType = this.servletContext.getMimeType(value.getFileName());
+                if (contentType == null || contentType.equals("application/octet-stream"))
{
+                    contentType = "application/octet-stream";
+                }
             }
-        }
 
-        // set properties
-        changes.add(Modification.onModified(
-            res.setProperty(JCR_LASTMODIFIED, Calendar.getInstance()).getPath()
-        ));
-        changes.add(Modification.onModified(
-            res.setProperty(JCR_MIMETYPE, contentType).getPath()
-        ));
-        try {
+            // set properties
             changes.add(Modification.onModified(
-                res.setProperty(JCR_DATA, value.getInputStream()).getPath()
+                res.setProperty(JCR_LASTMODIFIED, Calendar.getInstance()).getPath()
             ));
-        } catch (IOException e) {
-            throw new RepositoryException("Error while retrieving inputstream from parameter
value.", e);
-        }
+            changes.add(Modification.onModified(
+                res.setProperty(JCR_MIMETYPE, contentType).getPath()
+            ));
+            try {
+                changes.add(Modification.onModified(
+                    res.setProperty(JCR_DATA, value.getInputStream()).getPath()
+                ));
+            } catch (IOException e) {
+                throw new RepositoryException("Error while retrieving inputstream from parameter
value.", e);
+            }
+		}
     }
 }
\ No newline at end of file



Mime
View raw message