RESTEasy Interceptors (Proposed Feature)
A number of new ideas have cropped up on the RESTEasy developers list that indicate that adding some type of request/response interceptor API to RESTEasy could aid in adding these features. Such features include:
- RESTEasy Server-Side Caching API Ideas
- Content Negotiation customization
- GZip Input/Output streams
- Security
There could more, but you get the point. Below are a few approaches we could take.
Filter-Style Interceptor
This type of interceptor looks similar to a Servelt Filter and provides access to the raw request and response. more details TDB.
Annotation-based Interceptor
The annotation approach allows the developer to define specific points in the request/response lifecycle where we can alter the process. There are a few directions we could take this such as having really broad point cuts like @BeforeRequest and @AfterRequest. Or, we could have more fine grained pointcuts such as:
- @BeforeResresource: executes after the request has been processed, but before the resource method has been invoked
- @AfterResource: executes after the resource method has been executed, but before the MessageBodyWriter is invoked
- @BeforeWriter: after the request has been provecces and the resource method has been executed, but before the invocation of the MessgaeBodyWriter
- @AfterWriter: executed after the message body reader
- @BeforeReader: executed before the MessageBodyReader is executed
- @AfterReader: executed after the MessageBodyReade, but before the resource method
- Perhaps more? (some of these maybe redundant, but we'll work that out)
An interceptor might end up looking something like this:
@Provider
@Interceptor
public class CacheControlInterceptor
{
@BeforeResource
public Response checkCache(@Context HttpRequest request,
@Context HttpResponse response,
@Context RequestContext ctx)
{
...check the cache...
}
@BeforeWriter
public void addCacheControlHeaders(@Context HttpResponse response
@Context RequestContext ctx)
{
... write some cache control headers...
}
@AfterWriter
public void grabBytesToMarshall(@Context HttpResponse response
@Context RequestContext) {
}
}
Unresolved Issues
One thing that still needs to get worked out if how to declare precedence of interceptors, what gets executed first, etc.
Comments