KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > theme > ThemeChangeInterceptor


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.theme;
18
19 import javax.servlet.ServletException JavaDoc;
20 import javax.servlet.http.HttpServletRequest JavaDoc;
21 import javax.servlet.http.HttpServletResponse JavaDoc;
22
23 import org.springframework.web.servlet.ThemeResolver;
24 import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
25 import org.springframework.web.servlet.support.RequestContextUtils;
26
27 /**
28  * Interceptor that allows for changing the current theme on every request,
29  * via a configurable request parameter.
30  *
31  * @author Juergen Hoeller
32  * @since 20.06.2003
33  * @see org.springframework.web.servlet.ThemeResolver
34  */

35 public class ThemeChangeInterceptor extends HandlerInterceptorAdapter {
36
37     /**
38      * Default name of the theme specification parameter: "theme".
39      */

40     public static final String JavaDoc DEFAULT_PARAM_NAME = "theme";
41
42     private String JavaDoc paramName = DEFAULT_PARAM_NAME;
43
44
45     /**
46      * Set the name of the parameter that contains a theme specification
47      * in a theme change request. Default is "theme".
48      */

49     public void setParamName(String JavaDoc paramName) {
50         this.paramName = paramName;
51     }
52
53
54     public boolean preHandle(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, Object JavaDoc handler)
55             throws ServletException JavaDoc {
56
57         ThemeResolver themeResolver = RequestContextUtils.getThemeResolver(request);
58         if (themeResolver == null) {
59             throw new IllegalStateException JavaDoc("No ThemeResolver found: not in a DispatcherServlet request?");
60         }
61         String JavaDoc newTheme = request.getParameter(this.paramName);
62         if (newTheme != null) {
63             themeResolver.setThemeName(request, response, newTheme);
64         }
65         // Proceed in any case.
66
return true;
67     }
68
69 }
70
Popular Tags