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

For more information, please explore the Attic.

Clover coverage report - Cactus 1.8dev for J2EE API 1.3
Coverage timestamp: Sun Mar 26 2006 18:50:18 BRT
file stats: LOC: 565   Methods: 34
NCLOC: 290   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
BaseWebRequest.java 36.1% 38.7% 55.9% 41.3%
coverage coverage
 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;
 23   
 
 24   
 import java.util.Enumeration;
 25   
 import java.util.Hashtable;
 26   
 import java.util.Vector;
 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 238991 2004-05-22 11:34:50Z vmassol $
 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 parametersPost = new Hashtable();
 52   
 
 53   
     /**
 54   
      * The request parameters that need to be sent in the URL (GET)
 55   
      */
 56   
     private Hashtable parametersGet = new Hashtable();
 57   
 
 58   
     /**
 59   
      * The Cookies
 60   
      */
 61   
     private Vector cookies = new Vector();
 62   
 
 63   
     /**
 64   
      * HTTP Headers.
 65   
      */
 66   
     private Hashtable headers = new Hashtable();
 67   
 
 68   
     /**
 69   
      * Binary data to send in the request body (if any)
 70   
      */
 71   
     private InputStream dataStream;
 72   
 
 73   
     /**
 74   
      * The content type to set in the http request
 75   
      */
 76   
     private String 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  0
     public BaseWebRequest()
 90   
     {
 91   
     }
 92   
 
 93   
     /**
 94   
      * @param theConfiguration the Cactus configuration
 95   
      */
 96  48
     public BaseWebRequest(Configuration theConfiguration)
 97   
     {
 98  48
         this.configuration = theConfiguration;
 99   
     }
 100   
 
 101   
     /**
 102   
      * @return the Cactus configuration
 103   
      */
 104  0
     protected Configuration getConfiguration()
 105   
     {
 106  0
         return this.configuration;
 107   
     }
 108   
 
 109   
     /**
 110   
      * @param theConfiguration the cactus configuration to assign to this 
 111   
      *        request
 112   
      */
 113  0
     public void setConfiguration(Configuration theConfiguration)
 114   
     {
 115  0
         this.configuration = theConfiguration;
 116   
     }
 117   
 
 118   
     /**
 119   
      * @see WebRequest#setContentType(String)
 120   
      */
 121  0
     public void setContentType(String theContentType)
 122   
     {
 123  0
         this.contentType = theContentType;
 124   
     }
 125   
 
 126   
     /**
 127   
      * @see WebRequest#getContentType()
 128   
      */
 129  48
     public String getContentType()
 130   
     {
 131  48
         return this.contentType;
 132   
     }
 133   
 
 134   
     /**
 135   
      * @see WebRequest#setUserData(InputStream)
 136   
      */
 137  0
     public void setUserData(InputStream theDataStream)
 138   
     {
 139  0
         this.dataStream = theDataStream;
 140   
     }
 141   
 
 142   
     /**
 143   
      * @see WebRequest#getUserData()
 144   
      */
 145  95
     public InputStream getUserData()
 146   
     {
 147  95
         return this.dataStream;
 148   
     }
 149   
 
 150   
     /**
 151   
      * @see WebRequest#addParameter(String, String, String)
 152   
      */
 153  125
     public void addParameter(String theName, String theValue, String theMethod)
 154   
     {
 155  125
         Hashtable parameters;
 156   
 
 157   
         // Decide if the parameter is to be sent using in the url or not
 158  125
         if (theMethod.equalsIgnoreCase(BaseWebRequest.POST_METHOD))
 159   
         {
 160  1
             parameters = this.parametersPost;
 161   
         }
 162  124
         else if (theMethod.equalsIgnoreCase(BaseWebRequest.GET_METHOD))
 163   
         {
 164  124
             parameters = this.parametersGet;
 165   
         }
 166   
         else
 167   
         {
 168  0
             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  125
         if (parameters.containsKey(theName))
 176   
         {
 177  0
             Vector v = (Vector) parameters.get(theName);
 178   
 
 179  0
             v.addElement(theValue);
 180   
         }
 181   
         else
 182   
         {
 183  125
             Vector v = new Vector();
 184   
 
 185  125
             v.addElement(theValue);
 186  125
             parameters.put(theName, v);
 187   
         }
 188   
     }
 189   
 
 190   
     /**
 191   
      * @see WebRequest#addParameter(String, String)
 192   
      */
 193  2
     public void addParameter(String theName, String theValue)
 194   
     {
 195  2
         addParameter(theName, theValue, BaseWebRequest.GET_METHOD);
 196   
     }
 197   
 
 198   
     /**
 199   
      * @see WebRequest#getParameterNamesPost()
 200   
      */
 201  97
     public Enumeration getParameterNamesPost()
 202   
     {
 203  97
         return getParameterNames(this.parametersPost);
 204   
     }
 205   
 
 206   
     /**
 207   
      * @see WebRequest#getParameterNamesGet()
 208   
      */
 209  96
     public Enumeration getParameterNamesGet()
 210   
     {
 211  96
         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  193
     private Enumeration getParameterNames(Hashtable theParameters)
 221   
     {
 222  193
         return theParameters.keys();
 223   
     }
 224   
 
 225   
     /**
 226   
      * @see WebRequest#getParameterGet(String)
 227   
      */
 228  0
     public String getParameterGet(String theName)
 229   
     {
 230  0
         String[] values = getParameterValuesGet(theName);
 231   
 
 232  0
         if (values != null)
 233   
         {
 234  0
             return values[0];
 235   
         }
 236   
 
 237  0
         return null;
 238   
     }
 239   
 
 240   
     /**
 241   
      * @see WebRequest#getParameterPost(String)
 242   
      */
 243  0
     public String getParameterPost(String theName)
 244   
     {
 245  0
         String[] values = getParameterValuesPost(theName);
 246   
 
 247  0
         if (values != null)
 248   
         {
 249  0
             return values[0];
 250   
         }
 251   
 
 252  0
         return null;
 253   
     }
 254   
 
 255   
     /**
 256   
      * @see WebRequest#getParameterValuesGet(String)
 257   
      */
 258  124
     public String[] getParameterValuesGet(String theName)
 259   
     {
 260  124
         return getParameterValues(theName, this.parametersGet);
 261   
     }
 262   
 
 263   
     /**
 264   
      * @see WebRequest#getParameterValuesPost(String)
 265   
      */
 266  1
     public String[] getParameterValuesPost(String theName)
 267   
     {
 268  1
         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  125
     private String[] getParameterValues(String theName, Hashtable theParameters)
 281   
     {
 282  125
         if (theParameters.containsKey(theName))
 283   
         {
 284  125
             Vector v = (Vector) theParameters.get(theName);
 285   
 
 286  125
             Object[] objs = new Object[v.size()];
 287   
 
 288  125
             v.copyInto(objs);
 289   
 
 290  125
             String[] result = new String[objs.length];
 291   
 
 292  125
             for (int i = 0; i < objs.length; i++)
 293   
             {
 294  125
                 result[i] = (String) objs[i];
 295   
             }
 296   
 
 297  125
             return result;
 298   
         }
 299   
 
 300  0
         return null;
 301   
     }
 302   
 
 303   
     /**
 304   
      * @see WebRequest#addCookie(String, String)
 305   
      */
 306  3
     public void addCookie(String theName, String theValue)
 307   
     {
 308  3
         addCookie("localhost", theName, theValue);
 309   
     }
 310   
 
 311   
     /**
 312   
      * @see WebRequest#addCookie(String, String, String)
 313   
      */
 314  3
     public void addCookie(String theDomain, String theName, String theValue)
 315   
     {
 316  3
         addCookie(new Cookie(theDomain, theName, theValue));
 317   
     }
 318   
 
 319   
     /**
 320   
      * @see WebRequest#addCookie(Cookie)
 321   
      */
 322  3
     public void addCookie(Cookie theCookie)
 323   
     {
 324  3
         if (theCookie == null)
 325   
         {
 326  0
             throw new IllegalStateException("The cookie cannot be null");
 327   
         }
 328  3
         this.cookies.addElement(theCookie);
 329   
     }
 330   
 
 331   
     /**
 332   
      * @see WebRequest#getCookies()
 333   
      */
 334  48
     public Vector getCookies()
 335   
     {
 336  48
         return this.cookies;
 337   
     }
 338   
 
 339   
     /**
 340   
      * @see WebRequest#addHeader(String, String)
 341   
      */
 342  1
     public void addHeader(String theName, String 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  1
         if (theName.equalsIgnoreCase("Content-type"))
 347   
         {
 348  0
             setContentType(theValue);
 349   
 
 350  0
             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  1
         if (this.headers.containsKey(theName))
 357   
         {
 358  0
             Vector v = (Vector) this.headers.get(theName);
 359   
 
 360  0
             v.addElement(theValue);
 361   
         }
 362   
         else
 363   
         {
 364  1
             Vector v = new Vector();
 365   
 
 366  1
             v.addElement(theValue);
 367  1
             this.headers.put(theName, v);
 368   
         }
 369   
     }
 370   
 
 371   
     /**
 372   
      * @see WebRequest#getHeaderNames()
 373   
      */
 374  48
     public Enumeration getHeaderNames()
 375   
     {
 376  48
         return this.headers.keys();
 377   
     }
 378   
 
 379   
     /**
 380   
      * @see WebRequest#getHeader(String)
 381   
      */
 382  0
     public String getHeader(String theName)
 383   
     {
 384  0
         String[] values = getHeaderValues(theName);
 385   
 
 386  0
         if (values != null)
 387   
         {
 388  0
             return values[0];
 389   
         }
 390   
 
 391  0
         return null;
 392   
     }
 393   
 
 394   
     /**
 395   
      * @see WebRequest#getHeaderValues(String)
 396   
      */
 397  1
     public String[] getHeaderValues(String theName)
 398   
     {
 399  1
         if (this.headers.containsKey(theName))
 400   
         {
 401  1
             Vector v = (Vector) this.headers.get(theName);
 402   
 
 403  1
             Object[] objs = new Object[v.size()];
 404   
 
 405  1
             v.copyInto(objs);
 406   
 
 407  1
             String[] result = new String[objs.length];
 408   
 
 409  1
             for (int i = 0; i < objs.length; i++)
 410   
             {
 411  1
                 result[i] = (String) objs[i];
 412   
             }
 413   
 
 414  1
             return result;
 415   
         }
 416   
 
 417  0
         return null;
 418   
     }
 419   
 
 420   
     /**
 421   
      * @return a string representation of the request
 422   
      */
 423  0
     public String toString()
 424   
     {
 425  0
         StringBuffer buffer = new StringBuffer();
 426   
 
 427   
         // Append cookies
 428  0
         buffer.append("cookies = [");
 429  0
         buffer.append(toStringAppendCookies());
 430  0
         buffer.append("], ");
 431   
 
 432   
         // Append headers
 433  0
         buffer.append("headers = [");
 434  0
         buffer.append(toStringAppendHeaders());
 435  0
         buffer.append("], ");
 436   
 
 437   
         // Append parameters
 438  0
         buffer.append("GET parameters = [");
 439  0
         buffer.append(toStringAppendParametersGet());
 440  0
         buffer.append("], ");
 441  0
         buffer.append("POST parameters = [");
 442  0
         buffer.append(toStringAppendParametersPost());
 443  0
         buffer.append("]");
 444   
 
 445  0
         return buffer.toString();
 446   
     }
 447   
 
 448   
     /**
 449   
      * @return a string representation of the headers
 450   
      */
 451  0
     private String toStringAppendHeaders()
 452   
     {
 453  0
         StringBuffer buffer = new StringBuffer();
 454   
 
 455  0
         Enumeration headers = getHeaderNames();
 456   
 
 457  0
         while (headers.hasMoreElements())
 458   
         {
 459  0
             buffer.append("[");
 460   
 
 461  0
             String headerName = (String) headers.nextElement();
 462  0
             String[] headerValues = getHeaderValues(headerName);
 463   
 
 464  0
             buffer.append("[" + headerName + "] = [");
 465   
 
 466  0
             for (int i = 0; i < (headerValues.length - 1); i++)
 467   
             {
 468  0
                 buffer.append("[" + headerValues[i] + "], ");
 469   
             }
 470   
 
 471  0
             buffer.append("[" + headerValues[headerValues.length - 1] + "]]");
 472  0
             buffer.append("]");
 473   
         }
 474   
 
 475  0
         return buffer.toString();
 476   
     }
 477   
 
 478   
     /**
 479   
      * @return a string representation of the cookies
 480   
      */
 481  0
     private String toStringAppendCookies()
 482   
     {
 483  0
         StringBuffer buffer = new StringBuffer();
 484   
 
 485  0
         Enumeration cookies = getCookies().elements();
 486   
 
 487  0
         while (cookies.hasMoreElements())
 488   
         {
 489  0
             Cookie cookie = (Cookie) cookies.nextElement();
 490   
 
 491  0
             buffer.append("[" + cookie + "]");
 492   
         }
 493   
 
 494  0
         return buffer.toString();
 495   
     }
 496   
 
 497   
     /**
 498   
      * @return a string representation of the parameters to be added in the
 499   
      *         request body
 500   
      */
 501  0
     private String toStringAppendParametersPost()
 502   
     {
 503  0
         return toStringAppendParameters(this.parametersPost);
 504   
     }
 505   
 
 506   
     /**
 507   
      * @return a string representation of the parameters to be added in the
 508   
      *         URL
 509   
      */
 510  0
     private String toStringAppendParametersGet()
 511   
     {
 512  0
         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  0
     private String toStringAppendParameters(Hashtable theParameters)
 521   
     {
 522  0
         StringBuffer buffer = new StringBuffer();
 523   
 
 524  0
         Enumeration parameters = getParameterNames(theParameters);
 525   
 
 526  0
         while (parameters.hasMoreElements())
 527   
         {
 528  0
             buffer.append("[");
 529   
 
 530  0
             String parameterName = (String) parameters.nextElement();
 531  0
             String[] parameterValues = getParameterValues(parameterName, 
 532   
                 theParameters);
 533   
 
 534  0
             buffer.append("[" + parameterName + "] = [");
 535   
 
 536  0
             for (int i = 0; i < (parameterValues.length - 1); i++)
 537   
             {
 538  0
                 buffer.append("[" + parameterValues[i] + "], ");
 539   
             }
 540   
 
 541  0
             buffer.append("[" + parameterValues[parameterValues.length - 1]
 542   
                 + "]]");
 543  0
             buffer.append("]");
 544   
         }
 545   
 
 546  0
         return buffer.toString();
 547   
     }
 548   
 
 549   
     /**
 550   
      * @see WebRequest#setAuthentication(Authentication)
 551   
      */
 552  0
     public void setAuthentication(Authentication theAuthentication)
 553   
     {
 554  0
         this.authentication = theAuthentication;
 555   
     }
 556   
 
 557   
     /**
 558   
      * @see WebRequest#getAuthentication()
 559   
      */
 560  72
     public Authentication getAuthentication()
 561   
     {
 562  72
         return this.authentication;
 563   
     }
 564   
 }
 565