org.apache.struts2.dispatcher
Class FilterDispatcher

java.lang.Object
  extended by org.apache.struts2.dispatcher.FilterDispatcher
All Implemented Interfaces:
Filter, StrutsStatics
Direct Known Subclasses:
FilterDispatcherCompatWeblogic61

public class FilterDispatcher
extends Object
implements StrutsStatics, Filter

Master filter for Struts that handles four distinct responsibilities:

IMPORTANT: this filter must be mapped to all requests. Unless you know exactly what you are doing, always map to this URL pattern: /*

Executing actions

This filter executes actions by consulting the ActionMapper and determining if the requested URL should invoke an action. If the mapper indicates it should, the rest of the filter chain is stopped and the action is invoked. This is important, as it means that filters like the SiteMesh filter must be placed before this filter or they will not be able to decorate the output of actions.

Cleaning up the ActionContext

This filter will also automatically clean up the ActionContext for you, ensuring that no memory leaks take place. However, this can sometimes cause problems integrating with other products like SiteMesh. See ActionContextCleanUp for more information on how to deal with this.

Serving static content

This filter also serves common static content needed when using various parts of Struts, such as JavaScript files, CSS files, etc. It works by looking for requests to /struts/*, and then mapping the value after "/struts/" to common packages in Struts and, optionally, in your class path. By default, the following packages are automatically searched:

This means that you can simply request /struts/xhtml/styles.css and the XHTML UI theme's default stylesheet will be returned. Likewise, many of the AJAX UI components require various JavaScript files, which are found in the org.apache.struts2.static package. If you wish to add additional packages to be searched, you can add a comma separated (space, tab and new line will do as well) list in the filter init parameter named "packages". Be careful, however, to expose any packages that may have sensitive information, such as properties file with database access credentials.

This filter supports the following init-params:

To use a custom Dispatcher, the createDispatcher() method could be overriden by the subclass.

Version:
$Date: 2008-10-27 22:46:04 +0100 (Mo, 27. Okt 2008) $ $Id: FilterDispatcher.java 708334 2008-10-27 21:46:04Z rgielen $
See Also:
ActionMapper, ActionContextCleanUp

Field Summary
private static ActionMapper actionMapper
          Provide ActionMapper instance, set by injection.
(package private) static String DEFAULT_STATIC_PACKAGES
           
protected  Dispatcher dispatcher
          Expose Dispatcher instance to subclass.
private static String encoding
          Store state of StrutsConstants.STRUTS_I18N_ENCODING setting.
private  FilterConfig filterConfig
          Provide FilterConfig instance, set on init.
private  Calendar lastModifiedCal
          Provide a formatted date for setting heading information when caching static content.
private static Log LOG
          Provide a logging instance.
(package private)  String[] pathPrefixes
          Store set of path prefixes to use with static resources.
private static boolean serveStatic
          Store state of StrutsConstants.STRUTS_SERVE_STATIC_CONTENT setting.
private static boolean serveStaticBrowserCache
          Store state of StrutsConstants.STRUTS_SERVE_STATIC_BROWSER_CACHE setting.
 
Fields inherited from interface org.apache.struts2.StrutsStatics
HTTP_REQUEST, HTTP_RESPONSE, PAGE_CONTEXT, SERVLET_CONTEXT, SERVLET_DISPATCHER, STRUTS_PORTLET_CONTEXT
 
Constructor Summary
FilterDispatcher()
           
 
Method Summary
protected  String buildPath(String name, String packagePrefix)
           
protected  String cleanupPath(String path)
           
protected  void copy(InputStream input, OutputStream output)
          Copy bytes from the input stream to the output stream.
protected  Dispatcher createDispatcher(FilterConfig filterConfig)
          Create a default Dispatcher that subclasses can override with a custom Dispatcher, if needed.
 void destroy()
          Calls dispatcher.cleanup, which in turn releases local threads and destroys any DispatchListeners.
 void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
          Process an action or handle a request a static resource.
protected  List<URL> findAndCheckResources(String path)
          Locate a static classpath resource and check for safety constraints.
protected  InputStream findInputStream(String name, String packagePrefix)
          Look for a static resource in the classpath.
protected  URL findResource(String path)
          Look for a static resource in the classpath.
 void findStaticResource(String path, List<URL> resourceUrls, HttpServletRequest request, HttpServletResponse response)
          Locate a static resource and copy directly to the response, setting the appropriate caching headers.
protected  String getContentType(String name)
          Determine the content type for the resource name.
protected  FilterConfig getFilterConfig()
          Expose the FilterConfig instance.
protected  ServletContext getServletContext()
          Provide a workaround for some versions of WebLogic.
 void init(FilterConfig filterConfig)
          Initializes the filter by creating a default dispatcher and setting the default packages for static resources.
protected  String[] parse(String packages)
          Create a string array from a comma-delimited list of packages.
protected  HttpServletRequest prepareDispatcherAndWrapRequest(HttpServletRequest request, HttpServletResponse response)
          Wrap and return the given request, if needed, so as to to transparently handle multipart data as a wrapped class around the given request.
protected  void process(InputStream is, String path, HttpServletRequest request, HttpServletResponse response)
           
static void setActionMapper(ActionMapper mapper)
          Modify ActionMapper instance.
static void setEncoding(String val)
          Modify state of StrutsConstants.STRUTS_I18N_ENCODING setting.
static void setServeStaticBrowserCache(String val)
          Modify state of StrutsConstants.STRUTS_SERVE_STATIC_BROWSER_CACHE setting.
static void setServeStaticContent(String val)
          Modify state of StrutsConstants.STRUTS_SERVE_STATIC_CONTENT setting.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOG

private static final Log LOG
Provide a logging instance.


DEFAULT_STATIC_PACKAGES

static final String DEFAULT_STATIC_PACKAGES
See Also:
Constant Field Values

pathPrefixes

String[] pathPrefixes
Store set of path prefixes to use with static resources.


lastModifiedCal

private final Calendar lastModifiedCal
Provide a formatted date for setting heading information when caching static content.


serveStatic

private static boolean serveStatic
Store state of StrutsConstants.STRUTS_SERVE_STATIC_CONTENT setting.


serveStaticBrowserCache

private static boolean serveStaticBrowserCache
Store state of StrutsConstants.STRUTS_SERVE_STATIC_BROWSER_CACHE setting.


encoding

private static String encoding
Store state of StrutsConstants.STRUTS_I18N_ENCODING setting.


actionMapper

private static ActionMapper actionMapper
Provide ActionMapper instance, set by injection.


filterConfig

private FilterConfig filterConfig
Provide FilterConfig instance, set on init.


dispatcher

protected Dispatcher dispatcher
Expose Dispatcher instance to subclass.

Constructor Detail

FilterDispatcher

public FilterDispatcher()
Method Detail

init

public void init(FilterConfig filterConfig)
          throws ServletException
Initializes the filter by creating a default dispatcher and setting the default packages for static resources.

Specified by:
init in interface Filter
Parameters:
filterConfig - The filter configuration
Throws:
ServletException

destroy

public void destroy()
Calls dispatcher.cleanup, which in turn releases local threads and destroys any DispatchListeners.

Specified by:
destroy in interface Filter
See Also:
Filter.destroy()

createDispatcher

protected Dispatcher createDispatcher(FilterConfig filterConfig)
Create a default Dispatcher that subclasses can override with a custom Dispatcher, if needed.

Parameters:
filterConfig - Our FilterConfig
Returns:
Initialized Dispatcher

setServeStaticContent

public static void setServeStaticContent(String val)
Modify state of StrutsConstants.STRUTS_SERVE_STATIC_CONTENT setting.

Parameters:
val - New setting

setServeStaticBrowserCache

public static void setServeStaticBrowserCache(String val)
Modify state of StrutsConstants.STRUTS_SERVE_STATIC_BROWSER_CACHE setting.

Parameters:
val - New setting

setEncoding

public static void setEncoding(String val)
Modify state of StrutsConstants.STRUTS_I18N_ENCODING setting.

Parameters:
val - New setting

setActionMapper

public static void setActionMapper(ActionMapper mapper)
Modify ActionMapper instance.

Parameters:
mapper - New instance

getServletContext

protected ServletContext getServletContext()
Provide a workaround for some versions of WebLogic.

Servlet 2.3 specifies that the servlet context can be retrieved from the session. Unfortunately, some versions of WebLogic can only retrieve the servlet context from the filter config. Hence, this method enables subclasses to retrieve the servlet context from other sources.

Returns:
the servlet context.

getFilterConfig

protected FilterConfig getFilterConfig()
Expose the FilterConfig instance.

Returns:
Our FilterConfit instance

prepareDispatcherAndWrapRequest

protected HttpServletRequest prepareDispatcherAndWrapRequest(HttpServletRequest request,
                                                             HttpServletResponse response)
                                                      throws ServletException
Wrap and return the given request, if needed, so as to to transparently handle multipart data as a wrapped class around the given request.

Parameters:
request - Our ServletRequest object
response - Our ServerResponse object
Returns:
Wrapped HttpServletRequest object
Throws:
ServletException - on any error

parse

protected String[] parse(String packages)
Create a string array from a comma-delimited list of packages.

Parameters:
packages - A comma-delimited String listing packages
Returns:
A string array of packages

doFilter

public void doFilter(ServletRequest req,
                     ServletResponse res,
                     FilterChain chain)
              throws IOException,
                     ServletException
Process an action or handle a request a static resource.

The filter tries to match the request to an action mapping. If mapping is found, the action processes is delegated to the dispatcher's serviceAction method. If action processing fails, doFilter will try to create an error page via the dispatcher.

Otherwise, if the request is for a static resource, the resource is copied directly to the response, with the appropriate caching headers set.

If the request does not match an action mapping, or a static resource page, then it passes through.

Specified by:
doFilter in interface Filter
Throws:
IOException
ServletException
See Also:
Filter.doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)

findStaticResource

public void findStaticResource(String path,
                               List<URL> resourceUrls,
                               HttpServletRequest request,
                               HttpServletResponse response)
                        throws IOException
Locate a static resource and copy directly to the response, setting the appropriate caching headers.

Parameters:
path - The resource path
resourceUrls - List of matching resource URLs
request - The request
response - The response
Throws:
IOException - If anything goes wrong

findAndCheckResources

protected List<URL> findAndCheckResources(String path)
                                   throws IOException
Locate a static classpath resource and check for safety constraints.

Parameters:
path - The resource path to check for available resources
Returns:
verified classpath resource URLs
Throws:
IOException - If anything goes wrong

findResource

protected URL findResource(String path)
                    throws IOException
Look for a static resource in the classpath.

Parameters:
path - The resource path
Returns:
The inputstream of the resource
Throws:
IOException - If there is a problem locating the resource

buildPath

protected String buildPath(String name,
                           String packagePrefix)
                    throws UnsupportedEncodingException
Parameters:
name - resource name
packagePrefix - The package prefix to use to locate the resource
Returns:
full path
Throws:
UnsupportedEncodingException
IOException

process

protected void process(InputStream is,
                       String path,
                       HttpServletRequest request,
                       HttpServletResponse response)
                throws IOException
Throws:
IOException

getContentType

protected String getContentType(String name)
Determine the content type for the resource name.

Parameters:
name - The resource name
Returns:
The mime type

copy

protected void copy(InputStream input,
                    OutputStream output)
             throws IOException
Copy bytes from the input stream to the output stream.

Parameters:
input - The input stream
output - The output stream
Throws:
IOException - If anything goes wrong

findInputStream

protected InputStream findInputStream(String name,
                                      String packagePrefix)
                               throws IOException
Look for a static resource in the classpath.

Parameters:
name - The resource name
packagePrefix - The package prefix to use to locate the resource
Returns:
The inputstream of the resource
Throws:
IOException - If there is a problem locating the resource

cleanupPath

protected String cleanupPath(String path)
Parameters:
path - requested path
Returns:
path without leading "/struts" or "/static"


Copyright © 2000-2008 Apache Software Foundation. All Rights Reserved.