RequestContextUtils.java
Upload User: jiancairen
Upload Date: 2007-08-27
Package Size: 26458k
Code Size: 6k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * Copyright 2002-2004 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. package org.springframework.web.servlet.support;
  17. import java.util.Locale;
  18. import javax.servlet.ServletContext;
  19. import javax.servlet.ServletRequest;
  20. import javax.servlet.http.HttpServletRequest;
  21. import org.springframework.ui.context.Theme;
  22. import org.springframework.web.context.WebApplicationContext;
  23. import org.springframework.web.context.support.WebApplicationContextUtils;
  24. import org.springframework.web.multipart.MultipartResolver;
  25. import org.springframework.web.servlet.DispatcherServlet;
  26. import org.springframework.web.servlet.LocaleResolver;
  27. import org.springframework.web.servlet.ThemeResolver;
  28. /**
  29.  * Utility class for easy access to request-specific state
  30.  * which has been set by the DispatcherServlet.
  31.  *
  32.  * <p>Supports lookup of current WebApplicationContext, LocaleResolver,
  33.  * Locale, ThemeResolver, Theme, and MultipartResolver.
  34.  *
  35.  * @author Juergen Hoeller
  36.  * @since 03.03.2003
  37.  * @see RequestContext
  38.  * @see org.springframework.web.servlet.DispatcherServlet
  39.  */
  40. public abstract class RequestContextUtils {
  41. /**
  42.  * Look for the WebApplicationContext associated with the DispatcherServlet
  43.  * that has initiated request processing.
  44.  * @param request current HTTP request
  45.  * @return the request-specific web application context
  46.  * @throws IllegalStateException if no servlet-specific context has been found
  47.  */
  48. public static WebApplicationContext getWebApplicationContext(ServletRequest request)
  49.     throws IllegalStateException {
  50. return getWebApplicationContext(request, null);
  51. }
  52. /**
  53.  * Look for the WebApplicationContext associated with the DispatcherServlet
  54.  * that has initiated request processing, and for the global context if none
  55.  * was found associated with the current request. This method is useful to
  56.  * allow components outside the framework, such as JSP tag handlers,
  57.  * to access the most specific application context available.
  58.  * @param request current HTTP request
  59.  * @param servletContext current servlet context
  60.  * @return the request-specific WebApplicationContext, or the global one
  61.  * if no request-specific context has been found
  62.  * @throws IllegalStateException if neither a servlet-specific nor a
  63.  * global context has been found
  64.  */
  65. public static WebApplicationContext getWebApplicationContext(ServletRequest request,
  66.                                                              ServletContext servletContext)
  67.     throws IllegalStateException {
  68. WebApplicationContext webApplicationContext = (WebApplicationContext) request.getAttribute(
  69. DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE);
  70. if (webApplicationContext == null) {
  71. if (servletContext == null) {
  72. throw new IllegalStateException("No WebApplicationContext found: not in a DispatcherServlet request?");
  73. }
  74. webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
  75. if (webApplicationContext == null) {
  76. throw new IllegalStateException("No WebApplicationContext found: no ContextLoaderListener registered?");
  77. }
  78. }
  79. return webApplicationContext;
  80. }
  81. /**
  82.  * Return the LocaleResolver that has been bound to the request by the
  83.  * DispatcherServlet.
  84.  * @param request current HTTP request
  85.  * @return the current LocaleResolver
  86.  * @throws IllegalStateException if no LocaleResolver has been found
  87.  */
  88. public static LocaleResolver getLocaleResolver(HttpServletRequest request) throws IllegalStateException {
  89. LocaleResolver localeResolver = (LocaleResolver) request.getAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE);
  90. if (localeResolver == null) {
  91. throw new IllegalStateException("No LocaleResolver found: not in a DispatcherServlet request?");
  92. }
  93. return localeResolver;
  94. }
  95. /**
  96.  * Retrieves the current locale from the given request,
  97.  * using the LocaleResolver bound to the request by the DispatcherServlet.
  98.  * @param request current HTTP request
  99.  * @return the current locale
  100.  * @throws IllegalStateException if no LocaleResolver has been found
  101.  */
  102. public static Locale getLocale(HttpServletRequest request) throws IllegalStateException {
  103. return getLocaleResolver(request).resolveLocale(request);
  104. }
  105. /**
  106.  * Return the ThemeResolver that has been bound to the request by the
  107.  * DispatcherServlet.
  108.  * @param request current HTTP request
  109.  * @return the current ThemeResolver
  110.  * @throws IllegalStateException if no ThemeResolver has been found
  111.  */
  112. public static ThemeResolver getThemeResolver(HttpServletRequest request) throws IllegalStateException {
  113. ThemeResolver themeResolver = (ThemeResolver) request.getAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE);
  114. if (themeResolver == null) {
  115. throw new IllegalStateException("No ThemeResolver found: not in a DispatcherServlet request?");
  116. }
  117. return themeResolver;
  118. }
  119. /**
  120.  * Retrieves the current theme from the given request, using the
  121.  * ThemeResolver bound to the request by the DispatcherServlet.
  122.  * @param request current HTTP request
  123.  * @return the current theme
  124.  * @throws IllegalStateException if no ThemeResolver has been found
  125.  */
  126. public static Theme getTheme(HttpServletRequest request) throws IllegalStateException {
  127. WebApplicationContext context = getWebApplicationContext(request);
  128. String themeName = getThemeResolver(request).resolveThemeName(request);
  129. return context.getTheme(themeName);
  130. }
  131. /**
  132.  * Return the MultipartResolver that has been bound to the request by the
  133.  * DispatcherServlet.
  134.  * @param request current HTTP request
  135.  * @return the current MultipartResolver, or null if not a multipart request
  136.  */
  137. public static MultipartResolver getMultipartResolver(ServletRequest request) {
  138. return (MultipartResolver) request.getAttribute(DispatcherServlet.MULTIPART_RESOLVER_ATTRIBUTE);
  139. }
  140. }