com.niggle.servlet
Class ServletInteraction

java.lang.Object
  |
  +--com.niggle.servlet.ServletInteraction
All Implemented Interfaces:
NiggleConstants

public abstract class ServletInteraction
extends java.lang.Object
implements NiggleConstants

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.

Author:
Jonathan Revusky

Field Summary
protected  java.lang.String action
          name of the current action being executed by this ServletInteraction instance
 NiggleConfig config
          The information that contains the niggle servlet config info.
protected  boolean hasRedirected
          Indicates whether we handled the request by redirecting.
protected  java.util.Locale locale
          locale of the server, ideally of the user
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  boolean sessionCreated
          Did we create a new session? used for logging purposes.
protected  java.lang.String sessionID
          numeric session id.
protected  java.lang.String suppInfoParam
          a parameter from the servlet request used to hold supplemental information
protected  java.lang.String userID
          Login ID of the current user.
 
Fields inherited from interface com.niggle.servlet.NiggleConstants
ACTION_KEY, ERROR_TEMPLATE, LOGIN_TEMPLATE, LOGOUT_CONFIRM_TEMPLATE, MESSAGE_TEMPLATE, PASSWORD_KEY, SESSION_ID_KEY, SESSION_MANAGER_KEY, SESSION_STORE_FILENAME, SUPP_INFO_PARAM_KEY, USER_ID_KEY
 
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  void createNewSession()
           
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".
protected  void displayError(java.lang.String nextAction, java.lang.String title, java.lang.String message)
           
protected  void displayLogin()
          Throw up the login page template,
protected  void displayMessage(java.lang.String nextAction, java.lang.String title, java.lang.String message)
           
 void execDefault()
          default handler for requests in which no action is defined.
 void execLogin()
          default handler for the "login" action.
 void execLogout()
          default logout handler.
 void execStatic()
          A default handler for "action=static", which will simply display a static page.
protected  void exposeDefaultVariables()
          A hook that can be overridden to make sure that certain top-level template variables get exposed.
 java.util.Locale getLocale()
           
protected  Page getPage(java.lang.String template)
           
 java.lang.String getParameter(java.lang.String s)
          Check for presence of data field in CGI input.
 java.lang.String getURL(java.lang.String action)
          Builds complete URL with query string corresponding to the given action and current context.
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  void invalidateSession()
           
protected  void output()
          Finally output the page as a result of a servlet interaction We also touch the user's session if there is a sessionID.
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 convenient method that exposes all of the servlet request's parameters as template variables
 
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.

sessionID

protected java.lang.String sessionID
numeric session id.

sessionCreated

protected boolean sessionCreated
Did we create a new session? used for logging purposes.

userID

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

suppInfoParam

protected java.lang.String suppInfoParam
a parameter from the servlet request used to hold supplemental information

locale

protected java.util.Locale locale
locale of the server, ideally of the user

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.
Constructor Detail

ServletInteraction

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

getLocale

public final java.util.Locale getLocale()

displayMessage

protected void displayMessage(java.lang.String nextAction,
                              java.lang.String title,
                              java.lang.String message)
                       throws java.io.IOException

displayError

protected void displayError(java.lang.String nextAction,
                            java.lang.String title,
                            java.lang.String message)
                     throws java.io.IOException

displayLogin

protected void displayLogin()
                     throws java.io.IOException
Throw up the login page template,

getURL

public java.lang.String getURL(java.lang.String action)
Builds complete URL with query string corresponding to the given action and current context. e.g. action=home&username=joe&sid=23213

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.

invalidateSession

protected void invalidateSession()
                          throws java.io.IOException

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 We also touch the user's session if there is a sessionID.

createNewSession

protected void createNewSession()
                         throws 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, present a login page.
Returns:
true/false indicating whether the login/session info is present. NB: If no session manager has been initialized for this servlet, this method just returns true.

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.

execLogin

public void execLogin()
               throws java.io.IOException
default handler for the "login" action. Simply throws up a login page.

execLogout

public void execLogout()
                throws java.io.IOException
default logout handler.

execStatic

public void execStatic()
                throws java.io.IOException
A default handler for "action=static", which will simply display a static page. Actually, the page is mostly static, insofar as the exposeDefaultVariables() method still gets called, so the default template variables for the servlet are still available to the people doing the GUI templates.

execDefault

public void execDefault()
                 throws java.io.IOException
default handler for requests in which no action is defined. This base implementation assumes the "static" action. see #execStatic()

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.

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.

reexposeServletParams

protected void reexposeServletParams()
A convenient method that exposes all of the servlet request's parameters as template variables

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 false. Subclasses will likely override this.

dispatch

protected void dispatch()
                 throws java.io.IOException
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".

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.