From 64a69f7cf826c39c0b72da4f910b8445c4627815 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Wed, 1 Feb 2012 13:22:12 -0500 Subject: [PATCH] SPR-9079 Don't check for "POST" multipart request method arg resolvers --- .../resources/changelog.txt | 1 + .../RequestPartMethodArgumentResolver.java | 13 +++++------- ...equestPartMethodArgumentResolverTests.java | 14 +++++++++++-- .../RequestParamMethodArgumentResolver.java | 13 +++--------- ...questParamMethodArgumentResolverTests.java | 21 ++++++++++++++++--- 5 files changed, 39 insertions(+), 23 deletions(-) diff --git a/build-spring-framework/resources/changelog.txt b/build-spring-framework/resources/changelog.txt index a5ddc80ba75..0597b8d2357 100644 --- a/build-spring-framework/resources/changelog.txt +++ b/build-spring-framework/resources/changelog.txt @@ -30,6 +30,7 @@ Changes in version 3.1.1 (2012-02-06) * add property to RedirectView to disable expanding URI variables in redirect URL * fix request mapping bug involving direct vs pattern path matches with HTTP methods * revise the FlashMapManager contract and implemenation to address a flaw in its design +* Remove check for HTTP "POST" when resolving multipart request controller method arguments Changes in version 3.1 GA (2011-12-12) -------------------------------------- diff --git a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestPartMethodArgumentResolver.java b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestPartMethodArgumentResolver.java index 6b1c1125c0a..f517cc03501 100644 --- a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestPartMethodArgumentResolver.java +++ b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestPartMethodArgumentResolver.java @@ -110,9 +110,7 @@ public class RequestPartMethodArgumentResolver extends AbstractMessageConverterM NativeWebRequest request, WebDataBinderFactory binderFactory) throws Exception { HttpServletRequest servletRequest = request.getNativeRequest(HttpServletRequest.class); - if (!isMultipartRequest(servletRequest)) { - throw new MultipartException("The current request is not a multipart request"); - } + assertIsMultipartRequest(servletRequest); MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(servletRequest, MultipartHttpServletRequest.class); @@ -166,12 +164,11 @@ public class RequestPartMethodArgumentResolver extends AbstractMessageConverterM return arg; } - private boolean isMultipartRequest(HttpServletRequest request) { - if (!"post".equals(request.getMethod().toLowerCase())) { - return false; - } + private static void assertIsMultipartRequest(HttpServletRequest request) { String contentType = request.getContentType(); - return (contentType != null && contentType.toLowerCase().startsWith("multipart/")); + if (contentType == null || !contentType.toLowerCase().startsWith("multipart/")) { + throw new MultipartException("The current request is not a multipart request"); + } } private String getPartName(MethodParameter parameter) { diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestPartMethodArgumentResolverTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestPartMethodArgumentResolverTests.java index afbaf311d3c..94b18935fa2 100644 --- a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestPartMethodArgumentResolverTests.java +++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestPartMethodArgumentResolverTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -227,12 +227,22 @@ public class RequestPartMethodArgumentResolverTests { } @Test(expected=MultipartException.class) - public void notMultipartRequest() throws Exception { + public void isMultipartRequest() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); resolver.resolveArgument(paramMultipartFile, new ModelAndViewContainer(), new ServletWebRequest(request), null); fail("Expected exception"); } + // SPR-9079 + + @Test + public void isMultipartRequestPut() throws Exception { + this.multipartRequest.setMethod("PUT"); + Object actual = resolver.resolveArgument(paramMultipartFile, null, webRequest, null); + assertNotNull(actual); + assertSame(multipartFile1, actual); + } + private void testResolveArgument(SimpleBean argValue, MethodParameter parameter) throws IOException, Exception { MediaType contentType = MediaType.TEXT_PLAIN; diff --git a/org.springframework.web/src/main/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolver.java b/org.springframework.web/src/main/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolver.java index c659d699d29..0fb76c9bfb4 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolver.java +++ b/org.springframework.web/src/main/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolver.java @@ -179,17 +179,10 @@ public class RequestParamMethodArgumentResolver extends AbstractNamedValueMethod } private void assertIsMultipartRequest(HttpServletRequest request) { - if (!isMultipartRequest(request)) { - throw new MultipartException("The current request is not a multipart request."); - } - } - - private boolean isMultipartRequest(HttpServletRequest request) { - if (!"post".equals(request.getMethod().toLowerCase())) { - return false; - } String contentType = request.getContentType(); - return (contentType != null && contentType.toLowerCase().startsWith("multipart/")); + if (contentType == null || !contentType.toLowerCase().startsWith("multipart/")) { + throw new MultipartException("The current request is not a multipart request"); + } } private boolean isMultipartFileCollection(MethodParameter parameter) { diff --git a/org.springframework.web/src/test/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolverTests.java b/org.springframework.web/src/test/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolverTests.java index 38af0e20a12..fe4099c6811 100644 --- a/org.springframework.web/src/test/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolverTests.java +++ b/org.springframework.web/src/test/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolverTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -45,7 +45,6 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestPart; import org.springframework.web.context.request.NativeWebRequest; import org.springframework.web.context.request.ServletWebRequest; -import org.springframework.web.method.annotation.RequestParamMethodArgumentResolver; import org.springframework.web.multipart.MultipartException; import org.springframework.web.multipart.MultipartFile; @@ -183,11 +182,27 @@ public class RequestParamMethodArgumentResolverTests { } @Test(expected = MultipartException.class) - public void notMultipartRequest() throws Exception { + public void isMultipartRequest() throws Exception { resolver.resolveArgument(paramMultiPartFile, null, webRequest, null); fail("Expected exception: request is not a multipart request"); } + // SPR-9079 + + @Test + public void isMultipartRequestHttpPut() throws Exception { + MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest(); + MultipartFile expected = new MockMultipartFile("multipartFileList", "Hello World".getBytes()); + request.addFile(expected); + request.setMethod("PUT"); + webRequest = new ServletWebRequest(request); + + Object actual = resolver.resolveArgument(paramMultipartFileList, null, webRequest, null); + + assertTrue(actual instanceof List); + assertEquals(expected, ((List) actual).get(0)); + } + @Test(expected = IllegalArgumentException.class) public void missingMultipartFile() throws Exception { request.setMethod("POST");