Updated Branches:
  refs/heads/master 89c72d363 -> 4fc82e359

WICKET-4715 WebApplication doesn't recognize if an incoming request is 
multipart.


Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/4fc82e35
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/4fc82e35
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/4fc82e35

Branch: refs/heads/master
Commit: 4fc82e3598ec6d790fe13c05ef99b1a69095779e
Parents: 89c72d3
Author: Martin Tzvetanov Grigorov <[email protected]>
Authored: Mon Aug 20 11:13:43 2012 +0300
Committer: Martin Tzvetanov Grigorov <[email protected]>
Committed: Mon Aug 20 11:14:23 2012 +0300

----------------------------------------------------------------------
 .../org/apache/wicket/markup/html/form/Form.java   |    9 ++-
 .../wicket/protocol/http/WebApplication.java       |   19 ++++++
 .../protocol/http/MultiPartTestApplication.java    |   49 +++++++++++++++
 .../http/servlet/ServletWebRequestTest.java        |   22 +++++++
 4 files changed, 97 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/4fc82e35/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Form.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Form.java 
b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Form.java
index 9d0245d..9d46baf 100644
--- a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Form.java
+++ b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Form.java
@@ -147,6 +147,11 @@ public class Form<T> extends WebMarkupContainer implements 
IFormSubmitListener
        private static final String HIDDEN_DIV_START = "<div 
style=\"width:0px;height:0px;position:absolute;left:-100px;top:-100px;overflow:hidden\">";
 
        /**
+        * The value of HTMLFormElement's <code>enctype</code> attribute needed 
for file uploading.
+        */
+       public static final String ENCTYPE_MULTIPART_FORM_DATA = 
"multipart/form-data";
+
+       /**
         * Visitor used for validation
         * 
         * @author Igor Vaynberg (ivaynberg)
@@ -1540,7 +1545,7 @@ public class Form<T> extends WebMarkupContainer 
implements IFormSubmitListener
                                        tag.put("method", 
METHOD_POST.toLowerCase(Locale.ENGLISH));
                                }
 
-                               tag.put("enctype", "multipart/form-data");
+                               tag.put("enctype", ENCTYPE_MULTIPART_FORM_DATA);
                                //
                                // require the application-encoding for 
multipart/form-data to be sure to
                                // get multipart-uploaded characters with the 
proper encoding on the following
@@ -1555,7 +1560,7 @@ public class Form<T> extends WebMarkupContainer 
implements IFormSubmitListener
                        {
                                // sanity check
                                String enctype = 
(String)tag.getAttributes().get("enctype");
-                               if 
("multipart/form-data".equalsIgnoreCase(enctype))
+                               if 
(ENCTYPE_MULTIPART_FORM_DATA.equalsIgnoreCase(enctype))
                                {
                                        // though not set explicitly in Java, 
this is a multipart
                                        // form

http://git-wip-us.apache.org/repos/asf/wicket/blob/4fc82e35/wicket-core/src/main/java/org/apache/wicket/protocol/http/WebApplication.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/main/java/org/apache/wicket/protocol/http/WebApplication.java 
b/wicket-core/src/main/java/org/apache/wicket/protocol/http/WebApplication.java
index 42ab5d1..117947d 100644
--- 
a/wicket-core/src/main/java/org/apache/wicket/protocol/http/WebApplication.java
+++ 
b/wicket-core/src/main/java/org/apache/wicket/protocol/http/WebApplication.java
@@ -44,6 +44,7 @@ import org.apache.wicket.markup.head.JavaScriptHeaderItem;
 import org.apache.wicket.markup.html.WebPage;
 import org.apache.wicket.markup.html.form.AutoLabelResolver;
 import org.apache.wicket.markup.html.form.AutoLabelTextResolver;
+import org.apache.wicket.markup.html.form.Form;
 import org.apache.wicket.markup.html.pages.AccessDeniedPage;
 import org.apache.wicket.markup.html.pages.InternalErrorPage;
 import org.apache.wicket.markup.html.pages.PageExpiredErrorPage;
@@ -79,6 +80,7 @@ import org.apache.wicket.util.lang.Args;
 import org.apache.wicket.util.lang.PackageName;
 import org.apache.wicket.util.string.Strings;
 import org.apache.wicket.util.time.Duration;
+import org.apache.wicket.util.upload.FileUploadException;
 import org.apache.wicket.util.watch.IModificationWatcher;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -486,6 +488,23 @@ public abstract class WebApplication extends Application
 
                WebRequest webRequest = newWebRequest(servletRequest, 
filterPath);
 
+               String contentType = servletRequest.getContentType();
+               String method = servletRequest.getMethod();
+
+               if (webRequest instanceof ServletWebRequest && 
Form.METHOD_POST.equalsIgnoreCase(method) &&
+                               Strings.isEmpty(contentType) == false && 
contentType.toLowerCase().startsWith(Form.ENCTYPE_MULTIPART_FORM_DATA))
+               {
+                       try
+                       {
+                               return 
((ServletWebRequest)webRequest).newMultipartWebRequest(
+                                       
getApplicationSettings().getDefaultMaximumUploadSize(), "externalForm");
+                       }
+                       catch (FileUploadException e)
+                       {
+                               throw new RuntimeException(e);
+                       }
+               }
+
                return webRequest;
        }
 

http://git-wip-us.apache.org/repos/asf/wicket/blob/4fc82e35/wicket-core/src/test/java/org/apache/wicket/protocol/http/MultiPartTestApplication.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/test/java/org/apache/wicket/protocol/http/MultiPartTestApplication.java
 
b/wicket-core/src/test/java/org/apache/wicket/protocol/http/MultiPartTestApplication.java
new file mode 100644
index 0000000..6805d76
--- /dev/null
+++ 
b/wicket-core/src/test/java/org/apache/wicket/protocol/http/MultiPartTestApplication.java
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.protocol.http;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.wicket.Page;
+import org.apache.wicket.request.http.WebRequest;
+import org.apache.wicket.util.tester.DummyHomePage;
+
+/**
+ * An extension of WebApplication just to make its #createWebRequest() public
+ */
+public class MultiPartTestApplication extends WebApplication
+{
+       /**
+        * Extend #createWebRequest() just to make it public
+        * @param servletRequest
+        *            the current HTTP Sservlet request
+        * @param filterPath
+        *            the filter mapping read from web.xml
+        * @return
+        */
+       @Override
+       public WebRequest createWebRequest(HttpServletRequest servletRequest, 
String filterPath)
+       {
+               return super.createWebRequest(servletRequest, filterPath);
+       }
+
+       @Override
+       public Class<? extends Page> getHomePage()
+       {
+               return DummyHomePage.class;
+       }
+};
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/wicket/blob/4fc82e35/wicket-core/src/test/java/org/apache/wicket/protocol/http/servlet/ServletWebRequestTest.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/test/java/org/apache/wicket/protocol/http/servlet/ServletWebRequestTest.java
 
