Clover coverage report - Cactus 1.5 for J2EE API 1.2
Coverage timestamp: Wed Feb 18 2004 09:04:33 EST
file stats: LOC: 356   Methods: 10
NCLOC: 176   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
WebResponse.java 0% 0% 0% 0%
coverage
 1   
 /*
 2   
  * ====================================================================
 3   
  *
 4   
  * The Apache Software License, Version 1.1
 5   
  *
 6   
  * Copyright (c) 2001-2003 The Apache Software Foundation.  All rights
 7   
  * reserved.
 8   
  *
 9   
  * Redistribution and use in source and binary forms, with or without
 10   
  * modification, are permitted provided that the following conditions
 11   
  * are met:
 12   
  *
 13   
  * 1. Redistributions of source code must retain the above copyright
 14   
  *    notice, this list of conditions and the following disclaimer.
 15   
  *
 16   
  * 2. Redistributions in binary form must reproduce the above copyright
 17   
  *    notice, this list of conditions and the following disclaimer in
 18   
  *    the documentation and/or other materials provided with the
 19   
  *    distribution.
 20   
  *
 21   
  * 3. The end-user documentation included with the redistribution, if
 22   
  *    any, must include the following acknowlegement:
 23   
  *       "This product includes software developed by the
 24   
  *        Apache Software Foundation (http://www.apache.org/)."
 25   
  *    Alternately, this acknowlegement may appear in the software itself,
 26   
  *    if and wherever such third-party acknowlegements normally appear.
 27   
  *
 28   
  * 4. The names "The Jakarta Project", "Cactus" and "Apache Software
 29   
  *    Foundation" must not be used to endorse or promote products
 30   
  *    derived from this software without prior written permission. For
 31   
  *    written permission, please contact apache@apache.org.
 32   
  *
 33   
  * 5. Products derived from this software may not be called "Apache"
 34   
  *    nor may "Apache" appear in their names without prior written
 35   
  *    permission of the Apache Group.
 36   
  *
 37   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 38   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 39   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 40   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 41   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 42   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 43   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 44   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 45   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 46   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 47   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 48   
  * SUCH DAMAGE.
 49   
  * ====================================================================
 50   
  *
 51   
  * This software consists of voluntary contributions made by many
 52   
  * individuals on behalf of the Apache Software Foundation.  For more
 53   
  * information on the Apache Software Foundation, please see
 54   
  * <http://www.apache.org/>.
 55   
  *
 56   
  */
 57   
 package org.apache.cactus;
 58   
 
 59   
 import java.io.BufferedReader;
 60   
 import java.io.IOException;
 61   
 import java.io.InputStream;
 62   
 import java.io.StringReader;
 63   
 
 64   
 import java.net.HttpURLConnection;
 65   
 
 66   
 import java.util.Vector;
 67   
 
 68   
 import org.apache.cactus.util.ChainedRuntimeException;
 69   
 import org.apache.cactus.util.CookieUtil;
 70   
 import org.apache.cactus.util.IoUtil;
 71   
 import org.apache.commons.httpclient.Header;
 72   
 import org.apache.commons.httpclient.HttpException;
 73   
 import org.apache.commons.httpclient.cookie.CookiePolicy;
 74   
 import org.apache.commons.httpclient.cookie.CookieSpec;
 75   
 import org.apache.commons.logging.Log;
 76   
 import org.apache.commons.logging.LogFactory;
 77   
 
 78   
 /**
 79   
  * Default web response implementation that provides a minimal
 80   
  * API for asserting returned output stream from the server side. For more
 81   
  * complex assertions, use an <code>com.meterware.httpunit.WebResponse</code>
 82   
  * instead as parameter of your <code>endXXX()</code> methods.
 83   
  *
 84   
  * @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
 85   
  *
 86   
  * @version $Id: WebResponse.java,v 1.15 2003/07/07 10:50:07 cmlenz Exp $
 87   
  */
 88   
 public class WebResponse
 89   
 {
 90   
     /**
 91   
      * The logger
 92   
      */
 93   
     private static final Log LOGGER = LogFactory.getLog(WebResponse.class);
 94   
 
 95   
     /**
 96   
      * The connection object that was used to call the URL
 97   
      */
 98   
     private HttpURLConnection connection;
 99   
 
 100   
     /**
 101   
      * The request data that were used to open the connection to the server.
 102   
      */
 103   
     private WebRequest request;
 104   
 
 105   
     /**
 106   
      * Save the response content for repeatable reads.
 107   
      */
 108   
     private String content;
 109   
 
 110   
     /**
 111   
      * @param theRequest the request data that were used to open the
 112   
      *        connection to the server.
 113   
      * @param theConnection the original <code>HttpURLConnection</code> used
 114   
      *        to call the URL
 115   
      */
 116  0
     public WebResponse(WebRequest theRequest, HttpURLConnection theConnection)
 117   
     {
 118  0
         this.request = theRequest;
 119  0
         this.connection = theConnection;
 120   
     }
 121   
 
 122   
     /**
 123   
      * @return the original <code>HttpURLConnection</code> used to call the
 124   
      *         URL
 125   
      */
 126  0
     public HttpURLConnection getConnection()
 127   
     {
 128  0
         return this.connection;
 129   
     }
 130   
 
 131   
     /**
 132   
      * @return the request data the were used to open the connection to the
 133   
      *         server
 134   
      */
 135  0
     public WebRequest getWebRequest()
 136   
     {
 137  0
         return this.request;
 138   
     }
 139   
 
 140   
     /**
 141   
      * @return the text of the response (excluding headers) as a string.
 142   
      */
 143  0
     public String getText()
 144   
     {
 145   
         // Get the text from the save content if content has already been
 146   
         // read.
 147  0
         if (this.content == null)
 148   
         {
 149  0
             try
 150   
             {
 151  0
                 this.content = IoUtil.getText(this.connection.getInputStream());
 152   
             }
 153   
             catch (IOException e)
 154   
             {
 155  0
                 throw new ChainedRuntimeException(e);
 156   
             }
 157   
         }
 158   
 
 159  0
         return this.content;
 160   
     }
 161   
 
 162   
     /**
 163   
      * @return the text of the response (excluding headers) as an array of
 164   
      *         strings (each string is a separate line from the output stream).
 165   
      */
 166  0
     public String[] getTextAsArray()
 167   
     {
 168  0
         Vector lines = new Vector();
 169   
 
 170  0
         try
 171   
         {
 172   
             // Read content first
 173  0
             if (this.content == null)
 174   
             {
 175  0
                 getText();
 176   
             }
 177   
 
 178  0
             BufferedReader input = new BufferedReader(
 179   
                 new StringReader(this.content));
 180  0
             String str;
 181   
 
 182  0
             while (null != (str = input.readLine()))
 183   
             {
 184  0
                 lines.addElement(str);
 185   
             }
 186   
 
 187  0
             input.close();
 188   
         }
 189   
         catch (IOException e)
 190   
         {
 191  0
             throw new ChainedRuntimeException(e);
 192   
         }
 193   
 
 194   
         // Dummy variable to explicitely tell the object type to copy.
 195  0
         String[] dummy = new String[lines.size()];
 196   
 
 197  0
         return (String[]) (lines.toArray(dummy));
 198   
     }
 199   
 
 200   
     /**
 201   
      * @return a buffered input stream for reading the response data.
 202   
      **/
 203  0
     public InputStream getInputStream()
 204   
     {
 205  0
         try
 206   
         {
 207  0
             return this.connection.getInputStream();
 208   
         }
 209   
         catch (IOException e)
 210   
         {
 211  0
             throw new ChainedRuntimeException(e);
 212   
         }
 213   
     }
 214   
 
 215   
     /**
 216   
      * Return the first cookie found that has the specified name or null
 217   
      * if not found.
 218   
      *
 219   
      * @param theName the cookie name to find
 220   
      * @return the cookie or null if not found
 221   
      */
 222  0
     public Cookie getCookie(String theName)
 223   
     {
 224  0
         Cookie result = null;
 225   
 
 226  0
         Cookie[] cookies = getCookies();
 227   
 
 228  0
         for (int i = 0; i < cookies.length; i++)
 229   
         {
 230  0
             if (cookies[i].getName().equals(theName))
 231   
             {
 232  0
                 result = cookies[i];
 233   
 
 234  0
                 break;
 235   
             }
 236   
         }
 237   
 
 238  0
         return result;
 239   
     }
 240   
 
 241   
     /**
 242   
      * Return the first cookie found that has the specified name or null
 243   
      * if not found. The name is case-insensitive.
 244   
      *
 245   
      * @param theName the cookie name to find (case-insensitive)
 246   
      * @return the cookie or null if not found
 247   
      * @since 1.5
 248   
      */
 249  0
     public Cookie getCookieIgnoreCase(String theName)
 250   
     {
 251  0
         Cookie result = null;
 252   
 
 253  0
         Cookie[] cookies = getCookies();
 254   
 
 255  0
         for (int i = 0; i < cookies.length; i++)
 256   
         {
 257  0
             if (cookies[i].getName().equalsIgnoreCase(theName))
 258   
             {
 259  0
                 result = cookies[i];
 260   
 
 261  0
                 break;
 262   
             }
 263   
         }
 264   
 
 265  0
         return result;
 266   
     }
 267   
 
 268   
     /**
 269   
      * @return the cookies returned by the server
 270   
      */
 271  0
     public Cookie[] getCookies()
 272   
     {
 273  0
         Cookie[] returnCookies = null;
 274   
 
 275   
         // There can be several headers named "Set-Cookie", so loop through
 276   
         // all the headers, looking for cookies
 277  0
         String headerName = this.connection.getHeaderFieldKey(0);
 278  0
         String headerValue = this.connection.getHeaderField(0);
 279   
 
 280  0
         Vector cookieVector = new Vector();
 281  0
         CookieSpec cookieSpec = CookiePolicy.getDefaultSpec();
 282   
 
 283  0
         for (int i = 1; (headerName != null) || (headerValue != null); i++)
 284   
         {
 285  0
             LOGGER.debug("Header name  = [" + headerName + "]");
 286  0
             LOGGER.debug("Header value = [" + headerValue + "]");
 287   
 
 288  0
             if ((headerName != null)
 289   
                 && (headerName.toLowerCase().equals("set-cookie") 
 290   
                 || headerName.toLowerCase().equals("set-cookie2")))
 291   
             {
 292   
                 // Parse the cookie definition
 293  0
                 org.apache.commons.httpclient.Cookie[] cookies;
 294  0
                 try
 295   
                 {
 296  0
                     cookies = cookieSpec.parse(
 297   
                         CookieUtil.getCookieDomain(getWebRequest(), 
 298   
                             getConnection().getURL().getHost()), 
 299   
                         CookieUtil.getCookiePort(getWebRequest(), 
 300   
                             getConnection().getURL().getPort()), 
 301   
                         CookieUtil.getCookiePath(getWebRequest(), 
 302   
                             getConnection().getURL().getFile()),
 303   
                         false, new Header(headerName, headerValue));
 304   
                 }
 305   
                 catch (HttpException e)
 306   
                 {
 307  0
                     throw new ChainedRuntimeException(
 308   
                         "Error parsing cookies", e);
 309   
                 }
 310   
 
 311   
                 // Transform the HttpClient cookies into Cactus cookies and
 312   
                 // add them to the cookieVector vector
 313  0
                 for (int j = 0; j < cookies.length; j++)
 314   
                 {
 315  0
                     Cookie cookie = new Cookie(cookies[j].getDomain(), 
 316   
                         cookies[j].getName(), cookies[j].getValue());
 317   
 
 318  0
                     cookie.setComment(cookies[j].getComment());
 319  0
                     cookie.setExpiryDate(cookies[j].getExpiryDate());
 320  0
                     cookie.setPath(cookies[j].getPath());
 321  0
                     cookie.setSecure(cookies[j].getSecure());
 322   
 
 323  0
                     cookieVector.addElement(cookie);
 324   
                 }
 325   
             }
 326   
 
 327  0
             headerName = this.connection.getHeaderFieldKey(i);
 328  0
             headerValue = this.connection.getHeaderField(i);
 329   
         }
 330   
 
 331  0
         returnCookies = new Cookie[cookieVector.size()];
 332  0
         cookieVector.copyInto(returnCookies);
 333   
 
 334  0
         return returnCookies;
 335   
     }
 336   
 
 337   
     /**
 338   
      * Returns the status code returned by the server.
 339   
      * 
 340   
      * @return The status code
 341   
      * @since 1.5
 342   
      */
 343  0
     public int getStatusCode()
 344   
     {
 345  0
         try
 346   
         {
 347  0
             return this.connection.getResponseCode();
 348   
         }
 349   
         catch (IOException e)
 350   
         {
 351  0
             throw new ChainedRuntimeException(e);
 352   
         }
 353   
     }
 354   
 
 355   
 }
 356