Skip to content

Commit 786fd92

Browse files
committed
DispatcherServlet's checkMultipart detects wrapped MultipartRequest as well
Issue: SPR-12114
1 parent 439ce4a commit 786fd92

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
@@ -1061,7 +1061,7 @@ public Locale getLocale() {
10611061
*/
10621062
protected HttpServletRequest checkMultipart(HttpServletRequest request) throws MultipartException {
10631063
if (this.multipartResolver != null && this.multipartResolver.isMultipart(request)) {
1064-
if (request instanceof MultipartHttpServletRequest) {
1064+
if (WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class) != null) {
10651065
logger.debug("Request is already a MultipartHttpServletRequest - if not in a forward, " +
10661066
"this typically results from an additional MultipartFilter in web.xml");
10671067
}
@@ -1079,13 +1079,13 @@ else if (request.getAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE) instanceof Mul
10791079

10801080
/**
10811081
* Clean up any resources used by the given multipart request (if any).
1082-
* @param servletRequest current HTTP request
1082+
* @param request current HTTP request
10831083
* @see MultipartResolver#cleanupMultipart
10841084
*/
1085-
protected void cleanupMultipart(HttpServletRequest servletRequest) {
1086-
MultipartHttpServletRequest req = WebUtils.getNativeRequest(servletRequest, MultipartHttpServletRequest.class);
1087-
if (req != null) {
1088-
this.multipartResolver.cleanupMultipart(req);
1085+
protected void cleanupMultipart(HttpServletRequest request) {
1086+
MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class);
1087+
if (multipartRequest != null) {
1088+
this.multipartResolver.cleanupMultipart(multipartRequest);
10891089
}
10901090
}
10911091

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)