KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > i18n > LocaleChangeInterceptor


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

38 public class LocaleChangeInterceptor extends HandlerInterceptorAdapter {
39
40     /**
41      * Default name of the locale specification parameter: "locale".
42      */

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

52     public void setParamName(String JavaDoc paramName) {
53         this.paramName = paramName;
54     }
55
56
57     public boolean preHandle(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, Object JavaDoc handler)
58             throws ServletException JavaDoc {
59
60         String JavaDoc newLocale = request.getParameter(this.paramName);
61         if (newLocale != null) {
62             LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
63             if (localeResolver == null) {
64                 throw new IllegalStateException JavaDoc("No LocaleResolver found: not in a DispatcherServlet request?");
65             }
66             LocaleEditor localeEditor = new LocaleEditor();
67             localeEditor.setAsText(newLocale);
68             localeResolver.setLocale(request, response, (Locale JavaDoc) localeEditor.getValue());
69         }
70         // Proceed in any case.
71
return true;
72     }
73
74 }
75
Popular Tags