2011/08/05 - Jakarta Cactus has been retired.

For more information, please explore the Attic.

View Javadoc

1   /* 
2    * ========================================================================
3    * 
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements.  See the NOTICE file distributed with
6    * this work for additional information regarding copyright ownership.
7    * The ASF licenses this file to You under the Apache License, Version 2.0
8    * (the "License"); you may not use this file except in compliance with
9    * the License.  You may obtain a copy of the License at
10   * 
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
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   * Contains all HTTP request data for a test case but independently of
37   * the fact that there is or there is not a Cactus redirector. It is the 
38   * data that will be sent to the server side.
39   *
40   * @version $Id: BaseWebRequest.java 238991 2004-05-22 11:34:50Z vmassol $
41   */
42  public abstract class BaseWebRequest implements WebRequest
43  {
44      /**
45       * Cactus configuration.
46       */
47      private Configuration configuration;
48  
49      /**
50       * The request parameters that need to be sent in the body (POST).
51       */
52      private Hashtable parametersPost = new Hashtable();
53  
54      /**
55       * The request parameters that need to be sent in the URL (GET).
56       */
57      private Hashtable parametersGet = new Hashtable();
58  
59      /**
60       * The Cookies.
61       */
62      private Vector cookies = new Vector();
63  
64      /**
65       * HTTP Headers.
66       */
67      private Hashtable headers = new Hashtable();
68  
69      /**
70       * Binary data to send in the request body (if any).
71       */
72      private InputStream dataStream;
73  
74      /**
75       * The content type to set in the http request.
76       */
77      private String contentType = "application/x-www-form-urlencoded";
78  
79      /**
80       * The Authentication Object that will configure the http request.
81       */
82      private Authentication authentication;
83  
84      /**
85       * Default constructor that requires that 
86       * {@link #setConfiguration(Configuration)} be called before the methods
87       * requiring a configuration object.
88       * 
89       */
90      public BaseWebRequest()
91      {
92      }
93  
94      /**
95       * @param theConfiguration the Cactus configuration
96       */
97      public BaseWebRequest(Configuration theConfiguration)
98      {
99          this.configuration = theConfiguration;
100     }
101 
102     /**
103      * @return the Cactus configuration
104      */
105     protected Configuration getConfiguration()
106     {
107         return this.configuration;
108     }
109 
110     /**
111      * @param theConfiguration the cactus configuration to assign to this 
112      *        request
113      */
114     public void setConfiguration(Configuration theConfiguration)
115     {
116         this.configuration = theConfiguration;
117     }
118 
119     /**
120      * {@inheritDoc}
121      * @see WebRequest#setContentType(String)
122      */
123     public void setContentType(String theContentType)
124     {
125         this.contentType = theContentType;
126     }
127 
128     /**
129      * {@inheritDoc}
130      * @see WebRequest#getContentType()
131      */
132     public String getContentType()
133     {
134         return this.contentType;
135     }
136 
137     /**
138      * {@inheritDoc}
139      * @see WebRequest#setUserData(InputStream)
140      */
141     public void setUserData(InputStream theDataStream)
142     {
143         this.dataStream = theDataStream;
144     }
145 
146     /**
147      * {@inheritDoc}
148      * @see WebRequest#getUserData()
149      */
150     public InputStream getUserData()
151     {
152         return this.dataStream;
153     }
154 
155     /**
156      * {@inheritDoc}
157      * @see WebRequest#addParameter(String, String, String)
158      */
159     public void addParameter(String theName, String theValue, String theMethod)
160     {
161         Hashtable parameters;
162 
163         // Decide if the parameter is to be sent using in the url or not
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         // If there is already a parameter of the same name, add the
179         // new value to the Vector. If not, create a Vector an add it to the
180         // hashtable
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      * {@inheritDoc}
198      * @see WebRequest#addParameter(String, String)
199      */
200     public void addParameter(String theName, String theValue)
201     {
202         addParameter(theName, theValue, BaseWebRequest.GET_METHOD);
203     }
204 
205     /**
206      * {@inheritDoc}
207      * @see WebRequest#getParameterNamesPost()
208      */
209     public Enumeration getParameterNamesPost()
210     {
211         return getParameterNames(this.parametersPost);
212     }
213 
214     /**
215      * {@inheritDoc}
216      * @see WebRequest#getParameterNamesGet()
217      */
218     public Enumeration getParameterNamesGet()
219     {
220         return getParameterNames(this.parametersGet);
221     }
222 
223     /**
224      * Returns all the values in the passed hashtable of parameters.
225      *
226      * @param theParameters the hashtable of parameters
227      * @return the parameter names
228      */
229     private Enumeration getParameterNames(Hashtable theParameters)
230     {
231         return theParameters.keys();
232     }
233 
234     /**
235      * {@inheritDoc}
236      * @see WebRequest#getParameterGet(String)
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      * {@inheritDoc}
252      * @see WebRequest#getParameterPost(String)
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      * {@inheritDoc}
268      * @see WebRequest#getParameterValuesGet(String)
269      */
270     public String[] getParameterValuesGet(String theName)
271     {
272         return getParameterValues(theName, this.parametersGet);
273     }
274 
275     /**
276      * {@inheritDoc}
277      * @see WebRequest#getParameterValuesPost(String)
278      */
279     public String[] getParameterValuesPost(String theName)
280     {
281         return getParameterValues(theName, this.parametersPost);
282     }
283 
284     /**
285      * Returns all the values corresponding to this parameter's name in the
286      * provided hashtable.
287      *
288      * @param theName the parameter's name
289      * @param theParameters the hashtable containing the parameters
290      * @return the first value corresponding to this parameter's name or null
291      *         if not found in the passed hashtable
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      * {@inheritDoc}
318      * @see WebRequest#addCookie(String, String)
319      */
320     public void addCookie(String theName, String theValue)
321     {
322         addCookie("localhost", theName, theValue);
323     }
324 
325     /**
326      * {@inheritDoc}
327      * @see WebRequest#addCookie(String, String, String)
328      */
329     public void addCookie(String theDomain, String theName, String theValue)
330     {
331         addCookie(new Cookie(theDomain, theName, theValue));
332     }
333 
334     /**
335      * {@inheritDoc}
336      * @see WebRequest#addCookie(Cookie)
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      * {@inheritDoc}
349      * @see WebRequest#getCookies()
350      */
351     public Vector getCookies()
352     {
353         return this.cookies;
354     }
355 
356     /**
357      * {@inheritDoc}
358      * @see WebRequest#addHeader(String, String)
359      */
360     public void addHeader(String theName, String theValue)
361     {
362         // If the header is "Content-type", then call setContentType() instead.
363         // This is to prevent the content type to be set twice.
364         if (theName.equalsIgnoreCase("Content-type"))
365         {
366             setContentType(theValue);
367 
368             return;
369         }
370 
371         // If there is already a header of the same name, add the
372         // new header to the Vector. If not, create a Vector an add it to the
373         // hashtable
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      * {@inheritDoc}
391      * @see WebRequest#getHeaderNames()
392      */
393     public Enumeration getHeaderNames()
394     {
395         return this.headers.keys();
396     }
397 
398     /**
399      * {@inheritDoc}
400      * @see WebRequest#getHeader(String)
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      * {@inheritDoc}
416      * @see WebRequest#getHeaderValues(String)
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      * @return a string representation of the request
443      */
444     public String toString()
445     {
446         StringBuffer buffer = new StringBuffer();
447 
448         // Append cookies
449         buffer.append("cookies = [");
450         buffer.append(toStringAppendCookies());
451         buffer.append("], ");
452 
453         // Append headers
454         buffer.append("headers = [");
455         buffer.append(toStringAppendHeaders());
456         buffer.append("], ");
457 
458         // Append parameters
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      * @return a string representation of the headers
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      * @return a string representation of the cookies
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      * @return a string representation of the parameters to be added in the
520      *         request body
521      */
522     private String toStringAppendParametersPost()
523     {
524         return toStringAppendParameters(this.parametersPost);
525     }
526 
527     /**
528      * @return a string representation of the parameters to be added in the
529      *         URL
530      */
531     private String toStringAppendParametersGet()
532     {
533         return toStringAppendParameters(this.parametersGet);
534     }
535 
536     /**
537      * @param theParameters the HTTP parameters
538      * @return a string representation of the HTTP parameters passed as
539      *         parameters
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      * {@inheritDoc}
572      * @see WebRequest#setAuthentication(Authentication)
573      */
574     public void setAuthentication(Authentication theAuthentication)
575     {
576         this.authentication = theAuthentication;
577     }
578 
579     /**{@inheritDoc}
580      * @see WebRequest#getAuthentication()
581      */
582     public Authentication getAuthentication()
583     {
584         return this.authentication;
585     }
586 }