From 3643d92cb8f8d7ba3bee8d19db300c557e2e85a8 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Fri, 7 Dec 2012 15:19:38 -0500 Subject: [PATCH] Fix issue with creating ServletRequestAttributes Prevously the FrameworkServlet created a new ServletRequestAttributes instance for every request, unless the RequestContextHolder already contained an instance whose type is not ServletRequestAttributes. The main intent was that if RequestContextHolder contains a PortletRequestAttributes, it should be left in place. This change does an "instanceof" check against the request in RequestContextHolder instead of an "equals" check on the type. It still leaves PortletRequestAttributes in place but also allows the previous request to be any sub-class of ServletRequestAttributes. Issue: SPR-10025 --- build.gradle | 19 ++-- .../test/web/servlet/Spr10025Tests.java | 94 +++++++++++++++++++ .../web/servlet/FrameworkServlet.java | 2 +- 3 files changed, 102 insertions(+), 13 deletions(-) create mode 100644 spring-test-mvc/src/test/java/org/springframework/test/web/servlet/Spr10025Tests.java diff --git a/build.gradle b/build.gradle index 326fb3070d..061184a0d9 100644 --- a/build.gradle +++ b/build.gradle @@ -518,7 +518,10 @@ project('spring-webmvc-tiles3') { compile("javax.servlet.jsp:jsp-api:2.1", provided) compile("org.apache.tiles:tiles-request-api:1.0.1", optional) compile("org.apache.tiles:tiles-api:3.0.1", optional) - compile("org.apache.tiles:tiles-core:3.0.1", optional) + compile("org.apache.tiles:tiles-core:3.0.1") { dep -> + optional dep + exclude group: 'org.slf4j', module: 'jcl-over-slf4j' + } compile("org.apache.tiles:tiles-servlet:3.0.1", optional) compile("org.apache.tiles:tiles-jsp:3.0.1", optional) compile("org.apache.tiles:tiles-el:3.0.1", optional) @@ -586,16 +589,6 @@ project('spring-test-mvc') { compile("org.hamcrest:hamcrest-core:1.3", optional) compile("com.jayway.jsonpath:json-path:0.8.1", optional) compile("xmlunit:xmlunit:1.2", optional) - testCompile("org.slf4j:jcl-over-slf4j:${slf4jVersion}") - testCompile("org.slf4j:slf4j-log4j12:${slf4jVersion}") { - exclude group: 'log4j', module: 'log4j' - } - testCompile("log4j:log4j:1.2.15") { - exclude group: 'javax.mail', module: 'mail' - exclude group: 'javax.jms', module: 'jms' - exclude group: 'com.sun.jdmk', module: 'jmxtools' - exclude group: 'com.sun.jmx', module: 'jmxri' - } testCompile "javax.servlet:jstl:1.2" testCompile "org.hibernate:hibernate-validator:4.3.0.Final" testCompile "org.codehaus.jackson:jackson-mapper-asl:1.4.2" @@ -607,7 +600,9 @@ project('spring-test-mvc') { testCompile "org.easymock:easymockclassextension:${easymockVersion}" testCompile "org.apache.tiles:tiles-request-api:1.0.1" testCompile "org.apache.tiles:tiles-api:3.0.1" - testCompile "org.apache.tiles:tiles-core:3.0.1" + testCompile("org.apache.tiles:tiles-core:3.0.1") { + exclude group: 'org.slf4j', module: 'jcl-over-slf4j' + } testCompile "org.apache.tiles:tiles-servlet:3.0.1" } } diff --git a/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/Spr10025Tests.java b/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/Spr10025Tests.java new file mode 100644 index 0000000000..9091ecde49 --- /dev/null +++ b/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/Spr10025Tests.java @@ -0,0 +1,94 @@ +/* + * Copyright 2002-2012 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.test.web.servlet; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.stereotype.Controller; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.context.request.RequestAttributes; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; + +/** + * Test for SPR-10025. + * + * @author Rossen Stoyanchev + */ +@RunWith(SpringJUnit4ClassRunner.class) +@WebAppConfiguration +@ContextConfiguration +public class Spr10025Tests { + + @Autowired + private WebApplicationContext wac; + + @Autowired + private MockHttpServletRequest servletRequest; + + private MockMvc mockMvc; + + @Before + public void setup() { + this.mockMvc = webAppContextSetup(this.wac).build(); + } + + @Test + public void test() throws Exception { + this.servletRequest.setAttribute("foo1", "bar"); + this.mockMvc.perform(get("/myUrl").requestAttr("foo2", "bar")).andExpect(status().isOk()); + } + + + @Configuration + @EnableWebMvc + static class WebConfig extends WebMvcConfigurerAdapter { + + @Bean + public MyController myController() { + return new MyController(); + } + } + + @Controller + private static class MyController { + + @RequestMapping("/myUrl") + @ResponseBody + public void handle() { + RequestAttributes attributes = RequestContextHolder.getRequestAttributes(); + Assert.assertNull(attributes.getAttribute("foo1", RequestAttributes.SCOPE_REQUEST)); + Assert.assertNotNull(attributes.getAttribute("foo2", RequestAttributes.SCOPE_REQUEST)); + } + } + +} diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/FrameworkServlet.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/FrameworkServlet.java index 5005efb8eb..79c3885ef8 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/FrameworkServlet.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/FrameworkServlet.java @@ -904,7 +904,7 @@ public abstract class FrameworkServlet extends HttpServletBean { RequestAttributes previousAttributes = RequestContextHolder.getRequestAttributes(); ServletRequestAttributes requestAttributes = null; - if (previousAttributes == null || previousAttributes.getClass().equals(ServletRequestAttributes.class)) { + if (previousAttributes == null || (previousAttributes instanceof ServletRequestAttributes)) { requestAttributes = new ServletRequestAttributes(request); }