01: /*
02: * Copyright 2002-2006 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.i18n;
18:
19: import java.util.Locale;
20:
21: import javax.servlet.http.HttpServletRequest;
22: import javax.servlet.http.HttpServletResponse;
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 LOCALE_SESSION_ATTRIBUTE_NAME = SessionLocaleResolver.class
53: .getName()
54: + ".LOCALE";
55:
56: public Locale resolveLocale(HttpServletRequest request) {
57: Locale locale = (Locale) WebUtils.getSessionAttribute(request,
58: LOCALE_SESSION_ATTRIBUTE_NAME);
59: if (locale == null) {
60: locale = determineDefaultLocale(request);
61: }
62: return locale;
63: }
64:
65: /**
66: * Determine the default locale for the given request,
67: * Called if no locale session attribute has been found.
68: * <p>The default implementation returns the specified default locale,
69: * if any, else falls back to the request's accept-header locale.
70: * @param request the request to resolve the locale for
71: * @return the default locale (never <code>null</code>)
72: * @see #setDefaultLocale
73: * @see javax.servlet.http.HttpServletRequest#getLocale()
74: */
75: protected Locale determineDefaultLocale(HttpServletRequest request) {
76: Locale defaultLocale = getDefaultLocale();
77: if (defaultLocale == null) {
78: defaultLocale = request.getLocale();
79: }
80: return defaultLocale;
81: }
82:
83: public void setLocale(HttpServletRequest request,
84: HttpServletResponse response, Locale locale) {
85: WebUtils.setSessionAttribute(request,
86: LOCALE_SESSION_ATTRIBUTE_NAME, locale);
87: }
88:
89: }
|