com.opensymphony.webwork.dispatcher
Class ServletDispatcher

java.lang.Object
  extended byjavax.servlet.GenericServlet
      extended byjavax.servlet.http.HttpServlet
          extended bycom.opensymphony.webwork.dispatcher.ServletDispatcher
All Implemented Interfaces:
Serializable, Servlet, ServletConfig, WebWorkStatics
Direct Known Subclasses:
CoolUriServletDispatcher

public class ServletDispatcher
extends HttpServlet
implements WebWorkStatics

Main dispatcher servlet in WebWork2 which acts as the controller in the MVC paradigm.

When a request enters the servlet the following things will happen:

  1. The action name is parsed from the servlet path (i.e., /foo/bar/MyAction.action -> MyAction).
  2. A context consisting of the request, response, parameters, session and application properties is created.
  3. An XWork ActionProxy object is instantiated (wraps an Action) using the action name, path, and context then executed.
  4. Action output will channel back through the response to the user.

Any errors occurring during the action execution will result in a HttpServletResponse.SC_INTERNAL_SERVER_ERROR error and any resource errors (i.e., invalid action name or missing JSP page) will result in a HttpServletResponse.SC_NOT_FOUND error.

Instead of traditional servlet init params this servlet will initialize itself using WebWork2 properties. The following properties are used upon initialization:

Developers who want to subclass this servlet may be interested in the following protected methods:

Author:
Rickard ???berg, Matt Baldree, Jason Carreira, Cameron Braid, Bill Lynch
See Also:
ServletDispatcherResult, Serialized Form

Field Summary
protected static String encoding
           
protected static Locale locale
           
protected static org.apache.commons.logging.Log log
          Logger for this class.
 
Fields inherited from interface com.opensymphony.webwork.WebWorkStatics
HTTP_REQUEST, HTTP_RESPONSE, PAGE_CONTEXT, SERLVET_DISPATCHER, SERVLET_CONFIG, SERVLET_DISPATCHER
 
Constructor Summary
ServletDispatcher()
           
 
Method Summary
static HashMap createContextMap(Map requestMap, Map parameterMap, Map sessionMap, Map applicationMap, HttpServletRequest request, HttpServletResponse response, ServletConfig servletConfig)
          Merges all application and servlet attributes into a single HashMap to represent the entire Action context.
protected  String getActionName(HttpServletRequest request)
          Build the name of the action from the request.
protected  String getActionName(String name)
          Determine action name by extracting last string and removing extension (i.e., /.../.../Foo.action -> Foo).
protected  Map getApplicationMap()
          Returns a Map of all application attributes.
static String getEncoding()
           
static Locale getLocale()
           
protected  int getMaxSize()
          Returns the maximum upload size allowed for multipart requests (this is configurable).
protected  String getNameSpace(HttpServletRequest request)
          Gets the namespace of the action from the request.
static String getNamespaceFromServletPath(String servletPath)
          Returns the namespace (the context path) of the action.
protected  Map getParameterMap(HttpServletRequest request)
          Returns a Map of all request parameters.
protected  Map getRequestMap(HttpServletRequest request)
          Returns a Map of all request attributes.
protected  String getSaveDir()
          Returns the path to save uploaded files to (this is configurable).
protected  Map getSessionMap(HttpServletRequest request)
          Returns a Map of all session attributes.
 void init(ServletConfig servletConfig)
          Initalizes the servlet.
 boolean isParamsWorkaroundEnabled()
           
static Locale localeFromString(String localeStr)
          Builds a Locale from a String of the form en_US_foo into a Locale with language "en", country "US" and variant "foo".
protected  void sendError(HttpServletRequest request, HttpServletResponse response, int code, Exception e)
          Sends an HTTP error response code.
 void service(HttpServletRequest request, HttpServletResponse response)
          Services the request by determining the desired action to load, building the action context and then executing the action.
 void serviceAction(HttpServletRequest request, HttpServletResponse response, String namespace, String actionName, Map requestMap, Map parameterMap, Map sessionMap, Map applicationMap)
          Loads the action and executes it.
protected  HttpServletRequest wrapRequest(HttpServletRequest request)
          Wraps and returns the given response or returns the original response object.
 
Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doGet, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service
 
Methods inherited from class javax.servlet.GenericServlet
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected static final org.apache.commons.logging.Log log
Logger for this class.


encoding

protected static String encoding

locale

protected static Locale locale
Constructor Detail

ServletDispatcher

public ServletDispatcher()
Method Detail

getNamespaceFromServletPath

public static String getNamespaceFromServletPath(String servletPath)
Returns the namespace (the context path) of the action. I.e., "/foo/bar/MyAction.action" -> "/foo/bar" and "MyAction.action" -> "".

Parameters:
servletPath - the servlet URL path.
Returns:
the namespace (context path) of the action.

createContextMap

public static HashMap createContextMap(Map requestMap,
                                       Map parameterMap,
                                       Map sessionMap,
                                       Map applicationMap,
                                       HttpServletRequest request,
                                       HttpServletResponse response,
                                       ServletConfig servletConfig)
Merges all application and servlet attributes into a single HashMap to represent the entire Action context.

Parameters:
requestMap - a Map of all request attributes.
parameterMap - a Map of all request parameters.
sessionMap - a Map of all session attributes.
applicationMap - a Map of all servlet context attributes.
request - the HttpServletRequest object.
response - the HttpServletResponse object.
servletConfig - the ServletConfig object.
Returns:
a HashMap representing the Action context.

