- All Implemented Interfaces:
Filter
Each URL (except javascript:, mailto:, telnet:, tel:, cid:, file:, and data: schemes, case-insensitive) may be rewritten
using encodeURL to include a paramName
parameter. URLs to
non-localized resources are not rewritten.
During each request, the locale will be set according to:
- If
paramName
parameter exists and is a supported locale, then set response locale to the exact match. - If
paramName
parameter exists and can match a supported locale, then set response locale to the matched locale. - Otherwise, select the best locale from the Accept headers.
Also sets the JSTL fmt tag locale via the AttributeEE.Jstl.FMT_LOCALE
request attribute
to the current locale.
Also sets the ThreadLocale.
If the request is a GET request, is not in the DispatcherType.ERROR
dispatcher, and the request parameter is
missing, invalid, or does not match the final resolved locale, the client is redirected to the
URL including the paramName parameter. This is to help avoid possible duplicate content penalties for
search engines.
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionprotected static class
Records a locale and how matched. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
destroy()
void
doFilter
(ServletRequest request, ServletResponse response, FilterChain chain) protected Locale
getBestLocale
(HttpServletRequest request, Map<String, Locale> supportedLocales) Performs the language negotiation based on the Accept-Language header(s).protected LocaleFilter.MatchedLocale
getBestMatch
(Map<String, Locale> supportedLocales, String acceptLanguage) Resolves the best supported language for the given language, country, and variant.protected abstract Locale
getDefaultLocale
(ServletRequest request, Map<String, Locale> supportedLocales) Gets the default locale to be used when a best locale cannot be resolved.getEnabledLocales
(ServletRequest request) Gets the set of enabled locales for the provided request.protected String
Gets the name of the parameter that will contain the locale.getSupportedLocales
(ServletRequest request) Gets the supported locales as a mapping of localeString to locale.void
init
(FilterConfig config) protected boolean
isLocalizedPath
(IRI iri) Checks if the locale parameter should be added to the given URL.protected boolean
isLocalizedPath
(String url) CallsisLocalizedPath(com.aoapps.net.IRI)
after wrapping in a newIRI
.protected String
toLocaleString
(Locale locale) Gets a string representation of the given locale.
-
Constructor Details
-
LocaleFilter
public LocaleFilter()
-
-
Method Details
-
init
- Specified by:
init
in interfaceFilter
- Throws:
ServletException
-
getEnabledLocales
Gets the set of enabled locales for the provided request. This must be called from a request that has already been filtered through LocaleFilter. When container's default locale is used, will return an empty map.- Returns:
- The mapping from localeString to locale
-
doFilter
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException - Specified by:
doFilter
in interfaceFilter
- Throws:
IOException
ServletException
-
destroy
public void destroy() -
isLocalizedPath
Checks if the locale parameter should be added to the given URL.This default implementation will cause the parameter to be added to any URL that is not one of the excluded extensions (case-insensitive).
-
isLocalizedPath
CallsisLocalizedPath(com.aoapps.net.IRI)
after wrapping in a newIRI
. -
toLocaleString
Gets a string representation of the given locale. This default implementation only supports language, country, and variant. Country will only be added when language present. Variant will only be added when both language and country are present. -
getBestLocale
protected Locale getBestLocale(HttpServletRequest request, Map<String, Locale> supportedLocales) throws ServletExceptionPerforms the language negotiation based on the Accept-Language header(s).- Throws:
ServletException
-
getBestMatch
protected LocaleFilter.MatchedLocale getBestMatch(Map<String, Locale> supportedLocales, String acceptLanguage) Resolves the best supported language for the given language, country, and variant.- Exact match on language, country, and variant
- Match on language and country
- Match language
- null
-
getParamName
Gets the name of the parameter that will contain the locale.- See Also:
-
getSupportedLocales
protected abstract Map<String,Locale> getSupportedLocales(ServletRequest request) throws ServletException Gets the supported locales as a mapping of localeString to locale. The map key must be consistent withtoLocaleString
for each supported locale.If no specific locales are supported, and the responses should remain in the container's default locale, may return an empty map.
When less than two locales are supported, the URLs will not be rewritten and any paramName parameter will be stripped from incoming requests.
- Throws:
ServletException
- See Also:
-
getDefaultLocale
protected abstract Locale getDefaultLocale(ServletRequest request, Map<String, Locale> supportedLocales) throws ServletExceptionGets the default locale to be used when a best locale cannot be resolved. This will never be called whengetSupportedLocales
returns an empty map.This must be one of the supported locales.
- Throws:
ServletException
- See Also:
-