com.mortbay.Servlets
Class ServletDispatch

java.lang.Object
  |
  +--com.mortbay.Servlets.ServletDispatch

public class ServletDispatch
extends java.lang.Object

Class to aid in servlet method dispatching and argument parsing

Method Dispatching

This class takes a HttpRequest and looks through the pathInfo trying to call a matching method on the given object. Methods are matched by looking for a method matching the first part of the path info in the request. The ServletDispatch object caches the path info so that it can be called again to call out to another function on a lower level object. There is no requirement for the ServletDispatch method to dispatch to a Servlet Object - It can dispatch calls to any Object (although one implementing ServletDispatchHandler is often best...). The matching method has its signature checked before it is called. The parameters the method takes are checked and the following objects may be taken as parameters: The HttpServletRequest, the HttpServletResponse, The ServletDispatch object itself and the context argument passed to the dispatch call. These arguments can be accepted in any order. In addition, other parameters can be accepted by the method under certain conditions (see below). If no matching method is found, then if the Object implements ServletDispatchHandler, its defaultHandler method is called to handle the request. If not, the dispatch method returns null. The method can return any type which will be returned by the dispatch method.

Argument Parsing

The ServletDispatch object has several methods to aid in parsing the parameters out of the HttpServletRequest object. The first is the static method parseArg. Given the HttpServletRequest, different variables can be parsed and converted from their string values into native java types. See the parseArg function for an example. The functions parseLongArg, parseDoubleArg and parseBooleanArg handle the native java types. Alternatively, an Object can be defined which has the required types defined as public data members and it can be passed to the initArgObject method on the ServletDispatch object which will populate the data members of the function from the request parameters. See the initArgObject method for an example. The final method allows the programmer to define an Argument object and list it as one of the parameters accepted by their function. The dispatch method will create and initialise the object and pass it to the called function.

Usage

 public void doGet(HttpServletRequest req, HttpServletResponse res) {
     ServletDispatch disp = new ServletDispatch(req, res);
     if (!disp.dispatch(this, null)){
         // handle error...
     }
 }
 public boolean Add(ServletDispatch disp,
                    HttpServletRequest req,
                    HttpServletResponse res) {
     int foo[] = null;
     foo = (int[])ServletDispatch.parseArg(foo, "foo", req);
     int blah = (int)ServletDispatch.parseLongArg(3, "blah", req);
     String var = "not set";
     String var = ServletDispatch.parseArg(var, "var", req);
     // ...
     return disp.dispatch(this, null); // Call out to next part of path
 }
 public static Object Args{
     int count = 0;
     String name = null;
     int values[] = null;
 }
 public void Delete(ServletDispatch disp, HttpServletResponse res){
     Args args = new Args();
     disp.initArgObject(args);
     // ...
 }
 public void AltDelete(ServletDispatch disp,
                       HttpServletResponse res, Args args)
 {
     // ...
 }
 public boolean defaultDispatch(String method,
                                ServletDispatch dispatch,
                                Object context,
                                HttpServletRequest req,
                                HttpServletResponse res) {
     // ...
 }
 

Version:
$Version: $
Author:
Matthew Watson (watsonm)
See Also:
com.mortbay.Util.ServletNode, com.mortbay.Util.ServletDispatchHandler

Constructor Summary
ServletDispatch(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)
          Constructor.
 
Method Summary
 java.lang.Object dispatch(java.lang.Object obj, java.lang.Object context)
          Dispatch the servlet request to a named method on the given object
 java.lang.String getProcessedPath()
           
 java.lang.String getProcessedPathInfo()
           
 void initArgObject(java.lang.Object toInit)
          Initialise an arbitrary Object from the request parameters.
 ObjectConverter.ObjectConvertFail initArgObject(java.lang.Object toInit, boolean errors)
          Initialise an arbitrary Object from the request parameters.
static java.lang.Object parseArg(java.lang.Object defaultValue, java.lang.String name, javax.servlet.http.HttpServletRequest req)
          Initialise an argument from the request parameters
