KickJava   Java API By Example, From Geeks To Geeks.

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


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.Cookie JavaDoc;
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24
25 import org.springframework.util.StringUtils;
26 import org.springframework.web.servlet.LocaleResolver;
27 import org.springframework.web.util.CookieGenerator;
28 import org.springframework.web.util.WebUtils;
29
30 /**
31  * {@link LocaleResolver} implementation that uses a cookie sent back to the user
32  * in case of a custom setting, with a fallback to the specified default locale
33  * or the request's accept-header locale.
34  *
35  * <p>This is particularly useful for stateless applications without user sessions.
36  *
37  * <p>Custom controllers can thus override the user's locale by calling
38  * {@link #setLocale(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.util.Locale)},
39  * for example responding to a certain locale change request.
40  *
41  * @author Juergen Hoeller
42  * @author Jean-Pierre Pawlak
43  * @since 27.02.2003
44  * @see #setDefaultLocale
45  * @see #setLocale
46  */

47 public class CookieLocaleResolver extends CookieGenerator implements LocaleResolver {
48
49     /**
50      * The name of the request attribute that holds the locale.
51      * <p>Only used for overriding a cookie value if the locale has been
52      * changed in the course of the current request! Use
53      * {@link org.springframework.web.servlet.support.RequestContext#getLocale}
54      * to retrieve the current locale in controllers or views.
55      * @see org.springframework.web.servlet.support.RequestContext#getLocale
56      */

57     public static final String JavaDoc LOCALE_REQUEST_ATTRIBUTE_NAME = CookieLocaleResolver.class.getName() + ".LOCALE";
58
59     /**
60      * The default cookie name used if none is explicitly set.
61      */

62     public static final String JavaDoc DEFAULT_COOKIE_NAME = CookieLocaleResolver.class.getName() + ".LOCALE";
63
64
65     private Locale JavaDoc defaultLocale;
66
67
68     /**
69      * Creates a new instance of the {@link CookieLocaleResolver} class
70      * using the {@link #DEFAULT_COOKIE_NAME default cookie name}.
71      */

72     public CookieLocaleResolver() {
73         setCookieName(DEFAULT_COOKIE_NAME);
74     }
75
76     /**
77      * Set a fixed Locale that this resolver will return if no cookie found.
78      */

79     public void setDefaultLocale(Locale JavaDoc defaultLocale) {
80         this.defaultLocale = defaultLocale;
81     }
82
83     /**
84      * Return the fixed Locale that this resolver will return if no cookie found,
85      * if any.
86      */

87     protected Locale JavaDoc getDefaultLocale() {
88         return defaultLocale;
89     }
90
91
92     public Locale JavaDoc resolveLocale(HttpServletRequest JavaDoc request) {
93         // Check request for pre-parsed or preset locale.
94
Locale JavaDoc locale = (Locale JavaDoc) request.getAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME);
95         if (locale != null) {
96             return locale;
97         }
98
99         // Retrieve and parse cookie value.
100
Cookie JavaDoc cookie = WebUtils.getCookie(request, getCookieName());
101         if (cookie != null) {
102             locale = StringUtils.parseLocaleString(cookie.getValue());
103             if (logger.isDebugEnabled()) {
104                 logger.debug("Parsed cookie value [" + cookie.getValue() + "] into locale '" + locale + "'");
105             }
106             if (locale != null) {
107                 request.setAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME, locale);
108                 return locale;
109             }
110         }
111
112         return determineDefaultLocale(request);
113     }
114
115     /**
116      * Determine the default locale for the given request,
117      * Called if no locale cookie has been found.
118      * <p>The default implementation returns the specified default locale,
119      * if any, else falls back to the request's accept-header locale.
120      * @param request the request to resolve the locale for
121      * @return the default locale (never <code>null</code>)
122      * @see #setDefaultLocale
123      * @see javax.servlet.http.HttpServletRequest#getLocale()
124      */

125     protected Locale JavaDoc determineDefaultLocale(HttpServletRequest JavaDoc request) {
126         Locale JavaDoc defaultLocale = getDefaultLocale();
127         if (defaultLocale == null) {
128             defaultLocale = request.getLocale();
129         }
130         return defaultLocale;
131     }
132
133     public void setLocale(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, Locale JavaDoc locale) {
134         if (locale != null) {
135             // Set request attribute and add cookie.
136
request.setAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME, locale);
137             addCookie(response, locale.toString());
138         }
139         else {
140             // Set request attribute to fallback locale and remove cookie.
141
request.setAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME, request.getLocale());
142             removeCookie(response);
143         }
144     }
145
146 }
147
Popular Tags