Skip to content

Commit 249c688

Browse files
committed
DispatcherServlet's checkMultipart detects wrapped MultipartRequest as well
Issue: SPR-12114 (cherry picked from commit 786fd92)
1 parent e324c2a commit 249c688

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,7 +1055,7 @@ public Locale getLocale() {
10551055
*/
10561056
protected HttpServletRequest checkMultipart(HttpServletRequest request) throws MultipartException {
10571057
if (this.multipartResolver != null && this.multipartResolver.isMultipart(request)) {
1058-
if (request instanceof MultipartHttpServletRequest) {
1058+
if (WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class) != null) {
10591059
logger.debug("Request is already a MultipartHttpServletRequest - if not in a forward, " +
10601060
"this typically results from an additional MultipartFilter in web.xml");
10611061
}
@@ -1073,13 +1073,13 @@ else if (request.getAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE) instanceof Mul
10731073

10741074
/**
10751075
* Clean up any resources used by the given multipart request (if any).
1076-
* @param servletRequest current HTTP request
1076+
* @param request current HTTP request
10771077
* @see MultipartResolver#cleanupMultipart
10781078
*/
1079-
protected void cleanupMultipart(HttpServletRequest servletRequest) {
1080-
MultipartHttpServletRequest req = WebUtils.getNativeRequest(servletRequest, MultipartHttpServletRequest.class);
1081-
if (req != null) {
1082-
this.multipartResolver.cleanupMultipart(req);
1079+
protected void cleanupMultipart(HttpServletRequest request) {
1080+
MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class);
1081+
if (multipartRequest != null) {
1082+
this.multipartResolver.cleanupMultipart(multipartRequest);
10831083
}
10841084
}
10851085

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

Lines changed: 3 additions & 2 deletions
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.
@@ -67,6 +67,7 @@
6767
import org.springframework.web.servlet.theme.ThemeChangeInterceptor;
6868
import org.springframework.web.servlet.view.InternalResourceViewResolver;
6969
import org.springframework.web.servlet.view.ResourceBundleViewResolver;
70+
import org.springframework.web.util.WebUtils;
7071

7172
/**
7273
* @author Juergen Hoeller
@@ -401,7 +402,7 @@ public void doSomething(HttpServletRequest request) throws ServletException, Ill
401402
if (!(wac instanceof ComplexWebApplicationContext)) {
402403
throw new ServletException("Incorrect WebApplicationContext");
403404
}
404-
if (!(request instanceof MultipartHttpServletRequest)) {
405+
if (WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class) == null) {
405406
throw new ServletException("Not in a MultipartHttpServletRequest");
406407
}
407408
if (request.getParameter("fail") != null) {

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

Lines changed: 18 additions & 1 deletion
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;
@@ -217,6 +218,22 @@ public void testExistingMultipartRequest() throws Exception {
217218
MultipartHttpServletRequest multipartRequest = multipartResolver.resolveMultipart(request);
218219
complexDispatcherServlet.service(multipartRequest, response);
219220
multipartResolver.cleanupMultipart(multipartRequest);
221+
assertNull(request.getAttribute(SimpleMappingExceptionResolver.DEFAULT_EXCEPTION_ATTRIBUTE));
222+
assertNotNull(request.getAttribute("cleanedUp"));
223+
}
224+
225+
public void testExistingMultipartRequestButWrapped() throws Exception {
226+
MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do;abc=def");
227+
request.addPreferredLocale(Locale.CANADA);
228+
request.addUserRole("role1");
229+
MockHttpServletResponse response = new MockHttpServletResponse();
230+
ComplexWebApplicationContext.MockMultipartResolver multipartResolver =
231+
(ComplexWebApplicationContext.MockMultipartResolver) complexDispatcherServlet.getWebApplicationContext()
232+
.getBean("multipartResolver");
233+
MultipartHttpServletRequest multipartRequest = multipartResolver.resolveMultipart(request);
234+
complexDispatcherServlet.service(new HttpServletRequestWrapper(multipartRequest), response);
235+
multipartResolver.cleanupMultipart(multipartRequest);
236+
assertNull(request.getAttribute(SimpleMappingExceptionResolver.DEFAULT_EXCEPTION_ATTRIBUTE));
220237
assertNotNull(request.getAttribute("cleanedUp"));
221238
}
222239

0 commit comments

Comments
 (0)