01: /*
02: * requestPath.java
03: *
04: * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package org.pnuts.servlet;
10:
11: import pnuts.servlet.*;
12: import pnuts.lang.Context;
13: import pnuts.lang.PnutsFunction;
14: import pnuts.lang.Package;
15: import javax.servlet.*;
16: import javax.servlet.http.*;
17: import java.util.ArrayList;
18: import java.util.StringTokenizer;
19:
20: /*
21: * function requestPath()
22: */
23: public class requestPath extends PnutsFunction {
24: public requestPath() {
25: super ("requestPath");
26: }
27:
28: public boolean defined(int nargs) {
29: return (nargs == 0);
30: }
31:
32: protected Object exec(Object args[], Context context) {
33: if (args.length != 0) {
34: undefined(args, context);
35: return null;
36: }
37: HttpServletRequest request = (HttpServletRequest) context
38: .get(PnutsServlet.SERVLET_REQUEST);
39: // String pathInfo = request.getPathInfo();
40: String uri = request.getRequestURI().substring(
41: request.getContextPath().length());
42: ArrayList requestPaths = new ArrayList();
43: if (uri != null) {
44: String s = uri;
45: if (s.startsWith("/")) {
46: s = s.substring(1);
47: }
48: if (s.length() > 0) {
49: String[] tokens = s.split("/");
50: for (int i = 0; i < tokens.length; i++) {
51: requestPaths.add(tokens[i]);
52: }
53: }
54: }
55: return requestPaths;
56: }
57:
58: public String toString() {
59: return "function requestPath()";
60: }
61: }
|