01: package org.gomba.utils.servlet;
02:
03: import java.util.ArrayList;
04: import java.util.Collections;
05: import java.util.List;
06: import java.util.StringTokenizer;
07:
08: import javax.servlet.RequestDispatcher;
09: import javax.servlet.ServletContext;
10: import javax.servlet.ServletException;
11: import javax.servlet.http.HttpServletRequest;
12:
13: /**
14: * This class contains static methods useful for validating requests and
15: * obtaining typed parameters.
16: *
17: * @author Flavio Tordini
18: * @version $Id: ServletParameterUtils.java,v 1.4 2004/11/26 17:53:53 flaviotordini Exp $
19: * @see <a
20: * href="http://www.w3.org/TR/NOTE-datetime">http://www.w3.org/TR/NOTE-datetime
21: * </a>
22: */
23: public final class ServletParameterUtils {
24:
25: private final static List dummyList = Collections
26: .unmodifiableList(new ArrayList(0));
27:
28: /**
29: * Private constructor.
30: */
31: private ServletParameterUtils() {
32: // dummy
33: }
34:
35: /**
36: * Utility method to parse the HttpServletRequest.getPathInfo() result.
37: *
38: * @param request
39: * The HTTP request.
40: * @return A <code>List</code> of <code>String</code> objects.
41: */
42: public static List getPathElements(HttpServletRequest request) {
43: final String pathInfo = request.getPathInfo();
44: if (pathInfo == null) {
45: return dummyList;
46: }
47: StringTokenizer tokenizer = new StringTokenizer(pathInfo, "/");
48: List pathElements = new ArrayList(tokenizer.countTokens());
49: while (tokenizer.hasMoreTokens()) {
50: String token = tokenizer.nextToken();
51: pathElements.add(token);
52: }
53: return pathElements;
54: }
55:
56: public static RequestDispatcher getRequestDispatcher(
57: ServletContext context, String resourceNameOrPath)
58: throws ServletException {
59: RequestDispatcher dispatcher = context
60: .getNamedDispatcher(resourceNameOrPath);
61: if (dispatcher == null) {
62: dispatcher = context
63: .getRequestDispatcher(resourceNameOrPath);
64: }
65: if (dispatcher == null) {
66: throw new ServletException(
67: "Cannot get a RequestDispatcher for: "
68: + resourceNameOrPath);
69: }
70: return dispatcher;
71: }
72:
73: }
|