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

java.lang.Object
  extended by org.hd.d.pg2k.webSvr.util.SessionVarBean
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable

public final class SessionVarBean
extends java.lang.Object
implements java.io.Serializable, java.lang.Cloneable

JavaBean to hold explicit session variables such as user-selected-locale. Error tolerant on being set (should be able to be done by a filter using reflection) and can spit out a set of non-default values to adjust or pass to another server instance.

Names of all (visible) properties start "sessionVar" to avoid collision with other HTML (GET/POST) parameter users.

Not thread-safe; access to an instance in a session should be synchronised on the session object.

See Also:
Serialized Form

Field Summary
static java.lang.String ATTR_NAME_SVB
          Name of SessionVarBean instance in HttpSession.
static java.lang.String KEY_sessionVar_CLEAR
          HTTP tag/parameter we use to indicate that session variable should be set to defaults.
static java.lang.String KEY_sessionVarLiteUI
           
static java.lang.String KEY_sessionVarLocale
           
private  int rndToken
          Immutable random token (shared with any clones/copies to save time) for cache-busting in URLs.
private static long serialVersionUID
          Unique Serialisation class ID generated by http://random.hd.org/.
private  boolean sessionVarLiteUI
          Lite UI; if true we should run a "lite" UI for users on slow modems, etc, else full UI.
private  java.util.Locale sessionVarLocale
          Session locale: if non-null this overrides any locale information from elsewhere.
 
Constructor Summary
SessionVarBean()
           
 
Method Summary
 java.lang.Object clone()
          Make an independent clone of this object.
 SessionVarBean copy()
          Make an independent clone of this object, returning the correct type; never null.
static SessionVarBean getExtantSessionVars(javax.servlet.http.HttpServletRequest request, boolean create)
          Get (copy of) any existing value from the HTTP session; null if no session values and create is false.
 java.util.SortedMap<java.lang.String,java.lang.String> getNonDefaultValues()
          Get sorted map of attribute names to values for all non-default values; never null.
 java.util.Locale getSessionVarLocale()
           
 boolean isSessionVarLiteUI()
           
 java.lang.String makeSessionVarSetURL(javax.servlet.http.HttpServletRequest request, boolean attemptToPinToMirror, DataSourceBean vars)
          Generate new URL to set the desired state; never null.
 SessionVarBean setSessionVarLiteUI(boolean sessionVarLiteUI)
           
 SessionVarBean setSessionVarLocale(java.util.Locale sessionVarLocale)
           
static void updateSessionVarsFromRequest(javax.servlet.http.HttpServletRequest request)
          Update session vars from request, saving result back to session if need be.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

KEY_sessionVar_CLEAR

public static final java.lang.String KEY_sessionVar_CLEAR
HTTP tag/parameter we use to indicate that session variable should be set to defaults. The parameter with this name can be given a random value as a "cache buster".

When this is set, session variables should be set to all defaults and then set to whatever other actual values are present, meaning that only changes from defaults need be sent.

This parameter is set to a fixed value for spiders to avoid presenting them with a spurious vast URI space to index.

See Also:
Constant Field Values

rndToken

private final int rndToken
Immutable random token (shared with any clones/copies to save time) for cache-busting in URLs.


sessionVarLiteUI

private boolean sessionVarLiteUI
Lite UI; if true we should run a "lite" UI for users on slow modems, etc, else full UI. Full UI (flag off) is the default.


KEY_sessionVarLiteUI

public static final java.lang.String KEY_sessionVarLiteUI
See Also:
Constant Field Values

sessionVarLocale

private java.util.Locale sessionVarLocale
Session locale: if non-null this overrides any locale information from elsewhere. No override (null) is the default.

Note that this field can only be set to Locales with a valid language code (ie two-letter lowercase) and an optional valid country code (ie two-letter uppercase) and no variant and that are found in our full supported-locales list. Note thae we do this validation since the Locale object does not (SHAME)!

We ignore attempts to set to an unsupported/unsafe/broken locale.

We rely on immutability of the Locale object here.


KEY_sessionVarLocale

public static final java.lang.String KEY_sessionVarLocale
See Also:
Constant Field Values

ATTR_NAME_SVB

public static final java.lang.String ATTR_NAME_SVB
Name of SessionVarBean instance in HttpSession.

See Also:
Constant Field Values

serialVersionUID

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

See Also:
Constant Field Values
Constructor Detail

SessionVarBean

public SessionVarBean()
Method Detail

isSessionVarLiteUI

public boolean isSessionVarLiteUI()

setSessionVarLiteUI

public SessionVarBean setSessionVarLiteUI(boolean sessionVarLiteUI)

getSessionVarLocale

public java.util.Locale getSessionVarLocale()

setSessionVarLocale

public SessionVarBean setSessionVarLocale(java.util.Locale sessionVarLocale)

clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException
Make an independent clone of this object.

Overrides:
clone in class java.lang.Object
Throws:
java.lang.CloneNotSupportedException

copy

public SessionVarBean copy()
Make an independent clone of this object, returning the correct type; never null. Typically used to get a modified version of the current state to form a URL to set the new value.

Returns:
a non-null independent clone/copy of this instance

updateSessionVarsFromRequest

public static void updateSessionVarsFromRequest(javax.servlet.http.HttpServletRequest request)
Update session vars from request, saving result back to session if need be. This should be called early enough (before output is committed) that a session can be created if need be.

This should probably only be routinely applied to GET operations where it cannot accidentally gobble data such as in a file-upload POST.

If a "clear" tag is present then we ignore extant values and start from defaults.


makeSessionVarSetURL

public java.lang.String makeSessionVarSetURL(javax.servlet.http.HttpServletRequest request,
                                             boolean attemptToPinToMirror,
                                             DataSourceBean vars)
Generate new URL to set the desired state; never null. This generates a URL based on the current one with GET parameters added to set session values to those in this instance.

For brevity/efficiency of result, this returns the shortest unambiguous relative or full URL that can be used as an href in the current page to get to the desired page with the desired session values set.

This may drop any query component of the original URL.

This may drop or preserve any fragment/ref component of the original URL.

This involves resetting all implicit session values to default and then explicitly setting all non-default values with query parameters.

This also adds a "cache-busting" random component to help ensure that the browser or a proxy does not just replay an old value from cache. This is given a fixed value if the request appears to be from a spider to avoid creating a huge virtual page space.

This can optionally force a diversion to an optimal mirror for the current site, helping to ensure that the user's browser is correctly "pinned" to a server and thus will not "lose" the state (and will probably give better performance for the user and be cheaper to serve too).

Returns:
relative or full URL based on the current request; never null

getExtantSessionVars

public static SessionVarBean getExtantSessionVars(javax.servlet.http.HttpServletRequest request,
                                                  boolean create)
Get (copy of) any existing value from the HTTP session; null if no session values and create is false. This is suitable to then override with user-specified values as required.

Note that this returns a private copy of any value held in the session.

Parameters:
request - HTTP request object; never null
create - if true and session or session variable is missing then createand return a new all-default value, else if false and session or session variable is missing then return null

getNonDefaultValues

public java.util.SortedMap<java.lang.String,java.lang.String> getNonDefaultValues()
Get sorted map of attribute names to values for all non-default values; never null. Values are given as an HTML/HTTP-safe String.

If the result is not empty then at least one value is non-default and we should probably save this in the session (creating a session if need be).

The map is sorted to so that it is easier to dump keys into HTML in sorted order and thus help compression if we are doing any.

Returns:
non-null sorted map from attribute name to String representation of value

DHD Multimedia Gallery V1.57.21

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