RE: Change the ".do" extension

2003-09-29 Thread Matt Raible
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

RE: Change the ".do" extension

2003-09-29 Thread Steve Raeburn
http://jakarta.apache.org/struts/userGuide/configuration.html#dd_config_mapp
ing

Steve

> -Original Message-
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
> Sent: September 29, 2003 7: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]
>
>



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



RE: Change the ".do" extension

2003-09-29 Thread Matt Raible
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]

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



Re: Change the ".do" extension

2003-09-29 Thread Martin Gainty
Dirk-

in web.xml change





action

*.do



to whatever extension you wish to trigger action..

Vielen Dank,

-Martin

- Original Message - 
From: "Dirk Behrendt" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Monday, September 29, 2003 10:09 AM
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]
> 
> 

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