users@jersey.java.net

Re: [Jersey] Using a Filter instead of a Servlet for Jersey?

From: Martin Grotzke <martin.grotzke_at_freiheit.com>
Date: Thu, 09 Oct 2008 21:33:03 +0200

Hi,

On Thu, 2008-10-09 at 10:43 +0200, Paul Sandoz wrote:
> Hi Gili,
>
> Currently it is not possible without duplicating all the functionality
> in the current servlet. I have been meaning to look at this but never
> found the time. In general only having Jersey servlet support as a
> filter is probably the right solution. So perhaps we should change
> this for 1.0.1? could you log an issue to track this?
>
> I am not sure how to make things work as of now, perhaps it is
> possible to have a servlet filter that forwards to wicket or the
> Jersey servlet?
I've done exactly this for our application: I wrote a filter that can be
configured with a path, for that this filter should do a forward. The
forward bypasses the filters following this filter, if they are not
bound to the FORWARD dispatcher.

This is the servlet filter:

public class ExcludeServletFilter implements Filter {

    private static final Log LOG = LogFactory
            .getLog( ExcludeServletFilter.class );
    
    private String _excludePathPrefix;

    public void init( FilterConfig filterConfig ) throws ServletException {
        _excludePathPrefix = filterConfig.getInitParameter( "exclude-path-prefix" );
    }

    public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain ) throws IOException, ServletException {
        final HttpServletRequest httpRequest = (HttpServletRequest)request;
        final String servletPath = httpRequest.getServletPath();
        if ( LOG.isDebugEnabled() ) {
            LOG.debug( "Have servlet path: " + servletPath );
        }
        if ( _excludePathPrefix != null
                && servletPath.startsWith( _excludePathPrefix )
                && request.getParameter( "usechain" ) == null ) {
            final String requestURI = httpRequest.getRequestURI();
            final String requestPath = requestURI.substring(httpRequest.getContextPath().length());
            RequestDispatcher requestDispatcher = request.getRequestDispatcher( requestPath );
            try {
                LOG.debug( "Forwarding..." );
                requestDispatcher.forward( request, response );
            } catch ( RuntimeException e ) {
                LOG.error( "Caught exception during forward.", e );
                throw e;
            }
        }
        else {
            chain.doFilter( request, response );
        }
    }

    public void destroy() {
    }

}

This is the configuration in web.xml:

    <filter>
        <filter-name>ForwardToJerseyServletFilter</filter-name>
        <filter-class>com.freiheit.shopping24.shop.common.util.ExcludeServletFilter</filter-class>
        <init-param>
            <param-name>exclude-path-prefix</param-name>
            <param-value>/api/</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>ForwardToJerseyServletFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

If the url-pattern would be changed to /api/*, one wouldn't need the
exclude-path-prefix and it would be even more simple :)

Cheers,
Martin


>
> Paul.
>
> On Oct 8, 2008, at 8:17 PM, Gili wrote:
>
> >
> > Hi,
> >
> > I'd like to mix Jersey with Apache Wicket so that any HTTP GET/POST
> > requests
> > against HTML would get handled by Wicket and all others by Jersey.
> > Wicket
> > already uses a Filter which ignores any URLs it can't handle but
> > Jersey
> > still uses a servlet. Is it possible to create a Filter for Jersey? Or
> > perhaps you have another idea on how to mix the two technologies?
> >
> > Thank you,
> > Gili
> > --
> > View this message in context: http://n2.nabble.com/Using-a-Filter-instead-of-a-Servlet-for-Jersey--tp1307406p1307406.html
> > Sent from the Jersey mailing list archive at Nabble.com.
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> > For additional commands, e-mail: users-help_at_jersey.dev.java.net
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net

-- 
Martin Grotzke
http://www.javakaffee.de/blog/