Class CookieLocaleResolver
- java.lang.Object
-
- com.aspectran.core.support.i18n.locale.AbstractLocaleResolver
-
- com.aspectran.web.support.i18n.locale.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 Summary
Constructors Constructor Description CookieLocaleResolver()
Create a new instance of theCookieLocaleResolver
class.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description CookieGenerator
getLocaleCookieGenerator()
CookieGenerator
getTimeZoneCookieGenerator()
boolean
isLanguageTagCompliant()
Return whether this resolver's cookies should be compliant with BCP 47 language tags instead of Java's legacy locale specification format.boolean
isRejectInvalidCookies()
Return whether to reject cookies with invalid content (e.g.protected java.util.Locale
parseLocaleValue(java.lang.String localeValue)
Parse the given locale value coming from an incoming cookie.java.util.Locale
resolveLocale(Translet translet)
Resolve the current locale via the given translet.java.util.TimeZone
resolveTimeZone(Translet translet)
Resolve the current timezone via the given translet.void
setCookieDomain(java.lang.String cookieDomain)
Use the given domain for cookies.void
setCookieHttpOnly(boolean cookieHttpOnly)
Set whether the cookie is supposed to be marked with the "HttpOnly" attribute.void
setCookieMaxAge(java.lang.Integer cookieMaxAge)
Use the given maximum age (in seconds) for cookies.void
setCookiePath(java.lang.String cookiePath)
Use the given path for cookies.void
setCookieSecure(boolean cookieSecure)
Set whether the cookie should only be sent using a secure protocol, such as HTTPS (SSL).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.void
setLocale(Translet translet, java.util.Locale locale)
Set the current locale to the given one.void
setLocaleCookieGenerator(CookieGenerator localeCookieGenerator)
void
setRejectInvalidCookies(boolean rejectInvalidCookies)
Specify whether to reject cookies with invalid content (e.g.void
setTimeZone(Translet translet, java.util.TimeZone timeZone)
Set the current timezone to the given one.void
setTimeZoneCookieGenerator(CookieGenerator timeZoneCookieGenerator)
protected java.lang.String
toLocaleValue(java.util.Locale locale)
Render the given locale as a text value for inclusion in a cookie.-
Methods inherited from class com.aspectran.core.support.i18n.locale.AbstractLocaleResolver
determineDefaultLocale, determineDefaultTimeZone, getDefaultLocale, getDefaultTimeZone, getSupportedLocales, setDefaultLocale, setDefaultLocale, setDefaultTimeZone, setDefaultTimeZone, setSupportedLocales, setSupportedLocales
-
-
-
-
Constructor Detail
-
CookieLocaleResolver
public CookieLocaleResolver()
Create a new instance of theCookieLocaleResolver
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 tofalse
for rendering Java's legacy locale specification format. For parsing, this resolver leniently accepts the legacyLocale.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.
-
setRejectInvalidCookies
public void setRejectInvalidCookies(boolean rejectInvalidCookies)
Specify whether to reject cookies with invalid content (e.g. invalid format).The default is
true
. Turn this off for lenient handling of parse failures, falling back to the default locale and time zone in such a case.- See Also:
AbstractLocaleResolver.setDefaultLocale(java.util.Locale)
,AbstractLocaleResolver.setDefaultTimeZone(java.util.TimeZone)
,AbstractLocaleResolver.determineDefaultLocale(com.aspectran.core.activity.Translet)
,AbstractLocaleResolver.determineDefaultTimeZone(com.aspectran.core.activity.Translet)
-
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 forlocale
- the new locale, ornull
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 fortimeZone
- the new timezone, ornull
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 theLocale.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'sLocale.toLanguageTag()
, depending on the"languageTagCompliant"
configuration property.- Parameters:
locale
- the locale to stringify- Returns:
- a String representation for the given locale
- See Also:
isLanguageTagCompliant()
-
-