From 786fd927fa70a3c41bcb79a0d6adc38ff3a8cadc Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 22 Aug 2014 16:46:36 +0200 Subject: [PATCH] DispatcherServlet's checkMultipart detects wrapped MultipartRequest as well Issue: SPR-12114 --- .../web/servlet/DispatcherServlet.java | 12 ++++++------ .../servlet/ComplexWebApplicationContext.java | 5 +++-- .../web/servlet/DispatcherServletTests.java | 19 ++++++++++++++++++- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java index b053335858..532e705f6e 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java @@ -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 { /** * 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); } } diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/ComplexWebApplicationContext.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/ComplexWebApplicationContext.java index 6704c13a41..9069c6b7b2 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/ComplexWebApplicationContext.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/ComplexWebApplicationContext.java @@ -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; 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 { 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) { diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/DispatcherServletTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/DispatcherServletTests.java index f5b6515b41..6f3a4fff93 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/DispatcherServletTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/DispatcherServletTests.java @@ -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; 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 { 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")); }