1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package nl.hippo.client.webapp;
17
18 import java.io.UnsupportedEncodingException;
19 import java.net.URLDecoder;
20 import java.util.regex.Pattern;
21
22 import javax.servlet.ServletContext;
23 import javax.servlet.http.HttpServletRequest;
24
25 public class Request {
26
27 private String path;
28 private String contextPath;
29 private ServletContext context;
30
31 public Request(ServletContext context, HttpServletRequest req) {
32 try {
33 this.context = context;
34 this.contextPath = req.getContextPath();
35 this.path = req.getRequestURI().substring(contextPath.length());
36 this.path = URLDecoder.decode(path, "UTF-8");
37 } catch (UnsupportedEncodingException e) {
38
39 }
40 }
41
42 public String getPath() {
43 return path;
44 }
45
46 public String getContextPath() {
47 return contextPath;
48 }
49
50 public ServletContext getContext() {
51 return context;
52 }
53
54 public String[] getParameters(Pattern regexp) {
55 java.util.regex.Matcher matcher = regexp.matcher(path);
56 matcher.matches();
57
58 int groupCount = matcher.groupCount();
59 String[] parts = new String[groupCount];
60 for (int i = 0; i < groupCount; i++) {
61 parts[i] = matcher.group(i);
62 }
63 return parts;
64 }
65
66 public String toString() {
67 return path;
68 }
69
70 public String toString(Pattern regexp) {
71 String[] params = getParameters(regexp);
72 if (params == null) {
73 return "null";
74 }
75 if (params.length == 0) {
76 return "[]";
77 }
78 StringBuffer buf = new StringBuffer();
79 for (int i = 0; i < params.length; i++) {
80 if (i == 0) {
81 buf.append("[");
82 } else {
83 buf.append(", ");
84 }
85 buf.append(String.valueOf(params[i]));
86 }
87 buf.append("]");
88 return buf.toString();
89 }
90
91 }