velosurf.web.l10n
Class LocalizationFilter

java.lang.Object
  extended by velosurf.web.l10n.LocalizationFilter
All Implemented Interfaces:
javax.servlet.Filter

public class LocalizationFilter
extends java.lang.Object
implements javax.servlet.Filter

Localization filter. It's goal is to redirect or forward incoming unlocalized http requests (depending on the choosen method, FORWARD or REDIRECT) towards an address taking into account the best match between requested locales and supported locales, and also to deduce the locale from URLS (when REDIRECT is used).

Optional init parameters:

(1) for now, to find supported locales if this parameter is not provided, the filter try to use the rewrite-uri param and to check for the existence of corresponding directories (only if the rewriting string contains a pattern like '/@/', that is if you use directories to store localized sites).

(2) The different match- and rewrite- parameters pairs are mutually exclusive. All matches are case-insensitive.

When the redirect method is used, these supplementary parameters (mutually exclusive) allow the filter to know whether or not an incoming URI is localized.

Author:
Claude Brisson

Field Summary
private  javax.servlet.FilterConfig config
          filter config.
private static java.lang.String defaultInspectUri
          default inspect uri.
private  java.util.Locale defaultLocale
          default locale.
private static java.lang.String defaultMatchUri
          default match uri.
private static java.lang.String defaultRewriteUri
          default rewrite uri.
private static int FORWARD
          forward method constant.
private  java.util.regex.Pattern inspectUri
          inspect uri.
private  int l10nMethod
          localization method.
private  java.util.regex.Pattern matchUri
          match uri.
private static int REDIRECT
          redirect method constant.
private  java.lang.String rewriteUri
          rewrite uri
private static int SECONDS_IN_YEAR
          seconds in year (for setting cookies age).
private  java.util.List<java.util.Locale> supportedLocales
          supported locales.
 
Constructor Summary
LocalizationFilter()
           
 
Method Summary
 void destroy()
          Destroy the filter.
 void doFilter(javax.servlet.ServletRequest servletRequest, javax.servlet.ServletResponse servletResponse, javax.servlet.FilterChain chain)
          Filtering.
private  void findSupportedLocales(javax.servlet.FilterConfig config)
          Find supported locales.
private  java.lang.String getInitParameter(java.lang.String key)
          Helper function.
private  java.lang.String getInitParameter(java.lang.String key, java.lang.String defaultValue)
          Helper function.
private  java.util.Locale getMatchedLocale(java.lang.String candidate)
          get matched locale.
private  java.util.Locale getPreferredLocale(java.util.List<java.util.Locale> requestedLocales)
          Get preferred locale.
private  java.util.List<java.util.Locale> getRequestedLocales(javax.servlet.http.HttpServletRequest request)
          get the list of requested locales.
private  java.util.List<java.util.Locale> guessSupportedLocales(javax.servlet.ServletContext ctx, java.lang.String path)
          Guess supported locales.
 void init(javax.servlet.FilterConfig config)
          initialization.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

config

private javax.servlet.FilterConfig config
filter config.


supportedLocales

private java.util.List<java.util.Locale> supportedLocales
supported locales.


defaultLocale

private java.util.Locale defaultLocale
default locale.


SECONDS_IN_YEAR

private static int SECONDS_IN_YEAR
seconds in year (for setting cookies age).


defaultMatchUri

private static java.lang.String defaultMatchUri
default match uri.


defaultRewriteUri

private static java.lang.String defaultRewriteUri
default rewrite uri.


defaultInspectUri

private static java.lang.String defaultInspectUri
default inspect uri.


matchUri

private java.util.regex.Pattern matchUri
match uri.


rewriteUri

private java.lang.String rewriteUri
rewrite uri


inspectUri

private java.util.regex.Pattern inspectUri
inspect uri.


FORWARD

private static final int FORWARD
forward method constant.

See Also:
Constant Field Values

REDIRECT

private static final int REDIRECT
redirect method constant.

See Also:
Constant Field Values

l10nMethod

private int l10nMethod
localization method.

Constructor Detail

LocalizationFilter

public LocalizationFilter()
Method Detail

init

public void init(javax.servlet.FilterConfig config)
          throws javax.servlet.ServletException
initialization.

Specified by:
init in interface javax.servlet.Filter
Parameters:
config - filter config
Throws:
javax.servlet.ServletException

doFilter

public void doFilter(javax.servlet.ServletRequest servletRequest,
                     javax.servlet.ServletResponse servletResponse,
                     javax.servlet.FilterChain chain)
              throws java.io.IOException,
                     javax.servlet.ServletException
Filtering.

Specified by:
doFilter in interface javax.servlet.Filter
Parameters:
servletRequest - request
servletResponse - response
chain - filter chain
Throws:
java.io.IOException
javax.servlet.ServletException

findSupportedLocales

private void findSupportedLocales(javax.servlet.FilterConfig config)
Find supported locales.

Parameters:
config - filter config

getInitParameter

private java.lang.String getInitParameter(java.lang.String key)
Helper function.

Parameters:
key -
Returns:
init-parameter

getInitParameter

private java.lang.String getInitParameter(java.lang.String key,
                                          java.lang.String defaultValue)
Helper function.

Parameters:
key -
defaultValue -
Returns:
init-parameter

destroy

public void destroy()
Destroy the filter.

Specified by:
destroy in interface javax.servlet.Filter

guessSupportedLocales

private java.util.List<java.util.Locale> guessSupportedLocales(javax.servlet.ServletContext ctx,
                                                               java.lang.String path)
Guess supported locales.

Parameters:
ctx - servlet context
path - path
Returns:
list of locales

getRequestedLocales

private java.util.List<java.util.Locale> getRequestedLocales(javax.servlet.http.HttpServletRequest request)
get the list of requested locales.

Parameters:
request - request
Returns:
list of locales

getMatchedLocale

private java.util.Locale getMatchedLocale(java.lang.String candidate)
get matched locale.

Parameters:
candidate - candidate
Returns:
locale

getPreferredLocale

private java.util.Locale getPreferredLocale(java.util.List<java.util.Locale> requestedLocales)
Get preferred locale.

Parameters:
requestedLocales - requested locales
Returns:
preferred locale


~ooOoo~