getEncoding

public static String getEncoding()

getLocale

public static Locale getLocale()

isParamsWorkaroundEnabled

public boolean isParamsWorkaroundEnabled()

init

public void init(ServletConfig servletConfig)
          throws ServletException
Initalizes the servlet. Please read the class documentation for more detail.

Specified by:
init in interface Servlet
Parameters:
servletConfig - the ServletConfig object.
Throws:
ServletException - if an error occurs during initialization.

service

public void service(HttpServletRequest request,
                    HttpServletResponse response)
             throws ServletException
Services the request by determining the desired action to load, building the action context and then executing the action. This handles all servlet requests including GETs and POSTs.

This method also transparently handles multipart requests.

Parameters:
request - the HttpServletRequest object.
response - the HttpServletResponse object.
Throws:
ServletException - if an error occurs while loading or executing the action.

serviceAction

public void serviceAction(HttpServletRequest request,
                          HttpServletResponse response,
                          String namespace,
                          String actionName,
                          Map requestMap,
                          Map parameterMap,
                          Map sessionMap,
                          Map applicationMap)
Loads the action and executes it. This method first creates the action context from the given parameters then loads an ActionProxy from the given action name and namespace. After that, the action is executed and output channels throught the response object. Errors are also sent back to the user via the sendError(HttpServletRequest,HttpServletResponse,int,Exception) method.

Parameters:
request - the HttpServletRequest object.
response - the HttpServletResponse object.
namespace - the namespace or context of the action.
actionName - the name of the action to execute.
requestMap - a Map of request attributes.
parameterMap - a Map of request parameters.
sessionMap - a Map of all session attributes.
applicationMap - a Map of all application attributes.

getActionName

protected String getActionName(HttpServletRequest request)
Build the name of the action from the request. Override this method to customize the request to action name mapping. Default implementation is to call the getActionName(String) method with the servlet path as the parameter.

Parameters:
request - the HttpServletRequest object.
Returns:
the name or alias of the action to execute.

getActionName

protected String getActionName(String name)
Determine action name by extracting last string and removing extension (i.e., /.../.../Foo.action -> Foo).

Parameters:
name - the full action path.
Returns:
the action name stripped of path/context info.

getApplicationMap

protected Map getApplicationMap()
Returns a Map of all application attributes. The default implementation is to wrap the ServletContext in an ApplicationMap. Override this method to customize how application attributes are mapped.

Returns:
a Map of all application attributes.

getNameSpace

protected String getNameSpace(HttpServletRequest request)
Gets the namespace of the action from the request. Override this method to customize the request to action namespace mapping. Default implementation is to call getNamespaceFromServletPath(String) method with the servlet path as the parameter.

Parameters:
request - the HttpServletRequest object.
Returns:
the namespace (context path) of the action.

getParameterMap

protected Map getParameterMap(HttpServletRequest request)
                       throws IOException
Returns a Map of all request parameters. The default implementation just calls ServletRequest.getParameterMap(). Override this method to customize how application parameters are mapped.

Parameters:
request - the HttpServletRequest object.
Returns:
a Map of all request parameters.
Throws:
IOException - if an exception occurs while retrieving the parameter map.

getRequestMap

protected Map getRequestMap(HttpServletRequest request)
Returns a Map of all request attributes. The default implementation is to wrap the request in a RequestMap. Override this method to customize how request attributes are mapped.

Parameters:
request - the HttpServletRequest object.
Returns:
a Map of all request attributes.

getSessionMap

protected Map getSessionMap(HttpServletRequest request)
Returns a Map of all session attributes. The default implementation is to wrap the reqeust in a SessionMap. Override this method to customize how session attributes are mapped.

Parameters:
request - the HttpServletRequest object.
Returns:
a Map of all session attributes.

sendError

protected void sendError(HttpServletRequest request,
                         HttpServletResponse response,
                         int code,
                         Exception e)
Sends an HTTP error response code.

Parameters:
request - the HttpServletRequest object.
response - the HttpServletResponse object.
code - the HttpServletResponse error code (see HttpServletResponse for possible error codes).
e - the Exception that is reported.

wrapRequest

protected HttpServletRequest wrapRequest(HttpServletRequest request)
                                  throws IOException
Wraps and returns the given response or returns the original response object. This is used to transparently handle multipart data as a wrapped class around the given request. Override this method to handle multipart requests in a special way or to handle other types of requests. Note, MultiPartRequestWrapper is flexible - you should look to that first before overriding this method to handle multipart data.

Parameters:
request - the HttpServletRequest object.
Returns:
a wrapped request or original request.
Throws:
IOException
See Also:
MultiPartRequestWrapper

getSaveDir

protected String getSaveDir()
Returns the path to save uploaded files to (this is configurable).

Returns:
the path to save uploaded files to

getMaxSize

protected int getMaxSize()
Returns the maximum upload size allowed for multipart requests (this is configurable).

Returns:
the maximum upload size allowed for multipart requests

localeFromString

public static Locale localeFromString(String localeStr)
Builds a Locale from a String of the form en_US_foo into a Locale with language "en", country "US" and variant "foo". This will parse the output of Locale.toString(). todo move this to LocalizedTextUtil in xwork 1.0.6


WebWork Project Page