From 0a6f2348b8b0549c6a9ba64499d70be7964450ff Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 20 Jan 2010 22:09:13 +0000 Subject: [PATCH] WebRequest is a resolvable dependency in Servlet/Portlet web application contexts (SPR-6727) --- .../PortletApplicationContextUtils.java | 44 +++++++++++---- .../Portlet20AnnotationControllerTests.java | 7 ++- .../ServletAnnotationControllerTests.java | 9 +++- .../support/WebApplicationContextUtils.java | 53 +++++++++++++++---- 4 files changed, 87 insertions(+), 26 deletions(-) diff --git a/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/context/PortletApplicationContextUtils.java b/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/context/PortletApplicationContextUtils.java index 93469839166..3d36f2d3a75 100644 --- a/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/context/PortletApplicationContextUtils.java +++ b/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/context/PortletApplicationContextUtils.java @@ -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, 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, 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, Serializable { + + public WebRequest getObject() { + return new PortletWebRequest(currentRequestAttributes().getRequest()); + } + + @Override + public String toString() { + return "Current PortletWebRequest"; + } + } + } diff --git a/org.springframework.web.portlet/src/test/java/org/springframework/web/portlet/mvc/annotation/Portlet20AnnotationControllerTests.java b/org.springframework.web.portlet/src/test/java/org/springframework/web/portlet/mvc/annotation/Portlet20AnnotationControllerTests.java index 97460422892..cc50f5d9ce2 100644 --- a/org.springframework.web.portlet/src/test/java/org/springframework/web/portlet/mvc/annotation/Portlet20AnnotationControllerTests.java +++ b/org.springframework.web.portlet/src/test/java/org/springframework/web/portlet/mvc/annotation/Portlet20AnnotationControllerTests.java @@ -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"); diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/annotation/ServletAnnotationControllerTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/annotation/ServletAnnotationControllerTests.java index 4402b4f05ae..7dcbc5a3b86 100644 --- a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/annotation/ServletAnnotationControllerTests.java +++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/annotation/ServletAnnotationControllerTests.java @@ -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"}) diff --git a/org.springframework.web/src/main/java/org/springframework/web/context/support/WebApplicationContextUtils.java b/org.springframework.web/src/main/java/org/springframework/web/context/support/WebApplicationContextUtils.java index 483bbd9fc01..8e611e6b4d5 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/context/support/WebApplicationContextUtils.java +++ b/org.springframework.web/src/main/java/org/springframework/web/context/support/WebApplicationContextUtils.java @@ -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, 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, 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, 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() { public ExternalContext getObject() { return FacesContext.getCurrentInstance().getExternalContext(); } + @Override + public String toString() { + return "Current JSF ExternalContext"; + } }); } }