1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.util;
24  
25  import java.io.IOException;
26  
27  import java.net.URL;
28  
29  import java.util.Map;
30  
31  import javax.portlet.ActionRequest;
32  import javax.portlet.RenderRequest;
33  
34  import javax.servlet.http.Cookie;
35  import javax.servlet.http.HttpServletRequest;
36  
37  /**
38   * <a href="HttpUtil.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class HttpUtil {
44  
45      public static String addParameter(String url, String name, boolean value) {
46          return getHttp().addParameter(url, name, value);
47      }
48  
49      public static String addParameter(String url, String name, double value) {
50          return getHttp().addParameter(url, name, value);
51      }
52  
53      public static String addParameter(String url, String name, int value) {
54          return getHttp().addParameter(url, name, value);
55      }
56  
57      public static String addParameter(String url, String name, long value) {
58          return getHttp().addParameter(url, name, value);
59      }
60  
61      public static String addParameter(String url, String name, short value) {
62          return getHttp().addParameter(url, name, value);
63      }
64  
65      public static String addParameter(String url, String name, String value) {
66          return getHttp().addParameter(url, name, value);
67      }
68  
69      public static String decodeURL(String url) {
70          return getHttp().decodeURL(url);
71      }
72  
73      public static String decodeURL(String url, boolean unescapeSpace) {
74          return getHttp().decodeURL(url, unescapeSpace);
75      }
76  
77      public static String encodeURL(String url) {
78          return getHttp().encodeURL(url);
79      }
80  
81      public static String encodeURL(String url, boolean escapeSpaces) {
82          return getHttp().encodeURL(url, escapeSpaces);
83      }
84  
85      public static String getCompleteURL(HttpServletRequest request) {
86          return getHttp().getCompleteURL(request);
87      }
88  
89      public static String getDomain(String url) {
90          return getHttp().getDomain(url);
91      }
92  
93      public static Http getHttp() {
94          return _http;
95      }
96  
97      public static String getParameter(String url, String name) {
98          return getHttp().getParameter(url, name);
99      }
100 
101     public static String getParameter(
102         String url, String name, boolean escaped) {
103 
104         return getHttp().getParameter(url, name, escaped);
105     }
106 
107     public static Map<String, String[]> getParameterMap(String queryString) {
108         return getHttp().getParameterMap(queryString);
109     }
110 
111     public static String getProtocol(boolean secure) {
112         return getHttp().getProtocol(secure);
113     }
114 
115     public static String getProtocol(String url) {
116         return getHttp().getProtocol(url);
117     }
118 
119     public static String getProtocol(HttpServletRequest request) {
120         return getHttp().getProtocol(request);
121     }
122 
123     public static String getProtocol(ActionRequest actionRequest) {
124         return getHttp().getProtocol(actionRequest);
125     }
126 
127     public static String getProtocol(RenderRequest renderRequest) {
128         return getHttp().getProtocol(renderRequest);
129     }
130 
131     public static String getQueryString(String url) {
132         return getHttp().getQueryString(url);
133     }
134 
135     public static String getRequestURL(HttpServletRequest request) {
136         return getHttp().getRequestURL(request);
137     }
138 
139     public static boolean hasDomain(String url) {
140         return getHttp().hasDomain(url);
141     }
142 
143     public static boolean hasProxyConfig() {
144         return getHttp().hasProxyConfig();
145     }
146 
147     public static boolean isNonProxyHost(String host) {
148         return getHttp().isNonProxyHost(host);
149     }
150 
151     public static boolean isProxyHost(String host) {
152         return getHttp().isProxyHost(host);
153     }
154 
155     public static Map<String, String[]> parameterMapFromString(
156         String queryString) {
157 
158         return getHttp().parameterMapFromString(queryString);
159     }
160 
161     public static String parameterMapToString(
162         Map<String, String[]> parameterMap) {
163 
164         return getHttp().parameterMapToString(parameterMap);
165     }
166 
167     public static String parameterMapToString(
168         Map<String, String[]> parameterMap, boolean addQuestion) {
169 
170         return getHttp().parameterMapToString(parameterMap, addQuestion);
171     }
172 
173     public static String protocolize(String url, boolean secure) {
174         return getHttp().protocolize(url, secure);
175     }
176 
177     public static String protocolize(String url, HttpServletRequest request) {
178         return getHttp().protocolize(url, request);
179     }
180 
181     public static String protocolize(String url, ActionRequest actionRequest) {
182         return getHttp().protocolize(url, actionRequest);
183     }
184 
185     public static String protocolize(String url, RenderRequest renderRequest) {
186         return getHttp().protocolize(url, renderRequest);
187     }
188 
189     public static String removeDomain(String url) {
190         return getHttp().removeDomain(url);
191     }
192 
193     public static String removeParameter(String url, String name) {
194         return getHttp().removeParameter(url, name);
195     }
196 
197     public static String removeProtocol(String url) {
198         return getHttp().removeProtocol(url);
199     }
200 
201     public static String setParameter(String url, String name, boolean value) {
202         return getHttp().setParameter(url, name, value);
203     }
204 
205     public static String setParameter(String url, String name, double value) {
206         return getHttp().setParameter(url, name, value);
207     }
208 
209     public static String setParameter(String url, String name, int value) {
210         return getHttp().setParameter(url, name, value);
211     }
212 
213     public static String setParameter(String url, String name, long value) {
214         return getHttp().setParameter(url, name, value);
215     }
216 
217     public static String setParameter(String url, String name, short value) {
218         return getHttp().setParameter(url, name, value);
219     }
220 
221     public static String setParameter(String url, String name, String value) {
222         return getHttp().setParameter(url, name, value);
223     }
224 
225     public static void submit(String location) throws IOException {
226         getHttp().submit(location);
227     }
228 
229     public static void submit(String location, Cookie[] cookies)
230         throws IOException {
231 
232         getHttp().submit(location, cookies);
233     }
234 
235     public static void submit(String location, boolean post)
236         throws IOException {
237 
238         getHttp().submit(location, post);
239     }
240 
241     public static void submit(String location, Cookie[] cookies, boolean post)
242         throws IOException {
243 
244         getHttp().submit(location, cookies, post);
245     }
246 
247     public static void submit(
248             String location, Cookie[] cookies, Http.Body body, boolean post)
249         throws IOException {
250 
251         getHttp().submit(location, cookies, body, post);
252     }
253 
254     public static void submit(
255             String location, Cookie[] cookies, Map<String, String> parts,
256             boolean post)
257         throws IOException {
258 
259         getHttp().submit(location, cookies, parts, post);
260     }
261 
262     public static byte[] URLtoByteArray(String location) throws IOException {
263         return getHttp().URLtoByteArray(location);
264     }
265 
266     public static byte[] URLtoByteArray(String location, Cookie[] cookies)
267         throws IOException {
268 
269         return getHttp().URLtoByteArray(location, cookies);
270     }
271 
272     public static byte[] URLtoByteArray(String location, boolean post)
273         throws IOException {
274 
275         return getHttp().URLtoByteArray(location, post);
276     }
277 
278     public static byte[] URLtoByteArray(
279             String location, Cookie[] cookies, boolean post)
280         throws IOException {
281 
282         return getHttp().URLtoByteArray(location, cookies, post);
283     }
284 
285     public static byte[] URLtoByteArray(
286             String location, Cookie[] cookies, Http.Body body, boolean post)
287         throws IOException {
288 
289         return getHttp().URLtoByteArray(location, cookies, body, post);
290     }
291 
292     public static byte[] URLtoByteArray(
293             String location, Cookie[] cookies, Map<String, String> parts,
294             boolean post)
295         throws IOException {
296 
297         return getHttp().URLtoByteArray(location, cookies, parts, post);
298     }
299 
300     public static String URLtoString(String location) throws IOException {
301         return getHttp().URLtoString(location);
302     }
303 
304     public static String URLtoString(String location, Cookie[] cookies)
305         throws IOException {
306 
307          return getHttp().URLtoString(location, cookies);
308     }
309 
310     public static String URLtoString(String location, boolean post)
311         throws IOException {
312 
313         return getHttp().URLtoString(location, post);
314     }
315 
316     public static String URLtoString(
317             String location, Cookie[] cookies, boolean post)
318         throws IOException {
319 
320         return getHttp().URLtoString(location, cookies, post);
321     }
322 
323     public static String URLtoString(
324             String location, Cookie[] cookies, Http.Body body, boolean post)
325         throws IOException {
326 
327         return getHttp().URLtoString(location, cookies, body, post);
328     }
329 
330     public static String URLtoString(
331             String location, Cookie[] cookies, Map<String, String> parts,
332             boolean post)
333         throws IOException {
334 
335         return getHttp().URLtoString(location, cookies, parts, post);
336     }
337 
338     public static String URLtoString(
339             String location, String host, int port, String realm,
340             String username, String password)
341         throws IOException {
342 
343         return getHttp().URLtoString(
344             location, host, port, realm, username, password);
345     }
346 
347     /**
348      * This method only uses the default Commons HttpClient implementation when
349      * the URL object represents a HTTP resource. The URL object could also
350      * represent a file or some JNDI resource. In that case, the default Java
351      * implementation is used.
352      *
353      * @param       url URL object
354      * @return      A string representation of the resource referenced by the
355      *              URL object
356      * @throws      IOException
357      */
358     public static String URLtoString(URL url) throws IOException {
359         return getHttp().URLtoString(url);
360     }
361 
362     public void setHttp(Http http) {
363         _http = http;
364     }
365 
366     private static Http _http;
367 
368 }