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 names = Collections.list(filterChain.getRequest().getParameterNames()); + + assertNotSame("Request not wrapped", request, filterChain.getRequest()); assertEquals(Arrays.asList("name1", "name2", "name3", "name4"), names); } @@ -123,42 +128,50 @@ public class HttpPutFormContentFilterTests { request.addParameter("name", "value1"); request.addParameter("name", "value2"); request.setContent("name=value3&name=value4".getBytes("ISO-8859-1")); + filter.doFilter(request, response, filterChain); - String[] values = filterChain.getRequest().getParameterValues("name"); + + assertNotSame("Request not wrapped", request, filterChain.getRequest()); assertArrayEquals(new String[]{"value1", "value2", "value3", "value4"}, values); } @Test - public void getQueryStringParameterValuesOnly() throws Exception { + public void getParameterValuesFromQueryString() throws Exception { request.addParameter("name", "value1"); request.addParameter("name", "value2"); request.setContent("anotherName=anotherValue".getBytes("ISO-8859-1")); - filter.doFilter(request, response, filterChain); + filter.doFilter(request, response, filterChain); String[] values = filterChain.getRequest().getParameterValues("name"); + + assertNotSame("Request not wrapped", request, filterChain.getRequest()); assertArrayEquals(new String[]{"value1", "value2"}, values); } @Test - public void getFormParameterValuesOnly() throws Exception { + public void getParameterValuesFromFormContent() throws Exception { request.addParameter("name", "value1"); request.addParameter("name", "value2"); request.setContent("anotherName=anotherValue".getBytes("ISO-8859-1")); + filter.doFilter(request, response, filterChain); - String[] values = filterChain.getRequest().getParameterValues("anotherName"); + + assertNotSame("Request not wrapped", request, filterChain.getRequest()); assertArrayEquals(new String[]{"anotherValue"}, values); } @Test - public void noParameterValuesOnly() throws Exception { + public void getParameterValuesInvalidName() throws Exception { request.addParameter("name", "value1"); request.addParameter("name", "value2"); request.setContent("anotherName=anotherValue".getBytes("ISO-8859-1")); + filter.doFilter(request, response, filterChain); - String[] values = filterChain.getRequest().getParameterValues("noSuchParameter"); + + assertNotSame("Request not wrapped", request, filterChain.getRequest()); assertNull(values); } @@ -167,9 +180,11 @@ public class HttpPutFormContentFilterTests { request.addParameter("name", "value1"); request.addParameter("name", "value2"); request.setContent("name=value3&name4=value4".getBytes("ISO-8859-1")); - filter.doFilter(request, response, filterChain); + filter.doFilter(request, response, filterChain); Map parameters = filterChain.getRequest().getParameterMap(); + + assertNotSame("Request not wrapped", request, filterChain.getRequest()); assertEquals(2, parameters.size()); assertArrayEquals(new String[] {"value1", "value2", "value3"}, parameters.get("name")); assertArrayEquals(new String[] {"value4"}, parameters.get("name4"));