1 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 35 public class HttpUtil 36 { 37 45 public static URL addHttpGetParameters(WebRequest theRequest, URL theURL) 46 throws MalformedURLException 47 { 48 if (!theRequest.getParameterNamesGet().hasMoreElements()) 50 { 51 return theURL; 52 } 53 54 StringBuffer queryString = new StringBuffer (); 55 56 Enumeration keys = theRequest.getParameterNamesGet(); 57 58 if (keys.hasMoreElements()) 59 { 60 String key = (String ) keys.nextElement(); 61 String [] values = theRequest.getParameterValuesGet(key); 62 63 queryString.append(key); 64 queryString.append('='); 65 queryString.append(URLEncoder.encode(values[0])); 66 67 for (int i = 1; i < values.length; i++) 68 { 69 queryString.append('&'); 70 queryString.append(key); 71 queryString.append('='); 72 queryString.append(URLEncoder.encode(values[i])); 73 } 74 } 75 76 while (keys.hasMoreElements()) 77 { 78 String key = (String ) keys.nextElement(); 79 String [] values = theRequest.getParameterValuesGet(key); 80 81 for (int i = 0; i < values.length; i++) 82 { 83 queryString.append('&'); 84 queryString.append(key); 85 queryString.append('='); 86 queryString.append(URLEncoder.encode(values[i])); 87 } 88 } 89 90 String file = theURL.getFile(); 91 92 if (file.endsWith("/")) 94 { 95 file = file.substring(0, file.length() - 1); 96 } 97 98 if (theURL.toString().indexOf("?") > 0) 99 { 100 file = file + "&" + queryString.toString(); 101 } 102 else 103 { 104 file = file + "?" + queryString.toString(); 105 } 106 107 return new URL (theURL.getProtocol(), theURL.getHost(), 108 theURL.getPort(), file); 109 } 110 } 111 | Popular Tags |