users@jersey.java.net

Re: [Jersey] ResourceFilter to execute after the ServletFilters

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 31 Aug 2009 10:45:26 +0200

On Aug 28, 2009, at 5:06 PM, Alan McKernan wrote:

> Hi Paul,
>
> Thanks for your quick response.
>
> Indeed I do mean a servlet filter declared in the web.xml.
>
OK.

> 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.
>
You need to declare the servlet filters in the web.xml so that they
will get executed before the Jersey servlet or the Jersey servlet
filter (also declared in the web.xml i.e. the
com.sun.jersey.spi.container.servlet.ServletContainer).

Paul.
>
> 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.