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.ServletException;
20: import javax.servlet.http.HttpServletRequest;
21: import javax.servlet.http.HttpServletResponse;
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 DEFAULT_PARAM_NAME = "theme";
41:
42: private String paramName = DEFAULT_PARAM_NAME;
43:
44: /**
45: * Set the name of the parameter that contains a theme specification
46: * in a theme change request. Default is "theme".
47: */
48: public void setParamName(String paramName) {
49: this .paramName = paramName;
50: }
51:
52: public boolean preHandle(HttpServletRequest request,
53: HttpServletResponse response, Object handler)
54: throws ServletException {
55:
56: ThemeResolver themeResolver = RequestContextUtils
57: .getThemeResolver(request);
58: if (themeResolver == null) {
59: throw new IllegalStateException(
60: "No ThemeResolver found: not in a DispatcherServlet request?");
61: }
62: String newTheme = request.getParameter(this .paramName);
63: if (newTheme != null) {
64: themeResolver.setThemeName(request, response, newTheme);
65: }
66: // Proceed in any case.
67: return true;
68: }
69:
70: }
|