Class LocaleFilter

java.lang.Object
com.aoapps.servlet.filter.LocaleFilter
All Implemented Interfaces:
Filter

public abstract class LocaleFilter extends Object implements Filter
Maintains the user Locale through URL rewriting. The URL rewriting is preferred since it will allow search engines to properly crawl the site per-locale and does not depend on cookies.

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:

  1. If paramName parameter exists and is a supported locale, then set response locale to the exact match.
  2. If paramName parameter exists and can match a supported locale, then set response locale to the matched locale.
  3. 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:
  • Constructor Details

    • LocaleFilter

      public LocaleFilter()
  • Method Details

    • init

      public void init(FilterConfig config) throws ServletException
      Specified by:
      init in interface Filter
      Throws:
      ServletException
    • getEnabledLocales

      public static Map<String,Locale> getEnabledLocales(ServletRequest request)
      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 interface Filter
      Throws:
      IOException
      ServletException
    • destroy

      public void destroy()
      Specified by:
      destroy in interface Filter
    • isLocalizedPath

      protected boolean isLocalizedPath(IRI iri)
      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

      protected boolean isLocalizedPath(String url)
      Calls isLocalizedPath(com.aoapps.net.IRI) after wrapping in a new IRI.
    • toLocaleString

      protected String toLocaleString(Locale locale)
      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 ServletException
      Performs 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.
      1. Exact match on language, country, and variant
      2. Match on language and country
      3. Match language
      4. null
    • getParamName

      protected String getParamName()
      Gets the name of the parameter that will contain the locale.
      See Also:
      • DEFAULT_PARAM_NAME
    • 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 with toLocaleString 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 ServletException
      Gets the default locale to be used when a best locale cannot be resolved. This will never be called when getSupportedLocales returns an empty map.

      This must be one of the supported locales.

      Throws:
      ServletException
      See Also: