KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > internal > client > connector > http > HttpUtil


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 JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.net.URLEncoder JavaDoc;
25 import java.util.Enumeration JavaDoc;
26
27 import org.apache.cactus.WebRequest;
28
29 /**
30  * Utility methods to manipulate HTTP requests.
31  *
32  * @version $Id: HttpUtil.java,v 1.1 2004/05/22 11:34:46 vmassol Exp $
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     public static URL JavaDoc addHttpGetParameters(WebRequest theRequest, URL JavaDoc theURL)
46         throws MalformedURLException JavaDoc
47     {
48         // If no parameters, then exit
49
if (!theRequest.getParameterNamesGet().hasMoreElements())
50         {
51             return theURL;
52         }
53
54         StringBuffer JavaDoc queryString = new StringBuffer JavaDoc();
55
56         Enumeration JavaDoc keys = theRequest.getParameterNamesGet();
57
58         if (keys.hasMoreElements())
59         {
60             String JavaDoc key = (String JavaDoc) keys.nextElement();
61             String JavaDoc[] 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 JavaDoc key = (String JavaDoc) keys.nextElement();
79             String JavaDoc[] 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 JavaDoc file = theURL.getFile();
91
92         // Remove the trailing "/" if there is one
93
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 JavaDoc(theURL.getProtocol(), theURL.getHost(),
108             theURL.getPort(), file);
109     }
110 }
111
Popular Tags