Java Magazine, Jan/Feb 2016
ORACLE COM JAVAMAGAZINE JANUARY FEBRUARY 2016 34 web apps mutate aspects of the response for example HTTP headers and the programming model for example being executed as a chain in a developer defined or default order Setting up a server side response filter is as simple as providing an implementation for the javax ws rs container ContainerResponseFilter interface Just like its server side request counterpart the injection of ContainerResponseContext into the filter method of the ContainerResponseFilter interface is taken care of by the JAX RS runtime Server side response filters also need to be annotated with the javax ws rs ext Provider annotation in order for the JAX RS runtime to recognize them An example of this follows @ Provider public class AContainerResponseFilter public void filter ContainerRequestContext reqCtx ContainerResponseContext resCtx throws IOException adding a custom header to the response resCtx getHeaders add X Search ID qwer1234 tyuio5678 asdfg9876 Client side filters predictably include request and response filters which Ill quickly summarize Client side request filters These are invoked after the HTTP request has been constructed but prior to the request being dispatched to the server This type of filter provides an opportunity to mutate the properties of the HTTP request such as headers and cookies To implement a client side request filter implement the extension interface provided by javax ws rs client ClientRequestFilter Client side response filters Client side response filters are invoked after the HTTP response has been received from the server but before it is dispatched to the caller client for processing Like client side request filters these filters provide an opportunity to mutate the properties of the HTTP response To use a client side response filter implement the extension interface provided by javax ws rs client ClientResponseFilter as shown next public class ClientResponseLoggerFilter public void filter ClientRequestContext reqCtx ClientResponseContext resCtx throws IOException System out println Response status resCtx getStatus Interceptors Interceptors are similar to filters in the sense that they are also used to mutate HTTP requests and responses but the major diference lies in the fact that interceptors are used primarily to manipulate HTTP message payloads They are divided into two categories javax ws rs ext ReaderInterceptor and javax ws rs ext WriterInterceptor which are used to process HTTP requests and responses respectively Note that unlike filters the same set of interceptors is applicable on the server side and the client side Server side interceptors need to be annotated with @ Provider for the JAX RS runtime to detect them Clientside interceptors need to be registered with ClientBuilder Client or WebTarget class or interface using the register method
You must have JavaScript enabled to view digital editions.