SPR-8642 IMPROVE ERROR REPORTING WHEN RESOLVING MULTIPART REQUEST METHOD ARGUMENTS
Separate client from server errors as much as possible in this order: - raise MultipartException when resolving a multipart arg and the request is not a multipart request (400) - raise IllegalArgumentException when the arg type is MultipartFile but the request is not of type MultipartHttpServletRequest (500) - raise MissingServletRequestPartException when a MultipartResolver is in use but the part is not found (400) - detect presence of Servlet 3.0 before using standard multipart parsing to find a request part or raise IllegalArgumentException (500) Unfortunately it is not always possible to separate client from server errors mainly because the Servlet 3.0 API does not distinguish between the case of 0 request parts vs multipart processing not being configured.
This commit is contained in:
@@ -29,6 +29,7 @@ import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||
import org.springframework.core.GenericCollectionTypeResolver;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.MissingServletRequestParameterException;
|
||||
import org.springframework.web.bind.WebDataBinder;
|
||||
@@ -36,9 +37,9 @@ import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RequestPart;
|
||||
import org.springframework.web.bind.annotation.ValueConstants;
|
||||
import org.springframework.web.context.request.NativeWebRequest;
|
||||
import org.springframework.web.multipart.MultipartException;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
||||
import org.springframework.web.multipart.MultipartRequest;
|
||||
import org.springframework.web.multipart.MultipartResolver;
|
||||
import org.springframework.web.util.WebUtils;
|
||||
|
||||
@@ -58,8 +59,8 @@ import org.springframework.web.util.WebUtils;
|
||||
* parameter type, the {@link RequestParamMapMethodArgumentResolver} is used instead
|
||||
* providing access to all request parameters in the form of a map.
|
||||
*
|
||||
* <p>A {@link WebDataBinder} is invoked to apply type conversion to resolved request header values that
|
||||
* don't yet match the method parameter type.
|
||||
* <p>A {@link WebDataBinder} is invoked to apply type conversion to resolved request
|
||||
* header values that don't yet match the method parameter type.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @author Rossen Stoyanchev
|
||||
@@ -71,11 +72,13 @@ public class RequestParamMethodArgumentResolver extends AbstractNamedValueMethod
|
||||
private final boolean useDefaultResolution;
|
||||
|
||||
/**
|
||||
* @param beanFactory a bean factory to use for resolving ${...} placeholder and #{...} SpEL expressions
|
||||
* in default values, or {@code null} if default values are not expected to contain expressions
|
||||
* @param useDefaultResolution in default resolution mode a method argument that is a simple type, as
|
||||
* defined in {@link BeanUtils#isSimpleProperty(Class)}, is treated as a request parameter even if it doesn't have
|
||||
* an @{@link RequestParam} annotation, the request parameter name is derived from the method parameter name.
|
||||
* @param beanFactory a bean factory used for resolving ${...} placeholder
|
||||
* and #{...} SpEL expressions in default values, or {@code null} if default
|
||||
* values are not expected to contain expressions
|
||||
* @param useDefaultResolution in default resolution mode a method argument
|
||||
* that is a simple type, as defined in {@link BeanUtils#isSimpleProperty},
|
||||
* is treated as a request parameter even if it itsn't annotated, the
|
||||
* request parameter name is derived from the method parameter name.
|
||||
*/
|
||||
public RequestParamMethodArgumentResolver(ConfigurableBeanFactory beanFactory,
|
||||
boolean useDefaultResolution) {
|
||||
@@ -87,11 +90,15 @@ public class RequestParamMethodArgumentResolver extends AbstractNamedValueMethod
|
||||
* Supports the following:
|
||||
* <ul>
|
||||
* <li>@RequestParam-annotated method arguments.
|
||||
* This excludes {@link Map} parameters where the annotation does not specify a name value.
|
||||
* See {@link RequestParamMapMethodArgumentResolver} instead for such parameters.
|
||||
* <li>Arguments of type {@link MultipartFile} unless annotated with {@link RequestPart}.
|
||||
* <li>Arguments of type {@code javax.servlet.http.Part} unless annotated with {@link RequestPart}.
|
||||
* <li>In default resolution mode, simple type arguments even if not with @RequestParam.
|
||||
* This excludes {@link Map} params where the annotation doesn't
|
||||
* specify a name. See {@link RequestParamMapMethodArgumentResolver}
|
||||
* instead for such params.
|
||||
* <li>Arguments of type {@link MultipartFile}
|
||||
* unless annotated with @{@link RequestPart}.
|
||||
* <li>Arguments of type {@code javax.servlet.http.Part}
|
||||
* unless annotated with @{@link RequestPart}.
|
||||
* <li>In default resolution mode, simple type arguments
|
||||
* even if not with @{@link RequestParam}.
|
||||
* </ul>
|
||||
*/
|
||||
public boolean supportsParameter(MethodParameter parameter) {
|
||||
@@ -139,14 +146,17 @@ public class RequestParamMethodArgumentResolver extends AbstractNamedValueMethod
|
||||
WebUtils.getNativeRequest(servletRequest, MultipartHttpServletRequest.class);
|
||||
|
||||
if (MultipartFile.class.equals(parameter.getParameterType())) {
|
||||
assertMultipartRequest(multipartRequest, webRequest);
|
||||
assertIsMultipartRequest(servletRequest);
|
||||
Assert.notNull(multipartRequest, "Expected MultipartHttpServletRequest: is a MultipartResolver configured?");
|
||||
arg = multipartRequest.getFile(name);
|
||||
}
|
||||
else if (isMultipartFileCollection(parameter)) {
|
||||
assertMultipartRequest(multipartRequest, webRequest);
|
||||
assertIsMultipartRequest(servletRequest);
|
||||
Assert.notNull(multipartRequest, "Expected MultipartHttpServletRequest: is a MultipartResolver configured?");
|
||||
arg = multipartRequest.getFiles(name);
|
||||
}
|
||||
else if ("javax.servlet.http.Part".equals(parameter.getParameterType().getName())) {
|
||||
assertIsMultipartRequest(servletRequest);
|
||||
arg = servletRequest.getPart(name);
|
||||
}
|
||||
else {
|
||||
@@ -168,13 +178,20 @@ public class RequestParamMethodArgumentResolver extends AbstractNamedValueMethod
|
||||
return arg;
|
||||
}
|
||||
|
||||
private void assertMultipartRequest(MultipartHttpServletRequest multipartRequest, NativeWebRequest request) {
|
||||
if (multipartRequest == null) {
|
||||
throw new IllegalStateException("Current request is not of type [" + MultipartRequest.class.getName()
|
||||
+ "]: " + request + ". Do you have a MultipartResolver configured?");
|
||||
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/"));
|
||||
}
|
||||
|
||||
private boolean isMultipartFileCollection(MethodParameter parameter) {
|
||||
Class<?> paramType = parameter.getParameterType();
|
||||
if (Collection.class.equals(paramType) || List.class.isAssignableFrom(paramType)){
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2002-2011 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.
|
||||
* 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.springframework.web.multipart.support;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
|
||||
import org.springframework.web.multipart.MultipartResolver;
|
||||
|
||||
/**
|
||||
* Raised when the part of a "multipart/form-data" request identified by its
|
||||
* name cannot be found.
|
||||
*
|
||||
* <p>This may be because the request is not a multipart/form-data
|
||||
*
|
||||
* either because the part is not present in the request, or
|
||||
* because the web application is not configured correctly for processing
|
||||
* multipart requests -- e.g. no {@link MultipartResolver}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 3.1
|
||||
*/
|
||||
public class MissingServletRequestPartException extends ServletException {
|
||||
|
||||
private static final long serialVersionUID = -1255077391966870705L;
|
||||
|
||||
private final String partName;
|
||||
|
||||
public MissingServletRequestPartException(String partName) {
|
||||
super("Request part '" + partName + "' not found.");
|
||||
this.partName = partName;
|
||||
}
|
||||
|
||||
public String getRequestPartName() {
|
||||
return this.partName;
|
||||
}
|
||||
}
|
||||
@@ -19,18 +19,23 @@ package org.springframework.web.multipart.support;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.server.ServerHttpRequest;
|
||||
import org.springframework.http.server.ServletServerHttpRequest;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.web.multipart.MultipartException;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
||||
import org.springframework.web.multipart.MultipartResolver;
|
||||
|
||||
/**
|
||||
* {@link ServerHttpRequest} implementation that is based on a part of a {@link MultipartHttpServletRequest}.
|
||||
* The part is accessed as {@link MultipartFile} and adapted to the ServerHttpRequest contract.
|
||||
* {@link ServerHttpRequest} implementation that accesses one part of a multipart
|
||||
* request. If using {@link MultipartResolver} configuration the part is accessed
|
||||
* through a {@link MultipartFile}. Or if using Servlet 3.0 multipart processing
|
||||
* the part is accessed through {@code ServletRequest.getPart}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Juergen Hoeller
|
||||
@@ -46,23 +51,44 @@ public class RequestPartServletServerHttpRequest extends ServletServerHttpReques
|
||||
|
||||
|
||||
/**
|
||||
* Create a new {@link RequestPartServletServerHttpRequest} instance.
|
||||
* @param request the multipart request
|
||||
* Create a new instance.
|
||||
* @param request the current request
|
||||
* @param partName the name of the part to adapt to the {@link ServerHttpRequest} contract
|
||||
* @throws MissingServletRequestPartException if the request part cannot be found
|
||||
* @throws IllegalArgumentException if MultipartHttpServletRequest cannot be initialized
|
||||
*/
|
||||
public RequestPartServletServerHttpRequest(HttpServletRequest request, String partName) {
|
||||
public RequestPartServletServerHttpRequest(HttpServletRequest request, String partName)
|
||||
throws MissingServletRequestPartException {
|
||||
|
||||
super(request);
|
||||
|
||||
this.multipartRequest = (request instanceof MultipartHttpServletRequest ?
|
||||
(MultipartHttpServletRequest) request : new StandardMultipartHttpServletRequest(request));
|
||||
this.multipartRequest = asMultipartRequest(request);
|
||||
this.partName = partName;
|
||||
|
||||
this.headers = this.multipartRequest.getMultipartHeaders(this.partName);
|
||||
if (this.headers == null) {
|
||||
throw new IllegalArgumentException("No request part found for name '" + this.partName + "'");
|
||||
if (request instanceof MultipartHttpServletRequest) {
|
||||
throw new MissingServletRequestPartException(partName);
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException(
|
||||
"Failed to obtain request part: " + partName + ". " +
|
||||
"The part is missing or multipart processing is not configured. " +
|
||||
"Check for a MultipartResolver bean or if Servlet 3.0 multipart processing is enabled.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static MultipartHttpServletRequest asMultipartRequest(HttpServletRequest request) {
|
||||
if (request instanceof MultipartHttpServletRequest) {
|
||||
return (MultipartHttpServletRequest) request;
|
||||
}
|
||||
else if (ClassUtils.hasMethod(HttpServletRequest.class, "getParts")) {
|
||||
// Servlet 3.0 available ..
|
||||
return new StandardMultipartHttpServletRequest(request);
|
||||
}
|
||||
throw new IllegalArgumentException("Expected MultipartHttpServletRequest: is a MultipartResolver configured?");
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpHeaders getHeaders() {
|
||||
|
||||
@@ -16,10 +16,10 @@
|
||||
|
||||
package org.springframework.web.method.annotation.support;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
@@ -45,6 +45,7 @@ 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.multipart.MultipartException;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
/**
|
||||
@@ -180,16 +181,26 @@ public class RequestParamMethodArgumentResolverTests {
|
||||
assertEquals(Arrays.asList(expected1, expected2), result);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void missingMultipartFile() throws Exception {
|
||||
@Test(expected = MultipartException.class)
|
||||
public void notMultipartRequest() throws Exception {
|
||||
resolver.resolveArgument(paramMultiPartFile, null, webRequest, null);
|
||||
fail("Expected exception");
|
||||
fail("Expected exception: request is not a multipart request");
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void missingMultipartFile() throws Exception {
|
||||
request.setMethod("POST");
|
||||
request.setContentType("multipart/form-data");
|
||||
resolver.resolveArgument(paramMultiPartFile, null, webRequest, null);
|
||||
fail("Expected exception: request is not MultiPartHttpServletRequest but param is MultipartFile");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveServlet30Part() throws Exception {
|
||||
MockPart expected = new MockPart("servlet30Part", "Hello World".getBytes());
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setMethod("POST");
|
||||
request.setContentType("multipart/form-data");
|
||||
request.addPart(expected);
|
||||
webRequest = new ServletWebRequest(request);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user