org.apache.struts2.dispatcher
Class FilterDispatcher

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

public class FilterDispatcher
extends java.lang.Object
implements javax.servlet.Filter, StrutsStatics

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.

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

Version:
$Date: 2006-10-10 07:31:52 -0500 (Tue, 10 Oct 2006) $ $Id: FilterDispatcher.java 454720 2006-10-10 12:31:52Z tmjee $
See Also:
org.apache.struts2.lifecycle.LifecycleListener, ActionMapper, ActionContextCleanUp

Field Summary
 
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  void copy(java.io.InputStream input, java.io.OutputStream output)
          Copies the from the input stream to the output stream
protected  Dispatcher createDispatcher()
          Create a Dispatcher, this serves as a hook for subclass to overried such that a custom Dispatcher could be created.
 void destroy()
          Cleans up the dispatcher
 void doFilter(javax.servlet.ServletRequest req, javax.servlet.ServletResponse res, javax.servlet.FilterChain chain)
           
protected  java.io.InputStream findInputStream(java.lang.String name, java.lang.String packagePrefix)
          Looks for a static resource in the classpath
protected  void findStaticResource(java.lang.String name, javax.servlet.http.HttpServletResponse response)
          Fins a static resource
protected  java.lang.String getContentType(java.lang.String name)
          Determines the content type for the resource name
protected  javax.servlet.FilterConfig getFilterConfig()
          Gets this filter's configuration
protected  javax.servlet.ServletContext getServletContext(javax.servlet.http.HttpSession session)
          Servlet 2.3 specifies that the servlet context can be retrieved from the session.
 void init(javax.servlet.FilterConfig filterConfig)
          Initializes the dispatcher and filter
protected  java.lang.String[] parse(java.lang.String packages)
          Parses the list of packages
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FilterDispatcher

public FilterDispatcher()
Method Detail

getFilterConfig

protected javax.servlet.FilterConfig getFilterConfig()
Gets this filter's configuration

Returns:
The filter config

destroy

public void destroy()
Cleans up the dispatcher

Specified by:
destroy in interface javax.servlet.Filter

init

public void init(javax.servlet.FilterConfig filterConfig)
          throws javax.servlet.ServletException
Initializes the dispatcher and filter

Specified by:
init in interface javax.servlet.Filter
Throws:
javax.servlet.ServletException

parse

protected java.lang.String[] parse(java.lang.String packages)
Parses the list of packages

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

doFilter

public void doFilter(javax.servlet.ServletRequest req,
                     javax.servlet.ServletResponse res,
                     javax.servlet.FilterChain chain)
              throws java.io.IOException,
                     javax.servlet.ServletException
Specified by:
doFilter in interface javax.servlet.Filter
Throws:
java.io.IOException
javax.servlet.ServletException

getServletContext

protected javax.servlet.ServletContext getServletContext(javax.servlet.http.HttpSession session)
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.

Parameters:
session - the HTTP session where, in Servlet 2.3, the servlet context can be retrieved
Returns:
the servlet context.

findStaticResource

protected void findStaticResource(java.lang.String name,
                                  javax.servlet.http.HttpServletResponse response)
                           throws java.io.IOException
Fins a static resource

Parameters:
name - The resource name
response - The request
Throws:
java.io.IOException - If anything goes wrong

getContentType

protected java.lang.String getContentType(java.lang.String name)
Determines the content type for the resource name

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

copy

protected void copy(java.io.InputStream input,
                    java.io.OutputStream output)
             throws java.io.IOException
Copies the from the input stream to the output stream

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

findInputStream

protected java.io.InputStream findInputStream(java.lang.String name,
                                              java.lang.String packagePrefix)
                                       throws java.io.IOException
Looks 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:
java.io.IOException - If there is a problem locating the resource

createDispatcher

protected Dispatcher createDispatcher()
Create a Dispatcher, this serves as a hook for subclass to overried such that a custom Dispatcher could be created.

Returns:
Dispatcher


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