users@jersey.java.net

Re: Filters

From: Fabio Mancinelli <fabio.mancinelli_at_gmail.com>
Date: Sat, 7 Feb 2009 15:50:17 +0100

On Fri, Feb 6, 2009 at 6:47 PM, Fabio Mancinelli
<fabio.mancinelli_at_gmail.com> wrote:
> Dear all,
>
> I am stuck with the following problem: I would like to implement a
> "setup/cleanup" filter that, before calling any resource, must do the
> following
>
> 1) Perform some initializations
> 2) Make some environment objects available to the resource that will
> be called after.
> 3) Cleanup after the request has been served (no matter what, even if
> exceptions are thrown, a bit like a finally)
>
I think I've found a solution to the problem, so I am sharing it with you.

Basically I create a class that implements the ContainerRequestFilter
and ContainerResponseFilter and setup this filter in the web.xml

In the filter(ContainerRequest request) method I use the
request.getProperties() map in order to store my environment objects.

A resource willing to retrieve those object must declare a @Context
HttpContext variable.

The injected value contains a map that can be retrieved using the
getProperties() method.
This map will contain all the objects that I've stored in the filter method.

That's it.

Thanks,
Fabio