KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > internal > BaseWebRequest


1 /*
2  * ========================================================================
3  *
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * ========================================================================
19  */

20 package org.apache.cactus.internal;
21
22 import java.io.InputStream JavaDoc;
23
24 import java.util.Enumeration JavaDoc;
25 import java.util.Hashtable JavaDoc;
26 import java.util.Vector JavaDoc;
27
28 import org.apache.cactus.Cookie;
29 import org.apache.cactus.WebRequest;
30 import org.apache.cactus.client.authentication.Authentication;
31 import org.apache.cactus.internal.configuration.Configuration;
32 import org.apache.cactus.util.ChainedRuntimeException;
33
34 /**
35  * Contains all HTTP request data for a test case but independently of
36  * the fact that there is or there is not a Cactus redirector. It is the
37  * data that will be sent to the server side.
38  *
39  * @version $Id: BaseWebRequest.java,v 1.1 2004/05/22 11:34:47 vmassol Exp $
40  */

41 public abstract class BaseWebRequest implements WebRequest
42 {
43     /**
44      * Cactus configuration
45      */

46     private Configuration configuration;
47
48     /**
49      * The request parameters that need to be sent in the body (POST)
50      */

51     private Hashtable JavaDoc parametersPost = new Hashtable JavaDoc();
52
53     /**
54      * The request parameters that need to be sent in the URL (GET)
55      */

56     private Hashtable JavaDoc parametersGet = new Hashtable JavaDoc();
57
58     /**
59      * The Cookies
60      */

61     private Vector JavaDoc cookies = new Vector JavaDoc();
62
63     /**
64      * HTTP Headers.
65      */

66     private Hashtable JavaDoc headers = new Hashtable JavaDoc();
67
68     /**
69      * Binary data to send in the request body (if any)
70      */

71     private InputStream JavaDoc dataStream;
72
73     /**
74      * The content type to set in the http request
75      */

76     private String JavaDoc contentType = "application/x-www-form-urlencoded";
77
78     /**
79      * The Authentication Object that will configure the http request
80      */

81     private Authentication authentication;
82
83     /**
84      * Default constructor that requires that
85      * {@link #setConfiguration(Configuration)} be called before the methods
86      * requiring a configuration object.
87      *
88      */

89     public BaseWebRequest()
90     {
91     }
92
93     /**
94      * @param theConfiguration the Cactus configuration
95      */

96     public BaseWebRequest(Configuration theConfiguration)
97     {
98         this.configuration = theConfiguration;
99     }
100
101     /**
102      * @return the Cactus configuration
103      */

104     protected Configuration getConfiguration()
105     {
106         return this.configuration;
107     }
108
109     /**
110      * @param theConfiguration the cactus configuration to assign to this
111      * request
112      */

113     public void setConfiguration(Configuration theConfiguration)
114     {
115         this.configuration = theConfiguration;
116     }
117
118     /**
119      * @see WebRequest#setContentType(String)
120      */

121     public void setContentType(String JavaDoc theContentType)
122     {
123         this.contentType = theContentType;
124     }
125
126     /**
127      * @see WebRequest#getContentType()
128      */

129     public String JavaDoc getContentType()
130     {
131         return this.contentType;
132     }
133
134     /**
135      * @see WebRequest#setUserData(InputStream)
136      */

137     public void setUserData(InputStream JavaDoc theDataStream)
138     {
139         this.dataStream = theDataStream;
140     }
141
142     /**
143      * @see WebRequest#getUserData()
144      */

145     public InputStream JavaDoc getUserData()
146     {
147         return this.dataStream;
148     }
149
150     /**
151      * @see WebRequest#addParameter(String, String, String)
152      */

153     public void addParameter(String JavaDoc theName, String JavaDoc theValue, String JavaDoc theMethod)
154     {
155         Hashtable JavaDoc parameters;
156
157         // Decide if the parameter is to be sent using in the url or not
158
if (theMethod.equalsIgnoreCase(BaseWebRequest.POST_METHOD))
159         {
160             parameters = this.parametersPost;
161         }
162         else if (theMethod.equalsIgnoreCase(BaseWebRequest.GET_METHOD))
163         {
164             parameters = this.parametersGet;
165         }
166         else
167         {
168             throw new ChainedRuntimeException("The method need to be either "
169                 + "\"POST\" or \"GET\"");
170         }
171
172         // If there is already a parameter of the same name, add the
173
// new value to the Vector. If not, create a Vector an add it to the
174
// hashtable
175
if (parameters.containsKey(theName))
176         {
177             Vector JavaDoc v = (Vector JavaDoc) parameters.get(theName);
178
179             v.addElement(theValue);
180         }
181         else
182         {
183             Vector JavaDoc v = new Vector JavaDoc();
184
185             v.addElement(theValue);
186             parameters.put(theName, v);
187         }
188     }
189
190     /**
191      * @see WebRequest#addParameter(String, String)
192      */

193     public void addParameter(String JavaDoc theName, String JavaDoc theValue)
194     {
195         addParameter(theName, theValue, BaseWebRequest.GET_METHOD);
196     }
197
198     /**
199      * @see WebRequest#getParameterNamesPost()
200      */

201     public Enumeration JavaDoc getParameterNamesPost()
202     {
203         return getParameterNames(this.parametersPost);
204     }
205
206     /**
207      * @see WebRequest#getParameterNamesGet()
208      */

209     public Enumeration JavaDoc getParameterNamesGet()
210     {
211         return getParameterNames(this.parametersGet);
212     }
213
214     /**
215      * Returns all the values in the passed hashtable of parameters.
216      *
217      * @param theParameters the hashtable of parameters
218      * @return the parameter names
219      */

220     private Enumeration JavaDoc getParameterNames(Hashtable JavaDoc theParameters)
221     {
222         return theParameters.keys();
223     }
224
225     /**
226      * @see WebRequest#getParameterGet(String)
227      */

228     public String JavaDoc getParameterGet(String JavaDoc theName)
229     {
230         String JavaDoc[] values = getParameterValuesGet(theName);
231
232         if (values != null)
233         {
234             return values[0];
235         }
236
237         return null;
238     }
239
240     /**
241      * @see WebRequest#getParameterPost(String)
242      */

243     public String JavaDoc getParameterPost(String JavaDoc theName)
244     {
245         String JavaDoc[] values = getParameterValuesPost(theName);
246
247         if (values != null)
248         {
249             return values[0];
250         }
251
252         return null;
253     }
254
255     /**
256      * @see WebRequest#getParameterValuesGet(String)
257      */

258     public String JavaDoc[] getParameterValuesGet(String JavaDoc theName)
259     {
260         return getParameterValues(theName, this.parametersGet);
261     }
262
263     /**
264      * @see WebRequest#getParameterValuesPost(String)
265      */

266     public String JavaDoc[] getParameterValuesPost(String JavaDoc theName)
267     {
268         return getParameterValues(theName, this.parametersPost);
269     }
270
271     /**
272      * Returns all the values corresponding to this parameter's name in the
273      * provided hashtable.
274      *
275      * @param theName the parameter's name
276      * @param theParameters the hashtable containing the parameters
277      * @return the first value corresponding to this parameter's name or null
278      * if not found in the passed hashtable
279      */

280     private String JavaDoc[] getParameterValues(String JavaDoc theName, Hashtable JavaDoc theParameters)
281     {
282         if (theParameters.containsKey(theName))
283         {
284             Vector JavaDoc v = (Vector JavaDoc) theParameters.get(theName);
285
286             Object JavaDoc[] objs = new Object JavaDoc[v.size()];
287
288             v.copyInto(objs);
289
290             String JavaDoc[] result = new String JavaDoc[objs.length];
291
292             for (int i = 0; i < objs.length; i++)
293             {
294                 result[i] = (String JavaDoc) objs[i];
295             }
296
297             return result;
298         }
299
300         return null;
301     }
302
303     /**
304      * @see WebRequest#addCookie(String, String)
305      */

306     public void addCookie(String JavaDoc theName, String JavaDoc theValue)
307     {
308         addCookie("localhost", theName, theValue);
309     }
310
311     /**
312      * @see WebRequest#addCookie(String, String, String)
313      */

314     public void addCookie(String JavaDoc theDomain, String JavaDoc theName, String JavaDoc theValue)
315     {
316         addCookie(new Cookie(theDomain, theName, theValue));
317     }
318
319     /**
320      * @see WebRequest#addCookie(Cookie)
321      */

322     public void addCookie(Cookie theCookie)
323     {
324         if (theCookie == null)
325         {
326             throw new IllegalStateException JavaDoc("The cookie cannot be null");
327         }
328         this.cookies.addElement(theCookie);
329     }
330
331     /**
332      * @see WebRequest#getCookies()
333      */

334     public Vector JavaDoc getCookies()
335     {
336         return this.cookies;
337     }
338
339     /**
340      * @see WebRequest#addHeader(String, String)
341      */

342     public void addHeader(String JavaDoc theName, String JavaDoc theValue)
343     {
344         // If the header is "Content-type", then call setContentType() instead.
345
// This is to prevent the content type to be set twice.
346
if (theName.equalsIgnoreCase("Content-type"))
347         {
348             setContentType(theValue);
349
350             return;
351         }
352
353         // If there is already a header of the same name, add the
354
// new header to the Vector. If not, create a Vector an add it to the
355
// hashtable
356
if (this.headers.containsKey(theName))
357         {
358             Vector JavaDoc v = (Vector JavaDoc) this.headers.get(theName);
359
360             v.addElement(theValue);
361         }
362         else
363         {
364             Vector JavaDoc v = new Vector JavaDoc();
365
366             v.addElement(theValue);
367             this.headers.put(theName, v);
368         }
369     }
370
371     /**
372      * @see WebRequest#getHeaderNames()
373      */

374     public Enumeration JavaDoc getHeaderNames()
375     {
376         return this.headers.keys();
377     }
378
379     /**
380      * @see WebRequest#getHeader(String)
381      */

382     public String JavaDoc getHeader(String JavaDoc theName)
383     {
384         String JavaDoc[] values = getHeaderValues(theName);
385
386         if (values != null)
387         {
388             return values[0];
389         }
390
391         return null;
392     }
393
394     /**
395      * @see WebRequest#getHeaderValues(String)
396      */

397     public String JavaDoc[] getHeaderValues(String JavaDoc theName)
398     {
399         if (this.headers.containsKey(theName))
400         {
401             Vector JavaDoc v = (Vector JavaDoc) this.headers.get(theName);
402
403             Object JavaDoc[] objs = new Object JavaDoc[v.size()];
404
405             v.copyInto(objs);
406
407             String JavaDoc[] result = new String JavaDoc[objs.length];
408
409             for (int i = 0; i < objs.length; i++)
410             {
411                 result[i] = (String JavaDoc) objs[i];
412             }
413
414             return result;
415         }
416
417         return null;
418     }
419
420     /**
421      * @return a string representation of the request
422      */

423     public String JavaDoc toString()
424     {
425         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
426
427         // Append cookies
428
buffer.append("cookies = [");
429         buffer.append(toStringAppendCookies());
430         buffer.append("], ");
431
432         // Append headers
433
buffer.append("headers = [");
434         buffer.append(toStringAppendHeaders());
435         buffer.append("], ");
436
437         // Append parameters
438
buffer.append("GET parameters = [");
439         buffer.append(toStringAppendParametersGet());
440         buffer.append("], ");
441         buffer.append("POST parameters = [");
442         buffer.append(toStringAppendParametersPost());
443         buffer.append("]");
444
445         return buffer.toString();
446     }
447
448     /**
449      * @return a string representation of the headers
450      */

451     private String JavaDoc toStringAppendHeaders()
452     {
453         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
454
455         Enumeration JavaDoc headers = getHeaderNames();
456
457         while (headers.hasMoreElements())
458         {
459             buffer.append("[");
460
461             String JavaDoc headerName = (String JavaDoc) headers.nextElement();
462             String JavaDoc[] headerValues = getHeaderValues(headerName);
463
464             buffer.append("[" + headerName + "] = [");
465
466             for (int i = 0; i < (headerValues.length - 1); i++)
467             {
468                 buffer.append("[" + headerValues[i] + "], ");
469             }
470
471             buffer.append("[" + headerValues[headerValues.length - 1] + "]]");
472             buffer.append("]");
473         }
474
475         return buffer.toString();
476     }
477
478     /**
479      * @return a string representation of the cookies
480      */

481     private String JavaDoc toStringAppendCookies()
482     {
483         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
484
485         Enumeration JavaDoc cookies = getCookies().elements();
486
487         while (cookies.hasMoreElements())
488         {
489             Cookie cookie = (Cookie) cookies.nextElement();
490
491             buffer.append("[" + cookie + "]");
492         }
493
494         return buffer.toString();
495     }
496
497     /**
498      * @return a string representation of the parameters to be added in the
499      * request body
500      */

501     private String JavaDoc toStringAppendParametersPost()
502     {
503         return toStringAppendParameters(this.parametersPost);
504     }
505
506     /**
507      * @return a string representation of the parameters to be added in the
508      * URL
509      */

510     private String JavaDoc toStringAppendParametersGet()
511     {
512         return toStringAppendParameters(this.parametersGet);
513     }
514
515     /**
516      * @param theParameters the HTTP parameters
517      * @return a string representation of the HTTP parameters passed as
518      * parameters
519      */

520     private String JavaDoc toStringAppendParameters(Hashtable JavaDoc theParameters)
521     {
522         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
523
524         Enumeration JavaDoc parameters = getParameterNames(theParameters);
525
526         while (parameters.hasMoreElements())
527         {
528             buffer.append("[");
529
530             String JavaDoc parameterName = (String JavaDoc) parameters.nextElement();
531             String JavaDoc[] parameterValues = getParameterValues(parameterName,
532                 theParameters);
533
534             buffer.append("[" + parameterName + "] = [");
535
536             for (int i = 0; i < (parameterValues.length - 1); i++)
537             {
538                 buffer.append("[" + parameterValues[i] + "], ");
539             }
540
541             buffer.append("[" + parameterValues[parameterValues.length - 1]
542                 + "]]");
543             buffer.append("]");
544         }
545
546         return buffer.toString();
547     }
548
549     /**
550      * @see WebRequest#setAuthentication(Authentication)
551      */

552     public void setAuthentication(Authentication theAuthentication)
553     {
554         this.authentication = theAuthentication;
555     }
556
557     /**
558      * @see WebRequest#getAuthentication()
559      */

560     public Authentication getAuthentication()
561     {
562         return this.authentication;
563     }
564 }
565
Popular Tags