DispatcherServlet's checkMultipart detects wrapped MultipartRequest as well

Issue: SPR-12114
This commit is contained in:
Juergen Hoeller 2014-08-22 16:46:36 +02:00
parent 439ce4a1a5
commit 786fd927fa
3 changed files with 27 additions and 9 deletions

View File

@ -1061,7 +1061,7 @@ public class DispatcherServlet extends FrameworkServlet {
*/ */
protected HttpServletRequest checkMultipart(HttpServletRequest request) throws MultipartException { protected HttpServletRequest checkMultipart(HttpServletRequest request) throws MultipartException {
if (this.multipartResolver != null && this.multipartResolver.isMultipart(request)) { if (this.multipartResolver != null && this.multipartResolver.isMultipart(request)) {
if (request instanceof MultipartHttpServletRequest) { if (WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class) != null) {
logger.debug("Request is already a MultipartHttpServletRequest - if not in a forward, " + logger.debug("Request is already a MultipartHttpServletRequest - if not in a forward, " +
"this typically results from an additional MultipartFilter in web.xml"); "this typically results from an additional MultipartFilter in web.xml");
} }
@ -1079,13 +1079,13 @@ public class DispatcherServlet extends FrameworkServlet {
/** /**
* Clean up any resources used by the given multipart request (if any). * Clean up any resources used by the given multipart request (if any).
* @param servletRequest current HTTP request * @param request current HTTP request
* @see MultipartResolver#cleanupMultipart * @see MultipartResolver#cleanupMultipart
*/ */
protected void cleanupMultipart(HttpServletRequest servletRequest) { protected void cleanupMultipart(HttpServletRequest request) {
MultipartHttpServletRequest req = WebUtils.getNativeRequest(servletRequest, MultipartHttpServletRequest.class); MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class);
if (req != null) { if (multipartRequest != null) {
this.multipartResolver.cleanupMultipart(req); this.multipartResolver.cleanupMultipart(multipartRequest);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -67,6 +67,7 @@ import org.springframework.web.servlet.theme.SessionThemeResolver;
import org.springframework.web.servlet.theme.ThemeChangeInterceptor; import org.springframework.web.servlet.theme.ThemeChangeInterceptor;
import org.springframework.web.servlet.view.InternalResourceViewResolver; import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.ResourceBundleViewResolver; import org.springframework.web.servlet.view.ResourceBundleViewResolver;
import org.springframework.web.util.WebUtils;
/** /**
* @author Juergen Hoeller * @author Juergen Hoeller
@ -401,7 +402,7 @@ public class ComplexWebApplicationContext extends StaticWebApplicationContext {
if (!(wac instanceof ComplexWebApplicationContext)) { if (!(wac instanceof ComplexWebApplicationContext)) {
throw new ServletException("Incorrect WebApplicationContext"); throw new ServletException("Incorrect WebApplicationContext");
} }
if (!(request instanceof MultipartHttpServletRequest)) { if (WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class) == null) {
throw new ServletException("Not in a MultipartHttpServletRequest"); throw new ServletException("Not in a MultipartHttpServletRequest");
} }
if (request.getParameter("fail") != null) { if (request.getParameter("fail") != null) {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -23,6 +23,7 @@ import javax.servlet.ServletConfig;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import junit.framework.TestCase; import junit.framework.TestCase;
@ -217,6 +218,22 @@ public class DispatcherServletTests extends TestCase {
MultipartHttpServletRequest multipartRequest = multipartResolver.resolveMultipart(request); MultipartHttpServletRequest multipartRequest = multipartResolver.resolveMultipart(request);
complexDispatcherServlet.service(multipartRequest, response); complexDispatcherServlet.service(multipartRequest, response);
multipartResolver.cleanupMultipart(multipartRequest); multipartResolver.cleanupMultipart(multipartRequest);
assertNull(request.getAttribute(SimpleMappingExceptionResolver.DEFAULT_EXCEPTION_ATTRIBUTE));
assertNotNull(request.getAttribute("cleanedUp"));
}
public void testExistingMultipartRequestButWrapped() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do;abc=def");
request.addPreferredLocale(Locale.CANADA);
request.addUserRole("role1");
MockHttpServletResponse response = new MockHttpServletResponse();
ComplexWebApplicationContext.MockMultipartResolver multipartResolver =
(ComplexWebApplicationContext.MockMultipartResolver) complexDispatcherServlet.getWebApplicationContext()
.getBean("multipartResolver");
MultipartHttpServletRequest multipartRequest = multipartResolver.resolveMultipart(request);
complexDispatcherServlet.service(new HttpServletRequestWrapper(multipartRequest), response);
multipartResolver.cleanupMultipart(multipartRequest);
assertNull(request.getAttribute(SimpleMappingExceptionResolver.DEFAULT_EXCEPTION_ATTRIBUTE));
assertNotNull(request.getAttribute("cleanedUp")); assertNotNull(request.getAttribute("cleanedUp"));
} }