org.apache.struts2.dispatcher
public class: FilterDispatcher [javadoc |
source]
java.lang.Object
org.apache.struts2.dispatcher.FilterDispatcher
All Implemented Interfaces:
Filter, StrutsStatics
Direct Known Subclasses:
FilterDispatcherCompatWeblogic61
Deprecated! Since - Struts 2.1.3, use
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter instead or
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter and
org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter
if needing using the
ActionContextCleanUp filter in addition to this one
Master filter for Struts that handles four distinct
responsibilities:
- Executing actions
- Cleaning up the ActionContext (see note)
- Serving static content
- Kicking off XWork's interceptor chain for the request lifecycle
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:
- org.apache.struts2.static
- template
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:
- config - a comma-delimited list of XML configuration files to load.
- actionPackages - a comma-delimited list of Java packages to scan for Actions.
- configProviders - a comma-delimited list of Java classes that implement the
ConfigurationProvider interface that should be used for building the Configuration .
- loggerFactory - The class name of the LoggerFactory implementation.
- * - any other parameters are treated as framework constants.
To use a custom
Dispatcher , the
createDispatcher() method could be overriden by
the subclass.
| Field Summary |
|---|
| protected Dispatcher | dispatcher | Expose Dispatcher instance to subclass. |
| protected StaticContentLoader | staticResourceLoader | Loads stattic resources, set by injection |
| Methods from java.lang.Object: |
|---|
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method from org.apache.struts2.dispatcher.FilterDispatcher Detail: |
protected Dispatcher createDispatcher(FilterConfig filterConfig) {
Map< String, String > params = new HashMap< String, String >();
for (Enumeration e = filterConfig.getInitParameterNames(); e.hasMoreElements();) {
String name = (String) e.nextElement();
String value = filterConfig.getInitParameter(name);
params.put(name, value);
}
return new Dispatcher(filterConfig.getServletContext(), params);
} Deprecated!Create a default Dispatcher that subclasses can override
with a custom Dispatcher, if needed. |
public void destroy() {
if (dispatcher == null) {
log.warn("something is seriously wrong, Dispatcher is not initialized (null) ");
} else {
try {
dispatcher.cleanup();
} finally {
ActionContext.setContext(null);
}
}
} Deprecated!Calls dispatcher.cleanup,
which in turn releases local threads and destroys any DispatchListeners. |
public void doFilter(ServletRequest req,
ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
ServletContext servletContext = getServletContext();
String timerKey = "FilterDispatcher_doFilter: ";
try {
// FIXME: this should be refactored better to not duplicate work with the action invocation
ValueStack stack = dispatcher.getContainer().getInstance(ValueStackFactory.class).createValueStack();
ActionContext ctx = new ActionContext(stack.getContext());
ActionContext.setContext(ctx);
UtilTimerStack.push(timerKey);
request = prepareDispatcherAndWrapRequest(request, response);
ActionMapping mapping;
try {
mapping = actionMapper.getMapping(request, dispatcher.getConfigurationManager());
} catch (Exception ex) {
log.error("error getting ActionMapping", ex);
dispatcher.sendError(request, response, servletContext, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ex);
return;
}
if (mapping == null) {
// there is no action in this request, should we look for a static resource?
String resourcePath = RequestUtils.getServletPath(request);
if ("".equals(resourcePath) && null != request.getPathInfo()) {
resourcePath = request.getPathInfo();
}
if (staticResourceLoader.canHandle(resourcePath)) {
staticResourceLoader.findStaticResource(resourcePath, request, response);
} else {
// this is a normal request, let it pass through
chain.doFilter(request, response);
}
// The framework did its job here
return;
}
dispatcher.serviceAction(request, response, servletContext, mapping);
} finally {
try {
ActionContextCleanUp.cleanUp(req);
} finally {
UtilTimerStack.pop(timerKey);
}
}
} Deprecated!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. |
protected FilterConfig getFilterConfig() {
return filterConfig;
} Deprecated!Expose the FilterConfig instance. |
protected ServletContext getServletContext() {
return filterConfig.getServletContext();
} Deprecated!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. |
public void init(FilterConfig filterConfig) throws ServletException {
try {
this.filterConfig = filterConfig;
initLogging();
dispatcher = createDispatcher(filterConfig);
dispatcher.init();
dispatcher.getContainer().inject(this);
staticResourceLoader.setHostConfig(new FilterHostConfig(filterConfig));
} finally {
ActionContext.setContext(null);
}
} Deprecated!Initializes the filter by creating a default dispatcher
and setting the default packages for static resources. |
protected HttpServletRequest prepareDispatcherAndWrapRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException {
Dispatcher du = Dispatcher.getInstance();
// Prepare and wrap the request if the cleanup filter hasn't already, cleanup filter should be
// configured first before struts2 dispatcher filter, hence when its cleanup filter's turn,
// static instance of Dispatcher should be null.
if (du == null) {
Dispatcher.setInstance(dispatcher);
// prepare the request no matter what - this ensures that the proper character encoding
// is used before invoking the mapper (see WW-9127)
dispatcher.prepare(request, response);
} else {
dispatcher = du;
}
try {
// Wrap request first, just in case it is multipart/form-data
// parameters might not be accessible through before encoding (ww-1278)
request = dispatcher.wrapRequest(request, getServletContext());
} catch (IOException e) {
String message = "Could not wrap servlet request with MultipartRequestWrapper!";
log.error(message, e);
throw new ServletException(message, e);
}
return request;
} Deprecated!Wrap and return the given request, if needed, so as to to transparently
handle multipart data as a wrapped class around the given request. |
public void setActionMapper(ActionMapper mapper) {
actionMapper = mapper;
} Deprecated!Modify ActionMapper instance. |
public void setStaticResourceLoader(StaticContentLoader staticResourceLoader) {
this.staticResourceLoader = staticResourceLoader;
} Deprecated!Modify state of StrutsConstants.STRUTS_STATIC_CONTENT_LOADER setting. |