package org.gomba.utils.servlet;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
/**
* This class contains static methods useful for validating requests and
* obtaining typed parameters.
*
* @author Flavio Tordini
* @version $Id: ServletParameterUtils.java,v 1.4 2004/11/26 17:53:53 flaviotordini Exp $
* @see <a
* href="http://www.w3.org/TR/NOTE-datetime">http://www.w3.org/TR/NOTE-datetime
* </a>
*/
public final class ServletParameterUtils {
private final static List dummyList = Collections
.unmodifiableList(new ArrayList(0));
/**
* Private constructor.
*/
private ServletParameterUtils() {
// dummy
}
/**
* Utility method to parse the HttpServletRequest.getPathInfo() result.
*
* @param request
* The HTTP request.
* @return A <code>List</code> of <code>String</code> objects.
*/
public static List getPathElements(HttpServletRequest request) {
final String pathInfo = request.getPathInfo();
if (pathInfo == null) {
return dummyList;
}
StringTokenizer tokenizer = new StringTokenizer(pathInfo, "/");
List pathElements = new ArrayList(tokenizer.countTokens());
while (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
pathElements.add(token);
}
return pathElements;
}
public static RequestDispatcher getRequestDispatcher(
ServletContext context, String resourceNameOrPath)
throws ServletException {
RequestDispatcher dispatcher = context
.getNamedDispatcher(resourceNameOrPath);
if (dispatcher == null) {
dispatcher = context.getRequestDispatcher(resourceNameOrPath);
}
if (dispatcher == null) {
throw new ServletException("Cannot get a RequestDispatcher for: "
+ resourceNameOrPath);
}
return dispatcher;
}
}