KickJava   Java API By Example, From Geeks To Geeks.

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


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.HttpServletRequest JavaDoc;
20 import javax.servlet.http.HttpServletResponse JavaDoc;
21
22 import org.springframework.web.util.WebUtils;
23
24 /**
25  * Implementation of ThemeResolver that uses a theme attribute in the user's
26  * session in case of a custom setting, with a fallback to the default theme.
27  * This is most appropriate if the application needs user sessions anyway.
28  *
29  * <p>Custom controllers can override the user's theme by calling
30  * <code>setThemeName</code>, e.g. responding to a theme change request.
31  *
32  * @author Jean-Pierre Pawlak
33  * @author Juergen Hoeller
34  * @since 17.06.2003
35  * @see #setThemeName
36  */

37 public class SessionThemeResolver extends AbstractThemeResolver {
38
39     /**
40      * Name of the session attribute that holds the theme name.
41      * Only used internally by this implementation.
42      * Use <code>RequestContext(Utils).getTheme()</code>
43      * to retrieve the current theme in controllers or views.
44      * @see org.springframework.web.servlet.support.RequestContext#getTheme
45      * @see org.springframework.web.servlet.support.RequestContextUtils#getTheme
46      */

47     public static final String JavaDoc THEME_SESSION_ATTRIBUTE_NAME = SessionThemeResolver.class.getName() + ".THEME";
48
49     public String JavaDoc resolveThemeName(HttpServletRequest JavaDoc request) {
50         String JavaDoc theme = (String JavaDoc) WebUtils.getSessionAttribute(request, THEME_SESSION_ATTRIBUTE_NAME);
51         // specific theme, or fallback to default?
52
return (theme != null ? theme : getDefaultThemeName());
53     }
54
55     public void setThemeName(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, String JavaDoc themeName) {
56         WebUtils.setSessionAttribute(request, THEME_SESSION_ATTRIBUTE_NAME, themeName);
57     }
58
59 }
60
Popular Tags