Clover coverage report - Cactus 1.4 for J2EE API 13
Coverage timestamp: Sun Aug 25 2002 18:02:10 BST
file stats: LOC: 134   Methods: 3
NCLOC: 87   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
AbstractConnectionHelper.java 68.2% 82.7% 100% 79.2%
 1   
 /*   Generated by AspectJ version 1.0.5 */
 2   
 package org.apache.cactus.client;
 3   
 import org.apache.cactus.WebRequest;
 4   
 import org.apache.cactus.Cookie;
 5   
 import org.apache.commons.httpclient.Header;
 6   
 import java.net.MalformedURLException;
 7   
 import java.net.URL;
 8   
 import java.net.URLEncoder;
 9   
 import java.util.Enumeration;
 10   
 import java.util.Vector;
 11   
 
 12   
 /** 
 13   
  * Common helper methods for implementing <code>ConnectionHelper</code>. These 
 14   
  * methods are common to any implementation. 
 15   
  * 
 16   
  * @author <a href="mailto:vmassol@apache.org">Vincent Massol</a> 
 17   
  * 
 18   
  * @version $Id: AbstractConnectionHelper.java,v 1.1 2002/07/24 20:46:48 vmassol Exp $ 
 19   
  */
 20   
 public abstract class AbstractConnectionHelper implements ConnectionHelper {
 21   
   /** 
 22   
        * Add the HTTP parameters that need to be passed in the query string of 
 23   
        * the URL. 
 24   
        * 
 25   
        * @param theRequest the request containing all data to pass to the server 
 26   
        *        redirector. 
 27   
        * @param theURL the URL used to connect to the server redirector. 
 28   
        * @return the new URL 
 29   
        * @exception MalformedURLException if the URL is malformed 
 30   
        */
 31  128
   protected URL addParametersGet(WebRequest theRequest, URL theURL) throws MalformedURLException {
 32  128
     if (!theRequest.getParameterNamesGet().hasMoreElements()) {
 33  0
       return theURL;
 34   
     } 
 35  128
     StringBuffer queryString = new StringBuffer();
 36  128
     Enumeration keys = theRequest.getParameterNamesGet();
 37  128
     if (keys.hasMoreElements()) {
 38  128
       String key = (String)keys.nextElement();
 39  128
       String[] values = theRequest.getParameterValuesGet(key);
 40  128
       queryString.append(key);
 41  128
       queryString.append('=');
 42  128
       queryString.append(URLEncoder.encode(values[0]));
 43  128
       for (int i = 1; i < values.length; i++) {
 44  0
         queryString.append('&');
 45  0
         queryString.append(key);
 46  0
         queryString.append('=');
 47  0
         queryString.append(URLEncoder.encode(values[i]));
 48   
       } 
 49   
     } 
 50  128
     while (keys.hasMoreElements()){
 51  237
       String key = (String)keys.nextElement();
 52  237
       String[] values = theRequest.getParameterValuesGet(key);
 53  237
       for (int i = 0; i < values.length; i++) {
 54  238
         queryString.append('&');
 55  238
         queryString.append(key);
 56  238
         queryString.append('=');
 57  238
         queryString.append(URLEncoder.encode(values[i]));
 58   
       } 
 59   
     } 
 60  128
     String file = theURL.getFile();
 61  128
     if (file.endsWith("/")) {
 62  0
       file = file.substring(0, file.length() - 1);
 63   
     } 
 64  128
     if (theURL.toString().indexOf("?") > 0) {
 65  0
       file = file + "&" + queryString.toString();
 66   
     } else {
 67  128
       file = file + "?" + queryString.toString();
 68   
     } 
 69  128
     return new URL(theURL.getProtocol(), theURL.getHost(), theURL.getPort(), file);
 70   
   } 
 71   
 
 72   
   /** 
 73   
        * @return the cookie string which will be added as a HTTP "Cookie" header 
 74   
        *         or null if no cookie has been set 
 75   
        * @param theRequest the request containing all data to pass to the server 
 76   
        *        redirector. 
 77   
        * @param theUrl the URL to connect to 
 78   
        */
 79  128
   public String getCookieString(WebRequest theRequest, URL theUrl) {
 80  128
     Vector cookies = theRequest.getCookies();
 81  128
     if (!cookies.isEmpty()) {
 82  3
       org.apache.commons.httpclient.Cookie[] httpclientCookies = new 
 83   
           org.apache.commons.httpclient.Cookie[cookies.size()];
 84  3
       for (int i = 0; i < cookies.size(); i++) {
 85  4
         Cookie cactusCookie = (Cookie)cookies.elementAt(i);
 86  4
         String domain;
 87  4
         if (cactusCookie.getDomain() == null) {
 88  0
           domain = Cookie.getCookieDomain(theRequest, theUrl.getHost());
 89   
         } else {
 90  4
           domain = cactusCookie.getDomain();
 91   
         } 
 92  4
         String path;
 93  4
         if (cactusCookie.getPath() == null) {
 94  4
           path = Cookie.getCookiePath(theRequest, theUrl.getFile());
 95   
         } else {
 96  0
           path = cactusCookie.getPath();
 97   
         } 
 98  4
         httpclientCookies[i] = new org.apache.commons.httpclient.Cookie(domain, 
 99   
             cactusCookie.getName(), cactusCookie.getValue());
 100  4
         httpclientCookies[i].setComment(cactusCookie.getComment());
 101  4
         httpclientCookies[i].setExpiryDate(cactusCookie.getExpiryDate());
 102  4
         httpclientCookies[i].setPath(path);
 103  4
         httpclientCookies[i].setSecure(cactusCookie.isSecure());
 104   
       } 
 105  3
       Header cookieHeader = org.apache.commons.httpclient.Cookie.createCookieHeader(
 106   
           Cookie.getCookieDomain(theRequest, theUrl.getHost()), Cookie.getCookiePath(theRequest, 
 107   
           theUrl.getFile()), httpclientCookies);
 108  3
       return cookieHeader.getValue();
 109   
     } 
 110  125
     return ((String)(null));
 111   
   } 
 112   
 
 113   
   /** 
 114   
    * Common helper methods for implementing <code>ConnectionHelper</code>. These 
 115   
    * methods are common to any implementation. 
 116   
    * 
 117   
    * @author <a href="mailto:vmassol@apache.org">Vincent Massol</a> 
 118   
    * 
 119   
    * @version $Id: AbstractConnectionHelper.java,v 1.1 2002/07/24 20:46:48 vmassol Exp $ 
 120   
    */
 121  128
   public AbstractConnectionHelper() {
 122  128
     super();
 123   
   } 
 124   
   /** 
 125   
        * Connects to the Cactus Redirector using HTTP. 
 126   
        * 
 127   
        * @param theRequest the request containing all data to pass to the 
 128   
        *        server redirector. 
 129   
        * @return the HTTP Connection used to connect to the redirector. 
 130   
        * @exception Throwable if an unexpected error occured 
 131   
        */
 132   
   public abstract java.net.HttpURLConnection connect(WebRequest theRequest) throws Throwable;
 133   
 
 134   
 }