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