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 Cookie[] getCookies() {
90          return getHttp().getCookies();
91      }
92  
93      public static String getDomain(String url) {
94          return getHttp().getDomain(url);
95      }
96  
97      public static Http getHttp() {
98          return _http;
99      }
100 
101     public static String getParameter(String url, String name) {
102         return getHttp().getParameter(url, name);
103     }
104 
105     public static String getParameter(
106         String url, String name, boolean escaped) {
107 
108         return getHttp().getParameter(url, name, escaped);
109     }
110 
111     public static Map<String, String[]> getParameterMap(String queryString) {
112         return getHttp().getParameterMap(queryString);
113     }
114 
115     public static String getProtocol(ActionRequest actionRequest) {
116         return getHttp().getProtocol(actionRequest);
117     }
118 
119     public static String getProtocol(boolean secure) {
120         return getHttp().getProtocol(secure);
121     }
122 
123     public static String getProtocol(HttpServletRequest request) {
124         return getHttp().getProtocol(request);
125     }
126 
127     public static String getProtocol(RenderRequest renderRequest) {
128         return getHttp().getProtocol(renderRequest);
129     }
130 
131     public static String getProtocol(String url) {
132         return getHttp().getProtocol(url);
133     }
134 
135     public static String getQueryString(String url) {
136         return getHttp().getQueryString(url);
137     }
138 
139     public static String getRequestURL(HttpServletRequest request) {
140         return getHttp().getRequestURL(request);
141     }
142 
143     public static boolean hasDomain(String url) {
144         return getHttp().hasDomain(url);
145     }
146 
147     public static boolean hasProtocol(String url) {
148         return getHttp().hasProtocol(url);
149     }
150 
151     public static boolean hasProxyConfig() {
152         return getHttp().hasProxyConfig();
153     }
154 
155     public static boolean isNonProxyHost(String host) {
156         return getHttp().isNonProxyHost(host);
157     }
158 
159     public static boolean isProxyHost(String host) {
160         return getHttp().isProxyHost(host);
161     }
162 
163     public static Map<String, String[]> parameterMapFromString(
164         String queryString) {
165 
166         return getHttp().parameterMapFromString(queryString);
167     }
168 
169     public static String parameterMapToString(
170         Map<String, String[]> parameterMap) {
171 
172         return getHttp().parameterMapToString(parameterMap);
173     }
174 
175     public static String parameterMapToString(
176         Map<String, String[]> parameterMap, boolean addQuestion) {
177 
178         return getHttp().parameterMapToString(parameterMap, addQuestion);
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, boolean secure) {
186         return getHttp().protocolize(url, secure);
187     }
188 
189     public static String protocolize(String url, HttpServletRequest request) {
190         return getHttp().protocolize(url, request);
191     }
192 
193     public static String protocolize(String url, RenderRequest renderRequest) {
194         return getHttp().protocolize(url, renderRequest);
195     }
196 
197     public static String removeDomain(String url) {
198         return getHttp().removeDomain(url);
199     }
200 
201     public static String removeParameter(String url, String name) {
202         return getHttp().removeParameter(url, name);
203     }
204 
205     public static String removeProtocol(String url) {
206         return getHttp().removeProtocol(url);
207     }
208 
209     public static String setParameter(String url, String name, boolean value) {
210         return getHttp().setParameter(url, name, value);
211     }
212 
213     public static String setParameter(String url, String name, double value) {
214         return getHttp().setParameter(url, name, value);
215     }
216 
217     public static String setParameter(String url, String name, int value) {
218         return getHttp().setParameter(url, name, value);
219     }
220 
221     public static String setParameter(String url, String name, long value) {
222         return getHttp().setParameter(url, name, value);
223     }
224 
225     public static String setParameter(String url, String name, short value) {
226         return getHttp().setParameter(url, name, value);
227     }
228 
229     public static String setParameter(String url, String name, String value) {
230         return getHttp().setParameter(url, name, value);
231     }
232 
233     /**
234      * @deprecated
235      */
236     public static void submit(String location) throws IOException {
237         getHttp().submit(location);
238     }
239 
240     /**
241      * @deprecated
242      */
243     public static void submit(String location, boolean post)
244         throws IOException {
245 
246         getHttp().submit(location, post);
247     }
248 
249     /**
250      * @deprecated
251      */
252     public static void submit(String location, Cookie[] cookies)
253         throws IOException {
254 
255         getHttp().submit(location, cookies);
256     }
257 
258     /**
259      * @deprecated
260      */
261     public static void submit(String location, Cookie[] cookies, boolean post)
262         throws IOException {
263 
264         getHttp().submit(location, cookies, post);
265     }
266 
267     /**
268      * @deprecated
269      */
270     public static void submit(
271             String location, Cookie[] cookies, Http.Body body, boolean post)
272         throws IOException {
273 
274         getHttp().submit(location, cookies, body, post);
275     }
276 
277     /**
278      * @deprecated
279      */
280     public static void submit(
281             String location, Cookie[] cookies, Map<String, String> parts,
282             boolean post)
283         throws IOException {
284 
285         getHttp().submit(location, cookies, parts, post);
286     }
287 
288     public static byte[] URLtoByteArray(Http.Options options)
289         throws IOException {
290 
291         return getHttp().URLtoByteArray(options);
292     }
293 
294     public static byte[] URLtoByteArray(String location) throws IOException {
295         return getHttp().URLtoByteArray(location);
296     }
297 
298     public static byte[] URLtoByteArray(String location, boolean post)
299         throws IOException {
300 
301         return getHttp().URLtoByteArray(location, post);
302     }
303 
304     /**
305      * @deprecated
306      */
307     public static byte[] URLtoByteArray(String location, Cookie[] cookies)
308         throws IOException {
309 
310         return getHttp().URLtoByteArray(location, cookies);
311     }
312 
313     /**
314      * @deprecated
315      */
316     public static byte[] URLtoByteArray(
317             String location, Cookie[] cookies, boolean post)
318         throws IOException {
319 
320         return getHttp().URLtoByteArray(location, cookies, post);
321     }
322 
323     /**
324      * @deprecated
325      */
326     public static byte[] URLtoByteArray(
327             String location, Cookie[] cookies, Http.Auth auth, Http.Body body,
328             boolean post)
329         throws IOException {
330 
331         return getHttp().URLtoByteArray(location, cookies, auth, body, post);
332     }
333 
334     /**
335      * @deprecated
336      */
337     public static byte[] URLtoByteArray(
338             String location, Cookie[] cookies, Http.Auth auth,
339             Map<String, String> parts, boolean post)
340         throws IOException {
341 
342         return getHttp().URLtoByteArray(location, cookies, auth, parts, post);
343     }
344 
345     /**
346      * @deprecated
347      */
348     public static byte[] URLtoByteArray(
349             String location, Cookie[] cookies, Http.Body body, boolean post)
350         throws IOException {
351 
352         return getHttp().URLtoByteArray(location, cookies, body, post);
353     }
354 
355     /**
356      * @deprecated
357      */
358     public static byte[] URLtoByteArray(
359             String location, Cookie[] cookies, Map<String, String> parts,
360             boolean post)
361         throws IOException {
362 
363         return getHttp().URLtoByteArray(location, cookies, parts, post);
364     }
365 
366     public static String URLtoString(Http.Options options) throws IOException {
367         return getHttp().URLtoString(options);
368     }
369 
370     public static String URLtoString(String location) throws IOException {
371         return getHttp().URLtoString(location);
372     }
373 
374     public static String URLtoString(String location, boolean post)
375         throws IOException {
376 
377         return getHttp().URLtoString(location, post);
378     }
379 
380     /**
381      * @deprecated
382      */
383     public static String URLtoString(String location, Cookie[] cookies)
384         throws IOException {
385 
386         return getHttp().URLtoString(location, cookies);
387     }
388 
389     /**
390      * @deprecated
391      */
392     public static String URLtoString(
393             String location, Cookie[] cookies, boolean post)
394         throws IOException {
395 
396         return getHttp().URLtoString(location, cookies, post);
397     }
398 
399     /**
400      * @deprecated
401      */
402     public static String URLtoString(
403             String location, Cookie[] cookies, Http.Auth auth, Http.Body body,
404             boolean post)
405         throws IOException {
406 
407         return getHttp().URLtoString(location, cookies, auth, body, post);
408     }
409 
410     /**
411      * @deprecated
412      */
413     public static String URLtoString(
414             String location, Cookie[] cookies, Http.Auth auth,
415             Map<String, String> parts, boolean post)
416         throws IOException {
417 
418         return getHttp().URLtoString(location, cookies, auth, parts, post);
419     }
420 
421     /**
422      * @deprecated
423      */
424     public static String URLtoString(
425             String location, Cookie[] cookies, Http.Body body, boolean post)
426         throws IOException {
427 
428         return getHttp().URLtoString(location, cookies, body, post);
429     }
430 
431     /**
432      * @deprecated
433      */
434     public static String URLtoString(
435             String location, Cookie[] cookies, Map<String, String> parts,
436             boolean post)
437         throws IOException {
438 
439         return getHttp().URLtoString(location, cookies, parts, post);
440     }
441 
442     /**
443      * @deprecated
444      */
445     public static String URLtoString(
446             String location, String host, int port, String realm,
447             String username, String password)
448         throws IOException {
449 
450         return getHttp().URLtoString(
451             location, host, port, realm, username, password);
452     }
453 
454     /**
455      * This method only uses the default Commons HttpClient implementation when
456      * the URL object represents a HTTP resource. The URL object could also
457      * represent a file or some JNDI resource. In that case, the default Java
458      * implementation is used.
459      *
460      * @param       url URL object
461      * @return      A string representation of the resource referenced by the
462      *              URL object
463      * @throws      IOException
464      */
465     public static String URLtoString(URL url) throws IOException {
466         return getHttp().URLtoString(url);
467     }
468 
469     public void setHttp(Http http) {
470         _http = http;
471     }
472 
473     private static Http _http;
474 
475 }