001: /*
002: * Copyright 2002-2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.web.servlet.support;
018:
019: import java.util.Locale;
020:
021: import javax.servlet.ServletContext;
022: import javax.servlet.ServletRequest;
023: import javax.servlet.http.HttpServletRequest;
024:
025: import org.springframework.ui.context.Theme;
026: import org.springframework.ui.context.ThemeSource;
027: import org.springframework.web.context.WebApplicationContext;
028: import org.springframework.web.context.support.WebApplicationContextUtils;
029: import org.springframework.web.servlet.DispatcherServlet;
030: import org.springframework.web.servlet.LocaleResolver;
031: import org.springframework.web.servlet.ThemeResolver;
032:
033: /**
034: * Utility class for easy access to request-specific state
035: * which has been set by the DispatcherServlet.
036: *
037: * <p>Supports lookup of current WebApplicationContext, LocaleResolver,
038: * Locale, ThemeResolver, Theme, and MultipartResolver.
039: *
040: * @author Juergen Hoeller
041: * @since 03.03.2003
042: * @see RequestContext
043: * @see org.springframework.web.servlet.DispatcherServlet
044: */
045: public abstract class RequestContextUtils {
046:
047: /**
048: * Look for the WebApplicationContext associated with the DispatcherServlet
049: * that has initiated request processing.
050: * @param request current HTTP request
051: * @return the request-specific web application context
052: * @throws IllegalStateException if no servlet-specific context has been found
053: */
054: public static WebApplicationContext getWebApplicationContext(
055: ServletRequest request) throws IllegalStateException {
056:
057: return getWebApplicationContext(request, null);
058: }
059:
060: /**
061: * Look for the WebApplicationContext associated with the DispatcherServlet
062: * that has initiated request processing, and for the global context if none
063: * was found associated with the current request. This method is useful to
064: * allow components outside the framework, such as JSP tag handlers,
065: * to access the most specific application context available.
066: * @param request current HTTP request
067: * @param servletContext current servlet context
068: * @return the request-specific WebApplicationContext, or the global one
069: * if no request-specific context has been found
070: * @throws IllegalStateException if neither a servlet-specific nor a
071: * global context has been found
072: */
073: public static WebApplicationContext getWebApplicationContext(
074: ServletRequest request, ServletContext servletContext)
075: throws IllegalStateException {
076:
077: WebApplicationContext webApplicationContext = (WebApplicationContext) request
078: .getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE);
079: if (webApplicationContext == null) {
080: if (servletContext == null) {
081: throw new IllegalStateException(
082: "No WebApplicationContext found: not in a DispatcherServlet request?");
083: }
084: webApplicationContext = WebApplicationContextUtils
085: .getWebApplicationContext(servletContext);
086: if (webApplicationContext == null) {
087: throw new IllegalStateException(
088: "No WebApplicationContext found: no ContextLoaderListener registered?");
089: }
090: }
091: return webApplicationContext;
092: }
093:
094: /**
095: * Return the LocaleResolver that has been bound to the request by the
096: * DispatcherServlet.
097: * @param request current HTTP request
098: * @return the current LocaleResolver, or <code>null</code> if not found
099: */
100: public static LocaleResolver getLocaleResolver(
101: HttpServletRequest request) {
102: return (LocaleResolver) request
103: .getAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE);
104: }
105:
106: /**
107: * Retrieves the current locale from the given request,
108: * using the LocaleResolver bound to the request by the DispatcherServlet
109: * (if available), falling back to the request's accept-header Locale.
110: * @param request current HTTP request
111: * @return the current locale, either from the LocaleResolver or from
112: * the plain request
113: * @see #getLocaleResolver
114: * @see javax.servlet.http.HttpServletRequest#getLocale()
115: */
116: public static Locale getLocale(HttpServletRequest request) {
117: LocaleResolver localeResolver = getLocaleResolver(request);
118: if (localeResolver != null) {
119: return localeResolver.resolveLocale(request);
120: } else {
121: return request.getLocale();
122: }
123: }
124:
125: /**
126: * Return the ThemeResolver that has been bound to the request by the
127: * DispatcherServlet.
128: * @param request current HTTP request
129: * @return the current ThemeResolver, or <code>null</code> if not found
130: */
131: public static ThemeResolver getThemeResolver(
132: HttpServletRequest request) {
133: return (ThemeResolver) request
134: .getAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE);
135: }
136:
137: /**
138: * Return the ThemeSource that has been bound to the request by the
139: * DispatcherServlet.
140: * @param request current HTTP request
141: * @return the current ThemeSource
142: */
143: public static ThemeSource getThemeSource(HttpServletRequest request) {
144: return (ThemeSource) request
145: .getAttribute(DispatcherServlet.THEME_SOURCE_ATTRIBUTE);
146: }
147:
148: /**
149: * Retrieves the current theme from the given request, using the ThemeResolver
150: * and ThemeSource bound to the request by the DispatcherServlet.
151: * @param request current HTTP request
152: * @return the current theme, or <code>null</code> if not found
153: * @see #getThemeResolver
154: */
155: public static Theme getTheme(HttpServletRequest request) {
156: ThemeResolver themeResolver = getThemeResolver(request);
157: ThemeSource themeSource = getThemeSource(request);
158: if (themeResolver != null && themeSource != null) {
159: String themeName = themeResolver.resolveThemeName(request);
160: return themeSource.getTheme(themeName);
161: } else {
162: return null;
163: }
164: }
165:
166: }
|