11/*
2- * Copyright 2002-2013 the original author or authors.
2+ * Copyright 2002-2015 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
2727import org .springframework .context .i18n .TimeZoneAwareLocaleContext ;
2828import org .springframework .ui .context .Theme ;
2929import org .springframework .ui .context .ThemeSource ;
30+ import org .springframework .web .context .ContextLoader ;
3031import org .springframework .web .context .WebApplicationContext ;
3132import org .springframework .web .context .support .WebApplicationContextUtils ;
3233import org .springframework .web .servlet .DispatcherServlet ;
5152 */
5253public abstract class RequestContextUtils {
5354
55+ /**
56+ * The name of the bean to use to look up in an implementation of
57+ * {@link RequestDataValueProcessor} has been configured.
58+ * @since 4.2.1
59+ */
60+ public static final String REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME = "requestDataValueProcessor" ;
61+
62+
5463 /**
5564 * Look for the WebApplicationContext associated with the DispatcherServlet
5665 * that has initiated request processing.
5766 * @param request current HTTP request
5867 * @return the request-specific web application context
5968 * @throws IllegalStateException if no servlet-specific context has been found
69+ * @see #getWebApplicationContext(ServletRequest, ServletContext)
70+ * @deprecated as of Spring 4.2.1, in favor of
71+ * {@link #findWebApplicationContext(HttpServletRequest)}
6072 */
61- public static WebApplicationContext getWebApplicationContext (ServletRequest request )
62- throws IllegalStateException {
63-
73+ @ Deprecated
74+ public static WebApplicationContext getWebApplicationContext (ServletRequest request ) throws IllegalStateException {
6475 return getWebApplicationContext (request , null );
6576 }
6677
@@ -76,7 +87,12 @@ public static WebApplicationContext getWebApplicationContext(ServletRequest requ
7687 * if no request-specific context has been found
7788 * @throws IllegalStateException if neither a servlet-specific nor a
7889 * global context has been found
90+ * @see DispatcherServlet#WEB_APPLICATION_CONTEXT_ATTRIBUTE
91+ * @see WebApplicationContextUtils#getRequiredWebApplicationContext(ServletContext)
92+ * @deprecated as of Spring 4.2.1, in favor of
93+ * {@link #findWebApplicationContext(HttpServletRequest, ServletContext)}
7994 */
95+ @ Deprecated
8096 public static WebApplicationContext getWebApplicationContext (
8197 ServletRequest request , ServletContext servletContext ) throws IllegalStateException {
8298
@@ -91,6 +107,57 @@ public static WebApplicationContext getWebApplicationContext(
91107 return webApplicationContext ;
92108 }
93109
110+ /**
111+ * Look for the WebApplicationContext associated with the DispatcherServlet
112+ * that has initiated request processing, and for the global context if none
113+ * was found associated with the current request. The global context will
114+ * be found via the ServletContext or via ContextLoader's current context.
115+ * <p>NOTE: This variant remains compatible with Servlet 2.5, explicitly
116+ * checking a given ServletContext instead of deriving it from the request.
117+ * @param request current HTTP request
118+ * @param servletContext current servlet context
119+ * @return the request-specific WebApplicationContext, or the global one
120+ * if no request-specific context has been found, or {@code null} if none
121+ * @since 4.2.1
122+ * @see DispatcherServlet#WEB_APPLICATION_CONTEXT_ATTRIBUTE
123+ * @see WebApplicationContextUtils#getWebApplicationContext(ServletContext)
124+ * @see ContextLoader#getCurrentWebApplicationContext()
125+ */
126+ public static WebApplicationContext findWebApplicationContext (
127+ HttpServletRequest request , ServletContext servletContext ) {
128+
129+ WebApplicationContext webApplicationContext = (WebApplicationContext ) request .getAttribute (
130+ DispatcherServlet .WEB_APPLICATION_CONTEXT_ATTRIBUTE );
131+ if (webApplicationContext == null ) {
132+ if (servletContext != null ) {
133+ webApplicationContext = WebApplicationContextUtils .getWebApplicationContext (servletContext );
134+ }
135+ if (webApplicationContext == null ) {
136+ webApplicationContext = ContextLoader .getCurrentWebApplicationContext ();
137+ }
138+ }
139+ return webApplicationContext ;
140+ }
141+
142+ /**
143+ * Look for the WebApplicationContext associated with the DispatcherServlet
144+ * that has initiated request processing, and for the global context if none
145+ * was found associated with the current request. The global context will
146+ * be found via the ServletContext or via ContextLoader's current context.
147+ * <p>NOTE: This variant requires Servlet 3.0+ and is generally recommended
148+ * for forward-looking custom user code.
149+ * @param request current HTTP request
150+ * @return the request-specific WebApplicationContext, or the global one
151+ * if no request-specific context has been found, or {@code null} if none
152+ * @since 4.2.1
153+ * @see #findWebApplicationContext(HttpServletRequest, ServletContext)
154+ * @see ServletRequest#getServletContext()
155+ * @see ContextLoader#getCurrentWebApplicationContext()
156+ */
157+ public static WebApplicationContext findWebApplicationContext (HttpServletRequest request ) {
158+ return findWebApplicationContext (request , request .getServletContext ());
159+ }
160+
94161 /**
95162 * Return the LocaleResolver that has been bound to the request by the
96163 * DispatcherServlet.
0 commit comments