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:
Rossen Stoyanchev
2011-09-09 09:09:10 +00:00
parent b6c1e88e4a
commit 56c8c69c4c
9 changed files with 260 additions and 84 deletions

View File

@@ -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);