incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r605921 - in /incubator/sling/trunk/microsling/microsling-core: pom.xml src/main/java/org/apache/sling/microsling/request/helpers/SlingRequestParameterMap.java
Date Thu, 20 Dec 2007 13:31:47 GMT
Author: bdelacretaz
Date: Thu Dec 20 05:31:46 2007
New Revision: 605921

URL: http://svn.apache.org/viewvc?rev=605921&view=rev
Log:
SLING-144 - multipart form support for microsling, thanks to Philipp Koch

Modified:
    incubator/sling/trunk/microsling/microsling-core/pom.xml
    incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/request/helpers/SlingRequestParameterMap.java

Modified: incubator/sling/trunk/microsling/microsling-core/pom.xml
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/pom.xml?rev=605921&r1=605920&r2=605921&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/pom.xml (original)
+++ incubator/sling/trunk/microsling/microsling-core/pom.xml Thu Dec 20 05:31:46 2007
@@ -166,7 +166,12 @@
       <groupId>org.slf4j</groupId>
       <artifactId>jcl104-over-slf4j</artifactId>
       <version>1.3.0</version>
-    </dependency>  
+    </dependency>
+    <dependency>
+        <groupId>commons-fileupload</groupId>
+        <artifactId>commons-fileupload</artifactId>
+        <version>1.1.1</version>
+    </dependency>
   </dependencies>
 
   <build>

Modified: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/request/helpers/SlingRequestParameterMap.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/request/helpers/SlingRequestParameterMap.java?rev=605921&r1=605920&r2=605921&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/request/helpers/SlingRequestParameterMap.java
(original)
+++ incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/request/helpers/SlingRequestParameterMap.java
Thu Dec 20 05:31:46 2007
@@ -21,13 +21,21 @@
 import java.io.ByteArrayInputStream;
 import java.io.InputStream;
 import java.io.UnsupportedEncodingException;
+import java.io.IOException;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Iterator;
+import java.util.List;
 
 import javax.servlet.http.HttpServletRequest;
 
 import org.apache.sling.api.request.RequestParameter;
 import org.apache.sling.api.request.RequestParameterMap;
+import org.apache.commons.fileupload.servlet.ServletFileUpload;
+import org.apache.commons.fileupload.FileUploadException;
+import org.apache.commons.fileupload.FileItem;
+import org.apache.commons.fileupload.FileItemFactory;
+import org.apache.commons.fileupload.disk.DiskFileItemFactory;
 
 /**
  * The <code>SlingRequestParameterMap</code> implements the
@@ -39,14 +47,16 @@
 
     /** Create from the HTTP request parameters */
     public SlingRequestParameterMap(HttpServletRequest request) {
-        Map<?, ?> parameters = request.getParameterMap();
-        for (Map.Entry<?, ?> entry : parameters.entrySet()) {
-            String[] values = (String[]) entry.getValue();
-            RequestParameter[] rpValues = new RequestParameter[values.length];
-            for (int i = 0; i < values.length; i++) {
-                rpValues[i] = new SimpleRequestParameter(values[i]);
+        if (ServletFileUpload.isMultipartContent(request)) {
+            try {
+                initializeMultipart(request);
+            } catch (UnsupportedEncodingException e) {
+                // ignore for the moment
+            } catch (FileUploadException e) {
+                // ignore for the moment
             }
-            put((String) entry.getKey(), rpValues);
+        } else {
+            initializeStandard(request);
         }
     }
 
@@ -59,17 +69,58 @@
         return (values != null && values.length > 0) ? values[0] : null;
     }
 
+    /** initialize from a non-multipart request */
+    private void initializeStandard(HttpServletRequest request) {
+        Map<?, ?> parameters = request.getParameterMap();
+        for (Map.Entry<?, ?> entry : parameters.entrySet()) {
+            String[] values = (String[]) entry.getValue();
+            RequestParameter[] rpValues = new RequestParameter[values.length];
+            for (int i = 0; i < values.length; i++) {
+                rpValues[i] = new SimpleRequestParameter(values[i]);
+            }
+            put((String) entry.getKey(), rpValues);
+        }
+    }
+
+    /** initialize from a multipart request */
+    private void initializeMultipart(HttpServletRequest request)
+            throws FileUploadException,UnsupportedEncodingException {
+        // use commons fileupload to parse request
+        FileItemFactory factory = new DiskFileItemFactory();
+        ServletFileUpload upload = new ServletFileUpload(factory);
+        List items = upload.parseRequest(request);
+
+        for(Iterator it = items.iterator(); it.hasNext(); ) {
+            final FileItem fi = (FileItem)it.next();
+            put(fi.getFieldName(), new RequestParameter[] {new SimpleRequestParameter(fi)});
+        }
+    }
+
     /** Simple implementation of the RequestParameter interface */
     private static class SimpleRequestParameter implements RequestParameter {
 
         private String value;
 
+        private FileItem fileItem;
+
         private byte[] cachedBytes;
 
         SimpleRequestParameter(String value) {
             this.value = value;
         }
 
+        SimpleRequestParameter(FileItem fi) {
+            if(fi.isFormField()) {
+              // TODO: FileItem does not seem to support multiple values
+              // if there are multiple values we lose them
+              value = fi.getString();
+              fileItem = null;
+            } else {
+              value = null;
+              fileItem = fi;
+            }
+        }
+
         /**
          * Convert the parameter string value to a byte[] using ISO-8859-1
          * encoding, which is assumed to be the default for parameters
@@ -92,11 +143,18 @@
         }
 
         public String getFileName() {
-            return null;
+            return fileItem.getName();
         }
 
         public InputStream getInputStream() {
-            return new ByteArrayInputStream(get());
+            if (value != null) {
+                return new ByteArrayInputStream(get());
+            } else {
+                try {
+                    return fileItem.getInputStream();
+                } catch (IOException ioe) {}
+            }
+            return new ByteArrayInputStream(new byte[0]);
         }
 
         public long getSize() {



Mime
View raw message