Java Reference
In-Depth Information
the abortWith(Response) method of a ClientRequestFilter or ContainerResponseFilter can stop
execution of the filter chain.
Entity Interceptors
As mentioned in the previous section, an extension point is an interface that includes a method, which is
responsible for filtering or intercepting the request or response. Entity interceptors have two such extension
points: ReaderInterceptor and WriterInterceptor . An entity interceptor class must implement one or
both of these extension points. Also mentioned previously, entity interceptors wrap calls to methods. More
specifically, MessageBodyWriter implementations wrap calls to the writeTo method, whereas MessageBodyReader
implementations wrap calls to the readFrom method.
Let's take a look at an interceptor so that you can more easily visualize how they are coded. In the following
example, the aroundWriteTo method converts an OutputStream to a ZipOutputStream :
public class ZipEncoder implements WriterInterceptor {
public void aroundWriteTo(WriterInterceptorContext ctx) throws IOException,
WebApplicationException {
ZipOutputStream zip = new ZipOutputStream(ctx.getOutputStream());
try {
zip.putNextEntry(new ZipEntry("Test));
} catch (ZipException ex){
System.out.println("ZipEncoder ERROR: " + ex);
} finally {
As you can see in the example, a aroundWriteTo method must accept a WriteInterceptorContext argument,
whereas a readFrom method must accept a ReaderInterceptorContext argument. These arguments provide read and
write access to any parameters that are passed into the wrapped methods. Entity interceptors can be chained, just like
filters. To proceed to the next interceptor in the chain, an interceptor must call its proceed method.
Binding Filters and Interceptors
Filters and interceptors must be associated to application classes or methods, and this process is also known as
binding . The default type of binding is global binding, and any filter or interceptor that does not include annotations
is bound globally. Global binding associates the filter or interceptor with all resource methods (those annotated with
@GET , @POST , and so on) in an application. That said, any time a resource method is invoked, all globally bound filters
and interceptors are processed as well.
Filters and interceptors can be registered manually via Application or Configuration , or they can be registered
dynamically. To indicate that a filter or interceptor should be registered dynamically, it can be annotated with
@Provider . If a filter or interceptor is not annotated as such, it must be registered manually.
Search WWH ::

Custom Search