2011/08/05 - Jakarta Cactus has been retired.
For more information, please explore the Attic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.cactus.internal;
22
23 import java.io.InputStream;
24
25 import java.util.Enumeration;
26 import java.util.Hashtable;
27 import java.util.Vector;
28
29 import org.apache.cactus.Cookie;
30 import org.apache.cactus.WebRequest;
31 import org.apache.cactus.client.authentication.Authentication;
32 import org.apache.cactus.internal.configuration.Configuration;
33 import org.apache.cactus.util.ChainedRuntimeException;
34
35
36
37
38
39
40
41
42 public abstract class BaseWebRequest implements WebRequest
43 {
44
45
46
47 private Configuration configuration;
48
49
50
51
52 private Hashtable parametersPost = new Hashtable();
53
54
55
56
57 private Hashtable parametersGet = new Hashtable();
58
59
60
61
62 private Vector cookies = new Vector();
63
64
65
66
67 private Hashtable headers = new Hashtable();
68
69
70
71
72 private InputStream dataStream;
73
74
75
76
77 private String contentType = "application/x-www-form-urlencoded";
78
79
80
81
82 private Authentication authentication;
83
84
85
86
87
88
89
90 public BaseWebRequest()
91 {
92 }
93
94
95
96
97 public BaseWebRequest(Configuration theConfiguration)
98 {
99 this.configuration = theConfiguration;
100 }
101
102
103
104
105 protected Configuration getConfiguration()
106 {
107 return this.configuration;
108 }
109
110
111
112
113
114 public void setConfiguration(Configuration theConfiguration)
115 {
116 this.configuration = theConfiguration;
117 }
118
119
120
121
122
123 public void setContentType(String theContentType)
124 {
125 this.contentType = theContentType;
126 }
127
128
129
130
131
132 public String getContentType()
133 {
134 return this.contentType;
135 }
136
137
138
139
140
141 public void setUserData(InputStream theDataStream)
142 {
143 this.dataStream = theDataStream;
144 }
145
146
147
148
149
150 public InputStream getUserData()
151 {
152 return this.dataStream;
153 }
154
155
156
157
158
159 public void addParameter(String theName, String theValue, String theMethod)
160 {
161 Hashtable parameters;
162
163
164 if (theMethod.equalsIgnoreCase(BaseWebRequest.POST_METHOD))
165 {
166 parameters = this.parametersPost;
167 }
168 else if (theMethod.equalsIgnoreCase(BaseWebRequest.GET_METHOD))
169 {
170 parameters = this.parametersGet;
171 }
172 else
173 {
174 throw new ChainedRuntimeException("The method need to be either "
175 + "\"POST\" or \"GET\"");
176 }
177
178
179
180
181 if (parameters.containsKey(theName))
182 {
183 Vector v = (Vector) parameters.get(theName);
184
185 v.addElement(theValue);
186 }
187 else
188 {
189 Vector v = new Vector();
190
191 v.addElement(theValue);
192 parameters.put(theName, v);
193 }
194 }
195
196
197
198
199
200 public void addParameter(String theName, String theValue)
201 {
202 addParameter(theName, theValue, BaseWebRequest.GET_METHOD);
203 }
204
205
206
207
208
209 public Enumeration getParameterNamesPost()
210 {
211 return getParameterNames(this.parametersPost);
212 }
213
214
215
216
217
218 public Enumeration getParameterNamesGet()
219 {
220 return getParameterNames(this.parametersGet);
221 }
222
223
224
225
226
227
228
229 private Enumeration getParameterNames(Hashtable theParameters)
230 {
231 return theParameters.keys();
232 }
233
234
235
236
237
238 public String getParameterGet(String theName)
239 {
240 String[] values = getParameterValuesGet(theName);
241
242 if (values != null)
243 {
244 return values[0];
245 }
246
247 return null;
248 }
249
250
251
252
253
254 public String getParameterPost(String theName)
255 {
256 String[] values = getParameterValuesPost(theName);
257
258 if (values != null)
259 {
260 return values[0];
261 }
262
263 return null;
264 }
265
266
267
268
269
270 public String[] getParameterValuesGet(String theName)
271 {
272 return getParameterValues(theName, this.parametersGet);
273 }
274
275
276
277
278
279 public String[] getParameterValuesPost(String theName)
280 {
281 return getParameterValues(theName, this.parametersPost);
282 }
283
284
285
286
287
288
289
290
291
292
293 private String[] getParameterValues(String theName, Hashtable theParameters)
294 {
295 if (theParameters.containsKey(theName))
296 {
297 Vector v = (Vector) theParameters.get(theName);
298
299 Object[] objs = new Object[v.size()];
300
301 v.copyInto(objs);
302
303 String[] result = new String[objs.length];
304
305 for (int i = 0; i < objs.length; i++)
306 {
307 result[i] = (String) objs[i];
308 }
309
310 return result;
311 }
312
313 return null;
314 }
315
316
317
318
319
320 public void addCookie(String theName, String theValue)
321 {
322 addCookie("localhost", theName, theValue);
323 }
324
325
326
327
328
329 public void addCookie(String theDomain, String theName, String theValue)
330 {
331 addCookie(new Cookie(theDomain, theName, theValue));
332 }
333
334
335
336
337
338 public void addCookie(Cookie theCookie)
339 {
340 if (theCookie == null)
341 {
342 throw new IllegalStateException("The cookie cannot be null");
343 }
344 this.cookies.addElement(theCookie);
345 }
346
347
348
349
350
351 public Vector getCookies()
352 {
353 return this.cookies;
354 }
355
356
357
358
359
360 public void addHeader(String theName, String theValue)
361 {
362
363
364 if (theName.equalsIgnoreCase("Content-type"))
365 {
366 setContentType(theValue);
367
368 return;
369 }
370
371
372
373
374 if (this.headers.containsKey(theName))
375 {
376 Vector v = (Vector) this.headers.get(theName);
377
378 v.addElement(theValue);
379 }
380 else
381 {
382 Vector v = new Vector();
383
384 v.addElement(theValue);
385 this.headers.put(theName, v);
386 }
387 }
388
389
390
391
392
393 public Enumeration getHeaderNames()
394 {
395 return this.headers.keys();
396 }
397
398
399
400
401
402 public String getHeader(String theName)
403 {
404 String[] values = getHeaderValues(theName);
405
406 if (values != null)
407 {
408 return values[0];
409 }
410
411 return null;
412 }
413
414
415
416
417
418 public String[] getHeaderValues(String theName)
419 {
420 if (this.headers.containsKey(theName))
421 {
422 Vector v = (Vector) this.headers.get(theName);
423
424 Object[] objs = new Object[v.size()];
425
426 v.copyInto(objs);
427
428 String[] result = new String[objs.length];
429
430 for (int i = 0; i < objs.length; i++)
431 {
432 result[i] = (String) objs[i];
433 }
434
435 return result;
436 }
437
438 return null;
439 }
440
441
442
443
444 public String toString()
445 {
446 StringBuffer buffer = new StringBuffer();
447
448
449 buffer.append("cookies = [");
450 buffer.append(toStringAppendCookies());
451 buffer.append("], ");
452
453
454 buffer.append("headers = [");
455 buffer.append(toStringAppendHeaders());
456 buffer.append("], ");
457
458
459 buffer.append("GET parameters = [");
460 buffer.append(toStringAppendParametersGet());
461 buffer.append("], ");
462 buffer.append("POST parameters = [");
463 buffer.append(toStringAppendParametersPost());
464 buffer.append("]");
465
466 return buffer.toString();
467 }
468
469
470
471
472 private String toStringAppendHeaders()
473 {
474 StringBuffer buffer = new StringBuffer();
475
476 Enumeration headers = getHeaderNames();
477
478 while (headers.hasMoreElements())
479 {
480 buffer.append("[");
481
482 String headerName = (String) headers.nextElement();
483 String[] headerValues = getHeaderValues(headerName);
484
485 buffer.append("[" + headerName + "] = [");
486
487 for (int i = 0; i < (headerValues.length - 1); i++)
488 {
489 buffer.append("[" + headerValues[i] + "], ");
490 }
491
492 buffer.append("[" + headerValues[headerValues.length - 1] + "]]");
493 buffer.append("]");
494 }
495
496 return buffer.toString();
497 }
498
499
500
501
502 private String toStringAppendCookies()
503 {
504 StringBuffer buffer = new StringBuffer();
505
506 Enumeration cookies = getCookies().elements();
507
508 while (cookies.hasMoreElements())
509 {
510 Cookie cookie = (Cookie) cookies.nextElement();
511
512 buffer.append("[" + cookie + "]");
513 }
514
515 return buffer.toString();
516 }
517
518
519
520
521
522 private String toStringAppendParametersPost()
523 {
524 return toStringAppendParameters(this.parametersPost);
525 }
526
527
528
529
530
531 private String toStringAppendParametersGet()
532 {
533 return toStringAppendParameters(this.parametersGet);
534 }
535
536
537
538
539
540
541 private String toStringAppendParameters(Hashtable theParameters)
542 {
543 StringBuffer buffer = new StringBuffer();
544
545 Enumeration parameters = getParameterNames(theParameters);
546
547 while (parameters.hasMoreElements())
548 {
549 buffer.append("[");
550
551 String parameterName = (String) parameters.nextElement();
552 String[] parameterValues = getParameterValues(parameterName,
553 theParameters);
554
555 buffer.append("[" + parameterName + "] = [");
556
557 for (int i = 0; i < (parameterValues.length - 1); i++)
558 {
559 buffer.append("[" + parameterValues[i] + "], ");
560 }
561
562 buffer.append("[" + parameterValues[parameterValues.length - 1]
563 + "]]");
564 buffer.append("]");
565 }
566
567 return buffer.toString();
568 }
569
570
571
572
573
574 public void setAuthentication(Authentication theAuthentication)
575 {
576 this.authentication = theAuthentication;
577 }
578
579
580
581
582 public Authentication getAuthentication()
583 {
584 return this.authentication;
585 }
586 }