users@jersey.java.net

[Jersey] ResourceFilter to execute after the ServletFilters

From: Alan McKernan <alan_at_doyoupoken.com>
Date: Fri, 28 Aug 2009 17:06:26 +0200

Hi Paul,

Thanks for your quick response.

Indeed I do mean a servlet filter declared in the web.xml.

Is it possible to get the ResourceFilter's to execute after the
servlet filters have executed?

I can indeed get the HttpServletRequest using the code you specified,
so I could theoretically
move my servlet filter code into a ResourceFilter, but would require
some re-factoring, and is not
the ideal solution.

Thanks again,
Alan

On Aug 28, 2009, at 2:37 PM, Alan McKernan wrote:

> Hi everyone,
>
> I have a number of ServletFilters, and one ResourceFilterFactory.
>
> I need the ServletFilters to execute before the
> ResourceFilterFactory as the ServletFilters setup some context
> required by the ResourceFilterFactory.
>
> Anyone know how I can achieve this?
>

What is a "ServletFilter" ? do you mean a servlet filter declared in
the web.xml?


> Otherwise I can move some of my ServletFilter logic into a
> ResourceFilter, but there I cant seem to access the servlet request
> via @Context, it always comes out null. Any solution for this maybe?
>

You need to inject onto ResourceFilterFactory and pass those injected
values to the ResourceFilter instances your factory is responsible for
creating when the create method is called.

For example:

   public class MyRFF implements ResourceFilterFactory {
     private final HttpServletRequest hsr;

     public ResourceFilterFactory(@Context HttpServletRequest hsr) {
       this.hsr = hsr;
     }

     public List<ResourceFilter> create(AbstractMethod am) {
       // no need to pass reference if MyRF is an non-static inner class
       ResourceFilter rf = new MyRF(hsr);
       return Collections.singletonList(rf);
     }

   }


Paul.