1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.messagelet.impl;
19
20 import java.io.UnsupportedEncodingException;
21 import java.security.Principal;
22 import java.text.ParseException;
23 import java.text.SimpleDateFormat;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.Date;
27 import java.util.Enumeration;
28 import java.util.HashMap;
29 import java.util.Locale;
30 import java.util.Map;
31
32 import javax.servlet.ServletContext;
33 import javax.servlet.http.Cookie;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.servlet.http.HttpSession;
36
37 import org.apache.commons.collections.iterators.IteratorEnumeration;
38
39
40
41
42
43
44
45
46
47 public class HttpServletRequestImpl extends ServletRequestImpl implements HttpServletRequest {
48
49
50
51
52
53 protected String authType = null;
54
55
56
57
58
59 protected String contextPath = "";
60
61
62
63
64
65 protected ArrayList cookies = new ArrayList();
66
67
68
69
70
71 protected SimpleDateFormat formats[] = {
72 new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US),
73 new SimpleDateFormat("EEEEEE, dd-MMM-yy HH:mm:ss zzz", Locale.US),
74 new SimpleDateFormat("EEE MMMM d HH:mm:ss yyyy", Locale.US)
75 };
76
77
78
79
80
81
82
83 protected HashMap headers = new HashMap();
84
85
86
87
88
89
90 protected String method = null;
91
92
93
94
95
96 protected String pathInfo = null;
97
98
99
100
101
102 protected String queryString = null;
103
104
105
106
107
108 protected boolean requestedSessionCookie = false;
109
110
111
112
113
114 protected String requestedSessionId = null;
115
116
117
118
119
120 protected boolean requestedSessionURL = false;
121
122
123
124
125
126 protected String requestURI = null;
127
128
129
130
131
132 protected String servletPath = null;
133
134
135
136
137
138 protected HttpSessionImpl session = null;
139
140
141
142
143
144 protected Principal userPrincipal = null;
145
146
147
148
149
150 private Map parameters = null;
151
152
153
154
155 public HttpServletRequestImpl(ServletContext servletContext) {
156 super( servletContext );
157 }
158
159
160
161
162
163
164
165 public void addCookie(Cookie cookie) {
166
167 synchronized (cookies) {
168 cookies.add(cookie);
169 }
170
171 }
172
173
174
175
176
177
178
179
180 public void addHeader(String name, String value) {
181
182 name = name.toLowerCase();
183 synchronized (headers) {
184 ArrayList values = (ArrayList) headers.get(name);
185 if (values == null) {
186 values = new ArrayList();
187 headers.put(name, values);
188 }
189 values.add(value);
190 }
191
192 }
193
194
195
196
197
198 public void clearCookies() {
199
200 synchronized (cookies) {
201 cookies.clear();
202 }
203
204 }
205
206
207
208
209
210 public void clearHeaders() {
211
212 headers.clear();
213
214 }
215
216
217
218
219
220
221
222
223
224
225
226 public void setAuthType(String authType) {
227
228 this.authType = authType;
229
230 }
231
232
233
234
235
236
237
238
239
240 public void setContextPath(String path) {
241
242 if (path == null)
243 this.contextPath = "";
244 else
245 this.contextPath = path;
246
247 }
248
249
250
251
252
253
254
255 public void setMethod(String method) {
256
257 this.method = method;
258
259 }
260
261
262
263
264
265
266
267
268
269 public void setPathInfo(String path) {
270
271 this.pathInfo = path;
272
273 }
274
275
276
277
278
279
280
281
282 public void setQueryString(String query) {
283
284 this.queryString = query;
285 this.parameters = null;
286
287 }
288
289
290
291
292
293
294
295
296
297 public void setRequestedSessionCookie(boolean flag) {
298
299 this.requestedSessionCookie = flag;
300
301 }
302
303
304
305
306
307
308
309
310 public void setRequestedSessionId(String id) {
311
312 this.requestedSessionId = id;
313
314 }
315
316
317
318
319
320
321
322
323
324 public void setRequestedSessionURL(boolean flag) {
325
326 this.requestedSessionURL = flag;
327
328 }
329
330
331
332
333
334
335
336
337 public void setRequestURI(String uri) {
338
339 this.requestURI = uri;
340
341 }
342
343
344
345
346
347
348
349
350
351 public void setServletPath(String path) {
352
353 this.servletPath = path;
354
355 }
356
357
358
359
360
361
362
363
364
365 public void setUserPrincipal(Principal principal) {
366
367 this.userPrincipal = principal;
368
369 }
370
371
372
373
374
375
376
377
378
379 public String getAuthType() {
380
381 return (authType);
382
383 }
384
385
386
387
388
389
390 public String getContextPath() {
391
392 return (contextPath);
393
394 }
395
396
397
398
399
400 public Cookie[] getCookies() {
401
402 synchronized (cookies) {
403 if (cookies.size() < 1)
404 return (null);
405 Cookie results[] = new Cookie[cookies.size()];
406 return ((Cookie[]) cookies.toArray(results));
407 }
408
409 }
410
411
412
413
414
415
416
417
418
419
420
421 public long getDateHeader(String name) {
422
423 String value = getHeader(name);
424 if (value == null)
425 return (-1L);
426
427
428
429 value += " ";
430
431
432 for (int i = 0; i < formats.length; i++) {
433 try {
434 Date date = formats[i].parse(value);
435 return (date.getTime());
436 } catch (ParseException e) {
437 ;
438 }
439 }
440 throw new IllegalArgumentException(value);
441
442 }
443
444
445
446
447
448
449
450
451 public String getHeader(String name) {
452
453 name = name.toLowerCase();
454 synchronized (headers) {
455 ArrayList values = (ArrayList) headers.get(name);
456 if (values != null)
457 return ((String) values.get(0));
458 else
459 return (null);
460 }
461
462 }
463
464
465
466
467
468
469
470
471 public Enumeration getHeaders(String name) {
472
473 name = name.toLowerCase();
474 synchronized (headers) {
475 ArrayList values = (ArrayList) headers.get(name);
476 if (values != null)
477 return (new IteratorEnumeration( values.iterator() ));
478 else
479 return (new IteratorEnumeration( Collections.EMPTY_LIST.iterator() ));
480 }
481
482 }
483
484
485
486
487
488 public Enumeration getHeaderNames() {
489
490 synchronized (headers) {
491 return (new IteratorEnumeration( headers.keySet().iterator() ));
492 }
493
494 }
495
496
497
498
499
500
501
502
503
504
505
506 public int getIntHeader(String name) {
507
508 String value = getHeader(name);
509 if (value == null)
510 return (-1);
511 else
512 return (Integer.parseInt(value));
513
514 }
515
516
517
518
519
520 public String getMethod() {
521
522 return (method);
523
524 }
525
526
527
528
529
530
531
532
533
534
535
536 public Map getParameterMap() {
537 if ( parameters == null ) {
538 parameters = new HashMap();
539 if ( queryString != null ) {
540 try {
541 RequestUtil.parseParameters(parameters, queryString, getCharacterEncoding());
542 }
543 catch (UnsupportedEncodingException e) {
544 servletContext.log( "Could not parse query string: " + queryString, e);
545 }
546 }
547 }
548 return parameters;
549 }
550
551
552
553
554 public String getPathInfo() {
555
556 return (pathInfo);
557
558 }
559
560
561
562
563
564
565 public String getPathTranslated() {
566
567 if (pathInfo == null)
568 return (null);
569 else
570 return (servletContext.getRealPath(pathInfo));
571
572 }
573
574
575
576
577
578 public String getQueryString() {
579
580 return (queryString);
581
582 }
583
584
585
586
587
588
589 public String getRemoteUser() {
590
591 if (userPrincipal != null)
592 return (userPrincipal.getName());
593 else
594 return (null);
595
596 }
597
598
599
600
601
602 public String getRequestedSessionId() {
603
604 return (requestedSessionId);
605
606 }
607
608
609
610
611
612 public String getRequestURI() {
613
614 return (requestURI);
615
616 }
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635 public StringBuffer getRequestURL() {
636
637 StringBuffer url = new StringBuffer();
638 String scheme = getScheme();
639 int port = getServerPort();
640 if (port < 0)
641 port = 80;
642
643 url.append(scheme);
644 url.append("://");
645 url.append(getServerName());
646 if ((scheme.equals("http") && (port != 80))
647 || (scheme.equals("https") && (port != 443))) {
648 url.append(':');
649 url.append(port);
650 }
651 url.append(getRequestURI());
652
653 return (url);
654
655 }
656
657
658
659
660
661
662 public String getServletPath() {
663
664 return (servletPath);
665
666 }
667
668
669
670
671
672
673 public HttpSession getSession() {
674
675 return (getSession(true));
676
677 }
678
679
680
681
682
683
684
685
686 public HttpSession getSession(boolean create) {
687
688 if ((session != null) && !session.isValid())
689 session = null;
690
691 if ( create && session == null) {
692 session = new HttpSessionImpl( servletContext );
693 }
694 return session;
695 }
696
697
698
699
700
701
702 public boolean isRequestedSessionIdFromCookie() {
703
704 if (requestedSessionId != null)
705 return (requestedSessionCookie);
706 else
707 return (false);
708
709 }
710
711
712
713
714
715
716 public boolean isRequestedSessionIdFromURL() {
717
718 if (requestedSessionId != null)
719 return (requestedSessionURL);
720 else
721 return (false);
722
723 }
724
725
726
727
728
729
730
731
732
733 public boolean isRequestedSessionIdFromUrl() {
734
735 return (isRequestedSessionIdFromURL());
736
737 }
738
739
740
741
742
743
744 public boolean isRequestedSessionIdValid() {
745 return false;
746
747 }
748
749
750
751
752
753
754
755
756 public boolean isUserInRole(String role) {
757 return false;
758 }
759
760
761
762
763
764 public Principal getUserPrincipal() {
765
766 return (userPrincipal);
767
768 }
769 }