📄 filterdispatcher.java
字号:
/* * $Id: FilterDispatcher.java 530439 2007-04-19 15:00:20Z hermanns $ * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */package org.apache.struts2.dispatcher;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.URLDecoder;import java.util.ArrayList;import java.util.Calendar;import java.util.Enumeration;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.StringTokenizer;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.struts2.RequestUtils;import org.apache.struts2.StrutsConstants;import org.apache.struts2.StrutsStatics;import org.apache.struts2.dispatcher.mapper.ActionMapper;import org.apache.struts2.dispatcher.mapper.ActionMapping;import com.opensymphony.xwork2.inject.Inject;import com.opensymphony.xwork2.util.ClassLoaderUtil;import com.opensymphony.xwork2.util.profiling.UtilTimerStack;import com.opensymphony.xwork2.ActionContext;/** * Master filter for Struts that handles four distinct * responsibilities: * * <ul> * * <li>Executing actions</li> * * <li>Cleaning up the {@link ActionContext} (see note)</li> * * <li>Serving static content</li> * * <li>Kicking off XWork's interceptor chain for the request lifecycle</li> * * </ul> * * <p/> <b>IMPORTANT</b>: this filter must be mapped to all requests. Unless you know exactly what you are doing, always * map to this URL pattern: /* * * <p/> <b>Executing actions</b> * * <p/> This filter executes actions by consulting the {@link ActionMapper} and determining if the requested URL should * invoke an action. If the mapper indicates it should, <b>the rest of the filter chain is stopped</b> and the action is * invoked. This is important, as it means that filters like the SiteMesh filter must be placed <b>before</b> this * filter or they will not be able to decorate the output of actions. * * <p/> <b>Cleaning up the {@link ActionContext}</b> * * <p/> This filter will also automatically clean up the {@link ActionContext} for you, ensuring that no memory leaks * take place. However, this can sometimes cause problems integrating with other products like SiteMesh. See {@link * ActionContextCleanUp} for more information on how to deal with this. * * <p/> <b>Serving static content</b> * * <p/> 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: * * <ul> * * <li>org.apache.struts2.static</li> * * <li>template</li> * * </ul> * * <p/> 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". <b>Be * careful</b>, however, to expose any packages that may have sensitive information, such as properties file with * database access credentials. * * <p/> * * <p> * * This filter supports the following init-params: * <!-- START SNIPPET: params --> * * <ul> * * <li><b>config</b> - a comma-delimited list of XML configuration files to load.</li> * * <li><b>actionPackages</b> - a comma-delimited list of Java packages to scan for Actions.</li> * * <li><b>configProviders</b> - a comma-delimited list of Java classes that implement the * {@link com.opensymphony.xwork2.config.ConfigurationProvider} interface that should be used for building the {@link com.opensymphony.xwork2.config.Configuration}.</li> * * <li><b>*</b> - any other parameters are treated as framework constants.</li> * * </ul> * * <!-- END SNIPPET: params --> * * </p> * * To use a custom {@link Dispatcher}, the <code>createDispatcher()</code> method could be overriden by * the subclass. * * @see ActionMapper * @see ActionContextCleanUp * * @version $Date: 2007-04-19 17:00:20 +0200 (Do, 19 Apr 2007) $ $Id: FilterDispatcher.java 530439 2007-04-19 15:00:20Z hermanns $ */public class FilterDispatcher implements StrutsStatics, Filter { /** * Provide a logging instance. */ private static final Log LOG = LogFactory.getLog(FilterDispatcher.class); /** * Store set of path prefixes to use with static resources. */ private String[] pathPrefixes; /** * Provide a formatted date for setting heading information when caching static content. */ private final Calendar lastModifiedCal = Calendar.getInstance(); /** * Store state of StrutsConstants.STRUTS_SERVE_STATIC_CONTENT setting. */ private static boolean serveStatic; /** * Store state of StrutsConstants.STRUTS_SERVE_STATIC_BROWSER_CACHE setting. */ private static boolean serveStaticBrowserCache; /** * Store state of StrutsConstants.STRUTS_I18N_ENCODING setting. */ private static String encoding; /** * Provide ActionMapper instance, set by injection. */ private static ActionMapper actionMapper; /** * Provide FilterConfig instance, set on init. */ private FilterConfig filterConfig; /** * Expose Dispatcher instance to subclass. */ protected Dispatcher dispatcher; /** * Initializes the filter by creating a default dispatcher * and setting the default packages for static resources. * * @param filterConfig The filter configuration */ public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; dispatcher = createDispatcher(filterConfig); dispatcher.init(); String param = filterConfig.getInitParameter("packages"); String packages = "org.apache.struts2.static template org.apache.struts2.interceptor.debugging"; if (param != null) { packages = param + " " + packages; } this.pathPrefixes = parse(packages); } /** * Calls dispatcher.cleanup, * which in turn releases local threads and destroys any DispatchListeners. * * @see javax.servlet.Filter#destroy() */ public void destroy() { if (dispatcher == null) { LOG.warn("something is seriously wrong, Dispatcher is not initialized (null) "); } else { dispatcher.cleanup(); } } /** * Create a default {@link Dispatcher} that subclasses can override * with a custom Dispatcher, if needed. * * @param filterConfig Our FilterConfig * @return Initialized Dispatcher */ 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); } /** * Modify state of StrutsConstants.STRUTS_SERVE_STATIC_CONTENT setting. * @param val New setting */ @Inject(StrutsConstants.STRUTS_SERVE_STATIC_CONTENT) public static void setServeStaticContent(String val) { serveStatic = "true".equals(val); } /** * Modify state of StrutsConstants.STRUTS_SERVE_STATIC_BROWSER_CACHE setting. * @param val New setting */ @Inject(StrutsConstants.STRUTS_SERVE_STATIC_BROWSER_CACHE) public static void setServeStaticBrowserCache(String val) { serveStaticBrowserCache = "true".equals(val); } /** * Modify state of StrutsConstants.STRUTS_I18N_ENCODING setting. * @param val New setting */ @Inject(StrutsConstants.STRUTS_I18N_ENCODING) public static void setEncoding(String val) { encoding = val; } /** * Modify ActionMapper instance. * @param mapper New instance */ @Inject public static void setActionMapper(ActionMapper mapper) { actionMapper = mapper; } /** * Provide a workaround for some versions of WebLogic. * <p/> * 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -