1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.el;
18
19 import java.util.ArrayList;
20 import java.util.Enumeration;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24
25 import javax.servlet.ServletContext;
26 import javax.servlet.http.Cookie;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.jsp.PageContext;
29
30
31
32
33
34
35
36
37
38
39
40 public class ImplicitObjects
41 {
42
43
44
45
46 static final String sAttributeName =
47 "org.apache.commons.el.ImplicitObjects";
48
49
50
51
52
53 PageContext mContext;
54 Map mPage;
55 Map mRequest;
56 Map mSession;
57 Map mApplication;
58 Map mParam;
59 Map mParams;
60 Map mHeader;
61 Map mHeaders;
62 Map mInitParam;
63 Map mCookie;
64
65
66
67
68
69
70 public ImplicitObjects (PageContext pContext)
71 {
72 mContext = pContext;
73 }
74
75
76
77
78
79
80
81 public static ImplicitObjects getImplicitObjects (PageContext pContext)
82 {
83 ImplicitObjects objs =
84 (ImplicitObjects)
85 pContext.getAttribute (sAttributeName,
86 PageContext.PAGE_SCOPE);
87 if (objs == null) {
88 objs = new ImplicitObjects (pContext);
89 pContext.setAttribute (sAttributeName,
90 objs,
91 PageContext.PAGE_SCOPE);
92 }
93 return objs;
94 }
95
96
97
98
99
100
101 public Map getPageScopeMap ()
102 {
103 if (mPage == null) {
104 mPage = createPageScopeMap (mContext);
105 }
106 return mPage;
107 }
108
109
110
111
112
113
114 public Map getRequestScopeMap ()
115 {
116 if (mRequest == null) {
117 mRequest = createRequestScopeMap (mContext);
118 }
119 return mRequest;
120 }
121
122
123
124
125
126
127 public Map getSessionScopeMap ()
128 {
129 if (mSession == null) {
130 mSession = createSessionScopeMap (mContext);
131 }
132 return mSession;
133 }
134
135
136
137
138
139
140 public Map getApplicationScopeMap ()
141 {
142 if (mApplication == null) {
143 mApplication = createApplicationScopeMap (mContext);
144 }
145 return mApplication;
146 }
147
148
149
150
151
152
153
154 public Map getParamMap ()
155 {
156 if (mParam == null) {
157 mParam = createParamMap (mContext);
158 }
159 return mParam;
160 }
161
162
163
164
165
166
167
168 public Map getParamsMap ()
169 {
170 if (mParams == null) {
171 mParams = createParamsMap (mContext);
172 }
173 return mParams;
174 }
175
176
177
178
179
180
181
182 public Map getHeaderMap ()
183 {
184 if (mHeader == null) {
185 mHeader = createHeaderMap (mContext);
186 }
187 return mHeader;
188 }
189
190
191
192
193
194
195
196 public Map getHeadersMap ()
197 {
198 if (mHeaders == null) {
199 mHeaders = createHeadersMap (mContext);
200 }
201 return mHeaders;
202 }
203
204
205
206
207
208
209
210 public Map getInitParamMap ()
211 {
212 if (mInitParam == null) {
213 mInitParam = createInitParamMap (mContext);
214 }
215 return mInitParam;
216 }
217
218
219
220
221
222
223
224 public Map getCookieMap ()
225 {
226 if (mCookie == null) {
227 mCookie = createCookieMap (mContext);
228 }
229 return mCookie;
230 }
231
232
233
234
235
236
237
238
239 public static Map createPageScopeMap (PageContext pContext)
240 {
241 final PageContext context = pContext;
242 return new EnumeratedMap ()
243 {
244 public Enumeration enumerateKeys ()
245 {
246 return context.getAttributeNamesInScope
247 (PageContext.PAGE_SCOPE);
248 }
249
250 public Object getValue (Object pKey)
251 {
252 if (pKey instanceof String) {
253 return context.getAttribute
254 ((String) pKey,
255 PageContext.PAGE_SCOPE);
256 }
257 else {
258 return null;
259 }
260 }
261
262 public boolean isMutable ()
263 {
264 return true;
265 }
266 };
267 }
268
269
270
271
272
273
274 public static Map createRequestScopeMap (PageContext pContext)
275 {
276 final PageContext context = pContext;
277 return new EnumeratedMap ()
278 {
279 public Enumeration enumerateKeys ()
280 {
281 return context.getAttributeNamesInScope
282 (PageContext.REQUEST_SCOPE);
283 }
284
285 public Object getValue (Object pKey)
286 {
287 if (pKey instanceof String) {
288 return context.getAttribute
289 ((String) pKey,
290 PageContext.REQUEST_SCOPE);
291 }
292 else {
293 return null;
294 }
295 }
296
297 public boolean isMutable ()
298 {
299 return true;
300 }
301 };
302 }
303
304
305
306
307
308
309 public static Map createSessionScopeMap (PageContext pContext)
310 {
311 final PageContext context = pContext;
312 return new EnumeratedMap ()
313 {
314 public Enumeration enumerateKeys ()
315 {
316 return context.getAttributeNamesInScope
317 (PageContext.SESSION_SCOPE);
318 }
319
320 public Object getValue (Object pKey)
321 {
322 if (pKey instanceof String) {
323 return context.getAttribute
324 ((String) pKey,
325 PageContext.SESSION_SCOPE);
326 }
327 else {
328 return null;
329 }
330 }
331
332 public boolean isMutable ()
333 {
334 return true;
335 }
336 };
337 }
338
339
340
341
342
343
344 public static Map createApplicationScopeMap (PageContext pContext)
345 {
346 final PageContext context = pContext;
347 return new EnumeratedMap ()
348 {
349 public Enumeration enumerateKeys ()
350 {
351 return context.getAttributeNamesInScope
352 (PageContext.APPLICATION_SCOPE);
353 }
354
355 public Object getValue (Object pKey)
356 {
357 if (pKey instanceof String) {
358 return context.getAttribute
359 ((String) pKey,
360 PageContext.APPLICATION_SCOPE);
361 }
362 else {
363 return null;
364 }
365 }
366
367 public boolean isMutable ()
368 {
369 return true;
370 }
371 };
372 }
373
374
375
376
377
378
379
380 public static Map createParamMap (PageContext pContext)
381 {
382 final HttpServletRequest request =
383 (HttpServletRequest) pContext.getRequest ();
384 return new EnumeratedMap ()
385 {
386 public Enumeration enumerateKeys ()
387 {
388 return request.getParameterNames ();
389 }
390
391 public Object getValue (Object pKey)
392 {
393 if (pKey instanceof String) {
394 return request.getParameter ((String) pKey);
395 }
396 else {
397 return null;
398 }
399 }
400
401 public boolean isMutable ()
402 {
403 return false;
404 }
405 };
406 }
407
408
409
410
411
412
413
414 public static Map createParamsMap (PageContext pContext)
415 {
416 final HttpServletRequest request =
417 (HttpServletRequest) pContext.getRequest ();
418 return new EnumeratedMap ()
419 {
420 public Enumeration enumerateKeys ()
421 {
422 return request.getParameterNames ();
423 }
424
425 public Object getValue (Object pKey)
426 {
427 if (pKey instanceof String) {
428 return request.getParameterValues ((String) pKey);
429 }
430 else {
431 return null;
432 }
433 }
434
435 public boolean isMutable ()
436 {
437 return false;
438 }
439 };
440 }
441
442
443
444
445
446
447
448 public static Map createHeaderMap (PageContext pContext)
449 {
450 final HttpServletRequest request =
451 (HttpServletRequest) pContext.getRequest ();
452 return new EnumeratedMap ()
453 {
454 public Enumeration enumerateKeys ()
455 {
456 return request.getHeaderNames ();
457 }
458
459 public Object getValue (Object pKey)
460 {
461 if (pKey instanceof String) {
462 return request.getHeader ((String) pKey);
463 }
464 else {
465 return null;
466 }
467 }
468
469 public boolean isMutable ()
470 {
471 return false;
472 }
473 };
474 }
475
476
477
478
479
480
481
482 public static Map createHeadersMap (PageContext pContext)
483 {
484 final HttpServletRequest request =
485 (HttpServletRequest) pContext.getRequest ();
486 return new EnumeratedMap ()
487 {
488 public Enumeration enumerateKeys ()
489 {
490 return request.getHeaderNames ();
491 }
492
493 public Object getValue (Object pKey)
494 {
495 if (pKey instanceof String) {
496
497 List l = new ArrayList ();
498 Enumeration e = request.getHeaders ((String) pKey);
499 if (e != null) {
500 while (e.hasMoreElements ()) {
501 l.add (e.nextElement ());
502 }
503 }
504 String [] ret = (String []) l.toArray (new String [l.size ()]);
505 return ret;
506 }
507 else {
508 return null;
509 }
510 }
511
512 public boolean isMutable ()
513 {
514 return false;
515 }
516 };
517 }
518
519
520
521
522
523
524
525 public static Map createInitParamMap (PageContext pContext)
526 {
527 final ServletContext context = pContext.getServletContext ();
528 return new EnumeratedMap ()
529 {
530 public Enumeration enumerateKeys ()
531 {
532 return context.getInitParameterNames ();
533 }
534
535 public Object getValue (Object pKey)
536 {
537 if (pKey instanceof String) {
538 return context.getInitParameter ((String) pKey);
539 }
540 else {
541 return null;
542 }
543 }
544
545 public boolean isMutable ()
546 {
547 return false;
548 }
549 };
550 }
551
552
553
554
555
556
557
558 public static Map createCookieMap (PageContext pContext)
559 {
560
561 HttpServletRequest request = (HttpServletRequest) pContext.getRequest ();
562 Cookie [] cookies = request.getCookies ();
563 Map ret = new HashMap ();
564 for (int i = 0; cookies != null && i < cookies.length; i++) {
565 Cookie cookie = cookies [i];
566 if (cookie != null) {
567 String name = cookie.getName ();
568 if (!ret.containsKey (name)) {
569 ret.put (name, cookie);
570 }
571 }
572 }
573 return ret;
574 }
575
576
577 }