static boolean parseBooleanArg(boolean defaultValue, java.lang.String name, javax.servlet.http.HttpServletRequest req)
          Version of parseArg to handle booleans
static double parseDoubleArg(double defaultValue, java.lang.String name, javax.servlet.http.HttpServletRequest req)
          Version of parseArg to handle doubles (and floats)
static long parseLongArg(long defaultValue, java.lang.String name, javax.servlet.http.HttpServletRequest req)
          Version of parseArg to handle longs (and short and int)
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ServletDispatch

public ServletDispatch(javax.servlet.http.HttpServletRequest req,
                       javax.servlet.http.HttpServletResponse res)
Constructor.
Parameters:
req -  
res -  
Method Detail

dispatch

public java.lang.Object dispatch(java.lang.Object obj,
                                 java.lang.Object context)
                          throws java.lang.reflect.InvocationTargetException,
                                 java.lang.IllegalAccessException,
                                 java.lang.InstantiationException
Dispatch the servlet request to a named method on the given object
Parameters:
obj - The object to dispatch the request to
context - The context object to pass to the called function.
Returns:
null if it cannot be dispatched
Throws:
java.lang.reflect.InvocationTargetException -  
java.lang.IllegalAccessException -  
java.lang.InstantiationException -  

getProcessedPathInfo

public java.lang.String getProcessedPathInfo()
Returns:
The part of the request pathInfo that has been processed so far in the dispatch process.

getProcessedPath

public java.lang.String getProcessedPath()
Returns:
The part of the request path (including the servlet path) that has been processed so far in the dispatch process.

parseArg

public static java.lang.Object parseArg(java.lang.Object defaultValue,
                                        java.lang.String name,
                                        javax.servlet.http.HttpServletRequest req)
Initialise an argument from the request parameters

E.g.

 {
        int foo[];
        foo = (int[])ServletDispatch.parseArg(foo, "foo", req);
        //...
 

This function does no checking that the parameter is actually set or has a valid value in it.
Parameters:
defaultValue - The default value to give the object (must be the same type as the object, since it is used to determine the type to convert the parameter to...)
name - The name of the parameter
req - The request
Returns:
The value for the object

parseLongArg

public static long parseLongArg(long defaultValue,
                                java.lang.String name,
                                javax.servlet.http.HttpServletRequest req)
Version of parseArg to handle longs (and short and int)
Parameters:
defaultValue - If the param is not set or not parseable
name - Name of the param
req - The Request
Returns:
The value

parseDoubleArg

public static double parseDoubleArg(double defaultValue,
                                    java.lang.String name,
                                    javax.servlet.http.HttpServletRequest req)
Version of parseArg to handle doubles (and floats)
Parameters:
defaultValue - If the param is not set or not parseable
name - Name of the param
req - The Request
Returns:
The value

parseBooleanArg

public static boolean parseBooleanArg(boolean defaultValue,
                                      java.lang.String name,
                                      javax.servlet.http.HttpServletRequest req)
Version of parseArg to handle booleans
Parameters:
defaultValue - If the param is not set or not parseable
name - Name of the param
req - The Request
Returns:
The value

initArgObject

public void initArgObject(java.lang.Object toInit)
Initialise an arbitrary Object from the request parameters.
Parameters:
toInit - The object to initialise. If parameters exist corresponding to the names of the public data members of this Object, then they will be initialised. E.g.
    public static Object Args{
        int count = 0;
        String name = null;
        int values[] = null;
    }
    public void Add(ServletDispatch disp, HttpServletResponse res){
        Args args = new Args();
        disp.initArgObject(args);
        // ...
 

initArgObject

public ObjectConverter.ObjectConvertFail initArgObject(java.lang.Object toInit,
                                                       boolean errors)
Initialise an arbitrary Object from the request parameters.
See Also:
- This version accepts a boolean param to indicate whether error checking should be done. If true and there are conversion/parse errors of the request parameters, an Object of type com.mortbay.Util.ObjectConverter.ObjectConvertFail will be returned.