public final class RequestDispatcher
extends java.lang.Object
Modifier and Type | Method and Description |
---|---|
ResponseMessage |
dispatch(RequestMessage requestMessage)
Match the request to a registered handler and invoke the handler.
|
static RequestDispatcher |
getInstance()
The RequestDispatcher is a singleton.
|
RequestHandler |
getRequestHandler(java.lang.String endpointId,
java.lang.String path)
Lookup a RequestHandler for the given endpointId and path.
|
void |
registerRequestHandler(java.lang.String endpointId,
java.lang.String path,
RequestHandler handler)
Register a handler for the given endpoint and path.
|
void |
unregisterRequestHandler(RequestHandler handler)
Un-register the given handler for all endpoints and paths for which the
handler may be registered.
|
void |
unregisterRequestHandler(java.lang.String endpointId,
java.lang.String path)
Un-register the handler for the given endpoint and path.
|
public static RequestDispatcher getInstance()
public void registerRequestHandler(java.lang.String endpointId, java.lang.String path, RequestHandler handler)
handler
parameter may not be null.
Registering a handler with
registerRequestHandler("*", "*", handler)
will register a default,
catch-all handler. Note that this utility class already provides a
reasonable default handler that can be replaced with this technique.
The implementation ensures there is always a default handler.
endpointId
- the endpoint id that has the given the resource path,
or a wildcard ("*") for any endpoint.path
- the resource path, or a wildcard ("*") for any resourcehandler
- the handler to invoke for requests to this endpoint and pathjava.lang.IllegalArgumentException
- if handler is nullpublic void unregisterRequestHandler(java.lang.String endpointId, java.lang.String path)
registerRequestHandler("0-AB", "resource", handler)
will not be un-registered by calling
unregisterRequestHandler("*", "resource")
.
Nor will calling unregisterRequestHandler("0-AB", "*")
result
in un-registering all handlers for endpoint "0-AB". In short, the
arguments to registerRequestHandler
should match the
arguments to unregisterRequestHandler
.
A default handler can be registered by calling
registerRequestHandler("*", "*", handler)
. Calling
unregisterRequestHandler("*", "*")
will restore the
default handler. The implementation ensures there is always a
default handler.
endpointId
- the endpoint id that has the given the resource path,
or a wildcard ("*").path
- the resource path, or a wildcard ("*")public void unregisterRequestHandler(RequestHandler handler)
handler
parameter may not be null.handler
- the handler to un-registerpublic ResponseMessage dispatch(RequestMessage requestMessage)
requestMessage
- The request message to be dispatchedjava.lang.IllegalArgumentException
- if requestMessage is nullpublic RequestHandler getRequestHandler(java.lang.String endpointId, java.lang.String path)
("*", path)
. Failing that, the lookup will return
a handler for ("*", "*")
, or the default handler if no handler
for ("*","*")
has been registered.
Will not return null.endpointId
- the endpoint id that has the given the resource path,
or a wildcard ("*").path
- the resource path, or a wildcard ("*")