org.niggle.servlet
Class ServletInteraction

java.lang.Object
  |
  +--org.niggle.servlet.ServletInteraction

public abstract class ServletInteraction
extends java.lang.Object

An abstract class that encapsulates the various paths of execution involved in fulfilling a servlet request. What is here is meant to be generic to the "typical" web app. Application-specific functionality will be supplied by concrete subclasses. Here are some additional notes about how to extend this class in your application.

Author:
Jonathan Revusky

Field Summary
protected  java.lang.String action
          name of the current action being executed by this ServletInteraction instance
static java.lang.String ACTION_KEY
           
static java.lang.String ANON
           
 NiggleConfig config
          The information that contains the niggle servlet config info.
static java.lang.String DEFAULT_TEMPLATE
           
static java.lang.String EXEC_PREFIX
           
protected  boolean hasRedirected
          Indicates whether we handled the request by redirecting.
protected  boolean isStaticResponse
          If this variable is unset, the HTTP response will contain a hint that indicates not to cache the page.
protected  java.util.Locale locale
          locale associated with this request
protected  Page page
          The Page template object that we are working with.
 javax.servlet.http.HttpServletRequest request
           
 javax.servlet.http.HttpServletResponse response
          Our HTML response back to the user.
protected  javax.servlet.http.HttpSession session
          Our session, assuming we have one, and we are using the servlet womb's default session-tracking scheme.
static java.lang.String SESSION_ID_KEY
           
protected  java.lang.String sessionID
          The session ID.
static java.lang.String USER_ID_KEY
           
protected  java.lang.String userID
          Login ID of the current user.
 
Constructor Summary
ServletInteraction(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, NiggleConfig config)
           
 
Method Summary
protected  boolean checkSessionInfo()
          Method to call if a given action requires a valid session.
protected  boolean createNewSession()
          Method that creates a new session.
protected  void deduceAction()
          A hook that you might want to override if you want to change the default scheme for how we decide what the current action is.
protected  void deduceLocale()
          A hook that you might want to override if you want to be more sophisticated about how you decide what the preferred locale for the request is.
protected  void deduceSessionInfo()
          A hook that you might want to override if you want to change the default scheme for session tracking.
protected  void dispatch()
          Dispatches the request based on the action This actually delegates to the NiggleConfig object that does some reflective magic to invoke the method this.execFoo() for action "foo".
 void execDefault()
          A default handler that simply displays the default template, index.nhtml.
protected  void exposeActionURL(java.lang.String action)
          a convenience for exposing the URL corresponding to an action on the page.
protected  void exposeDefaultVariables()
          A hook that can be overridden to make sure that certain top-level template variables get exposed.
 void fillInFields(Record rec)
          Given a record, attempts to fill in the record's fields based on the information in the servlet request.
 OreoDataSource getDataSource(java.lang.String name)
          convenient wrapper to get a data source by name.
 java.util.Locale getLocale()
           
 Record getNewRecord(java.lang.String type)
          convenient wrapper to get a record exemplar data source by name.
protected  Page getPage(java.lang.String template)
           
 java.lang.String getParameter(java.lang.String s)
          Check for presence of data field in CGI input.
static ServletInteraction getServletInteraction()
           
 java.lang.String getURL(java.lang.String action)
          Builds the URL that corresponds to a given action.
static boolean hasContent(java.lang.String s)
          check for empty strings.
 boolean hasParameter(java.lang.String param)
           
protected  boolean hasSession()
          Indicates whether we have a session.
protected  boolean hasValidLoginInfo()
          Override this method to check whether the servlet request has the info for a login (thus allowing us to create a new session).
protected  java.lang.String methodNameFromAction(java.lang.String action)
          Override this if you want to change the default execXXX naming scheme.
protected  void navigationError()
          A convenient method to override if you want to handle this kind of condition more gracefully.
 java.lang.String newTransaction(java.lang.String dsName, java.lang.String lookupName)
          convenient wrapper that returns a unique transaction ID for transactions against a datasource that span multiple requests.
