Handler Chain - Runtime Extension
The Apache Wink runtime utilizes three Handler Chains for the complete processing of a request: Request chain, Response chain and Error chain. A handler receives a MessageContext instance for accessing and manipulating the current request information and a HandlerChain instance for advancing the chain. It is the responsibility of the handler to pass control to the next handler on the chain by invoking the doChain() method on the HandlerChain instance.
A handler may call the doChain() method several times if needed, so handlers are required to consider the possibility they will be invoked more than once for the same request.
All handler related interfaces reside in the org.apache.wink.server.handlers package.
The implementation of separate chains provides the ability to move up and down one chain before moving on to the next chain. This is particularly useful for the implementation of the JAX-RS resource-method search algorithm that includes invoking sub-resource locators, and implementing the Continued Search mode.
Handlers
There are two types of handlers:
- System Handler - are the handlers that implement the core engine of the Apache Wink runtime. The Apache Wink runtime will not function correctly if any of the system handlers are removed from the chain.
- User Handler - are the handlers that are provided by an application to customize a chains behavior and to add unique functionality to it. User handlers are not part of the core functionality of Apache Wink.
Reference
In order to customize a handler chain refer to section 2 "Apache Wink Building Blocks", Customization of the Handlers Chain
Message Context
The MessageContext allows the following:
Allows handlers to access and manipulate the current request informationAllows handlers to maintain a state by setting attributes on the message context, as the handlers themselves are singletons and therefore statelessAllows handlers to pass information to other handlers on the chain
Request Handler Chain
The Request Handler Chain is responsible for processing a request according to the JAX-RS specification by accepting the request, searching for the resource method to invoke, de-serializing the request entity and finally for invoking the resource method. It is responsible for invoking sub-resource locators by moving up and down the chain as needed. A Request handler is a class that implements the org.apache.wink.server.handlers.RequestHandler interface.
System Request Handlers
The following is a list of system handlers comprising the request handler chain in the order that they appear in the chain.
Handler |
Description |
---|---|
SearchResultHandler |
Responsible for throwing the search result error if there was one during the search for the resource method |
OptionsMethodHandler |
Generates a response for an OPTIONS request in case that there is no resource method that is associated with OPTIONS, according to the JAX-RS spec |
HeadMethodHandler |
Handles a response for a HEAD request in case that there is no resource method that is associated with HEAD, according to the JAX-RS spec |
FindRootResourceHandler |
Locates the root resource that best matches the request |
FindResourceMethodHandler |
Locates the actual method to invoke that matches the request, invoking sub-resource locators as needed |
CreateInvocationParametersHandler |
Creates the parameters of the resource method to invoke and de-serializes the request entity using the appropriate MessageBodyReader |
InvokeMethodHandler |
Invokes the resource method |
User Request Handlers
User request handlers are inserted before the InvokeMethodHandler handler.
Reference
In order to customize a handler chain refer to section 2 "Apache Wink Building Blocks", Customization of the Handlers Chain
Response Handler Chain
The Response Handler Chain is responsible for handling the object returned from invoking a resource method or sub-resource method according to the JAX-RS specification. It is responsible for determining the response status code, selecting the response media type and for serializing the response entity.
A Response handler is a class that implements the org.apache.wink.server.handlers.ResponseHandler interface.
System Response Handlers
The following is a list of system handlers comprising the response handler chain in the order that they appear in the chain.
Handler |
Description |
---|---|
PopulateResponseStatusHandler |
Determines the response status code, according to the JAX-RS spec |
PopulateResponseMediaTypeHandler |
Determines the response media type, according to the JAX-RS spec |
FlushResultHandler |
Serializes the response entity using the appropriate MessageBodyWriter |
HeadMethodHandler |
Performs cleanup operations in case that there was no resource method that was associated with HEAD. |
User Response Handlers
User response handlers are inserted before the FlushResultHandler handler. Apache Wink initializes the user response handler chain with the CheckLocationHeaderHandler handler that verifies that the "Location" response header is present on a response when there is a status code that requires it, for example, status code: 201.
Reference
In order to customize a handler chain refer to section 2 "Apache Wink Building Blocks", Customization of the Handlers Chain
Error Handler Chain
The Error Handler Chain is responsible for handling all of the exceptions that are thrown during the invocation of the Request and Response handler chains, according to the JAX-RS specification for handling exceptions. It is responsible for determining the response status code, selecting the response media type and for serializing the response entity.
An Error handler is a class that implements the org.apache.wink.server.handlers.ResponseHandler interface.
System Error Handlers
The following is a list of system handlers comprising the error handler chain in the order that they appear in the chain.
Error Handlers
Handler |
Description |
---|---|
PopulateErrorResponseHandler |
Prepares the response entity from a thrown exception according to the JAX-RS specification |
PopulateResponseStatusHandler |
Determines the response status code according to the JAX-RS spec |
PopulateResponseMediaTypeHandler |
Determines the response media type, according to the JAX-RS spec |
FlushResultHandler |
Serializes the response entity using the appropriate MessageBodyWriter |
User Error Handlers
User error handlers are inserted before the FlushResultHandler handler.
Reference
In order to customize a handler chain refer to section 2 "Apache Wink Building Blocks", Customization of the Handlers Chain
Request Processing
The following details how the Apache Wink runtime performs request processing:
- Create new instances of the three handler chains. The handlers themselves are singletons.
- Create a new instance of a MessageContext to pass between the handlers.
- Invoke the first handler on the Request chain.
- Once the request chain is complete, invoke the Response chain and pass it the MessageContext that was used in the Request chain.
- Make both chains and the MessageContext available for garbage collection.
If at any time during the execution of a Request or Response chain an exception is thrown, catch the exception, wrap it in a new MessageContext instance and invoke the Error chain to produce an appropriate response.