Clover coverage report - Cactus 1.4.1 for J2EE API 13
Coverage timestamp: Sat Aug 31 2002 22:02:23 BST
file stats: LOC: 197   Methods: 8
NCLOC: 117   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
WebResponse.java 81.2% 89.8% 87.5% 88%
 1   
 /*   Generated by AspectJ version 1.0.5 */
 2   
 package org.apache.cactus;
 3   
 import java.io.BufferedReader;
 4   
 import java.io.IOException;
 5   
 import java.io.InputStream;
 6   
 import java.io.StringReader;
 7   
 import java.net.HttpURLConnection;
 8   
 import java.util.Vector;
 9   
 import org.apache.commons.httpclient.Header;
 10   
 import org.apache.commons.httpclient.HttpException;
 11   
 import org.apache.commons.logging.Log;
 12   
 import org.apache.commons.logging.LogFactory;
 13   
 import org.apache.cactus.util.ChainedRuntimeException;
 14   
 import org.apache.cactus.util.IoUtil;
 15   
 
 16   
 /** 
 17   
  * Default web response implementation that provides a minimal 
 18   
  * API for asserting returned output stream from the server side. For more 
 19   
  * complex assertions, use an <code>com.meterware.httpunit.WebResponse</code> 
 20   
  * instead as parameter of your <code>endXXX()</code> methods. 
 21   
  * 
 22   
  * @author <a href="mailto:vmassol@apache.org">Vincent Massol</a> 
 23   
  * 
 24   
  * @version $Id: WebResponse.html,v 1.1 2003/04/14 12:27:31 sinisa Exp $ 
 25   
  */
 26   
 public class WebResponse {
 27   
   /** 
 28   
        * The logger 
 29   
        */
 30   
   private static Log LOGGER;
 31   
   /** 
 32   
        * The connection object that was used to call the URL 
 33   
        */
 34   
   private HttpURLConnection connection;
 35   
   /** 
 36   
        * The request data that were used to open the connection to the server. 
 37   
        */
 38   
   private WebRequest request;
 39   
   /** 
 40   
        * Save the response content for repeatable reads. 
 41   
        */
 42   
   private String content;
 43   
   /** 
 44   
        * @param theRequest the request data that were used to open the 
 45   
        *        connection to the server. 
 46   
        * @param theConnection the original <code>HttpURLConnection</code> used 
 47   
        *        to call the URL 
 48   
        */
 49  106
   public WebResponse(WebRequest theRequest, HttpURLConnection theConnection) {
 50  106
     super();
 51  106
     this.request = theRequest;
 52  106
     this.connection = theConnection;
 53   
   } 
 54   
   /** 
 55   
        * @return the original <code>HttpURLConnection</code> used to call the 
 56   
        *         URL 
 57   
        */
 58  45
   public HttpURLConnection getConnection() {
 59  45
     return this.connection;
 60   
   } 
 61   
 
 62   
   /** 
 63   
        * @return the request data the were used to open the connection to the 
 64   
        *         server 
 65   
        */
 66  15
   public WebRequest getWebRequest() {
 67  15
     return this.request;
 68   
   } 
 69   
 
 70   
   /** 
 71   
        * @return the text of the response (excluding headers) as a string. 
 72   
        */
 73  85
   public String getText() {
 74  85
     if (this.content == null) {
 75  70
       try {
 76  70
         this.content = IoUtil.getText(this.connection.getInputStream());
 77   
       } catch (IOException e) {
 78  0
         throw new ChainedRuntimeException(e);
 79   
       } 
 80   
     } 
 81  85
     return this.content;
 82   
   } 
 83   
 
 84   
   /** 
 85   
        * @return the text of the response (excluding headers) as an array of 
 86   
        *         strings (each string is a separate line from the output stream). 
 87   
        */
 88  10
   public String[] getTextAsArray() {
 89  10
     Vector lines = new Vector();
 90  10
     try {
 91  10
       if (this.content == null) {
 92  5
         this.getText();
 93   
       } 
 94  10
       BufferedReader input = new BufferedReader(new StringReader(this.content));
 95  10
       String str;
 96  ?
       while (null != (str = input.readLine())){
 97  30
         lines.addElement(str);
 98   
       } 
 99  10
       input.close();
 100   
     } catch (IOException e) {
 101  0
       throw new ChainedRuntimeException(e);
 102   
     } 
 103  10
     String[] dummy = new java.lang.String[lines.size()];
 104  10
     return ((String[])(lines.toArray(dummy)));
 105   
   } 
 106   
 
 107   
   /** 
 108   
        * @return a buffered input stream for reading the response data. 
 109   
        **/
 110  0
   public InputStream getInputStream() {
 111  0
     try {
 112  0
       return this.connection.getInputStream();
 113   
     } catch (IOException e) {
 114  0
       throw new ChainedRuntimeException(e);
 115   
     } 
 116   
   } 
 117   
 
 118   
   /** 
 119   
        * Return the first cookie found that has the specified name or null 
 120   
        * if not found. 
 121   
        * 
 122   
        * @param theName the cookie name to find 
 123   
        * @return the cookie or null if not found 
 124   
        */
 125  5
   public Cookie getCookie(String theName) {
 126  5
     Cookie result = null;
 127  5
     Cookie[] cookies = this.getCookies();
 128  6
     for (int i = 0; i < cookies.length; i++) {
 129  6
       if (cookies[i].getName().equals(theName)) {
 130  5
         result = cookies[i];
 131  5
         break;
 132   
       } 
 133   
     } 
 134  5
     return result;
 135   
   } 
 136   
 
 137   
   /** 
 138   
        * @return the cookies returned by the server 
 139   
        */
 140  5
   public Cookie[] getCookies() {
 141  5
     Cookie[] returnCookies = null;
 142  5
     String headerName = this.connection.getHeaderFieldKey(0);
 143  5
     String headerValue = this.connection.getHeaderField(0);
 144  5
     WebResponse.LOGGER.debug("Header name [0]  = [" + headerName + "]");
 145  5
     WebResponse.LOGGER.debug("Header value [0] = [" + headerValue + "]");
 146  5
     Vector cookieVector = new Vector();
 147  5
     for (int i = 1; (headerName != null) || (headerValue != null); i++) {
 148  28
       WebResponse.LOGGER.debug("Header name  = [" + headerName + "]");
 149  28
       WebResponse.LOGGER.debug("Header value = [" + headerValue + "]");
 150  28
       if ((headerName != null) && (headerName.toLowerCase().equals("set-cookie") || 
 151   
           headerName.toLowerCase().equals("set-cookie2"))) {
 152  5
         org.apache.commons.httpclient.Cookie[] cookies;
 153  5
         try {
 154  5
           cookies = org.apache.commons.httpclient.Cookie.parse(Cookie.getCookieDomain(
 155   
               this.getWebRequest(), this.getConnection().getURL().getHost()), 
 156   
               Cookie.getCookiePort(this.getWebRequest(), this.getConnection().getURL().getPort()), 
 157   
               Cookie.getCookiePath(this.getWebRequest(), this.getConnection().getURL().getFile()), 
 158   
               new Header(headerName, headerValue));
 159   
         } catch (HttpException e) {
 160  0
           throw new ChainedRuntimeException("Error parsing cookies", e);
 161   
         } 
 162  5
         for (int j = 0; j < cookies.length; j++) {
 163  10
           Cookie cookie = new Cookie(cookies[j].getDomain(), cookies[j].getName(), 
 164   
               cookies[j].getValue());
 165  10
           cookie.setComment(cookies[j].getComment());
 166  10
           cookie.setExpiryDate(cookies[j].getExpiryDate());
 167  10
           cookie.setPath(cookies[j].getPath());
 168  10
           cookie.setSecure(cookies[j].getSecure());
 169  10
           cookieVector.addElement(cookie);
 170   
         } 
 171   
       } 
 172  28
       headerName = this.connection.getHeaderFieldKey(i);
 173  28
       headerValue = this.connection.getHeaderField(i);
 174   
     } 
 175  5
     returnCookies = new org.apache.cactus.Cookie[cookieVector.size()];
 176  5
     cookieVector.copyInto(returnCookies);
 177  5
     return returnCookies;
 178   
   } 
 179   
 
 180   
   /** 
 181   
    * Default web response implementation that provides a minimal 
 182   
    * API for asserting returned output stream from the server side. For more 
 183   
    * complex assertions, use an <code>com.meterware.httpunit.WebResponse</code> 
 184   
    * instead as parameter of your <code>endXXX()</code> methods. 
 185   
    * 
 186   
    * @author <a href="mailto:vmassol@apache.org">Vincent Massol</a> 
 187   
    * 
 188   
    * @version $Id: WebResponse.html,v 1.1 2003/04/14 12:27:31 sinisa Exp $ 
 189   
    */
 190   
   static {
 191   
     /** 
 192   
          * The logger 
 193   
          */
 194  11
     WebResponse.LOGGER = LogFactory.getLog(WebResponse.class);
 195   
   } 
 196   
 
 197   
 }