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:

  <filter>
    <filter-name>requestFilter</filter-name>
    <display-name>Request Filter</display-name>
 
<filter-class>org.appfuse.webapp.filter.ExtensionRequestFilter</filter-class
>
    <init-param>
      <param-name>allowedExtensions</param-name>
      <param-value>.html,.asp,.cfm</param-value>
    </init-param>
    <init-param>
      <param-name>ignorePaths</param-name>
      <param-value>/images,/includes</param-value>
    </init-param>
  </filter>

  <filter-mapping>
    <filter-name>requestFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

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 <code>Log</code> 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


Hello Matt!
 
That sounds good.
 
But can you give full code examples (web.xml, other necessary codes)? I
do not know, how to realize it.
 
 
Dirk
 
From: Matt Raible <[EMAIL PROTECTED]>
Subject: Change the ".do" extension
Date: Mon, 29 Sep 2003 09:27:05 -0500
Content-Type: text/plain;
            charset="iso-8859-1"
 
Yes this is possible.  I use a filter mapped to /* and perform logic to
lookup all the actions and attempt a match, then forward to the action
if
it's found.
 
http://www.raibledesigns.com/page/rd?anchor=vanity_urls_in_struts
 
HTH,
 
Matt
 
-----Original Message-----
From: Dirk Behrendt [mailto:[EMAIL PROTECTED]
Sent: Monday, September 29, 2003 8:09 AM
To: [EMAIL PROTECTED]
Subject: Change the ".do" extension
 
 
Hello!
 
Is there a possibility to get rid of the .do extension in the actions?
 
For example:
 
 http://localhost/login.do
 
should be
--> http://localhost/login
 
 
Ist this possible?
 
Thanks!
 
Dirk

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to