Container filters are Server API filters. A ContainerRequestFilter
filter is run after receiving a request from the client. A ContainerResponseFilter
is run in the response pipeline before the HTTP response is delivered to the client. Next, we will create a container filter for logging/outputting some information about the request. Extension points before and after the match are provided in the ContainerRequestFilter
interface. The pre-match filter is run before the request has been matched with a resource method, and the post-match filter is applied after the resource method matching; the default is post-match. We will use pre-match with the @PreMatching
annotation. Annotate the filter class with @Provider
for the filter to be discovered by the JAX-RS runtime during the scanning phase.
Make the example container filter LoggingFilter
, implement the ContainerRequestFilter
, ContainerResponseFilter
interfaces, and provide implementation for the filter(ContainerRequestContext requestContext)
and filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
methods. In the ContainerRequestFilter
implementation method filter(ContainerRequestContext requestContext)
, output the request method with the getMethod()
method, request URI with the getUriInfo().getAbsolutePath()
, media type with getMediaType()
, and acceptable media types with getAcceptableMediaTypes()
. Include a no-argument constructor in the LoggingFilter
so that the filter may be instantiated. Register the LoggingFilter
with the client configuration using the register
method. Comment out the registration of the ClientFilter
as we will apply only the LoggingFilter
in the RESTEasyClient
class:
Client client = ClientBuilder.newClient(); //client.register(ClientFilter.class); client.register(LoggingFilter.class);
By default, container filters are bound to all the resources the client request is sent to, but a resource-specific container filter can be applied using the @NameBinding
annotation:
The LoggingFilter.java
class is listed as follows:
package org.jboss.resteasy.rest; import java.io.IOException; import java.util.Iterator; import java.util.List; import javax.ws.rs.container.ContainerRequestContext; import javax.ws.rs.container.ContainerRequestFilter; import javax.ws.rs.container.ContainerResponseContext; import javax.ws.rs.container.ContainerResponseFilter; import javax.ws.rs.container.PreMatching; import javax.ws.rs.core.MediaType; import javax.ws.rs.ext.Provider; @Provider @PreMatching public class LoggingFilter implements ContainerRequestFilter, ContainerResponseFilter { public LoggingFilter() { } @Override public void filter(ContainerRequestContext requestContext) throws IOException { System.out.println("Request Method: " + requestContext.getMethod()); System.out.println("Request URI: "+ requestContext.getUriInfo().getAbsolutePath()); System.out.println("Media Type : "+ requestContext.getMediaType()); List<MediaType> mediaTypes = requestContext.getAcceptableMediaTypes(); Iterator<MediaType> iter = mediaTypes.iterator(); System.out.println("Acceptable Media Types: "); while (iter.hasNext()) { MediaType mediaType = iter.next(); System.out.println(mediaType.getType() + ", "); } } @Override public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { } }
Keep RESTEasyClient
and HelloWorldResource
the same as the ClientFilter
example and redeploy the jboss-restaesy
application. Run the RESTEasyClient
application to generate the output shown from the container filter, which is shown here:
18.116.51.6