Start to hook-up the commons-file upload port to the Servlet3 file upload API
authormarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Fri, 20 Nov 2009 13:46:21 +0000 (13:46 +0000)
committermarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Fri, 20 Nov 2009 13:46:21 +0000 (13:46 +0000)
 - Make sure the upload config is available when required
 - Start to migrate the HTML manager to the upload API

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@882550 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/catalina/Wrapper.java
java/org/apache/catalina/connector/Request.java
java/org/apache/catalina/core/StandardWrapper.java
java/org/apache/catalina/deploy/MultipartDef.java
java/org/apache/catalina/deploy/ServletDef.java
java/org/apache/catalina/manager/HTMLManagerServlet.java
java/org/apache/catalina/startup/WebRuleSet.java
java/org/apache/catalina/startup/WebXml.java
webapps/manager/WEB-INF/web.xml

index a795dfe..465924e 100644 (file)
@@ -19,6 +19,7 @@
 package org.apache.catalina;
 
 
+import javax.servlet.MultipartConfigElement;
 import javax.servlet.Servlet;
 import javax.servlet.ServletException;
 import javax.servlet.UnavailableException;
@@ -334,4 +335,17 @@ public interface Wrapper extends Container {
     public void unload() throws ServletException;
 
 
+    /**
+     * Get the multi-part configuration for the associated servlet. If no
+     * multi-part configuration has been defined, then <code>null</code> will be
+     * returned.
+     */
+    public MultipartConfigElement getMultipartConfig();
+    
+    
+    /**
+     * Set the multi-part configuration for the associated servlet. To clear the
+     * multi-part configuration specify <code>null</code> as the new value.
+     */
+    public void setMultipartConfig(MultipartConfigElement multipartConfig);
 }
index f0a1fb3..ec6660d 100644 (file)
@@ -27,6 +27,7 @@ import java.security.Principal;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -39,6 +40,7 @@ import javax.security.auth.Subject;
 import javax.servlet.AsyncContext;
 import javax.servlet.DispatcherType;
 import javax.servlet.FilterChain;
+import javax.servlet.MultipartConfigElement;
 import javax.servlet.RequestDispatcher;
 import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
@@ -77,8 +79,10 @@ import org.apache.tomcat.util.http.Cookies;
 import org.apache.tomcat.util.http.FastHttpDateFormat;
 import org.apache.tomcat.util.http.Parameters;
 import org.apache.tomcat.util.http.ServerCookie;
+import org.apache.tomcat.util.http.fileupload.FileUploadBase;
 import org.apache.tomcat.util.http.mapper.MappingData;
 import org.apache.tomcat.util.res.StringManager;
+import org.apache.tools.ant.util.CollectionUtils;
 
 
 /**
@@ -2382,8 +2386,20 @@ public class Request
     
     public Collection<Part> getParts() throws IOException, IllegalStateException,
             ServletException {
+        
+        String contentType = getContentType();
+        if (contentType == null ||
+                !contentType.startsWith(FileUploadBase.MULTIPART_FORM_DATA)) {
+            return Collections.emptyList();
+        }
+        
+        MultipartConfigElement mce = getWrapper().getMultipartConfig();
+        if (mce == null) {
+            return Collections.emptyList();
+        }
+        
         // TODO SERVLET3 - file upload
-        return null;
+        return Collections.emptyList();
     }
     
     public Part getPart(String name) throws IOException, IllegalStateException,
index 818de2d..1ab5b02 100644 (file)
@@ -35,6 +35,7 @@ import javax.management.NotificationEmitter;
 import javax.management.NotificationFilter;
 import javax.management.NotificationListener;
 import javax.management.ObjectName;
+import javax.servlet.MultipartConfigElement;
 import javax.servlet.Servlet;
 import javax.servlet.ServletConfig;
 import javax.servlet.ServletContext;
@@ -255,6 +256,11 @@ public class StandardWrapper
     protected int classLoadTime=0;
     
     /**
+     * Multipart config
+     */
+    protected MultipartConfigElement multipartConfig = null;
+
+    /**
      * Static class array used when the SecurityManager is turned on and 
      * <code>Servlet.init</code> is invoked.
      */
@@ -1477,6 +1483,14 @@ public class StandardWrapper
         return classLoadTime;
     }
 
+    public MultipartConfigElement getMultipartConfig() {
+        return multipartConfig;
+    }
+
+    public void setMultipartConfig(MultipartConfigElement multipartConfig) {
+        this.multipartConfig = multipartConfig;
+    }
+
     // -------------------------------------------------------- Package Methods
 
 
index 47930aa..d987f8f 100644 (file)
@@ -36,39 +36,39 @@ public class MultipartDef {
     }
     
     
-    private Long maxFileSize;
+    private String maxFileSize;
 
