net.sourceforge.stripes.controller
Class DynamicMappingFilter

java.lang.Object
  extended by net.sourceforge.stripes.controller.DynamicMappingFilter
All Implemented Interfaces:
Filter

public class DynamicMappingFilter
extends Object
implements Filter

A servlet filter that dynamically maps URLs to ActionBeans. This filter can be used to allow Stripes to dispatch requests to ActionBeans based on their URL binding, even if the URL to which they are bound is not explicitly mapped in web.xml.

There are a few caveats that must be observed when using this filter:

StripesFilter and DispatcherServlet need not be mapped to any URL patterns in web.xml since this filter will determine at runtime whether or not they need to be invoked. In fact, you don't even need to define DispatcherServlet in web.xml at all because this filter uses an instance it creates and manages internally. However, some resources, such as JSPs, may require access to the Stripes Configuration. Thus, StripesFilter should be mapped to *.jsp if you intend to access JSPs directly.

This filter takes the following approach to determining when to dispatch an ActionBean:

  1. Allow the request to process normally, trapping any HTTP errors that are returned.
  2. If no error was returned then do nothing, allowing the request to complete successfully. If any error other than 404 was returned then send the error through. Otherwise ...
  3. Check the ActionResolver to see if an ActionBean is mapped to the URL. If not, then send the 404 error through. Otherwise...
  4. Invoke StripesFilter and DispatcherServlet

One benefit of this approach is that static resources can be delivered from the same namespace to which an ActionBean is mapped using clean URLs. (Form more information on clean URLs, see UrlBinding.) For example, if your UserActionBean is mapped to @UrlBinding("/user/{id}/{$event}") and you have a static file at /user/icon.gif, then your icon will be delivered correctly because the initial request will not have returned a 404 error.

This filter accepts one init-param. IncludeBufferSize (optional, default 1024) sets the number of characters to be buffered by DynamicMappingFilter.TempBufferWriter for include requests. See DynamicMappingFilter.TempBufferWriter for more information.

This is the suggested mapping for this filter in web.xml.

  <filter>
      <description>Dynamically maps URLs to ActionBeans.</description>
      <display-name>Stripes Dynamic Mapping Filter</display-name>
      <filter-name>DynamicMappingFilter</filter-name>
      <filter-class>
          net.sourceforge.stripes.controller.DynamicMappingFilter
      </filter-class>
  </filter>
  
  <filter-mapping>
      <filter-name>DynamicMappingFilter</filter-name>
      <url-pattern>/*</url-pattern>
      <dispatcher>REQUEST</dispatcher>
      <dispatcher>FORWARD</dispatcher>
      <dispatcher>INCLUDE</dispatcher>
  </filter-mapping>
 

Since:
Stripes 1.5
Author:
Ben Gunter
See Also:
UrlBinding

Nested Class Summary
static class DynamicMappingFilter.ErrorTrappingResponseWrapper
          An HttpServletResponseWrapper that traps HTTP errors by overriding sendError(int, ..).
static class DynamicMappingFilter.TempBufferWriter
           A Writer that passes characters to a PrintWriter.
 
Constructor Summary
DynamicMappingFilter()
           
 
Method Summary
 void destroy()
           
 void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
           
protected  void doOneTimeConfiguration()
          Perform initialization that can't be done in init(..).
protected  String getRequestURI(HttpServletRequest request)
          Deprecated. Use HttpUtil.getRequestedPath(HttpServletRequest) instead.
 void init(FilterConfig config)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DynamicMappingFilter

public DynamicMappingFilter()
Method Detail

init

public void init(FilterConfig config)
          throws ServletException
Specified by:
init in interface Filter
Throws:
ServletException

destroy

public void destroy()
Specified by:
destroy in interface Filter

doFilter

public void doFilter(ServletRequest request,
                     ServletResponse response,
                     FilterChain chain)
              throws IOException,
                     ServletException
Specified by:
doFilter in interface Filter
Throws:
IOException
ServletException

doOneTimeConfiguration

protected void doOneTimeConfiguration()
                               throws ServletException
Perform initialization that can't be done in init(..). This is normally called only once, on the first invocation of doFilter(..).

Throws:
ServletException

getRequestURI

@Deprecated
protected String getRequestURI(HttpServletRequest request)
Deprecated. Use HttpUtil.getRequestedPath(HttpServletRequest) instead.

Get the context-relative URI of the current include, forward or request.



? Copyright 2005-2006, Stripes Development Team.