Java Reference
In-Depth Information
throws IOException {
String encoding = ctx . getHeaders (). getFirst ( "Content-Encoding" );
iif (! "gzip" . equalsIgnoreCase ( encoding )) {
return
throws
return ctx . proceed ();
}
GZipInputStream is = new
new GZipInputStream ( ctx . getInputStream ());
ctx . setInputStream ( is );
return
return ctx . proceed ( is );
}
}
The ReaderInterceptorContext parameter allows you to view and modify the HTTP
headers associated with this invocation. Since interceptors can be used on both the client and
server side, these headers represent either a client response or a server request. In the ex-
ample, our aroundReadFrom() method uses the ReaderInterceptorContext to first check
to see if the message body is GZIP encoded. If not, it returns with a call to ReaderInter-
ceptorContext.proceed() . The ReaderInterceptorContext is also used to get and re-
place the InputStream of the HTTP message body with a GZipInputStream . The call to
ReaderInterceptorContext.proceed() will either invoke the next registered ReaderIn-
terceptor , or if there aren't any, invoke the underlying MessageBodyReader.readFrom()
method. The value returned by proceed() is whatever was returned by MessageBodyRead-
er.readFrom() . You can change this value if you want, by returning a different value from
your aroundReadFrom() method.
There's a lot of other use cases for interceptors that I'm not going to go into detail with. For
example, the RESTEasy project uses interceptors to digitally sign and/or encrypt message
bodies into a variety of Internet formats. You could also use a WriterInterceptor to add a
JSONP wrapper to your JSON content. A ReaderInterceptor could augment the unmar-
shalled Java object with additional data pulled from the request or response. The rest is up to
your imagination.
Client-Side Filters
The JAX-RS Client API also has its own set of request and response filter interfaces:
package
package javax . ws . rs . client ;
public
public interface
interface ClientRequestFilter
ClientRequestFilter {
public
public void
void filter ( ClientRequestContext requestContext ) throws
throws IOException ;
}
public
public interface
interface ClientResponseFilter
ClientResponseFilter {
Search WWH ::




Custom Search