protected  void output()
          Finally output the page as a result of a servlet interaction
protected  void recover(java.io.IOException e)
          override this method to provide some default exception handling for when an execXXX method fails for whatever reason.
protected  void redirectAction(java.lang.String action)
          redirects the browser to the URL corresponding to a given action.
protected  void reexposeServletParams()
          A convenience method to expose all of the servlet request's parameters as template variables
protected  void unauthorized()
          Override this method to handle this condition more gracefully.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

request

public final javax.servlet.http.HttpServletRequest request

response

public final javax.servlet.http.HttpServletResponse response
Our HTML response back to the user.


config

public final NiggleConfig config
The information that contains the niggle servlet config info.


session

protected javax.servlet.http.HttpSession session
Our session, assuming we have one, and we are using the servlet womb's default session-tracking scheme.


sessionID

protected java.lang.String sessionID
The session ID. If this is unset, we have no session.


userID

protected java.lang.String userID
Login ID of the current user.


locale

protected java.util.Locale locale
locale associated with this request


action

protected java.lang.String action
name of the current action being executed by this ServletInteraction instance


page

protected Page page
The Page template object that we are working with.


hasRedirected

protected boolean hasRedirected
Indicates whether we handled the request by redirecting. A bit hacky.


isStaticResponse

protected boolean isStaticResponse
If this variable is unset, the HTTP response will contain a hint that indicates not to cache the page. Set this to true to override this behavior. (Perhaps needless to say, this must be done before outputPage() is called.


ACTION_KEY

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

SESSION_ID_KEY

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

USER_ID_KEY

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

EXEC_PREFIX

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

DEFAULT_TEMPLATE

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

ANON

public static final java.lang.String ANON
See Also:
Constant Field Values
Constructor Detail

ServletInteraction

public ServletInteraction(javax.servlet.http.HttpServletRequest request,
                          javax.servlet.http.HttpServletResponse response,
                          NiggleConfig config)
                   throws java.io.IOException
Method Detail

getServletInteraction

public static ServletInteraction getServletInteraction()

deduceLocale

protected void deduceLocale()
A hook that you might want to override if you want to be more sophisticated about how you decide what the preferred locale for the request is.


deduceAction

protected void deduceAction()
A hook that you might want to override if you want to change the default scheme for how we decide what the current action is. If you override this, you will probably have to override the getURL() method also.

See Also:
getURL(java.lang.String)

deduceSessionInfo

protected void deduceSessionInfo()
A hook that you might want to override if you want to change the default scheme for session tracking. The default base implementation simply piggy-backs on what the servlet runner provides.


methodNameFromAction

protected java.lang.String methodNameFromAction(java.lang.String action)
Override this if you want to change the default execXXX naming scheme. There is probably little practical reason to do this.


getLocale

public final java.util.Locale getLocale()
Returns:
the preferred locale of this servlet request.

getURL

public java.lang.String getURL(java.lang.String action)
Builds the URL that corresponds to a given action. You might want to override this to automatically put more information in a URL's query string.


getParameter

public java.lang.String getParameter(java.lang.String s)
Check for presence of data field in CGI input. wrapper for the ServletRequest's getParameter method returns "" instead of null if the param is not found, avoids annoying glitches.


hasParameter

public boolean hasParameter(java.lang.String param)
Returns:
whether the param is present in the servlet request.

hasContent

public static boolean hasContent(java.lang.String s)
check for empty strings.

Returns:
true if string at least one char (possibly blank).

output

protected void output()
               throws java.io.IOException
Finally output the page as a result of a servlet interaction

java.io.IOException

createNewSession

protected boolean createNewSession()
                            throws java.io.IOException
Method that creates a new session. This is a convenient method to override or extend if you want certain things to happen right after a login, say.

java.io.IOException

checkSessionInfo

protected boolean checkSessionInfo()
                            throws java.io.IOException
Method to call if a given action requires a valid session. NB: If there is no valid session info, but there is enough info in the request to create a session (typically id/password or possibly a cookie) then create a new session. Otherwise, the user is unauthorized.

Returns:
true/false indicating whether the login/session info is present.
java.io.IOException

exposeDefaultVariables

protected void exposeDefaultVariables()
                               throws java.io.IOException
A hook that can be overridden to make sure that certain top-level template variables get exposed.

java.io.IOException

unauthorized

protected void unauthorized()
                     throws java.io.IOException
Override this method to handle this condition more gracefully. The default base implementation simply sends a 401 error code. Your subclass might override this and put up a login or signup page in order to be more graceful about things.

java.io.IOException

navigationError

protected void navigationError()
                        throws java.io.IOException
A convenient method to override if you want to handle this kind of condition more gracefully. The default base implementation sends a 404 error code.

java.io.IOException

execDefault

public void execDefault()
                 throws java.io.IOException
A default handler that simply displays the default template, index.nhtml.

java.io.IOException

getPage

protected Page getPage(java.lang.String template)
                throws java.io.IOException
Returns:
the Page object for a given template. delegates to the PageFactory object.
java.io.IOException

hasSession

protected boolean hasSession()
Indicates whether we have a session.


redirectAction

protected void redirectAction(java.lang.String action)
                       throws java.io.IOException
redirects the browser to the URL corresponding to a given action.

java.io.IOException

reexposeServletParams

protected void reexposeServletParams()
A convenience method to expose all of the servlet request's parameters as template variables


exposeActionURL

protected void exposeActionURL(java.lang.String action)
a convenience for exposing the URL corresponding to an action on the page. The URL for action=foo gets exposed as ${fooURL}


hasValidLoginInfo

protected boolean hasValidLoginInfo()
                             throws java.io.IOException
Override this method to check whether the servlet request has the info for a login (thus allowing us to create a new session). In this base implementation, this always returns true and sets the userID to "anon". Subclasses will likely override this.

java.io.IOException

dispatch

protected void dispatch()
                 throws java.io.IOException,
                        javax.servlet.ServletException
Dispatches the request based on the action This actually delegates to the NiggleConfig object that does some reflective magic to invoke the method this.execFoo() for action "foo".

java.io.IOException
javax.servlet.ServletException

recover

protected void recover(java.io.IOException e)
                throws java.io.IOException
override this method to provide some default exception handling for when an execXXX method fails for whatever reason. NB: This default implementation simply rethrows the exception.

java.io.IOException

getDataSource

public OreoDataSource getDataSource(java.lang.String name)
                             throws java.io.IOException
convenient wrapper to get a data source by name.

java.io.IOException

newTransaction

public java.lang.String newTransaction(java.lang.String dsName,
                                       java.lang.String lookupName)
                                throws java.io.IOException
convenient wrapper that returns a unique transaction ID for transactions against a datasource that span multiple requests.

Parameters:
dsName - the lookup name for the transactional data source we are working against.
lookupName - the name by which to store the txnID in the session context. If this is null, we do not store it.
Returns:
a unique transactionID that is a handle for this transaction.
java.io.IOException

getNewRecord

public Record getNewRecord(java.lang.String type)
                    throws java.io.IOException
convenient wrapper to get a record exemplar data source by name.

java.io.IOException

fillInFields

public void fillInFields(Record rec)
                  throws java.io.IOException
Given a record, attempts to fill in the record's fields based on the information in the servlet request. requires that the record be mutable Generic heuristics for filling in a data record based on key=value pairs in a ServletRequest object. In practice, this method may not be enough and you may need to extend it to write some custom code to fill in your records from a servlet request. Nonetheless, this method provides a good starting point.

Parameters:
rec - the record to fill in.
java.io.IOException