users@jersey.java.net

Using filters

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 03 Feb 2009 16:49:45 +0100

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>

Paul.