Request response flow in client server (REST Web service)
We have shown the request response mechanism of service and client in Fig 1.
- Request initiated from Client
- Request data written to transport layer (written to network)
- Request reaches the Service
- Request data read from transport channel
- Service resource process the requests and generate the response
- Response sent back from service client
- Response data written to transport layer (written to network)
- Response reaches the client
- Response data read from transport layer.
The execution flow shown in Fig 1, is applicable for client server paradigm. We would like to intercepts or filters the data for following cases.
- When data written to transport layer or
- When the data read from transport layer.
We have already discussed about the filters in enterprise application.
JAX-RS specification provides the couple of extension points to customize the request/response on client and server sides. There are couple of extension points like filters and interceptors.
The extension points can be used for
- Authentication
- Logging
- Encoding/Decoding of entities
- Modification of request/response headers
- Entities transformations etc.
How filters works in enterprise applications (JAX-RS Jersey framework – java):
The filters are mostly used to modify the request or response parameters. Generally we will find the authentication filters, which are used to authenticate the user before the request reaches to any resource.
We have already discussed about the request logging filters using jersey wherein we have used the server side (ContainerRequestFilter, ContainerRequestFilterfilters
- ContainerRequestFilter: The request filter comes into the picture before request is being served by actual resource.
- ContainerResponseFilter: The response filter comes into the picture after the request is being serviced by resource.
Similar to server side, we will have corresponding client side request and response filters.
- ClientRequestFilter: When the request is about to send to server, the request filter intercepts the request.
- ClientResponseFilter: The filter is responsible for intercepting the response, generated by request.
How Interceptors works in RESTFul web service (JAX-RS) ?
Interceptors are used to modify the entities, mostly the interceptors are used for encoding or decoding of entities.
Let us take a look into the execution order of filters and interceptors is as follows:
The interceptors are of two types:
- ReadInterceptors: ReadInterceptors are used to modify the input entities, readinterceptors kind of wrapper around message reader (responsible for reading the streams).
- WriteInterceptors: WriteInterceptors are used to modify the output entities, writeinterceptors kind of wrapper around message writer (responsible for writing the streams).