Skip to content

Commit 7cf4a4e

Browse files
committed
DispatcherServlet's checkMultipart detects wrapped MultipartRequest as well
Issue: SPR-12114 (cherry picked from commit 786fd92)
1 parent 388561d commit 7cf4a4e

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -1033,7 +1033,7 @@ public String toString() {
10331033
*/
10341034
protected HttpServletRequest checkMultipart(HttpServletRequest request) throws MultipartException {
10351035
if (this.multipartResolver != null && this.multipartResolver.isMultipart(request)) {
1036-
if (request instanceof MultipartHttpServletRequest) {
1036+
if (WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class) != null) {
10371037
logger.debug("Request is already a MultipartHttpServletRequest - if not in a forward, " +
10381038
"this typically results from an additional MultipartFilter in web.xml");
10391039
}
@@ -1047,13 +1047,13 @@ protected HttpServletRequest checkMultipart(HttpServletRequest request) throws M
10471047

10481048
/**
10491049
* Clean up any resources used by the given multipart request (if any).
1050-
* @param servletRequest current HTTP request
1050+
* @param request current HTTP request
10511051
* @see MultipartResolver#cleanupMultipart
10521052
*/
1053-
protected void cleanupMultipart(HttpServletRequest servletRequest) {
1054-
MultipartHttpServletRequest req = WebUtils.getNativeRequest(servletRequest, MultipartHttpServletRequest.class);
1055-
if (req != null) {
1056-
this.multipartResolver.cleanupMultipart(req);
1053+
protected void cleanupMultipart(HttpServletRequest request) {
1054+
MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class);
1055+
if (multipartRequest != null) {
1056+
this.multipartResolver.cleanupMultipart(multipartRequest);
10571057
}
10581058
}
10591059

spring-webmvc/src/test/java/org/springframework/web/servlet/ComplexWebApplicationContext.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -66,6 +66,7 @@
6666
import org.springframework.web.servlet.theme.ThemeChangeInterceptor;
6767
import org.springframework.web.servlet.view.InternalResourceViewResolver;
6868
import org.springframework.web.servlet.view.ResourceBundleViewResolver;
69+
import org.springframework.web.util.WebUtils;
6970

7071
/**
7172
* @author Juergen Hoeller
@@ -405,7 +406,7 @@ public void doSomething(HttpServletRequest request) throws ServletException, Ill
405406
if (!(wac instanceof ComplexWebApplicationContext)) {
406407
throw new ServletException("Incorrect WebApplicationContext");
407408
}
408-
if (!(request instanceof MultipartHttpServletRequest)) {
409+
if (WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class) == null) {
409410
throw new ServletException("Not in a MultipartHttpServletRequest");
410411
}
411412
if (!(RequestContextUtils.getLocaleResolver(request) instanceof SessionLocaleResolver)) {

spring-webmvc/src/test/java/org/springframework/web/servlet/DispatcherServletTests.java

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323
import javax.servlet.ServletContext;
2424
import javax.servlet.ServletException;
2525
import javax.servlet.http.HttpServletRequest;
26+
import javax.servlet.http.HttpServletRequestWrapper;
2627
import javax.servlet.http.HttpServletResponse;
2728

2829
import junit.framework.TestCase;
@@ -305,6 +306,22 @@ public void testExistingMultipartRequest() throws Exception {
305306
MultipartHttpServletRequest multipartRequest = multipartResolver.resolveMultipart(request);
306307
complexDispatcherServlet.service(multipartRequest, response);
307308
multipartResolver.cleanupMultipart(multipartRequest);
309+
assertNull(request.getAttribute(SimpleMappingExceptionResolver.DEFAULT_EXCEPTION_ATTRIBUTE));
310+
assertNotNull(request.getAttribute("cleanedUp"));
311+
}
312+
313+
public void testExistingMultipartRequestButWrapped() throws Exception {
314+
MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do;abc=def");
315+
request.addPreferredLocale(Locale.CANADA);
316+
request.addUserRole("role1");
317+
MockHttpServletResponse response = new MockHttpServletResponse();
318+
ComplexWebApplicationContext.MockMultipartResolver multipartResolver =
319+
(ComplexWebApplicationContext.MockMultipartResolver) complexDispatcherServlet.getWebApplicationContext()
320+
.getBean("multipartResolver");
321+
MultipartHttpServletRequest multipartRequest = multipartResolver.resolveMultipart(request);
322+
complexDispatcherServlet.service(new HttpServletRequestWrapper(multipartRequest), response);
323+
multipartResolver.cleanupMultipart(multipartRequest);
324+
assertNull(request.getAttribute(SimpleMappingExceptionResolver.DEFAULT_EXCEPTION_ATTRIBUTE));
308325
assertNotNull(request.getAttribute("cleanedUp"));
309326
}
310327

0 commit comments

Comments
 (0)