Browse Source

DispatcherServlet's checkMultipart detects wrapped MultipartRequest as well

Issue: SPR-12114
pull/635/head
Juergen Hoeller 10 years ago
parent
commit
786fd927fa
  1. 12
      spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java
  2. 5
      spring-webmvc/src/test/java/org/springframework/web/servlet/ComplexWebApplicationContext.java
  3. 19
      spring-webmvc/src/test/java/org/springframework/web/servlet/DispatcherServletTests.java

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

@ -1061,7 +1061,7 @@ public class DispatcherServlet extends FrameworkServlet { @@ -1061,7 +1061,7 @@ public class DispatcherServlet extends FrameworkServlet {
*/
protected HttpServletRequest checkMultipart(HttpServletRequest request) throws MultipartException {
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, " +
"this typically results from an additional MultipartFilter in web.xml");
}
@ -1079,13 +1079,13 @@ public class DispatcherServlet extends FrameworkServlet { @@ -1079,13 +1079,13 @@ public class DispatcherServlet extends FrameworkServlet {
/**
* 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
*/
protected void cleanupMultipart(HttpServletRequest servletRequest) {
MultipartHttpServletRequest req = WebUtils.getNativeRequest(servletRequest, MultipartHttpServletRequest.class);
if (req != null) {
this.multipartResolver.cleanupMultipart(req);
protected void cleanupMultipart(HttpServletRequest request) {
MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class);
if (multipartRequest != null) {
this.multipartResolver.cleanupMultipart(multipartRequest);
}
}

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

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -67,6 +67,7 @@ import org.springframework.web.servlet.theme.SessionThemeResolver; @@ -67,6 +67,7 @@ import org.springframework.web.servlet.theme.SessionThemeResolver;
import org.springframework.web.servlet.theme.ThemeChangeInterceptor;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.ResourceBundleViewResolver;
import org.springframework.web.util.WebUtils;
/**
* @author Juergen Hoeller
@ -401,7 +402,7 @@ public class ComplexWebApplicationContext extends StaticWebApplicationContext { @@ -401,7 +402,7 @@ public class ComplexWebApplicationContext extends StaticWebApplicationContext {
if (!(wac instanceof ComplexWebApplicationContext)) {
throw new ServletException("Incorrect WebApplicationContext");
}
if (!(request instanceof MultipartHttpServletRequest)) {
if (WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class) == null) {
throw new ServletException("Not in a MultipartHttpServletRequest");
}
if (request.getParameter("fail") != null) {

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

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -23,6 +23,7 @@ import javax.servlet.ServletConfig; @@ -23,6 +23,7 @@ import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import junit.framework.TestCase;
@ -217,6 +218,22 @@ public class DispatcherServletTests extends TestCase { @@ -217,6 +218,22 @@ public class DispatcherServletTests extends TestCase {
MultipartHttpServletRequest multipartRequest = multipartResolver.resolveMultipart(request);
complexDispatcherServlet.service(multipartRequest, response);
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"));
}

Loading…
Cancel
Save