Here's the code - enjoy. I wrote a different one for PathRequestFilter
(i.e. /do/*), but this one should work for both - my local versions have a
lot of extra logic in it - so I trimmed that. This is untested and I don't
even know if it'll compile. I'll be adding to my appfuse project shortly,
so I'll know in the next week or two.
Here's the settings in web.xml:
requestFilter
Request Filter
org.appfuse.webapp.filter.ExtensionRequestFilter
allowedExtensions
.html,.asp,.cfm
ignorePaths
/images,/includes
requestFilter
/*
ExtensionRequestFilter.java
/**
* This class is used to filter all requests and to determine
* if a request should forward to an Action.
*
* @author Matt Raible
* @version $Revision: 1.2 $ $Date: 2003/09/29 15:28:41 $
*/
public class ExtensionRequestFilter implements Filter {
//~ Static fields/initializers
=
private static String[] allowedExtensions = null;
private static String[] ignorePaths = null;
//~ Instance fields
/**
* The Log instance for this class
*/
private Log log = LogFactory.getLog(ExtensionRequestFilter.class);
private FilterConfig config = null;
//~ Methods
public void init(FilterConfig config) throws ServletException {
this.config = config;
allowedExtensions =
StringUtils.split(config.getInitParameter("allowedExtensions"),
",");
ignorePaths =
StringUtils.split(config.getInitParameter("ignorePaths"), ",");
}
/**
* Destroys the filter.
*/
public void destroy() {
config = null;
}
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain)
throws IOException, ServletException {
// cast to the types I want to use
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
// flag to attempt to process this URL
boolean process = true;
// get the requested path
String path = request.getServletPath();
// we don't care to check the ignoredPaths
for (int j = 0; j < ignorePaths.length; j++) {
if (path.startsWith(ignorePaths[j])) {
process = false;
break;
}
}
if (process && !StringUtils.equals(path, "/") &&
!StringUtils.equals(path, "")) {
// get Struts' configuration
ModuleConfig m = getModuleConfig(request);
// get a list of actions
ActionConfig[] actions = m.findActionConfigs();
// check to see if path ends with a vanity extension
for (int j = 0; j < allowedExtensions.length; j++) {
if (path.endsWith(allowedExtensions[j])) {
path =
path.substring(0,
path.indexOf(allowedExtensions[j]));
break;
}
}
String params = RequestUtil.getRequestParameters(request);
StringBuffer url = new StringBuffer();
// check all the actions to see if we have a match
for (int i = 0; i < actions.length; i++) {
ActionConfig action = actions[i];
String actionPath = action.getPath();
if (StringUtils.equalsIgnoreCase(actionPath, path)) {
url.append(actionPath + ".html");
url.append((!StringUtils.isEmpty(params)) ? ("?" +
params)
: "");
log.debug("requesting action: " + actionPath);
RequestDispatcher dispatcher =
request.getRequestDispatcher(url.toString());
dispatcher.forward(request, response);
return;
}
}
}
chain.doFilter(request, response);
}
/**
* Return the module configuration object for the currently selected
* module. MR - Copied from Struts' ActionServlet
*
* @param request The servlet request we are processing
* @since Struts 1.1
*/
protected ModuleConfig getModuleConfig(HttpServletRequest request) {
ModuleConfig mConfig =
(ModuleConfig) request.getAttribute(Globals.MODULE_KEY);
if (mConfig == null) {
mConfig =
(ModuleConfig)
config.getServletContext().getAttribute(Globals.MODULE_KEY);
}
return mConfig;
}
}
-Original Message-
From: Dirk Behrendt [mailto:[EMAIL PROTECTED]
Sent: Monday, September 29, 2003 11:59 AM
To: [EMAIL PROTECTED]
Subject: Change the ".do" extension
H