-    public Long getMaxFileSize() {
+    public String getMaxFileSize() {
         return maxFileSize;
     }
 
-    public void setMaxFileSize(Long maxFileSize) {
+    public void setMaxFileSize(String maxFileSize) {
         this.maxFileSize = maxFileSize;
     }
     
     
-    private Long maxRequestSize;
+    private String maxRequestSize;
 
-    public Long getMaxRequestSize() {
+    public String getMaxRequestSize() {
         return maxRequestSize;
     }
 
-    public void setMaxRequestSize(Long maxRequestSize) {
+    public void setMaxRequestSize(String maxRequestSize) {
         this.maxRequestSize = maxRequestSize;
     }
 
     
-    private Integer fileSizeThreshold;
+    private String fileSizeThreshold;
     
-    public Integer getFileSizeThreshold() {
+    public String getFileSizeThreshold() {
         return fileSizeThreshold;
     }
 
-    public void setFileSizeThreshold(Integer fileSizeThreshold) {
+    public void setFileSizeThreshold(String fileSizeThreshold) {
         this.fileSizeThreshold = fileSizeThreshold;
     }
 
-    
+
     // ---------------------------------------------------------- Object methods
 
     @Override
index 6fec592..9ec90ba 100644 (file)
@@ -211,7 +211,7 @@ public class ServletDef implements Serializable {
     /**
      * The multipart configuration, if any, for this servlet
      */
-    private MultipartDef multipartDef = new MultipartDef();
+    private MultipartDef multipartDef = null;
     
     public MultipartDef getMultipartDef() {
         return this.multipartDef;
index 6054b62..338e465 100644 (file)
@@ -38,6 +38,7 @@ import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpSession;
+import javax.servlet.http.Part;
 
 import org.apache.catalina.Container;
 import org.apache.catalina.Context;
@@ -258,9 +259,13 @@ public final class HTMLManagerServlet extends ManagerServlet {
         return buffer.toString();
     }
 
-    protected String upload(HttpServletRequest request) throws IOException {
+    protected String upload(HttpServletRequest request)
+            throws IOException, ServletException {
         String message = "";
 
+        // TODO - Rewrite this to use the Servlet 3 file upload API
+        Part part = request.getPart("deployWar");
+        
         // Get the tempdir
         File tempdir = (File) getServletContext().getAttribute
             (ServletContext.TEMPDIR);
index 7c57131..c49ec87 100644 (file)
@@ -372,7 +372,8 @@ public class WebRuleSet extends RuleSetBase {
         digester.addObjectCreate(fullPrefix + "/servlet/multipart-config",
                                  "org.apache.catalina.deploy.MultipartDef");
         digester.addSetNext(fullPrefix + "/servlet/multipart-config",
-                            "setMultipartConfig");
+                            "setMultipartDef",
+                            "org.apache.catalina.deploy.MultipartDef");
         digester.addCallMethod(fullPrefix + "/servlet/multipart-config/location",
                                "setLocation", 0);
         digester.addCallMethod(fullPrefix + "/servlet/multipart-config/max-file-size",
index c49a85f..0088e4a 100644 (file)
@@ -28,6 +28,8 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
+import javax.servlet.MultipartConfigElement;
+
 import org.apache.catalina.Context;
 import org.apache.catalina.Wrapper;
 import org.apache.catalina.deploy.ContextEjb;
@@ -562,7 +564,22 @@ public class WebXml {
                         roleRef.getName(), roleRef.getLink());
             }
             wrapper.setServletClass(servlet.getServletClass());
-            // TODO SERVLET3 - Multipart config
+            MultipartDef multipartdef = servlet.getMultipartDef();
+            if (multipartdef != null) {
+                if (multipartdef.getMaxFileSize() != null &&
+                        multipartdef.getMaxRequestSize()!= null &&
+                        multipartdef.getFileSizeThreshold() != null) {
+                    wrapper.setMultipartConfig(new MultipartConfigElement(
+                            multipartdef.getLocation(),
+                            Long.parseLong(multipartdef.getMaxFileSize()),
+                            Long.parseLong(multipartdef.getMaxRequestSize()),
+                            Integer.parseInt(
+                                    multipartdef.getFileSizeThreshold())));
+                } else {
+                    wrapper.setMultipartConfig(new MultipartConfigElement(
+                            multipartdef.getLocation()));
+                }
+            }
             context.addChild(wrapper);
         }
         for (String pattern : servletMappings.keySet()) {
index b27a255..e8d1aed 100644 (file)
@@ -17,9 +17,9 @@
 -->
 
 <web-app xmlns="http://java.sun.com/xml/ns/javaee"
-   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
-   version="2.5"> 
+  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
+  version="3.0">  
 
   <display-name>Tomcat Manager Application</display-name>
   <description>
       <param-name>debug</param-name>
       <param-value>2</param-value>
     </init-param>
+    <multipart-config>
+      <!-- 50MB max -->
+      <max-file-size>52428800</max-file-size>
+      <max-request-size>52428800</max-request-size>
+      <file-size-threshold>0</file-size-threshold>
+    </multipart-config>
   </servlet>
   <servlet>
     <servlet-name>Status</servlet-name>