WebRequest is a resolvable dependency in Servlet/Portlet web application contexts (SPR-6727)

This commit is contained in:
Juergen Hoeller 2010-01-20 22:09:13 +00:00
parent 1b6f3f1f22
commit 0a6f2348b8
4 changed files with 87 additions and 26 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 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.
@ -36,6 +36,7 @@ import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.RequestScope;
import org.springframework.web.context.request.SessionScope;
import org.springframework.web.context.request.WebRequest;
/**
* Convenience methods for retrieving the root WebApplicationContext for a given
@ -123,6 +124,7 @@ public abstract class PortletApplicationContextUtils {
beanFactory.registerResolvableDependency(PortletRequest.class, new RequestObjectFactory());
beanFactory.registerResolvableDependency(PortletSession.class, new SessionObjectFactory());
beanFactory.registerResolvableDependency(WebRequest.class, new WebRequestObjectFactory());
}
/**
@ -182,6 +184,18 @@ public abstract class PortletApplicationContextUtils {
}
}
/**
* Return the current RequestAttributes instance as PortletRequestAttributes.
* @see RequestContextHolder#currentRequestAttributes()
*/
private static PortletRequestAttributes currentRequestAttributes() {
RequestAttributes requestAttr = RequestContextHolder.currentRequestAttributes();
if (!(requestAttr instanceof PortletRequestAttributes)) {
throw new IllegalStateException("Current request is not a portlet request");
}
return (PortletRequestAttributes) requestAttr;
}
/**
* Factory that exposes the current request object on demand.
@ -189,11 +203,7 @@ public abstract class PortletApplicationContextUtils {
private static class RequestObjectFactory implements ObjectFactory<PortletRequest>, Serializable {
public PortletRequest getObject() {
RequestAttributes requestAttr = RequestContextHolder.currentRequestAttributes();
if (!(requestAttr instanceof PortletRequestAttributes)) {
throw new IllegalStateException("Current request is not a portlet request");
}
return ((PortletRequestAttributes) requestAttr).getRequest();
return currentRequestAttributes().getRequest();
}
@Override
@ -209,11 +219,7 @@ public abstract class PortletApplicationContextUtils {
private static class SessionObjectFactory implements ObjectFactory<PortletSession>, Serializable {
public PortletSession getObject() {
RequestAttributes requestAttr = RequestContextHolder.currentRequestAttributes();
if (!(requestAttr instanceof PortletRequestAttributes)) {
throw new IllegalStateException("Current request is not a portlet request");
}
return ((PortletRequestAttributes) requestAttr).getRequest().getPortletSession();
return currentRequestAttributes().getRequest().getPortletSession();
}
@Override
@ -222,4 +228,20 @@ public abstract class PortletApplicationContextUtils {
}
}
/**
* Factory that exposes the current WebRequest object on demand.
*/
private static class WebRequestObjectFactory implements ObjectFactory<WebRequest>, Serializable {
public WebRequest getObject() {
return new PortletWebRequest(currentRequestAttributes().getRequest());
}
@Override
public String toString() {
return "Current PortletWebRequest";
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 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.
@ -902,9 +902,12 @@ public class Portlet20AnnotationControllerTests {
@Autowired
private PortletRequest request;
@Autowired
private WebRequest webRequest;
@RenderMapping
public void myHandle(RenderResponse response) throws IOException {
if (this.portletContext == null || this.session == null || this.request == null) {
if (this.portletContext == null || this.session == null || this.request == null || this.webRequest == null) {
throw new IllegalStateException();
}
response.getWriter().write("myView");

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 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.
@ -612,6 +612,7 @@ public class ServletAnnotationControllerTests {
assertSame(servletConfig, request.getAttribute("servletConfig"));
assertSame(session.getId(), request.getAttribute("sessionId"));
assertSame(request.getRequestURI(), request.getAttribute("requestUri"));
assertSame(request.getLocale(), request.getAttribute("locale"));
request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
response = new MockHttpServletResponse();
@ -1652,10 +1653,13 @@ public class ServletAnnotationControllerTests {
@Autowired
private HttpServletRequest request;
@Autowired
private WebRequest webRequest;
@RequestMapping
public void myHandle(HttpServletResponse response, HttpServletRequest request) throws IOException {
if (this.servletContext == null || this.servletConfig == null || this.session == null ||
this.request == null) {
this.request == null || this.webRequest == null) {
throw new IllegalStateException();
}
response.getWriter().write("myView");
@ -1663,6 +1667,7 @@ public class ServletAnnotationControllerTests {
request.setAttribute("servletConfig", this.servletConfig);
request.setAttribute("sessionId", this.session.getId());
request.setAttribute("requestUri", this.request.getRequestURI());
request.setAttribute("locale", this.webRequest.getLocale());
}
@RequestMapping(params = {"view", "!lang"})

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 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.
@ -38,7 +38,9 @@ import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.RequestScope;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.context.request.SessionScope;
import org.springframework.web.context.request.WebRequest;
/**
* Convenience methods for retrieving the root
@ -153,6 +155,7 @@ public abstract class WebApplicationContextUtils {
beanFactory.registerResolvableDependency(ServletRequest.class, new RequestObjectFactory());
beanFactory.registerResolvableDependency(HttpSession.class, new SessionObjectFactory());
beanFactory.registerResolvableDependency(WebRequest.class, new WebRequestObjectFactory());
if (jsfPresent) {
FacesDependencyRegistrar.registerFacesDependencies(beanFactory);
}
@ -220,6 +223,18 @@ public abstract class WebApplicationContextUtils {
}
}
/**
* Return the current RequestAttributes instance as ServletRequestAttributes.
* @see RequestContextHolder#currentRequestAttributes()
*/
private static ServletRequestAttributes currentRequestAttributes() {
RequestAttributes requestAttr = RequestContextHolder.currentRequestAttributes();
if (!(requestAttr instanceof ServletRequestAttributes)) {
throw new IllegalStateException("Current request is not a servlet request");
}
return (ServletRequestAttributes) requestAttr;
}
/**
* Factory that exposes the current request object on demand.
@ -227,11 +242,7 @@ public abstract class WebApplicationContextUtils {
private static class RequestObjectFactory implements ObjectFactory<ServletRequest>, Serializable {
public ServletRequest getObject() {
RequestAttributes requestAttr = RequestContextHolder.currentRequestAttributes();
if (!(requestAttr instanceof ServletRequestAttributes)) {
throw new IllegalStateException("Current request is not a servlet request");
}
return ((ServletRequestAttributes) requestAttr).getRequest();
return currentRequestAttributes().getRequest();
}
@Override
@ -247,11 +258,7 @@ public abstract class WebApplicationContextUtils {
private static class SessionObjectFactory implements ObjectFactory<HttpSession>, Serializable {
public HttpSession getObject() {
RequestAttributes requestAttr = RequestContextHolder.currentRequestAttributes();
if (!(requestAttr instanceof ServletRequestAttributes)) {
throw new IllegalStateException("Current request is not a servlet request");
}
return ((ServletRequestAttributes) requestAttr).getRequest().getSession();
return currentRequestAttributes().getRequest().getSession();
}
@Override
@ -261,6 +268,22 @@ public abstract class WebApplicationContextUtils {
}
/**
* Factory that exposes the current WebRequest object on demand.
*/
private static class WebRequestObjectFactory implements ObjectFactory<WebRequest>, Serializable {
public WebRequest getObject() {
return new ServletWebRequest(currentRequestAttributes().getRequest());
}
@Override
public String toString() {
return "Current ServletWebRequest";
}
}
/**
* Inner class to avoid hard-coded JSF dependency.
*/
@ -271,11 +294,19 @@ public abstract class WebApplicationContextUtils {
public FacesContext getObject() {
return FacesContext.getCurrentInstance();
}
@Override
public String toString() {
return "Current JSF FacesContext";
}
});
beanFactory.registerResolvableDependency(ExternalContext.class, new ObjectFactory<ExternalContext>() {
public ExternalContext getObject() {
return FacesContext.getCurrentInstance().getExternalContext();
}
@Override
public String toString() {
return "Current JSF ExternalContext";
}
});
}
}