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
21 import java.io.BufferedReader;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25 import java.io.UnsupportedEncodingException;
26 import java.util.ArrayList;
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.RequestDispatcher;
33 import javax.servlet.ServletContext;
34 import javax.servlet.ServletInputStream;
35 import javax.servlet.ServletRequest;
36
37 import org.apache.commons.collections.iterators.IteratorEnumeration;
38 import org.apache.commons.collections.iterators.SingletonIterator;
39
40
41
42
43
44
45
46
47
48 public class ServletRequestImpl implements ServletRequest {
49
50
51
52
53
54
55
56
57 protected HashMap attributes = new HashMap();
58
59
60
61
62
63 protected String authorization = null;
64
65
66
67
68
69 protected String characterEncoding = null;
70
71
72
73
74
75
76 protected int contentLength = -1;
77
78
79
80
81
82 protected String contentType = null;
83
84
85
86
87
88 protected static Locale defaultLocale = Locale.getDefault();
89
90
91
92
93
94 protected InputStream input = null;
95
96
97
98
99
100 protected ArrayList locales = new ArrayList();
101
102
103
104
105
106
107 protected String protocol = null;
108
109
110
111
112
113 protected BufferedReader reader = null;
114
115
116
117
118
119 protected String remoteAddr = null;
120
121
122
123
124
125 protected String remoteHost = null;
126
127
128
129
130
131
132 protected String scheme = null;
133
134
135
136
137
138 protected boolean secure = false;
139
140
141
142
143
144 protected String serverName = null;
145
146
147
148
149
150 protected int serverPort = -1;
151
152
153
154
155
156
157 protected ServletInputStream stream = null;
158
159
160
161
162
163 protected ServletContext servletContext;
164
165
166
167 public ServletRequestImpl(ServletContext servletContext) {
168 this.servletContext = servletContext;
169 }
170
171
172
173
174
175
176
177
178 public InputStream getStream() {
179
180 return (this.input);
181
182 }
183
184
185
186
187
188
189
190 public void setStream(InputStream input) {
191
192 this.input = input;
193
194 }
195
196
197
198
199
200
201
202
203
204
205
206
207 public void addLocale(Locale locale) {
208
209 synchronized (locales) {
210 locales.add(locale);
211 }
212
213 }
214
215
216
217
218
219
220
221
222
223
224 public ServletInputStream createInputStream() throws IOException {
225
226
227 return null;
228
229 }
230
231
232
233
234
235
236
237
238 public void finishRequest() throws IOException {
239
240
241 if (reader != null) {
242 try {
243 reader.close();
244 } catch (IOException e) {
245 ;
246 }
247 }
248
249
250 if (stream != null) {
251 try {
252 stream.close();
253 } catch (IOException e) {
254 ;
255 }
256 }
257
258
259
260
261 }
262
263
264
265
266
267
268
269 public void setContentLength(int length) {
270
271 this.contentLength = length;
272
273 }
274
275
276
277
278
279
280
281
282
283 public void setContentType(String type) {
284
285 this.contentType = type;
286 if (type.indexOf(';') >= 0) {
287
288 }
289
290 }
291
292
293
294
295
296
297
298
299 public void setProtocol(String protocol) {
300
301 this.protocol = protocol;
302
303 }
304
305
306
307
308
309
310
311 public void setRemoteAddr(String remoteAddr) {
312
313 this.remoteAddr = remoteAddr;
314
315 }
316
317
318
319
320
321
322
323
324 public void setRemoteHost(String remoteHost) {
325
326 this.remoteHost = remoteHost;
327
328 }
329
330
331
332
333
334
335
336
337 public void setScheme(String scheme) {
338
339 this.scheme = scheme;
340
341 }
342
343
344
345
346
347
348
349
350 public void setSecure(boolean secure) {
351
352 this.secure = secure;
353
354 }
355
356
357
358
359
360
361
362 public void setServerName(String name) {
363
364 this.serverName = name;
365
366 }
367
368
369
370
371
372
373
374 public void setServerPort(int port) {
375
376 this.serverPort = port;
377
378 }
379
380
381
382
383
384
385
386
387
388
389
390 public Object getAttribute(String name) {
391
392 synchronized (attributes) {
393 return (attributes.get(name));
394 }
395
396 }
397
398
399
400
401
402
403 public Enumeration getAttributeNames() {
404
405 synchronized (attributes) {
406 return new IteratorEnumeration(attributes.keySet().iterator());
407 }
408
409 }
410
411
412
413
414
415 public String getCharacterEncoding() {
416
417 if (characterEncoding== null) {
418 characterEncoding= "ISO-8859-1";
419 }
420 return (this.characterEncoding);
421 }
422
423
424
425
426
427 public int getContentLength() {
428
429 return (this.contentLength);
430
431 }
432
433
434
435
436
437 public String getContentType() {
438
439 return (contentType);
440
441 }
442
443
444
445
446
447
448
449
450
451
452
453 public ServletInputStream getInputStream() throws IOException {
454
455 if (reader != null) {
456 throw new IllegalStateException( "getReader() has already been called" );
457 }
458
459 if (stream == null)
460 stream = createInputStream();
461 return (stream);
462
463 }
464
465
466
467
468
469
470
471
472 public Locale getLocale() {
473
474 synchronized (locales) {
475 if (locales.size() > 0)
476 return ((Locale) locales.get(0));
477 else
478 return (defaultLocale);
479 }
480
481 }
482
483
484
485
486
487
488
489
490 public Enumeration getLocales() {
491
492 synchronized (locales) {
493 if (locales.size() > 0) {
494 return new IteratorEnumeration( locales.iterator() );
495 }
496 }
497 return new IteratorEnumeration( new SingletonIterator( defaultLocale ) );
498
499 }
500
501
502
503
504
505
506
507
508
509 public String getParameter(String name) {
510 String values[] = (String[]) getParameterMap().get(name);
511 if (values != null)
512 return (values[0]);
513 else
514 return (null);
515
516 }
517
518
519
520
521
522
523
524
525 public String[] getParameterValues(String name) {
526 String values[] = (String[]) getParameterMap().get(name);
527 if (values != null)
528 return (values);
529 else
530 return (null);
531 }
532
533
534
535
536
537
538
539
540
541
542
543 public Map getParameterMap() {
544 return new HashMap();
545 }
546
547
548
549
550
551 public Enumeration getParameterNames() {
552 return new IteratorEnumeration(getParameterMap().keySet().iterator());
553 }
554
555
556
557
558
559 public String getProtocol() {
560
561 return (this.protocol);
562
563 }
564
565
566
567
568
569
570
571
572
573
574
575 public BufferedReader getReader() throws IOException {
576
577 if (stream != null) {
578 throw new IllegalStateException( "getInputStream() has already been called" );
579 }
580
581 if (reader == null) {
582 String encoding = getCharacterEncoding();
583 InputStreamReader isr =
584 new InputStreamReader(createInputStream(), encoding);
585 reader = new BufferedReader(isr);
586 }
587 return (reader);
588
589 }
590
591
592
593
594
595
596
597
598
599
600 public String getRealPath(String path) {
601
602 if (servletContext == null)
603 return (null);
604 else {
605 try {
606 return (servletContext.getRealPath(path));
607 } catch (IllegalArgumentException e) {
608 return (null);
609 }
610 }
611
612 }
613
614
615
616
617
618 public String getRemoteAddr() {
619
620 return (this.remoteAddr);
621
622 }
623
624
625
626
627
628 public String getRemoteHost() {
629
630 return (this.remoteHost);
631
632 }
633
634
635
636
637
638
639
640
641 public RequestDispatcher getRequestDispatcher(String path) {
642 return servletContext.getRequestDispatcher(path);
643 }
644
645
646
647
648
649 public String getScheme() {
650
651 return (this.scheme);
652
653 }
654
655
656
657
658
659 public String getServerName() {
660
661 return (this.serverName);
662
663 }
664
665
666
667
668
669 public int getServerPort() {
670
671 return (this.serverPort);
672
673 }
674
675
676
677
678
679 public boolean isSecure() {
680
681 return (this.secure);
682
683 }
684
685
686
687
688
689
690
691 public void removeAttribute(String name) {
692
693 synchronized (attributes) {
694 attributes.remove(name);
695 }
696
697 }
698
699
700
701
702
703
704
705
706 public void setAttribute(String name, Object value) {
707
708
709 if (name == null) {
710 throw new IllegalArgumentException( "Attribute name cannot be null" );
711 }
712
713
714 if (value == null) {
715 removeAttribute(name);
716 return;
717 }
718
719 synchronized (attributes) {
720 attributes.put(name, value);
721 }
722
723 }
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738 public void setCharacterEncoding(String enc)
739 throws UnsupportedEncodingException {
740
741
742 byte buffer[] = new byte[1];
743 buffer[0] = (byte) 'a';
744 String dummy = new String(buffer, enc);
745
746
747 this.characterEncoding = enc;
748 }
749
750
751
752
753
754
755 protected void log(String message) {
756
757 servletContext.log(message);
758
759 }
760
761
762
763
764
765
766
767
768 protected void log(String message, Throwable throwable) {
769
770 servletContext.log(message, throwable);
771
772 }
773
774 }