01: /*
02: * Copyright 2002-2005 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
20: import javax.servlet.http.HttpServletResponse;
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 THEME_SESSION_ATTRIBUTE_NAME = SessionThemeResolver.class
48: .getName()
49: + ".THEME";
50:
51: public String resolveThemeName(HttpServletRequest request) {
52: String theme = (String) WebUtils.getSessionAttribute(request,
53: THEME_SESSION_ATTRIBUTE_NAME);
54: // specific theme, or fallback to default?
55: return (theme != null ? theme : getDefaultThemeName());
56: }
57:
58: public void setThemeName(HttpServletRequest request,
59: HttpServletResponse response, String themeName) {
60: WebUtils.setSessionAttribute(request,
61: THEME_SESSION_ATTRIBUTE_NAME, themeName);
62: }
63:
64: }
|