1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.springframework.extensions.webscripts;
20
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Set;
26
27
28
29
30
31
32
33
34 public abstract class WebScriptRequestURLImpl extends WebScriptRequestImpl
35 {
36
37 protected String contextPath;
38 protected String servletPath;
39 protected String pathInfo;
40 protected String queryString;
41 protected Map<String, String> queryArgs;
42 protected Map<String, List<String>> queryArgsMulti;
43
44
45 protected Match serviceMatch;
46
47
48
49
50
51
52
53
54 public static String[] splitURL(String scriptUrl)
55 {
56 return splitURL(true, scriptUrl);
57 }
58
59
60
61
62
63
64
65
66 public static String[] splitURL(boolean context, String scriptUrl)
67 {
68 String[] urlParts = new String[4];
69 String path;
70 String queryString;
71
72 int argsIndex = scriptUrl.indexOf("?");
73 if (argsIndex != -1)
74 {
75 path = scriptUrl.substring(0, argsIndex);
76 queryString = scriptUrl.substring(argsIndex + 1);
77 }
78 else
79 {
80 path = scriptUrl;
81 queryString = null;
82 }
83
84 String[] pathSegments = path.split("/");
85 String pathInfo = "";
86 for (int i = (context ? 3 : 2); i < pathSegments.length; i++)
87 {
88 pathInfo += "/" + pathSegments[i];
89 }
90
91 urlParts[0] = context ? "/" + pathSegments[1] : "";
92 urlParts[1] = "/" + pathSegments[context ? 2 : 1];
93 urlParts[2] = pathInfo;
94 urlParts[3] = queryString;
95
96 return urlParts;
97 }
98
99
100
101
102
103
104
105
106
107 public WebScriptRequestURLImpl(Runtime runtime, String scriptUrl, Match serviceMatch)
108 {
109 this(runtime, splitURL(scriptUrl), serviceMatch);
110 }
111
112
113
114
115
116
117
118 public WebScriptRequestURLImpl(Runtime runtime, String[] scriptUrlParts, Match serviceMatch)
119 {
120 super(runtime);
121
122 this.contextPath = scriptUrlParts[0];
123 this.servletPath = scriptUrlParts[1];
124 this.pathInfo = scriptUrlParts[2];
125 this.queryString = scriptUrlParts[3];
126 this.queryArgs = new HashMap<String, String>(8, 1.0f);
127 this.queryArgsMulti = new HashMap<String, List<String>>(8, 1.0f);
128 if (this.queryString != null)
129 {
130 String[] args = this.queryString.split("&");
131 for (String arg : args)
132 {
133 String[] parts = arg.split("=");
134 if (this.queryArgs.containsKey(parts[0]))
135 {
136 List<String> values = this.queryArgsMulti.get(parts[0]);
137 if (values == null)
138 {
139 values = new ArrayList<String>(4);
140 this.queryArgsMulti.put(parts[0], values);
141 }
142 values.add(parts.length == 2 ? parts[1] : "");
143 }
144 else
145 {
146 this.queryArgs.put(parts[0], parts.length == 2 ? parts[1] : "");
147 }
148 }
149 }
150 this.serviceMatch = serviceMatch;
151 }
152
153 @Override
154 public String toString()
155 {
156 return "Request Service Match: " + serviceMatch +
157 " URL: " + this.contextPath + this.servletPath + this.pathInfo + "?" + queryString;
158 }
159
160
161
162
163 public Match getServiceMatch()
164 {
165 return serviceMatch;
166 }
167
168
169
170
171 public String getContextPath()
172 {
173 return contextPath;
174 }
175
176
177
178
179 public String getServiceContextPath()
180 {
181 return getContextPath() + servletPath;
182 }
183
184
185
186
187 public String getServicePath()
188 {
189 return getServiceContextPath() + pathInfo;
190 }
191
192
193
194
195 public String getURL()
196 {
197 return getServicePath() + (queryString != null ? "?" + queryString : "");
198 }
199
200
201
202
203 public String getPathInfo()
204 {
205 return pathInfo;
206 }
207
208
209
210
211 public String getQueryString()
212 {
213 return queryString;
214 }
215
216
217
218
219 public String[] getParameterNames()
220 {
221 Set<String> keys = queryArgs.keySet();
222 String[] names = new String[keys.size()];
223 keys.toArray(names);
224 return names;
225 }
226
227
228
229
230 public String getParameter(String name)
231 {
232 return queryArgs.get(name);
233 }
234
235
236
237
238 public String[] getParameterValues(String name)
239 {
240 List<String> values = queryArgsMulti.get(name);
241 if (values != null)
242 {
243 String[] array = new String[values.size()];
244 values.toArray(array);
245 return array;
246 }
247 else
248 {
249 String value = queryArgs.get(name);
250 if (value != null)
251 {
252 return new String[]{value};
253 }
254 }
255 return null;
256 }
257
258 }