Removed Servlet 2.4 forward attribute support in InternalResourceView and TilesView

This commit is contained in:
Juergen Hoeller 2013-03-19 14:26:56 +01:00
parent 0f0c93a559
commit a03d125b4e
5 changed files with 19 additions and 218 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 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.
@ -428,27 +428,6 @@ public abstract class WebUtils {
return (request.getAttribute(INCLUDE_REQUEST_URI_ATTRIBUTE) != null);
}
/**
* Expose the current request URI and paths as {@link javax.servlet.http.HttpServletRequest}
* attributes under the keys defined in the Servlet 2.4 specification,
* for containers that implement 2.3 or an earlier version of the Servlet API:
* {@code javax.servlet.forward.request_uri},
* {@code javax.servlet.forward.context_path},
* {@code javax.servlet.forward.servlet_path},
* {@code javax.servlet.forward.path_info},
* {@code javax.servlet.forward.query_string}.
* <p>Does not override values if already present, to not cause conflicts
* with the attributes exposed by Servlet 2.4+ containers themselves.
* @param request current servlet request
*/
public static void exposeForwardRequestAttributes(HttpServletRequest request) {
exposeRequestAttributeIfNotPresent(request, FORWARD_REQUEST_URI_ATTRIBUTE, request.getRequestURI());
exposeRequestAttributeIfNotPresent(request, FORWARD_CONTEXT_PATH_ATTRIBUTE, request.getContextPath());
exposeRequestAttributeIfNotPresent(request, FORWARD_SERVLET_PATH_ATTRIBUTE, request.getServletPath());
exposeRequestAttributeIfNotPresent(request, FORWARD_PATH_INFO_ATTRIBUTE, request.getPathInfo());
exposeRequestAttributeIfNotPresent(request, FORWARD_QUERY_STRING_ATTRIBUTE, request.getQueryString());
}
/**
* Expose the Servlet spec's error attributes as {@link javax.servlet.http.HttpServletRequest}
* attributes under the keys defined in the Servlet 2.3 specification, for error pages that

View File

@ -13,12 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.servlet.view.tiles3;
import java.util.Locale;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@ -30,6 +29,7 @@ import org.apache.tiles.request.Request;
import org.apache.tiles.request.render.Renderer;
import org.apache.tiles.request.servlet.ServletRequest;
import org.apache.tiles.request.servlet.ServletUtil;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
@ -37,7 +37,6 @@ import org.springframework.web.servlet.support.JstlUtils;
import org.springframework.web.servlet.support.RequestContext;
import org.springframework.web.servlet.support.RequestContextUtils;
import org.springframework.web.servlet.view.AbstractUrlBasedView;
import org.springframework.web.util.WebUtils;
/**
* {@link org.springframework.web.servlet.View} implementation that renders
@ -53,8 +52,6 @@ public class TilesView extends AbstractUrlBasedView {
private Renderer renderer;
private boolean exposeForwardAttributes = false;
private boolean exposeJstlAttributes = true;
private ApplicationContext applicationContext;
@ -76,32 +73,24 @@ public class TilesView extends AbstractUrlBasedView {
this.exposeJstlAttributes = exposeJstlAttributes;
}
@Override
protected void initServletContext(ServletContext servletContext) {
super.initServletContext(servletContext);
if (servletContext.getMajorVersion() == 2 && servletContext.getMinorVersion() < 5) {
this.exposeForwardAttributes = true;
}
}
@Override
public void afterPropertiesSet() throws Exception {
super.afterPropertiesSet();
this.applicationContext = ServletUtil.getApplicationContext(getServletContext());
if (this.renderer == null) {
TilesContainer container = TilesAccess.getContainer(this.applicationContext);
this.renderer = new DefinitionRenderer(container);
}
}
@Override
public boolean checkResource(final Locale locale) throws Exception {
HttpServletRequest servletRequest = null;
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
if(requestAttributes != null && requestAttributes instanceof ServletRequestAttributes) {
servletRequest = ((ServletRequestAttributes)requestAttributes).getRequest();
if (requestAttributes instanceof ServletRequestAttributes) {
servletRequest = ((ServletRequestAttributes) requestAttributes).getRequest();
}
Request request = new ServletRequest(this.applicationContext, servletRequest, null) {
@Override
@ -117,28 +106,8 @@ public class TilesView extends AbstractUrlBasedView {
HttpServletResponse response) throws Exception {
exposeModelAsRequestAttributes(model, request);
if (this.exposeJstlAttributes) {
ServletContext servletContext = getServletContext();
JstlUtils.exposeLocalizationContext(new RequestContext(request, servletContext));
}
if (!response.isCommitted()) {
// Tiles is going to use a forward, but some web containers (e.g.
// OC4J 10.1.3)
// do not properly expose the Servlet 2.4 forward request
// attributes... However,
// must not do this on Servlet 2.5 or above, mainly for GlassFish
// compatibility.
if (this.exposeForwardAttributes) {
try {
WebUtils.exposeForwardRequestAttributes(request);
} catch (Exception ex) {
// Servlet container rejected to set internal attributes,
// e.g. on TriFork.
this.exposeForwardAttributes = false;
}
}
JstlUtils.exposeLocalizationContext(new RequestContext(request, getServletContext()));
}
Request tilesRequest = createTilesRequest(request, response);
@ -146,9 +115,8 @@ public class TilesView extends AbstractUrlBasedView {
}
/**
* Create a Tiles {@link Request}. This implementation creates a
* {@link ServletRequest}.
*
* Create a Tiles {@link Request}.
* <p>This implementation creates a {@link ServletRequest}.
* @param request the current request
* @param response the current response
* @return the Tiles request

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 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.
@ -21,7 +21,6 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@ -69,8 +68,6 @@ public class InternalResourceView extends AbstractUrlBasedView {
private boolean alwaysInclude = false;
private volatile Boolean exposeForwardAttributes;
private boolean exposeContextBeansAsAttributes = false;
private Set<String> exposedContextBeanNames;
@ -118,18 +115,6 @@ public class InternalResourceView extends AbstractUrlBasedView {
this.alwaysInclude = alwaysInclude;
}
/**
* Set whether to explictly expose the Servlet 2.4 forward request attributes
* when forwarding to the underlying view resource.
* <p>Default is "true" on Servlet containers up until 2.4, and "false" for
* Servlet 2.5 and above. Note that Servlet containers at 2.4 level and above
* should expose those attributes automatically! This InternalResourceView
* feature exists for Servlet 2.3 containers and misbehaving 2.4 containers.
*/
public void setExposeForwardAttributes(boolean exposeForwardAttributes) {
this.exposeForwardAttributes = exposeForwardAttributes;
}
/**
* Set whether to make all Spring beans in the application context accessible
* as request attributes, through lazy checking once an attribute gets accessed.
@ -179,19 +164,6 @@ public class InternalResourceView extends AbstractUrlBasedView {
return false;
}
/**
* Checks whether we need to explicitly expose the Servlet 2.4 request attributes
* by default.
* @see #setExposeForwardAttributes
* @see #exposeForwardRequestAttributes(javax.servlet.http.HttpServletRequest)
*/
@Override
protected void initServletContext(ServletContext sc) {
if (this.exposeForwardAttributes == null && sc.getMajorVersion() == 2 && sc.getMinorVersion() < 5) {
this.exposeForwardAttributes = Boolean.TRUE;
}
}
/**
* Render the internal resource given the specified model.
@ -231,7 +203,6 @@ public class InternalResourceView extends AbstractUrlBasedView {
else {
// Note: The forwarded resource is supposed to determine the content type itself.
exposeForwardRequestAttributes(requestToExpose);
if (logger.isDebugEnabled()) {
logger.debug("Forwarding to resource [" + getUrl() + "] in InternalResourceView '" + getBeanName() + "'");
}
@ -328,28 +299,4 @@ public class InternalResourceView extends AbstractUrlBasedView {
return (this.alwaysInclude || WebUtils.isIncludeRequest(request) || response.isCommitted());
}
/**
* Expose the current request URI and paths as {@link HttpServletRequest}
* attributes under the keys defined in the Servlet 2.4 specification,
* for Servlet 2.3 containers as well as misbehaving Servlet 2.4 containers
* (such as OC4J).
* <p>Does not expose the attributes on Servlet 2.5 or above, mainly for
* GlassFish compatibility (GlassFish gets confused by pre-exposed attributes).
* In any case, Servlet 2.5 containers should finally properly support
* Servlet 2.4 features, shouldn't they...
* @param request current HTTP request
* @see org.springframework.web.util.WebUtils#exposeForwardRequestAttributes
*/
protected void exposeForwardRequestAttributes(HttpServletRequest request) {
if (this.exposeForwardAttributes != null && this.exposeForwardAttributes) {
try {
WebUtils.exposeForwardRequestAttributes(request);
}
catch (Exception ex) {
// Servlet container rejected to set internal attributes, e.g. on TriFork.
this.exposeForwardAttributes = Boolean.FALSE;
}
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2013 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.
@ -55,25 +55,6 @@ import org.springframework.web.util.WebUtils;
*/
public class TilesView extends AbstractUrlBasedView {
private volatile boolean exposeForwardAttributes = false;
/**
* Checks whether we need to explicitly expose the Servlet 2.4 request attributes
* by default.
* <p>This will be done by default on Servlet containers up until 2.4, and skipped
* for Servlet 2.5 and above. Note that Servlet containers at 2.4 level and above
* should expose those attributes automatically! This feature exists for
* Servlet 2.3 containers and misbehaving 2.4 containers only.
*/
@Override
protected void initServletContext(ServletContext sc) {
if (sc.getMajorVersion() == 2 && sc.getMinorVersion() < 5) {
this.exposeForwardAttributes = true;
}
}
@Override
public boolean checkResource(final Locale locale) throws Exception {
TilesContainer container = ServletUtil.getContainer(getServletContext());
@ -105,22 +86,6 @@ public class TilesView extends AbstractUrlBasedView {
exposeModelAsRequestAttributes(model, request);
JstlUtils.exposeLocalizationContext(new RequestContext(request, servletContext));
if (!response.isCommitted()) {
// Tiles is going to use a forward, but some web containers (e.g. OC4J 10.1.3)
// do not properly expose the Servlet 2.4 forward request attributes... However,
// must not do this on Servlet 2.5 or above, mainly for GlassFish compatibility.
if (this.exposeForwardAttributes) {
try {
WebUtils.exposeForwardRequestAttributes(request);
}
catch (Exception ex) {
// Servlet container rejected to set internal attributes, e.g. on TriFork.
this.exposeForwardAttributes = false;
}
}
}
container.render(getUrl(), request, response);
}

View File

@ -17,9 +17,7 @@
package org.springframework.web.servlet.view;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import junit.framework.TestCase;
@ -55,7 +53,7 @@ public class InternalResourceViewTests extends TestCase {
public void testForward() throws Exception {
HashMap<String, Object> model = new HashMap<String, Object>();
Object obj = new Integer(1);
Object obj = 1;
model.put("foo", "bar");
model.put("I", obj);
@ -81,67 +79,14 @@ public class InternalResourceViewTests extends TestCase {
assertEquals(url, response.getForwardedUrl());
Set<String> keys = model.keySet();
for (Iterator<String> it = keys.iterator(); it.hasNext();) {
String key = it.next();
for (String key : keys) {
assertEquals(model.get(key), request.getAttribute(key));
}
assertEquals("/myservlet/handler.do", request.getAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE));
assertEquals("/mycontext", request.getAttribute(WebUtils.FORWARD_CONTEXT_PATH_ATTRIBUTE));
assertEquals("/myservlet", request.getAttribute(WebUtils.FORWARD_SERVLET_PATH_ATTRIBUTE));
assertEquals(";mypathinfo", request.getAttribute(WebUtils.FORWARD_PATH_INFO_ATTRIBUTE));
assertEquals("?param1=value1", request.getAttribute(WebUtils.FORWARD_QUERY_STRING_ATTRIBUTE));
}
public void testForwardWithForwardAttributesPresent() throws Exception {
HashMap<String, Object> model = new HashMap<String, Object>();
Object obj = new Integer(1);
model.put("foo", "bar");
model.put("I", obj);
String url = "forward-to";
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myservlet/handler.do");
request.setContextPath("/mycontext");
request.setServletPath("/myservlet");
request.setPathInfo(";mypathinfo");
request.setQueryString("?param1=value1");
request.setAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE, "/MYservlet/handler.do");
request.setAttribute(WebUtils.FORWARD_CONTEXT_PATH_ATTRIBUTE, "/MYcontext");
request.setAttribute(WebUtils.FORWARD_SERVLET_PATH_ATTRIBUTE, "/MYservlet");
request.setAttribute(WebUtils.FORWARD_PATH_INFO_ATTRIBUTE, ";MYpathinfo");
request.setAttribute(WebUtils.FORWARD_QUERY_STRING_ATTRIBUTE, "?Param1=value1");
InternalResourceView view = new InternalResourceView();
view.setUrl(url);
view.setServletContext(new MockServletContext() {
@Override
public int getMinorVersion() {
return 4;
}
});
MockHttpServletResponse response = new MockHttpServletResponse();
view.render(model, request, response);
assertEquals(url, response.getForwardedUrl());
Set<String> keys = model.keySet();
for (Iterator<String> it = keys.iterator(); it.hasNext();) {
String key = it.next();
assertEquals(model.get(key), request.getAttribute(key));
}
assertEquals("/MYservlet/handler.do", request.getAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE));
assertEquals("/MYcontext", request.getAttribute(WebUtils.FORWARD_CONTEXT_PATH_ATTRIBUTE));
assertEquals("/MYservlet", request.getAttribute(WebUtils.FORWARD_SERVLET_PATH_ATTRIBUTE));
assertEquals(";MYpathinfo", request.getAttribute(WebUtils.FORWARD_PATH_INFO_ATTRIBUTE));
assertEquals("?Param1=value1", request.getAttribute(WebUtils.FORWARD_QUERY_STRING_ATTRIBUTE));
}
public void testAlwaysInclude() throws Exception {
HashMap<String, Object> model = new HashMap<String, Object>();
Object obj = new Integer(1);
Object obj = 1;
model.put("foo", "bar");
model.put("I", obj);
@ -161,15 +106,14 @@ public class InternalResourceViewTests extends TestCase {
assertEquals(url, response.getIncludedUrl());
Set<String> keys = model.keySet();
for (Iterator<String> iter = keys.iterator(); iter.hasNext();) {
String key = iter.next();
for (String key : keys) {
verify(request).setAttribute(key, model.get(key));
}
}
public void testIncludeOnAttribute() throws Exception {
HashMap<String, Object> model = new HashMap<String, Object>();
Object obj = new Integer(1);
Object obj = 1;
model.put("foo", "bar");
model.put("I", obj);
@ -190,15 +134,14 @@ public class InternalResourceViewTests extends TestCase {
assertEquals(url, response.getIncludedUrl());
Set<String> keys = model.keySet();
for (Iterator<String> iter = keys.iterator(); iter.hasNext();) {
String key = iter.next();
for (String key : keys) {
verify(request).setAttribute(key, model.get(key));
}
}
public void testIncludeOnCommitted() throws Exception {
HashMap<String, Object> model = new HashMap<String, Object>();
Object obj = new Integer(1);
Object obj = 1;
model.put("foo", "bar");
model.put("I", obj);
@ -220,8 +163,7 @@ public class InternalResourceViewTests extends TestCase {
assertEquals(url, response.getIncludedUrl());
Set<String> keys = model.keySet();
for (Iterator<String> iter = keys.iterator(); iter.hasNext();) {
String key = iter.next();
for (String key : keys) {
verify(request).setAttribute(key, model.get(key));
}
}