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
This commit is contained in:
Rossen Stoyanchev 2012-12-07 15:19:38 -05:00
parent 69ace01640
commit 3643d92cb8
3 changed files with 102 additions and 13 deletions

View File

@ -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"
}
}

View File

@ -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));
}
}
}

View File

@ -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);
}