Clover coverage report - Cactus 1.4b1 for J2EE API 13
Coverage timestamp: Mon Jul 29 2002 00:34:41 BST
file stats: LOC: 195   Methods: 8
NCLOC: 115   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
WebResponse.java 0% 7% 12.5% 6.2%
 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.java,v 1.6 2002/07/23 22:35:36 vmassol 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  1
   public WebResponse(WebRequest theRequest, HttpURLConnection theConnection) {
 50  1
     super();
 51  1
     this.request = theRequest;
 52  1
     this.connection = theConnection;
 53   
   } 
 54   
   /** 
 55   
        * @return the original <code>HttpURLConnection</code> used to call the 
 56   
        *         URL 
 57   
        */
 58  0
   public HttpURLConnection getConnection() {
 59  0
     return this.connection;
 60   
   } 
 61   
 
 62   
   /** 
 63   
        * @return the request data the were used to open the connection to the 
 64   
        *         server 
 65   
        */
 66  0
   public WebRequest getWebRequest() {
 67  0
     return this.request;
 68   
   } 
 69   
 
 70   
   /** 
 71   
        * @return the text of the response (excluding headers) as a string. 
 72   
        */
 73  0
   public String getText() {
 74  0
     if (this.content == null) {
 75  0
       try {
 76  0
         this.content = IoUtil.getText(this.connection.getInputStream());
 77   
       } catch (IOException e) {
 78  0
         throw new ChainedRuntimeException(e);
 79   
       } 
 80   
     } 
 81  0
     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  0
   public String[] getTextAsArray() {
 89  0
     Vector lines = new Vector();
 90  0
     try {
 91  0
       if (this.content == null) {
 92  0
         this.getText();
 93   
       } 
 94  0
       BufferedReader input = new BufferedReader(new StringReader(this.content));
 95  0
       String str;
 96  0
       while (null != (str = input.readLine())){
 97  0
         lines.addElement(str);
 98   
       } 
 99  0
       input.close();
 100   
     } catch (IOException e) {
 101  0
       throw new ChainedRuntimeException(e);
 102   
     } 
 103  0
     String[] dummy = new java.lang.String[lines.size()];
 104  0
     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  0
   public Cookie getCookie(String theName) {
 126  0
     Cookie result = null;
 127  0
     Cookie[] cookies = this.getCookies();
 128  0
     for (int i = 0; i < cookies.length; i++) {
 129  0
       if (cookies[i].getName().equals(theName)) {
 130  0
         result = cookies[i];
 131  0
         break;
 132   
       } 
 133   
     } 
 134  0
     return result;
 135   
   } 
 136   
 
 137   
   /** 
 138   
        * @return the cookies returned by the server 
 139   
        */
 140  0
   public Cookie[] getCookies() {
 141  0
     Cookie[] returnCookies = null;
 142  0
     String headerName = this.connection.getHeaderFieldKey(0);
 143  0
     String headerValue = this.connection.getHeaderField(0);
 144  0
     Vector cookieVector = new Vector();
 145  0
     for (int i = 1; (headerName != null) || (headerValue != null); i++) {
 146  0
       WebResponse.LOGGER.debug("Header name  = [" + headerName + "]");
 147  0
       WebResponse.LOGGER.debug("Header value = [" + headerValue + "]");
 148  0
       if ((headerName != null) && (headerName.toLowerCase().equals("set-cookie") || 
 149   
           headerName.toLowerCase().equals("set-cookie2"))) {
 150  0
         org.apache.commons.httpclient.Cookie[] cookies;
 151  0
         try {
 152  0
           cookies = org.apache.commons.httpclient.Cookie.parse(Cookie.getCookieDomain(
 153   
               this.getWebRequest(), this.getConnection().getURL().getHost()), 
 154   
               Cookie.getCookiePort(this.getWebRequest(), this.getConnection().getURL().getPort()), 
 155   
               Cookie.getCookiePath(this.getWebRequest(), this.getConnection().getURL().getFile()), 
 156   
               new Header(headerName, headerValue));
 157   
         } catch (HttpException e) {
 158  0
           throw new ChainedRuntimeException("Error parsing cookies", e);
 159   
         } 
 160  0
         for (int j = 0; j < cookies.length; j++) {
 161  0
           Cookie cookie = new Cookie(cookies[j].getDomain(), cookies[j].getName(), 
 162   
               cookies[j].getValue());
 163  0
           cookie.setComment(cookies[j].getComment());
 164  0
           cookie.setExpiryDate(cookies[j].getExpiryDate());
 165  0
           cookie.setPath(cookies[j].getPath());
 166  0
           cookie.setSecure(cookies[j].getSecure());
 167  0
           cookieVector.addElement(cookie);
 168   
         } 
 169   
       } 
 170  0
       headerName = this.connection.getHeaderFieldKey(i);
 171  0
       headerValue = this.connection.getHeaderField(i);
 172   
     } 
 173  0
     returnCookies = new org.apache.cactus.Cookie[cookieVector.size()];
 174  0
     cookieVector.copyInto(returnCookies);
 175  0
     return returnCookies;
 176   
   } 
 177   
 
 178   
   /** 
 179   
    * Default web response implementation that provides a minimal 
 180   
    * API for asserting returned output stream from the server side. For more 
 181   
    * complex assertions, use an <code>com.meterware.httpunit.WebResponse</code> 
 182   
    * instead as parameter of your <code>endXXX()</code> methods. 
 183   
    * 
 184   
    * @author <a href="mailto:vmassol@apache.org">Vincent Massol</a> 
 185   
    * 
 186   
    * @version $Id: WebResponse.java,v 1.6 2002/07/23 22:35:36 vmassol Exp $ 
 187   
    */
 188   
   static {
 189   
     /** 
 190   
          * The logger 
 191   
          */
 192  1
     WebResponse.LOGGER = LogFactory.getLog(WebResponse.class);
 193   
   } 
 194   
 
 195   
 }