users@jersey.java.net

Re: [Jersey] Using filters

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 10 Feb 2009 17:25:37 +0100

On Feb 10, 2009, at 5:21 PM, James Strachan wrote:

> 2009/2/3 Paul Sandoz <Paul.Sandoz_at_sun.com>:
>> Hi,
>>
>> I have managed to achieve some progress with filters. My latest
>> commit
>> modifies ServletContainer to be a Servlet or a Filter depending on
>> where you
>> use it. The same Servlet behavior is supported as before.
>>
>> The filter behavior is currently very simple:
>>
>> - the filter needs to be declared at the end of the filter chain.
>> It does
>> not pass on processing to the next filter in the chain, if
>> any is configured.
>>
>> - the base URI is set to:
>>
>> http:://<host>:<port>/<context path>/
>>
>> so even if your filter is configured to filter say "resources/*" the
>> "resources" path segment will not be considered part of the
>> base URI.
>>
>> IIUC to support such behavior we need to support an init-param that
>> declares a filter context path.
>>
>> - the same initialization parameters as for servlet initialization
>> apply.
>>
>> Since the SpringServlet extends from ServletContainer it can also
>> be used as
>> a filter.
>>
>> Thus it should be possible to add a filter before the
>> ServletContainer
>> filter to forward to static resources.
>>
>> If people could try this out it when a new build appears on the
>> repo that
>> would be much appreciated. The following is the web.xml of the
>> bookstore
>> sample modified to use the filter:
>>
>> <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
>> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>> xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
>> http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
>> version="2.4">
>> <filter>
>> <filter-name>Jersey Filter</filter-name>
>>
>> <filter-
>> class>com.sun.jersey.spi.container.servlet.ServletContainer</filter-
>> class>
>> <init-param>
>> <param-name>com.sun.jersey.config.feature.Redirect</param-
>> name>
>> <param-value>true</param-value>
>> </init-param>
>> <init-param>
>>
>> <param-name>com.sun.jersey.config.feature.ImplicitViewables</param-
>> name>
>> <param-value>true</param-value>
>> </init-param>
>> <init-param>
>> <param-name>com.sun.jersey.config.property.packages</
>> param-name>
>>
>> <param-value>com.sun.jersey.samples.bookstore.resources</param-value>
>> </init-param>
>> </filter>
>> <filter-mapping>
>> <filter-name>Jersey Filter</filter-name>
>> <url-pattern>/*</url-pattern>
>> </filter-mapping>
>> </web-app>
>
> I tried patching the web.xml of the bookstore sample to use this
> web.xml and it seems to work perfectly - apart from allowing static
> files through.
>
> I've raised issue 208 with a patch...
> https://jersey.dev.java.net/issues/show_bug.cgi?id=208
>
> which adds a String property to the Jersey filter to allow a regex to
> be used to filter on the servlet path to allow things to be excluded.
> For example setting the value of the regex to
>
> /(images|css|js)/.*
>
> will allow most static content through on most web apps. I've patched
> the bookstore sample to have a simple CSS file then I use this
> configuration property in the web.xml to allow Jersey to let the
> default web container render static content while Jersey deals with
> the JAX-RS resources. There's a simple test case included too.
>

Thanks! very neat. Too late for 1.0.2 unfortunately but not for 1.0.3-
SNAPSHOT, i will commit it next week.

Paul.