You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by mg...@apache.org on 2012/08/20 10:14:09 UTC

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

Updated Branches:
  refs/heads/wicket-1.5.x f28287c3f -> 3d80d9a88


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/3d80d9a8
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/3d80d9a8
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/3d80d9a8

Branch: refs/heads/wicket-1.5.x
Commit: 3d80d9a8824e858df9b6427d55261286402139f4
Parents: f28287c
Author: Martin Tzvetanov Grigorov <mg...@apache.org>
Authored: Mon Aug 20 11:13:43 2012 +0300
Committer: Martin Tzvetanov Grigorov <mg...@apache.org>
Committed: Mon Aug 20 11:13:43 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/3d80d9a8/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 d41d4ae..6ed988a 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)
@@ -1527,7 +1532,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
@@ -1542,7 +1547,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/3d80d9a8/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 6e0ba20..397370c 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
@@ -35,6 +35,7 @@ import org.apache.wicket.markup.MarkupType;
 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;
@@ -72,6 +73,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;
@@ -451,6 +453,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/3d80d9a8/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/3d80d9a8/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 fdfd171..e12dbca 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;