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