2011/08/05 - Jakarta Cactus has been retired.

For more information, please explore the Attic.

Clover coverage report - Cactus 1.8dev for J2EE API 1.3
Coverage timestamp: Sun Mar 26 2006 18:50:18 BRT
file stats: LOC: 111   Methods: 1
NCLOC: 61   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
HttpUtil.java 64.3% 76.7% 100% 73.3%
coverage coverage
 1   
 /* 
 2   
  * ========================================================================
 3   
  * 
 4   
  * Copyright 2001-2004 The Apache Software Foundation.
 5   
  *
 6   
  * Licensed under the Apache License, Version 2.0 (the "License");
 7   
  * you may not use this file except in compliance with the License.
 8   
  * You may obtain a copy of the License at
 9   
  * 
 10   
  *   http://www.apache.org/licenses/LICENSE-2.0
 11   
  * 
 12   
  * Unless required by applicable law or agreed to in writing, software
 13   
  * distributed under the License is distributed on an "AS IS" BASIS,
 14   
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15   
  * See the License for the specific language governing permissions and
 16   
  * limitations under the License.
 17   
  * 
 18   
  * ========================================================================
 19   
  */
 20   
 package org.apache.cactus.internal.client.connector.http;
 21   
 
 22   
 import java.net.MalformedURLException;
 23   
 import java.net.URL;
 24   
 import java.net.URLEncoder;
 25   
 import java.util.Enumeration;
 26   
 
 27   
 import org.apache.cactus.WebRequest;
 28   
 
 29   
 /**
 30   
  * Utility methods to manipulate HTTP requests.
 31   
  *
 32   
  * @version $Id: HttpUtil.java 238991 2004-05-22 11:34:50Z vmassol $
 33   
  * @since 1.5
 34   
  */
 35   
 public class HttpUtil
 36   
 {
 37   
     /**
 38   
      * Add HTTP GET parameters to the URL passed as parameter.
 39   
      *
 40   
      * @param theRequest the request containing the HTTP GET parameters to add
 41   
      * @param theURL the URL that will be enriched with the HTTP GET parameters
 42   
      * @return the enriched URL
 43   
      * @exception MalformedURLException if the URL is malformed
 44   
      */
 45  48
     public static URL addHttpGetParameters(WebRequest theRequest, URL theURL)
 46   
         throws MalformedURLException
 47   
     {
 48   
         // If no parameters, then exit
 49  48
         if (!theRequest.getParameterNamesGet().hasMoreElements())
 50   
         {
 51  0
             return theURL;
 52   
         }
 53   
 
 54  48
         StringBuffer queryString = new StringBuffer();
 55   
 
 56  48
         Enumeration keys = theRequest.getParameterNamesGet();
 57   
 
 58  48
         if (keys.hasMoreElements())
 59   
         {
 60  48
             String key = (String) keys.nextElement();
 61  48
             String[] values = theRequest.getParameterValuesGet(key);
 62   
 
 63  48
             queryString.append(key);
 64  48
             queryString.append('=');
 65  48
             queryString.append(URLEncoder.encode(values[0]));
 66   
 
 67  48
             for (int i = 1; i < values.length; i++)
 68   
             {
 69  0
                 queryString.append('&');
 70  0
                 queryString.append(key);
 71  0
                 queryString.append('=');
 72  0
                 queryString.append(URLEncoder.encode(values[i]));
 73   
             }
 74   
         }
 75   
 
 76  48
         while (keys.hasMoreElements())
 77   
         {
 78  76
             String key = (String) keys.nextElement();
 79  76
             String[] values = theRequest.getParameterValuesGet(key);
 80   
 
 81  76
             for (int i = 0; i < values.length; i++)
 82   
             {
 83  76
                 queryString.append('&');
 84  76
                 queryString.append(key);
 85  76
                 queryString.append('=');
 86  76
                 queryString.append(URLEncoder.encode(values[i]));
 87   
             }
 88   
         }
 89   
 
 90  48
         String file = theURL.getFile();
 91   
 
 92   
         // Remove the trailing "/" if there is one
 93  48
         if (file.endsWith("/"))
 94   
         {
 95  0
             file = file.substring(0, file.length() - 1);
 96   
         }
 97   
 
 98  48
         if (theURL.toString().indexOf("?") > 0)
 99   
         {
 100  0
             file = file + "&" + queryString.toString();
 101   
         }
 102   
         else
 103   
         {
 104  48
             file = file + "?" + queryString.toString();
 105   
         }
 106   
 
 107  48
         return new URL(theURL.getProtocol(), theURL.getHost(), 
 108   
             theURL.getPort(), file);
 109   
     }
 110   
 }
 111