Class CookieLocaleResolver

  • All Implemented Interfaces:
    LocaleResolver

    public class CookieLocaleResolver
    extends AbstractLocaleResolver

    This class is a clone of org.springframework.web.servlet.i18n.CookieLocaleResolver

    LocaleResolver implementation that uses a cookie sent back to the user in case of a custom setting, with a fallback to the specified default locale or the request's accept-header locale.

    This is particularly useful for stateless applications without user sessions. The cookie may optionally contain an associated time zone value as well; alternatively, you may specify a default time zone.

    • Constructor Detail

      • CookieLocaleResolver

        public CookieLocaleResolver()
        Create a new instance of the CookieLocaleResolver class.
    • Method Detail

      • setLocaleCookieGenerator

        public void setLocaleCookieGenerator​(CookieGenerator localeCookieGenerator)
      • getLocaleCookieGenerator

        public CookieGenerator getLocaleCookieGenerator()
      • setTimeZoneCookieGenerator

        public void setTimeZoneCookieGenerator​(CookieGenerator timeZoneCookieGenerator)
      • getTimeZoneCookieGenerator

        public CookieGenerator getTimeZoneCookieGenerator()
      • setCookieDomain

        public void setCookieDomain​(java.lang.String cookieDomain)
        Use the given domain for cookies. The cookie is only visible to servers in this domain.
        See Also:
        Cookie.setDomain(java.lang.String)
      • setCookiePath

        public void setCookiePath​(java.lang.String cookiePath)
        Use the given path for cookies. The cookie is only visible to URLs in this path and below.
        See Also:
        Cookie.setPath(java.lang.String)
      • setCookieMaxAge

        public void setCookieMaxAge​(java.lang.Integer cookieMaxAge)
        Use the given maximum age (in seconds) for cookies. Useful special value: -1 ... not persistent, deleted when client shuts down.

        Default is no specific maximum age at all, using the Servlet container's default.

        See Also:
        Cookie.setMaxAge(int)
      • setCookieSecure

        public void setCookieSecure​(boolean cookieSecure)
        Set whether the cookie should only be sent using a secure protocol, such as HTTPS (SSL). This is an indication to the receiving browser, not processed by the HTTP server itself.

        Default is "false".

        See Also:
        Cookie.setSecure(boolean)
      • setCookieHttpOnly

        public void setCookieHttpOnly​(boolean cookieHttpOnly)
        Set whether the cookie is supposed to be marked with the "HttpOnly" attribute.

        Default is "false".

        See Also:
        Cookie.setHttpOnly(boolean)
      • setLanguageTagCompliant

        public void setLanguageTagCompliant​(boolean languageTagCompliant)
        Specify whether this resolver's cookies should be compliant with BCP 47 language tags instead of Java's legacy locale specification format.

        The default is true, as of 5.1. Switch this to false for rendering Java's legacy locale specification format. For parsing, this resolver leniently accepts the legacy Locale.toString() format as well as BCP 47 language tags in any case.

        See Also:
        parseLocaleValue(String), toLocaleValue(Locale), Locale.forLanguageTag(String), Locale.toLanguageTag()
      • isLanguageTagCompliant

        public boolean isLanguageTagCompliant()
        Return whether this resolver's cookies should be compliant with BCP 47 language tags instead of Java's legacy locale specification format.
      • isRejectInvalidCookies

        public boolean isRejectInvalidCookies()
        Return whether to reject cookies with invalid content (e.g. invalid format).
      • resolveLocale

        public java.util.Locale resolveLocale​(Translet translet)
        Description copied from interface: LocaleResolver
        Resolve the current locale via the given translet. Can return a default locale as fallback in any case.
        Parameters:
        translet - the translet to resolve the locale for
        Returns:
        the current locale (never null)
      • resolveTimeZone

        public java.util.TimeZone resolveTimeZone​(Translet translet)
        Description copied from interface: LocaleResolver
        Resolve the current timezone via the given translet. Can return a default timezone as fallback in any case.
        Parameters:
        translet - the translet to resolve the timezone for
        Returns:
        the current timezone (never null)
      • setLocale

        public void setLocale​(Translet translet,
                              java.util.Locale locale)
        Description copied from interface: LocaleResolver
        Set the current locale to the given one.
        Parameters:
        translet - the translet to resolve the locale for
        locale - the new locale, or null to clear the locale
      • setTimeZone

        public void setTimeZone​(Translet translet,
                                java.util.TimeZone timeZone)
        Description copied from interface: LocaleResolver
        Set the current timezone to the given one.
        Parameters:
        translet - the translet to resolve the locale for
        timeZone - the new timezone, or null to clear the timezone
      • parseLocaleValue

        @Nullable
        protected java.util.Locale parseLocaleValue​(java.lang.String localeValue)
        Parse the given locale value coming from an incoming cookie.

        The default implementation calls StringUtils.parseLocale(String), accepting the Locale.toString() format as well as BCP 47 language tags.

        Parameters:
        localeValue - the locale value to parse
        Returns:
        the corresponding Locale instance
        See Also:
        StringUtils.parseLocale(String)
      • toLocaleValue

        protected java.lang.String toLocaleValue​(java.util.Locale locale)
        Render the given locale as a text value for inclusion in a cookie.

        The default implementation calls Locale.toString() or JDK 7's Locale.toLanguageTag(), depending on the "languageTagCompliant" configuration property.

        Parameters:
        locale - the locale to stringify
        Returns:
        a String representation for the given locale
        See Also:
        isLanguageTagCompliant()