Java Reference
In-Depth Information
Although the two concepts are very similar (as they all relate to interception), we
must say that the filter is often used for the processing of the headers of requests or
responses. Whereas, interceptors are generally set up to manipulate the content of
messages.
Filters
The JAX-RS 2.0 specification defines four types of filters: two types of filters on each
side (client and server). On the client side, one filter that must be run before sending
the HTTP request implements the ClientRequestFilter interface and the oth-
er filter, which must be run immediately after the receipt of the response from the
server, (but before the control is rendered to the application) implements the Cli-
entResponseFilter interface. On the server side, the filter that will be executed
before the access to a JAX-RS resource implements the ContainerRequestFil-
ter interface and the filter that will run just before the response is sent to the client
implements the ContainerResponseFilter interface. The following code shows
an example of ContainerRequestFilter implementation that verifies the inform-
ation that ensures a secure access to the resources available to external users
of our online preregistration application. The @Provider annotation on top of the
MyJaxRsRequestFilter classinthefollowing codeallowsthefilter tobeautomat-
ically discovered by the container and applied to all resources of the server. Failing
to use this annotation, you must manually registered the filter.
The following code is an example of ContainerRequestFilter implementation:
@Provider
public class MyJaxRsRequestFilter implements
ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext
crq) {
// If the user has not been
authenticated
if(crq.getSecurityContext().getUserPrincipal()
== null)
Search WWH ::




Custom Search