View Javadoc

1   /*
2    * Copyright 2008 Hippo
3    *
4    * Licensed under the Apache License, Version 2.0 (the  "License"); 
5    * you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" 
12   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License.
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              //ignore
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  }