KickJava   Java API By Example, From Geeks To Geeks.

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


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.http.Cookie 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.util.CookieGenerator;
25 import org.springframework.web.util.WebUtils;
26
27 /**
28  * Implementation of ThemeResolver that uses a cookie sent back to the user
29  * in case of a custom setting, with a fallback to the default theme.
30  * This is particularly useful for stateless applications without user sessions.
31  *
32  * <p>Custom controllers can thus override the user's theme by calling
33  * <code>setThemeName</code>, e.g. responding to a certain theme change request.
34  *
35  * @author Jean-Pierre Pawlak
36  * @author Juergen Hoeller
37  * @since 17.06.2003
38  * @see #setThemeName
39  */

40 public class CookieThemeResolver extends CookieGenerator implements ThemeResolver {
41
42     public final static String JavaDoc ORIGINAL_DEFAULT_THEME_NAME = "theme";
43
44     /**
45      * Name of the request attribute that holds the theme name. Only used
46      * for overriding a cookie value if the theme has been changed in the
47      * course of the current request! Use RequestContext.getTheme() to
48      * retrieve the current theme in controllers or views.
49      * @see org.springframework.web.servlet.support.RequestContext#getTheme
50      */

51     public static final String JavaDoc THEME_REQUEST_ATTRIBUTE_NAME = CookieThemeResolver.class.getName() + ".THEME";
52
53     public static final String JavaDoc DEFAULT_COOKIE_NAME = CookieThemeResolver.class.getName() + ".THEME";
54
55
56     private String JavaDoc defaultThemeName = ORIGINAL_DEFAULT_THEME_NAME;
57
58
59     public CookieThemeResolver() {
60         setCookieName(DEFAULT_COOKIE_NAME);
61     }
62
63
64     /**
65      * Set the name of the default theme.
66      */

67     public void setDefaultThemeName(String JavaDoc defaultThemeName) {
68         this.defaultThemeName = defaultThemeName;
69     }
70
71     /**
72      * Return the name of the default theme.
73      */

74     public String JavaDoc getDefaultThemeName() {
75         return defaultThemeName;
76     }
77
78
79     public String JavaDoc resolveThemeName(HttpServletRequest JavaDoc request) {
80         // Check request for preparsed or preset theme.
81
String JavaDoc theme = (String JavaDoc) request.getAttribute(THEME_REQUEST_ATTRIBUTE_NAME);
82         if (theme != null) {
83             return theme;
84         }
85
86         // Retrieve cookie value from request.
87
Cookie JavaDoc cookie = WebUtils.getCookie(request, getCookieName());
88         if (cookie != null) {
89             return cookie.getValue();
90         }
91
92         // Fall back to default theme.
93
return getDefaultThemeName();
94     }
95
96     public void setThemeName(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, String JavaDoc themeName) {
97         if (themeName != null) {
98             // Set request attribute and add cookie.
99
request.setAttribute(THEME_REQUEST_ATTRIBUTE_NAME, themeName);
100             addCookie(response, themeName);
101         }
102
103         else {
104             // Set request attribute to fallback theme and remove cookie.
105
request.setAttribute(THEME_REQUEST_ATTRIBUTE_NAME, getDefaultThemeName());
106             removeCookie(response);
107         }
108     }
109
110 }
111
Popular Tags