org.bibeault.frontman
Class CommandContextImplementation

java.lang.Object
  extended by org.bibeault.frontman.CommandContextImplementation
All Implemented Interfaces:
CommandContext

public final class CommandContextImplementation
extends java.lang.Object
implements CommandContext

A context object that maintains the state information for the invocation of a command, as well as making various useful methods available to Command implementations.

The state information includes the current request and response instances, as well as a reference back to the invoking broker.

A CommandContext is considered valid only for the duration of an invocation of the execute() method of the Command interface. A reference to the CommandContext should never be retained beyond the scope of that method, for example, by storing it in a scoped variable.


Constructor Summary
CommandContextImplementation(CommandBroker commandBroker, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
           
 
Method Summary
 java.lang.Object findScopedVariable(java.lang.Object name)
          Finds a scoped variable.
 void forward(java.lang.String path)
          Forwards the current request to the resource at the specified path.
 void forwardToCommand(java.lang.String verb)
          Convenience wrapper around the forward(String) method that forwards to the command specified by the passed command verb.
 void forwardToView(java.lang.String viewName)
          Convenience wrapper around the forward(String) method that forwards to the view specified by the passed view name.
 CommandBroker getCommandBroker()
           
 java.lang.String getPathInfo()
          Returns the path info for the current request.
 javax.servlet.http.HttpServletRequest getRequest()
           
 javax.servlet.http.HttpServletResponse getResponse()
           
 java.lang.Object getScopedVariable(java.lang.Object name, ScopedContext scope)
          Gets a scoped variable from the specified context.
 javax.servlet.ServletContext getServletContext()
           
 javax.servlet.http.HttpSession getSession()
           
 void redirect(java.lang.String location)
          Sends a temporary redirect response to the client using the specified redirect location URL.
 void redirect(java.lang.String location, java.util.Map<java.lang.String,java.lang.String[]> parameters)
          Sends a temporary redirect response to the client using the specified redirect location URL.
 void redirectToCommand(java.lang.String verb)
          Convenience wrapper around the redirect(String) method that redirects to the command specified by the passed command verb.
 void redirectToCommand(java.lang.String verb, java.util.Map<java.lang.String,java.lang.String[]> parameters)
          Convenience wrapper around the redirect(String) method that redirects to the command specified by the passed command verb, with one or more request parameters.
 void redirectToView(java.lang.String viewName)
          Convenience wrapper around the redirect(String) method that redirects to the view specified by the passed view name.
 void redirectToView(java.lang.String viewName, java.util.Map<java.lang.String,java.lang.String[]> parameters)
          Convenience wrapper around the redirect(String) method that redirects to the view specified by the passed view name, with one or more request parameters.
 void sendError(int statusCode)
          Sends an error response to the client with the specified status code.
 void setScopedVariable(java.lang.Object name, java.lang.Object value)
          Sets a scoped variable into the request context.
 void setScopedVariable(java.lang.Object name, java.lang.Object value, ScopedContext scope)
          Sets a scoped variable into the specified context.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CommandContextImplementation

public CommandContextImplementation(CommandBroker commandBroker,
                                    javax.servlet.http.HttpServletRequest request,
                                    javax.servlet.http.HttpServletResponse response)
Method Detail

getCommandBroker

public CommandBroker getCommandBroker()
Specified by:
getCommandBroker in interface CommandContext
Returns:
the Command Broker instance

getRequest

public javax.servlet.http.HttpServletRequest getRequest()
Specified by:
getRequest in interface CommandContext
Returns:
the current servlet request instance.

getResponse

public javax.servlet.http.HttpServletResponse getResponse()
Specified by:
getResponse in interface CommandContext
Returns:
the current response.

getSession

public javax.servlet.http.HttpSession getSession()
Specified by:
getSession in interface CommandContext
Returns:
the current session instance.

getServletContext

public javax.servlet.ServletContext getServletContext()
Specified by:
getServletContext in interface CommandContext
Returns:
the servlet context.

getPathInfo

public java.lang.String getPathInfo()
Returns the path info for the current request. This method is sensitive to requests initiated via an include and will return the path info of the originating request.

Specified by:
getPathInfo in interface CommandContext
Returns:
the determined path info

setScopedVariable

public void setScopedVariable(java.lang.Object name,
                              java.lang.Object value,
                              ScopedContext scope)
                       throws javax.servlet.ServletException,
                              java.lang.NullPointerException
Sets a scoped variable into the specified context.

This is a convenience method that shortens the syntax of setting scoped variables into their contexts as well as providing imporoved semantics.

Specified by:
setScopedVariable in interface CommandContext
Parameters:
name - the name of the scoped variable
value - the value of the scoped variable
scope - one of SCOPE_REQUEST, SCOPE_SESSION or SCOPE_APPLICATION
Throws:
javax.servlet.ServletException - if the specified scope is not a valid value
java.lang.NullPointerException - if name is null

setScopedVariable

public void setScopedVariable(java.lang.Object name,
                              java.lang.Object value)
                       throws javax.servlet.ServletException
Sets a scoped variable into the request context.

This is a convenience method that shortens the syntax of setting scoped variables into request context.

Specified by:
setScopedVariable in interface CommandContext
Parameters:
name - the name of the scoped variable
value - the value of the scoped variable
Throws:
java.lang.NullPointerException - if name is null
javax.servlet.ServletException - if anything else fails

getScopedVariable

public java.lang.Object getScopedVariable(java.lang.Object name,
                                          ScopedContext scope)
                                   throws javax.servlet.ServletException
Gets a scoped variable from the specified context. The name can be proviede as any object type whose string equivalent is used as the name.

Specified by:
getScopedVariable in interface CommandContext
Parameters:
name - the name of the scoped variable
scope - the scoped context in which to look
Returns:
the located object, or null if not found
Throws:
javax.servlet.ServletException - if anything goes awry

findScopedVariable

public java.lang.Object findScopedVariable(java.lang.Object name)
Finds a scoped variable. The name can be proviede as any object type whose string equivalent is used as the name.

Specified by:
findScopedVariable in interface CommandContext
Parameters:
name - the name of the scoped variable
Returns:
the located object, or null if not found

forward

public void forward(java.lang.String path)
             throws java.io.IOException,
                    javax.servlet.ServletException,
                    java.lang.IllegalStateException
Forwards the current request to the resource at the specified path.

The semantics of path are the same as that required by the javax.servlet.RequestDispatcher.forward() method.

Specified by:
forward in interface CommandContext
Parameters:
path - the path of the resource to whcih the request is to be forwarded
Throws:
java.io.IOException - if the request dispatcher throws this exception
javax.servlet.ServletException - if the request dispatcher throws this exception
java.lang.IllegalStateException - if the response is already committed
See Also:
RequestDispatcher

redirect

public void redirect(java.lang.String location)
              throws java.io.IOException,
                     java.lang.IllegalStateException
Sends a temporary redirect response to the client using the specified redirect location URL.

The semantics of location are the same as that required by the javax.servlet.http.HttpServletResponse.sendRedirect() method.

This method automatically ensures that the redirect is properly encoded to ensure that the session id is included in the URL if necessary.

Specified by:
redirect in interface CommandContext
Parameters:
location - the location of the redirect resource
Throws:
java.io.IOException - If an input or output exception occurs
java.lang.IllegalStateException - if the response is already committed, or the location cannot be made into a valid URL
See Also:
HttpServletResponse

redirect

public void redirect(java.lang.String location,
                     java.util.Map<java.lang.String,java.lang.String[]> parameters)
              throws java.io.IOException,
                     java.lang.IllegalStateException
Sends a temporary redirect response to the client using the specified redirect location URL.

The semantics of location are the same as that required by the javax.servlet.http.HttpServletResponse.sendRedirect() method.

This method automatically ensures that the redirect is properly encoded to ensure that the session id is included in the URL if necessary.

Specified by:
redirect in interface CommandContext
Parameters:
location - the location of the redirect resource
parameters - a Map of the request parameters to add the URL as a query string
Throws:
java.io.IOException - If an input or output exception occurs
java.lang.IllegalStateException - if the response is already committed, or the location cannot be made into a valid URL
See Also:
HttpServletResponse

sendError

public void sendError(int statusCode)
               throws java.io.IOException,
                      java.lang.IllegalStateException
Sends an error response to the client with the specified status code.

This method is a thin wrapper around the javax.servlet.http.HttpServletResponse.sendError() method.

Specified by:
sendError in interface CommandContext
Parameters:
statusCode - one of the SC_ codes defined by javax.servlet.http.HttpServletResponse
Throws:
java.io.IOException - If an input or output exception occurs
java.lang.IllegalStateException - if the response is already committed

forwardToCommand

public void forwardToCommand(java.lang.String verb)
                      throws java.io.IOException,
                             javax.servlet.ServletException
Convenience wrapper around the forward(String) method that forwards to the command specified by the passed command verb.

Specified by:
forwardToCommand in interface CommandContext
Parameters:
verb - the verb for the forward command
Throws:
java.io.IOException - see forward(String)
javax.servlet.ServletException - see forward(String)

forwardToView

public void forwardToView(java.lang.String viewName)
                   throws java.io.IOException,
                          javax.servlet.ServletException
Convenience wrapper around the forward(String) method that forwards to the view specified by the passed view name.

Specified by:
forwardToView in interface CommandContext
Parameters:
viewName - the name for the forward view
Throws:
java.io.IOException - see forward(String)
javax.servlet.ServletException - see forward(String)

redirectToCommand

public void redirectToCommand(java.lang.String verb)
                       throws java.io.IOException,
                              javax.servlet.ServletException
Convenience wrapper around the redirect(String) method that redirects to the command specified by the passed command verb.

Specified by:
redirectToCommand in interface CommandContext
Parameters:
verb - the verb for the redirect command
Throws:
java.io.IOException - see redirect(String)
javax.servlet.ServletException - see redirect(String)

redirectToCommand

public void redirectToCommand(java.lang.String verb,
                              java.util.Map<java.lang.String,java.lang.String[]> parameters)
                       throws java.io.IOException,
                              javax.servlet.ServletException
Convenience wrapper around the redirect(String) method that redirects to the command specified by the passed command verb, with one or more request parameters.

Specified by:
redirectToCommand in interface CommandContext
Parameters:
verb - the verb for the redirect command
parameters - a Map of the request parameters to add the URL as a query string
Throws:
java.io.IOException - see redirect(String)
javax.servlet.ServletException - see redirect(String)

redirectToView

public void redirectToView(java.lang.String viewName)
                    throws java.io.IOException,
                           javax.servlet.ServletException
Convenience wrapper around the redirect(String) method that redirects to the view specified by the passed view name.

Specified by:
redirectToView in interface CommandContext
Parameters:
viewName - the name for the redirect view
Throws:
java.io.IOException - see redirect(String)
javax.servlet.ServletException - see redirect(String)

redirectToView

public void redirectToView(java.lang.String viewName,
                           java.util.Map<java.lang.String,java.lang.String[]> parameters)
                    throws java.io.IOException,
                           javax.servlet.ServletException
Convenience wrapper around the redirect(String) method that redirects to the view specified by the passed view name, with one or more request parameters.

Specified by:
redirectToView in interface CommandContext
Parameters:
viewName - the name for the redirect view
parameters - a Map of the request parameters to add the URL as a query string
Throws:
java.io.IOException - see redirect(String)
javax.servlet.ServletException - see redirect(String)