1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
package org.apache.shale.test.mock; |
19 | |
|
20 | |
import java.io.IOException; |
21 | |
import java.io.InputStream; |
22 | |
import java.net.MalformedURLException; |
23 | |
import java.net.URL; |
24 | |
import java.util.Collections; |
25 | |
import java.util.Enumeration; |
26 | |
import java.util.HashMap; |
27 | |
import java.util.Iterator; |
28 | |
import java.util.Locale; |
29 | |
import java.util.Map; |
30 | |
import java.util.Set; |
31 | |
|
32 | |
import javax.faces.FacesException; |
33 | |
import javax.faces.context.ExternalContext; |
34 | |
import javax.servlet.ServletContext; |
35 | |
import javax.servlet.http.Cookie; |
36 | |
import javax.servlet.http.HttpServletRequest; |
37 | |
import javax.servlet.http.HttpServletResponse; |
38 | |
import javax.servlet.http.HttpSession; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
public class MockExternalContext extends ExternalContext { |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
public MockExternalContext(ServletContext context, |
60 | |
HttpServletRequest request, |
61 | 36 | HttpServletResponse response) { |
62 | |
|
63 | 36 | this.context = context; |
64 | 36 | this.request = request; |
65 | 36 | this.response = response; |
66 | 36 | applicationMap = new MockApplicationMap(context); |
67 | 36 | requestMap = new MockRequestMap(request); |
68 | |
|
69 | 36 | } |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | 36 | private Map applicationMap = null; |
79 | 36 | private ServletContext context = null; |
80 | 36 | protected HttpServletRequest request = null; |
81 | 36 | private Map requestMap = null; |
82 | 36 | protected HttpServletResponse response = null; |
83 | 36 | private Map sessionMap = null; |
84 | 36 | private Map requestCookieMap = new HashMap(); |
85 | 36 | private Map requestParameterMap = new HashMap(); |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
public void addRequestCookieMap(Cookie cookie) { |
97 | |
requestParameterMap.put(cookie.getName(), cookie); |
98 | |
} |
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
public void setRequestCookieMap(Map map) { |
107 | |
requestParameterMap = map; |
108 | |
} |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
public void addRequestParameterMap(String key, String value) { |
118 | |
requestParameterMap.put(key, value); |
119 | |
} |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
public void setRequestParameterMap(Map map) { |
128 | |
requestParameterMap = map; |
129 | |
} |
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
public void dispatch(String requestURI) |
137 | |
throws IOException, FacesException { |
138 | |
|
139 | |
throw new UnsupportedOperationException(); |
140 | |
|
141 | |
} |
142 | |
|
143 | |
|
144 | |
|
145 | |
public String encodeActionURL(String sb) { |
146 | |
|
147 | |
return sb; |
148 | |
|
149 | |
} |
150 | |
|
151 | |
|
152 | |
|
153 | |
public String encodeNamespace(String aValue) { |
154 | |
|
155 | |
return aValue; |
156 | |
|
157 | |
} |
158 | |
|
159 | |
|
160 | |
|
161 | |
public String encodeResourceURL(String sb) { |
162 | |
|
163 | |
return sb; |
164 | |
|
165 | |
} |
166 | |
|
167 | |
|
168 | |
|
169 | |
public Map getApplicationMap() { |
170 | |
|
171 | 6 | return this.applicationMap; |
172 | |
|
173 | |
} |
174 | |
|
175 | |
|
176 | |
|
177 | |
public String getAuthType() { |
178 | |
|
179 | |
return request.getAuthType(); |
180 | |
|
181 | |
} |
182 | |
|
183 | |
|
184 | |
|
185 | |
public Object getContext() { |
186 | |
|
187 | |
return context; |
188 | |
|
189 | |
} |
190 | |
|
191 | |
|
192 | |
|
193 | |
public String getInitParameter(String name) { |
194 | |
|
195 | |
return context.getInitParameter(name); |
196 | |
|
197 | |
} |
198 | |
|
199 | |
|
200 | |
|
201 | |
public Map getInitParameterMap() { |
202 | |
|
203 | |
Map parameterMap = new HashMap(); |
204 | |
Enumeration names = context.getInitParameterNames(); |
205 | |
while (names.hasMoreElements()) { |
206 | |
String name = (String) names.nextElement(); |
207 | |
parameterMap.put(name, context.getInitParameter(name)); |
208 | |
} |
209 | |
return Collections.unmodifiableMap(parameterMap); |
210 | |
|
211 | |
} |
212 | |
|
213 | |
|
214 | |
|
215 | |
public String getRemoteUser() { |
216 | |
|
217 | |
return request.getRemoteUser(); |
218 | |
|
219 | |
} |
220 | |
|
221 | |
|
222 | |
|
223 | |
public Object getRequest() { |
224 | |
|
225 | |
return request; |
226 | |
|
227 | |
} |
228 | |
|
229 | |
|
230 | |
|
231 | |
public String getRequestContextPath() { |
232 | |
|
233 | |
return request.getContextPath(); |
234 | |
|
235 | |
} |
236 | |
|
237 | |
|
238 | |
|
239 | |
public Map getRequestCookieMap() { |
240 | |
|
241 | |
return requestCookieMap; |
242 | |
|
243 | |
} |
244 | |
|
245 | |
|
246 | |
|
247 | |
public Map getRequestHeaderMap() { |
248 | |
|
249 | |
throw new UnsupportedOperationException(); |
250 | |
|
251 | |
} |
252 | |
|
253 | |
|
254 | |
|
255 | |
public Map getRequestHeaderValuesMap() { |
256 | |
|
257 | |
throw new UnsupportedOperationException(); |
258 | |
|
259 | |
} |
260 | |
|
261 | |
|
262 | |
|
263 | |
public Locale getRequestLocale() { |
264 | |
|
265 | |
return request.getLocale(); |
266 | |
|
267 | |
} |
268 | |
|
269 | |
|
270 | |
|
271 | |
public Iterator getRequestLocales() { |
272 | |
|
273 | |
return new LocalesIterator(request.getLocales()); |
274 | |
|
275 | |
} |
276 | |
|
277 | |
|
278 | |
|
279 | |
public Map getRequestMap() { |
280 | |
|
281 | 31 | return requestMap; |
282 | |
|
283 | |
} |
284 | |
|
285 | |
|
286 | |
|
287 | |
public Map getRequestParameterMap() { |
288 | |
|
289 | |
return requestParameterMap; |
290 | |
|
291 | |
} |
292 | |
|
293 | |
|
294 | |
|
295 | |
public Iterator getRequestParameterNames() { |
296 | |
|
297 | |
throw new UnsupportedOperationException(); |
298 | |
|
299 | |
} |
300 | |
|
301 | |
|
302 | |
|
303 | |
public Map getRequestParameterValuesMap() { |
304 | |
|
305 | |
throw new UnsupportedOperationException(); |
306 | |
|
307 | |
} |
308 | |
|
309 | |
|
310 | |
|
311 | |
public String getRequestPathInfo() { |
312 | |
|
313 | |
return request.getPathInfo(); |
314 | |
|
315 | |
} |
316 | |
|
317 | |
|
318 | |
|
319 | |
public String getRequestServletPath() { |
320 | |
|
321 | |
return request.getServletPath(); |
322 | |
|
323 | |
} |
324 | |
|
325 | |
|
326 | |
|
327 | |
public URL getResource(String path) throws MalformedURLException { |
328 | |
|
329 | |
return context.getResource(path); |
330 | |
|
331 | |
} |
332 | |
|
333 | |
|
334 | |
|
335 | |
public InputStream getResourceAsStream(String path) { |
336 | |
|
337 | |
return context.getResourceAsStream(path); |
338 | |
|
339 | |
} |
340 | |
|
341 | |
|
342 | |
|
343 | |
public Set getResourcePaths(String path) { |
344 | |
|
345 | |
throw new UnsupportedOperationException(); |
346 | |
|
347 | |
} |
348 | |
|
349 | |
|
350 | |
|
351 | |
public Object getResponse() { |
352 | |
|
353 | |
return response; |
354 | |
|
355 | |
} |
356 | |
|
357 | |
|
358 | |
|
359 | |
public Object getSession(boolean create) { |
360 | |
|
361 | |
return request.getSession(create); |
362 | |
|
363 | |
} |
364 | |
|
365 | |
|
366 | |
|
367 | |
public Map getSessionMap() { |
368 | |
|
369 | 7 | if (sessionMap == null) { |
370 | 2 | HttpSession session = request.getSession(true); |
371 | 2 | sessionMap = new MockSessionMap(session); |
372 | |
} |
373 | 7 | return sessionMap; |
374 | |
|
375 | |
} |
376 | |
|
377 | |
|
378 | |
|
379 | |
public java.security.Principal getUserPrincipal() { |
380 | |
|
381 | |
return request.getUserPrincipal(); |
382 | |
|
383 | |
} |
384 | |
|
385 | |
|
386 | |
|
387 | |
public boolean isUserInRole(String role) { |
388 | |
|
389 | |
return request.isUserInRole(role); |
390 | |
|
391 | |
} |
392 | |
|
393 | |
|
394 | |
|
395 | |
public void log(String message) { |
396 | |
|
397 | |
context.log(message); |
398 | |
|
399 | |
} |
400 | |
|
401 | |
|
402 | |
|
403 | |
public void log(String message, Throwable throwable) { |
404 | |
|
405 | |
context.log(message, throwable); |
406 | |
|
407 | |
} |
408 | |
|
409 | |
|
410 | |
|
411 | |
public void redirect(String requestURI) |
412 | |
throws IOException { |
413 | |
|
414 | |
throw new UnsupportedOperationException(); |
415 | |
|
416 | |
} |
417 | |
|
418 | |
|
419 | |
|
420 | |
|
421 | |
|
422 | |
|
423 | |
private class LocalesIterator implements Iterator { |
424 | |
|
425 | |
|
426 | |
|
427 | |
|
428 | |
|
429 | |
|
430 | |
|
431 | |
public LocalesIterator(Enumeration locales) { |
432 | |
this.locales = locales; |
433 | |
} |
434 | |
|
435 | |
|
436 | |
|
437 | |
|
438 | |
private Enumeration locales; |
439 | |
|
440 | |
|
441 | |
public boolean hasNext() { return locales.hasMoreElements(); } |
442 | |
|
443 | |
|
444 | |
public Object next() { return locales.nextElement(); } |
445 | |
|
446 | |
|
447 | |
public void remove() { throw new UnsupportedOperationException(); } |
448 | |
|
449 | |
} |
450 | |
|
451 | |
|
452 | |
} |