1. /*
  2. * Copyright 2002-2004 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. package org.springframework.web.servlet.i18n;
  17. import java.util.Locale;
  18. import java.util.StringTokenizer;
  19. import javax.servlet.http.Cookie;
  20. import javax.servlet.http.HttpServletRequest;
  21. import javax.servlet.http.HttpServletResponse;
  22. import org.springframework.web.servlet.LocaleResolver;
  23. import org.springframework.web.util.WebUtils;
  24. /**
  25. * Implementation of LocaleResolver that uses a cookie sent back to the user
  26. * in case of a custom setting, with a fallback to the accept header locale.
  27. * This is especially useful for stateless applications without user sessions.
  28. *
  29. * <p>Custom controllers can thus override the user's locale by calling setLocale,
  30. * e.g. responding to a certain locale change request.
  31. *
  32. * @author Juergen Hoeller
  33. * @author Jean-Pierre Pawlak
  34. * @since 27.02.2003
  35. */
  36. public class CookieLocaleResolver implements LocaleResolver {
  37. /**
  38. * Name of the request attribute that holds the locale. Only used for
  39. * overriding a cookie value if the locale has been changed in the
  40. * course of the current request! Use RequestContext.getLocale() to
  41. * retrieve the current locale in controllers or views.
  42. * @see org.springframework.web.servlet.support.RequestContext#getLocale
  43. */
  44. public static final String LOCALE_REQUEST_ATTRIBUTE_NAME = CookieLocaleResolver.class.getName() + ".LOCALE";
  45. public static final String DEFAULT_COOKIE_NAME = CookieLocaleResolver.class.getName() + ".LOCALE";
  46. public static final String DEFAULT_COOKIE_PATH = "/";
  47. public static final int DEFAULT_COOKIE_MAX_AGE = Integer.MAX_VALUE;
  48. private String cookieName = DEFAULT_COOKIE_NAME;
  49. private String cookiePath = DEFAULT_COOKIE_PATH;
  50. private int cookieMaxAge = DEFAULT_COOKIE_MAX_AGE;
  51. /**
  52. * Use the given name for locale cookies.
  53. */
  54. public void setCookieName(String cookieName) {
  55. this.cookieName = cookieName;
  56. }
  57. public String getCookieName() {
  58. return cookieName;
  59. }
  60. /**
  61. * Use the given path for theme cookies.
  62. * The cookie is only visible for URLs in the path and below.
  63. */
  64. public String getCookiePath() {
  65. return cookiePath;
  66. }
  67. public void setCookiePath(String cookiePath) {
  68. this.cookiePath = cookiePath;
  69. }
  70. /**
  71. * Use the given maximum age, specified in seconds, for locale cookies.
  72. * Useful special value: -1 ... not persistent, deleted when client shuts down
  73. */
  74. public void setCookieMaxAge(int cookieMaxAge) {
  75. this.cookieMaxAge = cookieMaxAge;
  76. }
  77. public int getCookieMaxAge() {
  78. return cookieMaxAge;
  79. }
  80. public Locale resolveLocale(HttpServletRequest request) {
  81. // check locale for preparsed resp. preset locale
  82. Locale locale = (Locale) request.getAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME);
  83. if (locale != null)
  84. return locale;
  85. // retrieve cookie value
  86. Cookie cookie = WebUtils.getCookie(request, getCookieName());
  87. if (cookie != null) {
  88. // parse cookie value
  89. String language = "";
  90. String country = "";
  91. String variant = "";
  92. StringTokenizer tokenizer = new StringTokenizer(cookie.getValue());
  93. if (tokenizer.hasMoreTokens())
  94. language = tokenizer.nextToken();
  95. if (tokenizer.hasMoreTokens())
  96. country = tokenizer.nextToken();
  97. if (tokenizer.hasMoreTokens())
  98. variant = tokenizer.nextToken();
  99. // evaluate results
  100. if (language != null) {
  101. locale = new Locale(language, country, variant);
  102. request.setAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME, locale);
  103. return locale;
  104. }
  105. }
  106. // fallback
  107. return request.getLocale();
  108. }
  109. public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
  110. Cookie cookie = null;
  111. if (locale != null) {
  112. // set request attribute and add cookie
  113. request.setAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME, locale);
  114. cookie = new Cookie(getCookieName(), locale.getLanguage() + " " + locale.getCountry() + " " + locale.getVariant());
  115. cookie.setMaxAge(getCookieMaxAge());
  116. cookie.setPath(cookiePath);
  117. }
  118. else {
  119. // set request attribute to fallback locale and remove cookie
  120. request.setAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME, request.getLocale());
  121. cookie = new Cookie(getCookieName(), "");
  122. cookie.setMaxAge(0);
  123. cookie.setPath(cookiePath);
  124. }
  125. response.addCookie(cookie);
  126. }
  127. }