WebRequest is a resolvable dependency in Servlet/Portlet web application contexts (SPR-6727)
This commit is contained in:
parent
1b6f3f1f22
commit
0a6f2348b8
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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.RequestContextHolder;
|
||||||
import org.springframework.web.context.request.RequestScope;
|
import org.springframework.web.context.request.RequestScope;
|
||||||
import org.springframework.web.context.request.SessionScope;
|
import org.springframework.web.context.request.SessionScope;
|
||||||
|
import org.springframework.web.context.request.WebRequest;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convenience methods for retrieving the root WebApplicationContext for a given
|
* 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(PortletRequest.class, new RequestObjectFactory());
|
||||||
beanFactory.registerResolvableDependency(PortletSession.class, new SessionObjectFactory());
|
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.
|
* 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 {
|
private static class RequestObjectFactory implements ObjectFactory<PortletRequest>, Serializable {
|
||||||
|
|
||||||
public PortletRequest getObject() {
|
public PortletRequest getObject() {
|
||||||
RequestAttributes requestAttr = RequestContextHolder.currentRequestAttributes();
|
return currentRequestAttributes().getRequest();
|
||||||
if (!(requestAttr instanceof PortletRequestAttributes)) {
|
|
||||||
throw new IllegalStateException("Current request is not a portlet request");
|
|
||||||
}
|
|
||||||
return ((PortletRequestAttributes) requestAttr).getRequest();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -209,11 +219,7 @@ public abstract class PortletApplicationContextUtils {
|
||||||
private static class SessionObjectFactory implements ObjectFactory<PortletSession>, Serializable {
|
private static class SessionObjectFactory implements ObjectFactory<PortletSession>, Serializable {
|
||||||
|
|
||||||
public PortletSession getObject() {
|
public PortletSession getObject() {
|
||||||
RequestAttributes requestAttr = RequestContextHolder.currentRequestAttributes();
|
return currentRequestAttributes().getRequest().getPortletSession();
|
||||||
if (!(requestAttr instanceof PortletRequestAttributes)) {
|
|
||||||
throw new IllegalStateException("Current request is not a portlet request");
|
|
||||||
}
|
|
||||||
return ((PortletRequestAttributes) requestAttr).getRequest().getPortletSession();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -902,9 +902,12 @@ public class Portlet20AnnotationControllerTests {
|
||||||
@Autowired
|
@Autowired
|
||||||
private PortletRequest request;
|
private PortletRequest request;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private WebRequest webRequest;
|
||||||
|
|
||||||
@RenderMapping
|
@RenderMapping
|
||||||
public void myHandle(RenderResponse response) throws IOException {
|
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();
|
throw new IllegalStateException();
|
||||||
}
|
}
|
||||||
response.getWriter().write("myView");
|
response.getWriter().write("myView");
|
||||||
|
|
|
||||||
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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(servletConfig, request.getAttribute("servletConfig"));
|
||||||
assertSame(session.getId(), request.getAttribute("sessionId"));
|
assertSame(session.getId(), request.getAttribute("sessionId"));
|
||||||
assertSame(request.getRequestURI(), request.getAttribute("requestUri"));
|
assertSame(request.getRequestURI(), request.getAttribute("requestUri"));
|
||||||
|
assertSame(request.getLocale(), request.getAttribute("locale"));
|
||||||
|
|
||||||
request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
|
request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
|
||||||
response = new MockHttpServletResponse();
|
response = new MockHttpServletResponse();
|
||||||
|
|
@ -1652,10 +1653,13 @@ public class ServletAnnotationControllerTests {
|
||||||
@Autowired
|
@Autowired
|
||||||
private HttpServletRequest request;
|
private HttpServletRequest request;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private WebRequest webRequest;
|
||||||
|
|
||||||
@RequestMapping
|
@RequestMapping
|
||||||
public void myHandle(HttpServletResponse response, HttpServletRequest request) throws IOException {
|
public void myHandle(HttpServletResponse response, HttpServletRequest request) throws IOException {
|
||||||
if (this.servletContext == null || this.servletConfig == null || this.session == null ||
|
if (this.servletContext == null || this.servletConfig == null || this.session == null ||
|
||||||
this.request == null) {
|
this.request == null || this.webRequest == null) {
|
||||||
throw new IllegalStateException();
|
throw new IllegalStateException();
|
||||||
}
|
}
|
||||||
response.getWriter().write("myView");
|
response.getWriter().write("myView");
|
||||||
|
|
@ -1663,6 +1667,7 @@ public class ServletAnnotationControllerTests {
|
||||||
request.setAttribute("servletConfig", this.servletConfig);
|
request.setAttribute("servletConfig", this.servletConfig);
|
||||||
request.setAttribute("sessionId", this.session.getId());
|
request.setAttribute("sessionId", this.session.getId());
|
||||||
request.setAttribute("requestUri", this.request.getRequestURI());
|
request.setAttribute("requestUri", this.request.getRequestURI());
|
||||||
|
request.setAttribute("locale", this.webRequest.getLocale());
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(params = {"view", "!lang"})
|
@RequestMapping(params = {"view", "!lang"})
|
||||||
|
|
|
||||||
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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.RequestContextHolder;
|
||||||
import org.springframework.web.context.request.RequestScope;
|
import org.springframework.web.context.request.RequestScope;
|
||||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
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.SessionScope;
|
||||||
|
import org.springframework.web.context.request.WebRequest;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convenience methods for retrieving the root
|
* Convenience methods for retrieving the root
|
||||||
|
|
@ -153,6 +155,7 @@ public abstract class WebApplicationContextUtils {
|
||||||
|
|
||||||
beanFactory.registerResolvableDependency(ServletRequest.class, new RequestObjectFactory());
|
beanFactory.registerResolvableDependency(ServletRequest.class, new RequestObjectFactory());
|
||||||
beanFactory.registerResolvableDependency(HttpSession.class, new SessionObjectFactory());
|
beanFactory.registerResolvableDependency(HttpSession.class, new SessionObjectFactory());
|
||||||
|
beanFactory.registerResolvableDependency(WebRequest.class, new WebRequestObjectFactory());
|
||||||
if (jsfPresent) {
|
if (jsfPresent) {
|
||||||
FacesDependencyRegistrar.registerFacesDependencies(beanFactory);
|
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.
|
* 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 {
|
private static class RequestObjectFactory implements ObjectFactory<ServletRequest>, Serializable {
|
||||||
|
|
||||||
public ServletRequest getObject() {
|
public ServletRequest getObject() {
|
||||||
RequestAttributes requestAttr = RequestContextHolder.currentRequestAttributes();
|
return currentRequestAttributes().getRequest();
|
||||||
if (!(requestAttr instanceof ServletRequestAttributes)) {
|
|
||||||
throw new IllegalStateException("Current request is not a servlet request");
|
|
||||||
}
|
|
||||||
return ((ServletRequestAttributes) requestAttr).getRequest();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -247,11 +258,7 @@ public abstract class WebApplicationContextUtils {
|
||||||
private static class SessionObjectFactory implements ObjectFactory<HttpSession>, Serializable {
|
private static class SessionObjectFactory implements ObjectFactory<HttpSession>, Serializable {
|
||||||
|
|
||||||
public HttpSession getObject() {
|
public HttpSession getObject() {
|
||||||
RequestAttributes requestAttr = RequestContextHolder.currentRequestAttributes();
|
return currentRequestAttributes().getRequest().getSession();
|
||||||
if (!(requestAttr instanceof ServletRequestAttributes)) {
|
|
||||||
throw new IllegalStateException("Current request is not a servlet request");
|
|
||||||
}
|
|
||||||
return ((ServletRequestAttributes) requestAttr).getRequest().getSession();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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.
|
* Inner class to avoid hard-coded JSF dependency.
|
||||||
*/
|
*/
|
||||||
|
|
@ -271,11 +294,19 @@ public abstract class WebApplicationContextUtils {
|
||||||
public FacesContext getObject() {
|
public FacesContext getObject() {
|
||||||
return FacesContext.getCurrentInstance();
|
return FacesContext.getCurrentInstance();
|
||||||
}
|
}
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Current JSF FacesContext";
|
||||||
|
}
|
||||||
});
|
});
|
||||||
beanFactory.registerResolvableDependency(ExternalContext.class, new ObjectFactory<ExternalContext>() {
|
beanFactory.registerResolvableDependency(ExternalContext.class, new ObjectFactory<ExternalContext>() {
|
||||||
public ExternalContext getObject() {
|
public ExternalContext getObject() {
|
||||||
return FacesContext.getCurrentInstance().getExternalContext();
|
return FacesContext.getCurrentInstance().getExternalContext();
|
||||||
}
|
}
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Current JSF ExternalContext";
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue