diff --git a/org.springframework.web/src/main/java/org/springframework/web/filter/HttpPutFormContentFilter.java b/org.springframework.web/src/main/java/org/springframework/web/filter/HttpPutFormContentFilter.java index a0b10b7dac0..540ed247416 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/filter/HttpPutFormContentFilter.java +++ b/org.springframework.web/src/main/java/org/springframework/web/filter/HttpPutFormContentFilter.java @@ -36,6 +36,7 @@ import javax.servlet.http.HttpServletRequestWrapper; import javax.servlet.http.HttpServletResponse; import org.springframework.http.HttpInputMessage; +import org.springframework.http.MediaType; import org.springframework.http.converter.FormHttpMessageConverter; import org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter; import org.springframework.http.server.ServletServerHttpRequest; @@ -43,22 +44,22 @@ import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; /** - * {@link javax.servlet.Filter} that makes form encoded data available through the - * {@code ServletRequest.getParameter*()} family of methods during HTTP PUT requests. + * {@link javax.servlet.Filter} that makes form encoded data available through + * the {@code ServletRequest.getParameter*()} family of methods during HTTP PUT + * requests. * - *
The Servlet spec requires form data to be available for HTTP POST but not for - * HTTP PUT requests. This filter intercepts HTTP PUT requests - * where {@code 'Content-Type:application/x-www-form-urlencoded'}, reads the form - * data from the body of the request, and wraps the ServletRequest in order to make - * the form data available as request parameters. + *
The Servlet spec requires form data to be available for HTTP POST but
+ * not for HTTP PUT requests. This filter intercepts HTTP PUT requests where
+ * content type is {@code 'application/x-www-form-urlencoded'}, reads form
+ * encoded content from the body of the request, and wraps the ServletRequest
+ * in order to make the form data available as request parameters just like
+ * it is for HTTP POST requests.
*
* @author Rossen Stoyanchev
* @since 3.1
*/
public class HttpPutFormContentFilter extends OncePerRequestFilter {
- private static final String FORM_CONTENT_TYPE = "application/x-www-form-urlencoded";
-
private final FormHttpMessageConverter formConverter = new XmlAwareFormHttpMessageConverter();
/**
@@ -69,8 +70,8 @@ public class HttpPutFormContentFilter extends OncePerRequestFilter {
}
@Override
- protected void doFilterInternal(final HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
- throws ServletException, IOException {
+ protected void doFilterInternal(final HttpServletRequest request, HttpServletResponse response,
+ FilterChain filterChain) throws ServletException, IOException {
if ("PUT".equals(request.getMethod()) && isFormContentType(request)) {
HttpInputMessage inputMessage = new ServletServerHttpRequest(request) {
@@ -91,7 +92,13 @@ public class HttpPutFormContentFilter extends OncePerRequestFilter {
private boolean isFormContentType(HttpServletRequest request) {
String contentType = request.getContentType();
- return ((contentType != null) && contentType.equals(FORM_CONTENT_TYPE));
+ if (contentType != null) {
+ MediaType mediaType = MediaType.parseMediaType(contentType);
+ return (MediaType.APPLICATION_FORM_URLENCODED.includes(mediaType));
+ }
+ else {
+ return false;
+ }
}
private static class HttpPutFormContentRequestWrapper extends HttpServletRequestWrapper {
diff --git a/org.springframework.web/src/test/java/org/springframework/web/filter/HttpPutFormContentFilterTests.java b/org.springframework.web/src/test/java/org/springframework/web/filter/HttpPutFormContentFilterTests.java
index a8b58ad14a3..17fdb035170 100644
--- a/org.springframework.web/src/test/java/org/springframework/web/filter/HttpPutFormContentFilterTests.java
+++ b/org.springframework.web/src/test/java/org/springframework/web/filter/HttpPutFormContentFilterTests.java
@@ -18,6 +18,7 @@ package org.springframework.web.filter;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
@@ -52,7 +53,7 @@ public class HttpPutFormContentFilterTests {
filter = new HttpPutFormContentFilter();
request = new MockHttpServletRequest("PUT", "/");
request.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=ISO-8859-1");
- request.setContentType("application/x-www-form-urlencoded");
+ request.setContentType("application/x-www-form-urlencoded; charset=ISO-8859-1");
response = new MockHttpServletResponse();
filterChain = new MockFilterChain();
}
@@ -90,20 +91,22 @@ public class HttpPutFormContentFilterTests {
}
@Test
- public void queryStringParam() throws Exception {
+ public void getParameterFromQueryString() throws Exception {
request.addParameter("name", "value1");
request.setContent("name=value2".getBytes("ISO-8859-1"));
filter.doFilter(request, response, filterChain);
-
+
+ assertNotSame("Request not wrapped", request, filterChain.getRequest());
assertEquals("Query string parameters should be listed ahead of form parameters",
"value1", filterChain.getRequest().getParameter("name"));
}
@Test
- public void nullParameter() throws Exception {
+ public void getParameterNullValue() throws Exception {
request.setContent("name=value".getBytes("ISO-8859-1"));
filter.doFilter(request, response, filterChain);
+ assertNotSame("Request not wrapped", request, filterChain.getRequest());
assertNull(filterChain.getRequest().getParameter("noSuchParam"));
}
@@ -112,9 +115,11 @@ public class HttpPutFormContentFilterTests {
request.addParameter("name1", "value1");
request.addParameter("name2", "value2");
request.setContent("name1=value1&name3=value3&name4=value4".getBytes("ISO-8859-1"));
+
filter.doFilter(request, response, filterChain);
-
List