KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2002-2006 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.http.HttpServletRequest JavaDoc;
22 import javax.servlet.http.HttpServletResponse JavaDoc;
23
24 import org.springframework.web.util.WebUtils;
25
26 /**
27  * Implementation of LocaleResolver that uses a locale attribute in the user's
28  * session in case of a custom setting, with a fallback to the specified default
29  * locale or the request's accept-header locale.
30  *
31  * <p>This is most appropriate if the application needs user sessions anyway,
32  * that is, when the HttpSession does not have to be created for the locale.
33  *
34  * <p>Custom controllers can override the user's locale by calling
35  * <code>setLocale</code>, e.g. responding to a locale change request.
36  *
37  * @author Juergen Hoeller
38  * @since 27.02.2003
39  * @see #setDefaultLocale
40  * @see #setLocale
41  */

42 public class SessionLocaleResolver extends AbstractLocaleResolver {
43
44     /**
45      * Name of the session attribute that holds the locale.
46      * Only used internally by this implementation.
47      * Use <code>RequestContext(Utils).getLocale()</code>
48      * to retrieve the current locale in controllers or views.
49      * @see org.springframework.web.servlet.support.RequestContext#getLocale
50      * @see org.springframework.web.servlet.support.RequestContextUtils#getLocale
51      */

52     public static final String JavaDoc LOCALE_SESSION_ATTRIBUTE_NAME = SessionLocaleResolver.class.getName() + ".LOCALE";
53     
54
55     public Locale JavaDoc resolveLocale(HttpServletRequest JavaDoc request) {
56         Locale JavaDoc locale = (Locale JavaDoc) WebUtils.getSessionAttribute(request, LOCALE_SESSION_ATTRIBUTE_NAME);
57         if (locale == null) {
58             locale = determineDefaultLocale(request);
59         }
60         return locale;
61     }
62
63     /**
64      * Determine the default locale for the given request,
65      * Called if no locale session attribute has been found.
66      * <p>The default implementation returns the specified default locale,
67      * if any, else falls back to the request's accept-header locale.
68      * @param request the request to resolve the locale for
69      * @return the default locale (never <code>null</code>)
70      * @see #setDefaultLocale
71      * @see javax.servlet.http.HttpServletRequest#getLocale()
72      */

73     protected Locale JavaDoc determineDefaultLocale(HttpServletRequest JavaDoc request) {
74         Locale JavaDoc defaultLocale = getDefaultLocale();
75         if (defaultLocale == null) {
76             defaultLocale = request.getLocale();
77         }
78         return defaultLocale;
79     }
80
81     public void setLocale(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, Locale JavaDoc locale) {
82         WebUtils.setSessionAttribute(request, LOCALE_SESSION_ATTRIBUTE_NAME, locale);
83     }
84
85 }
86
Popular Tags