Clover coverage report - Cactus 1.4b1 for J2EE API 13
Coverage timestamp: Mon Jul 29 2002 00:34:41 BST
file stats: LOC: 137   Methods: 5
NCLOC: 90   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
HttpClientConnectionHelper.java 0% 0% 0% 0%
 1   
 /*   Generated by AspectJ version 1.0.5 */
 2   
 package org.apache.cactus.client;
 3   
 import java.net.URL;
 4   
 import java.net.HttpURLConnection;
 5   
 import java.util.Enumeration;
 6   
 import java.io.InputStream;
 7   
 import java.io.IOException;
 8   
 import java.io.ByteArrayOutputStream;
 9   
 import org.apache.commons.httpclient.HttpClient;
 10   
 import org.apache.commons.httpclient.methods.PostMethod;
 11   
 import org.apache.commons.httpclient.methods.GetMethod;
 12   
 import org.apache.cactus.WebRequest;
 13   
 import org.apache.cactus.client.authentication.AbstractAuthentication;
 14   
 
 15   
 /** 
 16   
  * Implementation of <code>ConnectionHelper</code> using Jakarta Commons 
 17   
  * HttpClient. 
 18   
  * 
 19   
  * @author <a href="mailto:vmassol@apache.org">Vincent Massol</a> 
 20   
  * 
 21   
  * @version $Id: HttpClientConnectionHelper.java,v 1.1 2002/07/26 18:50:29 vmassol Exp $ 
 22   
  */
 23   
 public class HttpClientConnectionHelper extends AbstractConnectionHelper {
 24   
   /** 
 25   
        * The <code>HttpMethod</code> used to connect to the HTTP server. It is 
 26   
        * either a <code>GetMethod</code> or a <code>PostMethod</code>. 
 27   
        */
 28   
   private GetMethod method;
 29   
   /** 
 30   
        * The URL that will be used for the HTTP connection. 
 31   
        */
 32   
   private String url;
 33   
   /** 
 34   
        * @param theURL the URL that will be used for the HTTP connection. 
 35   
        */
 36  0
   public HttpClientConnectionHelper(String theURL) {
 37  0
     super();
 38  0
     this.url = theURL;
 39   
   } 
 40   
   /** 
 41   
        * @see ConnectionHelper#connect(WebRequest) 
 42   
        */
 43  0
   public HttpURLConnection connect(WebRequest theRequest) throws Throwable {
 44  0
     URL url = new URL(this.url);
 45  0
     AbstractAuthentication authentication = theRequest.getAuthentication();
 46  0
     if (authentication != null) {
 47  0
       authentication.configure(theRequest);
 48   
     } 
 49  0
     url = this.addParametersGet(theRequest, url);
 50  0
     if (theRequest.getParameterNamesPost().hasMoreElements() || (theRequest.getUserData() != 
 51   
         null)) {
 52  0
       this.method = new PostMethod();
 53   
     } else {
 54  0
       this.method = new GetMethod();
 55   
     } 
 56  0
     this.method.setUseDisk(false);
 57  0
     this.method.setFollowRedirects(false);
 58  0
     this.method.setPath(url.getPath());
 59  0
     this.method.setQueryString(url.getQuery());
 60  0
     this.method.setRequestHeader("Content-type", theRequest.getContentType());
 61  0
     this.addHeaders(theRequest);
 62  0
     String cookieString = this.getCookieString(theRequest, url);
 63  0
     if (cookieString != null) {
 64  0
       this.method.addRequestHeader("Cookie", cookieString);
 65   
     } 
 66  0
     if (theRequest.getUserData() != null) {
 67  0
       this.addUserData(theRequest);
 68   
     } else {
 69  0
       this.addParametersPost(theRequest);
 70   
     } 
 71  0
     HttpClient client = new HttpClient();
 72  0
     client.startSession(url.getHost(), url.getPort());
 73  0
     client.executeMethod(this.method);
 74  0
     return new org.apache.cactus.util.HttpURLConnection(this.method, url);
 75   
   } 
 76   
 
 77   
   /** 
 78   
        * Add the HTTP parameters that need to be passed in the request body. 
 79   
        * 
 80   
        * @param theRequest the request containing all data to pass to the server 
 81   
        *        redirector. 
 82   
        */
 83  0
   private void addParametersPost(WebRequest theRequest) {
 84  0
     if (!theRequest.getParameterNamesPost().hasMoreElements()) {
 85  0
       return;
 86   
     } 
 87  0
     Enumeration keys = theRequest.getParameterNamesPost();
 88  0
     if (keys.hasMoreElements()) {
 89  0
       String key = (String)keys.nextElement();
 90  0
       String[] values = theRequest.getParameterValuesPost(key);
 91  0
       for (int i = 0; i < values.length; i++) {
 92  0
         ((PostMethod)this.method).addParameter(key, values[i]);
 93   
       } 
 94   
     } 
 95   
   } 
 96   
 
 97   
   /** 
 98   
        * Add the Headers to the request. 
 99   
        * 
 100   
        * @param theRequest the request containing all data to pass to the server 
 101   
        *        redirector. 
 102   
        */
 103  0
   private void addHeaders(WebRequest theRequest) {
 104  0
     Enumeration keys = theRequest.getHeaderNames();
 105  0
     while (keys.hasMoreElements()){
 106  0
       String key = (String)keys.nextElement();
 107  0
       String[] values = theRequest.getHeaderValues(key);
 108  0
       StringBuffer fullHeaderValue = new StringBuffer(values[0]);
 109  0
       for (int i = 1; i < values.length; i++) {
 110  0
         fullHeaderValue.append("," + values[i]);
 111   
       } 
 112  0
       this.method.addRequestHeader(key, fullHeaderValue.toString());
 113   
     } 
 114   
   } 
 115   
 
 116   
   /** 
 117   
        * Add user data in the request body. 
 118   
        * 
 119   
        * @param theRequest the request containing all data to pass to the server 
 120   
        *        redirector. 
 121   
        * @exception IOException if we fail to read the user data 
 122   
        */
 123  0
   private void addUserData(WebRequest theRequest) throws IOException {
 124  0
     if (theRequest.getUserData() == null) {
 125  0
       return;
 126   
     } 
 127  0
     InputStream stream = theRequest.getUserData();
 128  0
     ByteArrayOutputStream baos = new ByteArrayOutputStream();
 129  0
     byte[] buffer = new byte[2048];
 130  0
     int length;
 131  0
     while ((length = stream.read(buffer)) != -1){
 132  0
       baos.write(buffer, 0, length);
 133   
     } 
 134  0
     ((PostMethod)this.method).setRequestBody(baos.toString());
 135   
   } 
 136   
 
 137   
 }