KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > support > RequestContextUtils


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.servlet.support;
18
19 import java.util.Locale JavaDoc;
20
21 import javax.servlet.ServletContext JavaDoc;
22 import javax.servlet.ServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24
25 import org.springframework.ui.context.Theme;
26 import org.springframework.ui.context.ThemeSource;
27 import org.springframework.web.context.WebApplicationContext;
28 import org.springframework.web.context.support.WebApplicationContextUtils;
29 import org.springframework.web.servlet.DispatcherServlet;
30 import org.springframework.web.servlet.LocaleResolver;
31 import org.springframework.web.servlet.ThemeResolver;
32
33 /**
34  * Utility class for easy access to request-specific state
35  * which has been set by the DispatcherServlet.
36  *
37  * <p>Supports lookup of current WebApplicationContext, LocaleResolver,
38  * Locale, ThemeResolver, Theme, and MultipartResolver.
39  *
40  * @author Juergen Hoeller
41  * @since 03.03.2003
42  * @see RequestContext
43  * @see org.springframework.web.servlet.DispatcherServlet
44  */

45 public abstract class RequestContextUtils {
46
47     /**
48      * Look for the WebApplicationContext associated with the DispatcherServlet
49      * that has initiated request processing.
50      * @param request current HTTP request
51      * @return the request-specific web application context
52      * @throws IllegalStateException if no servlet-specific context has been found
53      */

54     public static WebApplicationContext getWebApplicationContext(ServletRequest JavaDoc request)
55         throws IllegalStateException JavaDoc {
56
57         return getWebApplicationContext(request, null);
58     }
59
60     /**
61      * Look for the WebApplicationContext associated with the DispatcherServlet
62      * that has initiated request processing, and for the global context if none
63      * was found associated with the current request. This method is useful to
64      * allow components outside the framework, such as JSP tag handlers,
65      * to access the most specific application context available.
66      * @param request current HTTP request
67      * @param servletContext current servlet context
68      * @return the request-specific WebApplicationContext, or the global one
69      * if no request-specific context has been found
70      * @throws IllegalStateException if neither a servlet-specific nor a
71      * global context has been found
72      */

73     public static WebApplicationContext getWebApplicationContext(
74             ServletRequest JavaDoc request, ServletContext JavaDoc servletContext) throws IllegalStateException JavaDoc {
75
76         WebApplicationContext webApplicationContext = (WebApplicationContext) request.getAttribute(
77                 DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE);
78         if (webApplicationContext == null) {
79             if (servletContext == null) {
80                 throw new IllegalStateException JavaDoc("No WebApplicationContext found: not in a DispatcherServlet request?");
81             }
82             webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
83             if (webApplicationContext == null) {
84                 throw new IllegalStateException JavaDoc("No WebApplicationContext found: no ContextLoaderListener registered?");
85             }
86         }
87         return webApplicationContext;
88     }
89
90     /**
91      * Return the LocaleResolver that has been bound to the request by the
92      * DispatcherServlet.
93      * @param request current HTTP request
94      * @return the current LocaleResolver, or <code>null</code> if not found
95      */

96     public static LocaleResolver getLocaleResolver(HttpServletRequest JavaDoc request) {
97         return (LocaleResolver) request.getAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE);
98     }
99
100     /**
101      * Retrieves the current locale from the given request,
102      * using the LocaleResolver bound to the request by the DispatcherServlet
103      * (if available), falling back to the request's accept-header Locale.
104      * @param request current HTTP request
105      * @return the current locale, either from the LocaleResolver or from
106      * the plain request
107      * @see #getLocaleResolver
108      * @see javax.servlet.http.HttpServletRequest#getLocale()
109      */

110     public static Locale JavaDoc getLocale(HttpServletRequest JavaDoc request) {
111         LocaleResolver localeResolver = getLocaleResolver(request);
112         if (localeResolver != null) {
113             return localeResolver.resolveLocale(request);
114         }
115         else {
116             return request.getLocale();
117         }
118     }
119
120     /**
121      * Return the ThemeResolver that has been bound to the request by the
122      * DispatcherServlet.
123      * @param request current HTTP request
124      * @return the current ThemeResolver, or <code>null</code> if not found
125      */

126     public static ThemeResolver getThemeResolver(HttpServletRequest JavaDoc request) {
127         return (ThemeResolver) request.getAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE);
128     }
129
130     /**
131      * Return the ThemeSource that has been bound to the request by the
132      * DispatcherServlet.
133      * @param request current HTTP request
134      * @return the current ThemeSource
135      */

136     public static ThemeSource getThemeSource(HttpServletRequest JavaDoc request) {
137         return (ThemeSource) request.getAttribute(DispatcherServlet.THEME_SOURCE_ATTRIBUTE);
138     }
139
140     /**
141      * Retrieves the current theme from the given request, using the ThemeResolver
142      * and ThemeSource bound to the request by the DispatcherServlet.
143      * @param request current HTTP request
144      * @return the current theme, or <code>null</code> if not found
145      * @see #getThemeResolver
146      */

147     public static Theme getTheme(HttpServletRequest JavaDoc request) {
148         ThemeResolver themeResolver = getThemeResolver(request);
149         ThemeSource themeSource = getThemeSource(request);
150         if (themeResolver != null && themeSource != null) {
151             String JavaDoc themeName = themeResolver.resolveThemeName(request);
152             return themeSource.getTheme(themeName);
153         }
154         else {
155             return null;
156         }
157     }
158
159 }
160
Popular Tags