users@jersey.java.net

Re: [Jersey] Accessing HttpSession from a ContainerRequestFilter

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 25 Jan 2010 12:57:12 +0100

On Jan 25, 2010, at 12:18 PM, Erdinc Yilmazel wrote:

> Paul I think I got something wrong,
>
> I have a ResourceFilterFactory which returns a list of
> ResourceFilter instances on its create method. In this method I am
> returning only one instance of ResourceFilter.
>
> The following method is called by Jersey, only once during the
> application startup.
>
> public List<ResourceFilter> create(AbstractMethod am) {
> LoginRequired lr = am.getAnnotation(LoginRequired.class);
> if (lr == null) {
> lr = am.getResource().getAnnotation(LoginRequired.class);
> if (lr == null) {
> return null;
> }
> }
>
> final LoginResourceFilter filter = new LoginResourceFilter();
>
> return new ArrayList<ResourceFilter>() {{
> add(filter);
> }};
> }
>
> The actual resource filter is something like this:
>
> public class LoginResourceFilter implements ResourceFilter,
> ContainerRequestFilter {
> public ContainerRequestFilter getRequestFilter() {
> return this;
> }
>
> public ContainerResponseFilter getResponseFilter() {
> // Not Supported
> return null;
> }
>
> public ContainerRequest filter(ContainerRequest req) {
> return req;
> }
> }
>
> Where in this class can I inject HttpServletRequest? Since this is a
> single instance, injecting it as a class property will not work. I
> need it injected in the filter method, or I need to find a way to
> use a new instance of my ResourceFilter in every request.

You need to inject on the ResourceFilterFactory instance. A thread
local proxy will be injected. If you use inner classes for the
ResourceFilter you access the field reference on ResourceFilterFactory.

Since you are responsible for instantiating LoginResourceFilter no
injection will be performed.

Paul.