01: /*
02: * ========================================================================
03: *
04: * Copyright 2001-2004 The Apache Software Foundation.
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
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: * Add HTTP GET parameters to the URL passed as parameter.
38: *
39: * @param theRequest the request containing the HTTP GET parameters to add
40: * @param theURL the URL that will be enriched with the HTTP GET parameters
41: * @return the enriched URL
42: * @exception MalformedURLException if the URL is malformed
43: */
44: public static URL addHttpGetParameters(WebRequest theRequest,
45: URL theURL) throws MalformedURLException {
46: // If no parameters, then exit
47: if (!theRequest.getParameterNamesGet().hasMoreElements()) {
48: return theURL;
49: }
50:
51: StringBuffer queryString = new StringBuffer();
52:
53: Enumeration keys = theRequest.getParameterNamesGet();
54:
55: if (keys.hasMoreElements()) {
56: String key = (String) keys.nextElement();
57: String[] values = theRequest.getParameterValuesGet(key);
58:
59: queryString.append(key);
60: queryString.append('=');
61: queryString.append(URLEncoder.encode(values[0]));
62:
63: for (int i = 1; i < values.length; i++) {
64: queryString.append('&');
65: queryString.append(key);
66: queryString.append('=');
67: queryString.append(URLEncoder.encode(values[i]));
68: }
69: }
70:
71: while (keys.hasMoreElements()) {
72: String key = (String) keys.nextElement();
73: String[] values = theRequest.getParameterValuesGet(key);
74:
75: for (int i = 0; i < values.length; i++) {
76: queryString.append('&');
77: queryString.append(key);
78: queryString.append('=');
79: queryString.append(URLEncoder.encode(values[i]));
80: }
81: }
82:
83: String file = theURL.getFile();
84:
85: // Remove the trailing "/" if there is one
86: if (file.endsWith("/")) {
87: file = file.substring(0, file.length() - 1);
88: }
89:
90: if (theURL.toString().indexOf("?") > 0) {
91: file = file + "&" + queryString.toString();
92: } else {
93: file = file + "?" + queryString.toString();
94: }
95:
96: return new URL(theURL.getProtocol(), theURL.getHost(), theURL
97: .getPort(), file);
98: }
99: }
|