b/wicket-core/src/test/java/org/apache/wicket/protocol/http/servlet/ServletWebRequestTest.java
index 4a06a16..df6abdb 100644
--- 
a/wicket-core/src/test/java/org/apache/wicket/protocol/http/servlet/ServletWebRequestTest.java
+++ 
b/wicket-core/src/test/java/org/apache/wicket/protocol/http/servlet/ServletWebRequestTest.java
@@ -22,6 +22,8 @@ import org.apache.wicket.MarkupContainer;
 import org.apache.wicket.Page;
 import org.apache.wicket.markup.IMarkupResourceStreamProvider;
 import org.apache.wicket.markup.html.WebPage;
+import org.apache.wicket.markup.html.form.Form;
+import org.apache.wicket.protocol.http.MultiPartTestApplication;
 import org.apache.wicket.protocol.http.WebApplication;
 import org.apache.wicket.protocol.http.mock.MockHttpServletRequest;
 import org.apache.wicket.request.Url;
@@ -133,6 +135,26 @@ public class ServletWebRequestTest extends Assert
                tester.startPage(new CustomRequestPage());
        }
 
+       /**
+        * https://issues.apache.org/jira/browse/WICKET-4715
+        */
+       @Test
+       public void multiPartWebRequest()
+       {
+               MultiPartTestApplication application = new 
MultiPartTestApplication();
+               new WicketTester(application); // inits the app
+
+               MockHttpServletRequest httpRequest = new 
MockHttpServletRequest(null, null, null);
+               httpRequest.setURL("/");
+               httpRequest.setParameter("some", "parameter");
+               httpRequest.setMethod(Form.METHOD_POST);
+               httpRequest.setUseMultiPartContentType(true);
+
+               WebRequest webRequest = 
application.createWebRequest(httpRequest, "/");
+               assertTrue(webRequest instanceof MultipartServletWebRequest);
+       }
+
+
        private static class CustomRequestPage extends WebPage implements 
IMarkupResourceStreamProvider
        {
                private static final long serialVersionUID = 1L;

Reply via email to