com.revusky.niggle.servlet
Class ServletInteraction

java.lang.Object
  |
  +--com.revusky.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.

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 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.
protected  java.lang.String sessionID
          The session ID.
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)
           
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. wrapper for the ServletRequest's getParameter method returns "" instead of null if the param is not found, avoids annoying glitches.
 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.
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()
          default method to tell the user she's not authorized.
 
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.
Constructor Detail

ServletInteraction

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

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

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.

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.

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.

unauthorized

protected void unauthorized()
                     throws java.io.IOException
default method to tell the user she's not authorized. Subclass might override this and put up a login or signup page in order to be more graceful about things.

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.

execDefault

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

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 convenience method to expose all of the servlet request's parameters as template variables

exposeActionURL

protected void exposeActionURL(java.lang.String action)

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.