org.hd.d.pg2k.webSvr.util
Class LocaleBean

java.lang.Object
  extended by org.hd.d.pg2k.svrCore.LocaleBeanBase
      extended by org.hd.d.pg2k.webSvr.util.LocaleBean
All Implemented Interfaces:
java.io.Serializable

public final class LocaleBean
extends LocaleBeanBase

JavaBean encapsulating notions of locale for JSP/servlet pages. This should be created with request scope.

This puts off loading the properties bundle(s) until necessary, since it can suck CPU cycles.

If the user has a session in progress an explicit locale is looked for there, else the user's locale is obtained from the HTTP request object if the Accept-Languages header is present (which resorts to the server's locale if the user has not specified one), else we attempt to guess a locale given the user's IP address.

Access to this object is thread-safe, though since this object should appear at request (or page) level we do not actually expect threading to be an issue.

This is Serializable so as to be able to be stored in a servlet session; nothing especially long-lived or sensitive.

See Also:
Serialized Form

Field Summary
private static boolean ALLOW_LOCALE_GUESS_FROM_IP_ADDR
          If true then try to guess locale from user's IP address if no explicit locale indicated in HTTP request.
private  javax.servlet.http.HttpServletRequest cachedRequest
          Cached request object.
private static boolean LOG_MULTI_LOCALE_CLIENTS
          If true then note when we seem to be visited by a client with multiple locale preferences set.
private static WebUtils.ServletLoggerWithFallback logger
          Our logger which falls back to System.out if servlet log not available; never null.
private static long serialVersionUID
          Unique Serialisation class ID generated by http://random.hd.org/.
private static boolean SHOW_SERVER_ACCENT
          If true, we may default the locale to that of the server location rather than the client, if no explicit locale is specified.
private static StatsLogger.StatsConfig statsIDLang
          The stats set to which we log HTTP-client preferred language code counts.
private static StatsLogger.StatsConfig statsIDLangExplicit
          The stats set to which we log HTTP-client explicitly-set language code counts.
 
Fields inherited from class org.hd.d.pg2k.svrCore.LocaleBeanBase
SAFE_LOCALE
 
Constructor Summary
LocaleBean()
          Public no-arg constructor for ease of use as a JavaBean.
LocaleBean(java.util.Locale l)
          Public no-arg constructor for ease of use as a JavaBean.
 
Method Summary
 void setRequest(javax.servlet.http.HttpServletRequest request, javax.servlet.ServletContext ctxt)
          Set the current HTTP request to initialise the bean.
 
Methods inherited from class org.hd.d.pg2k.svrCore.LocaleBeanBase
getCommonResourceBundle, getLocale, getLocalisedMessage, getLocalisedMessage, setLocale, toString, validateObject
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

SHOW_SERVER_ACCENT

private static final boolean SHOW_SERVER_ACCENT
If true, we may default the locale to that of the server location rather than the client, if no explicit locale is specified. This may help SEs distinguish between different mirrors which may have some different i18n-ed content to offer.

This is only in cases where no locale is specified, usually by SEs, whose spiders are predominantly in the US.

See Also:
Constant Field Values

LOG_MULTI_LOCALE_CLIENTS

private static final boolean LOG_MULTI_LOCALE_CLIENTS
If true then note when we seem to be visited by a client with multiple locale preferences set. Probably very unusual, but if it becomes common we should handle it well.

See Also:
Constant Field Values

logger

private static final WebUtils.ServletLoggerWithFallback logger
Our logger which falls back to System.out if servlet log not available; never null.


statsIDLang

private static final StatsLogger.StatsConfig statsIDLang
The stats set to which we log HTTP-client preferred language code counts. This is a set of two-letter ISO 639 lower-case codes, with the default being the server's locale.

We attempt to ignore requests from spiders, ie we try to log requests only from live humans.

We may enable or disable this logging (etc) from system parameters, but this is nonetheless our unique identifier.


statsIDLangExplicit

private static final StatsLogger.StatsConfig statsIDLangExplicit
The stats set to which we log HTTP-client explicitly-set language code counts. This is a set of two-letter ISO 639 lower-case codes.

We attempt to ignore requests from spiders, ie we try to log requests only from live humans.

We may enable or disable this logging (etc) from system parameters, but this is nonetheless our unique identifier.


ALLOW_LOCALE_GUESS_FROM_IP_ADDR

private static final boolean ALLOW_LOCALE_GUESS_FROM_IP_ADDR
If true then try to guess locale from user's IP address if no explicit locale indicated in HTTP request.

See Also:
Constant Field Values

cachedRequest

private transient volatile javax.servlet.http.HttpServletRequest cachedRequest
Cached request object. Marked volatile so as to be safe to access without a lock.


serialVersionUID

private static final long serialVersionUID
Unique Serialisation class ID generated by http://random.hd.org/.

See Also:
Constant Field Values
Constructor Detail

LocaleBean

public LocaleBean()
Public no-arg constructor for ease of use as a JavaBean. Sets to a safe locale.


LocaleBean

public LocaleBean(java.util.Locale l)
Public no-arg constructor for ease of use as a JavaBean. This defers as much work as it reasonably can.

Parameters:
l - initial locale; never null
Method Detail

setRequest

public void setRequest(javax.servlet.http.HttpServletRequest request,
                       javax.servlet.ServletContext ctxt)
Set the current HTTP request to initialise the bean. This allows us to retrieve any locale information that we need.

This first looks to see if there is a session containing a locale-override value; if so then it is used. No session is created if none exists.

Else, this looks for a locale set by the user's browser, and uses that if present.

Else, this tried to guess a suitable locale from the user's IP address, using the IP-to-location tools.

Else a `safe' locale is used, usually the server's.

If the request is null or some other invalid condition is encountered then this resets the local to a safe value (usually the server's locale).

If the new cached request is identical to the old one, then nothing is done.

Parameters:
request - if non-null is used to try to determine the user's locale
ctxt - if non-null selects the servlet log to write to (if null, output will go to System.out)

DHD Multimedia Gallery V1.57.21

Copyright (c) 1996-2011, Damon Hart-Davis. All rights reserved.