1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.util;
16  
17  import java.io.IOException;
18  
19  import java.net.URL;
20  
21  import java.util.Map;
22  
23  import javax.portlet.ActionRequest;
24  import javax.portlet.RenderRequest;
25  
26  import javax.servlet.http.Cookie;
27  import javax.servlet.http.HttpServletRequest;
28  
29  /**
30   * <a href="HttpUtil.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Brian Wing Shun Chan
33   */
34  public class HttpUtil {
35  
36      public static String addParameter(String url, String name, boolean value) {
37          return getHttp().addParameter(url, name, value);
38      }
39  
40      public static String addParameter(String url, String name, double value) {
41          return getHttp().addParameter(url, name, value);
42      }
43  
44      public static String addParameter(String url, String name, int value) {
45          return getHttp().addParameter(url, name, value);
46      }
47  
48      public static String addParameter(String url, String name, long value) {
49          return getHttp().addParameter(url, name, value);
50      }
51  
52      public static String addParameter(String url, String name, short value) {
53          return getHttp().addParameter(url, name, value);
54      }
55  
56      public static String addParameter(String url, String name, String value) {
57          return getHttp().addParameter(url, name, value);
58      }
59  
60      public static String decodeURL(String url) {
61          return getHttp().decodeURL(url);
62      }
63  
64      public static String decodeURL(String url, boolean unescapeSpace) {
65          return getHttp().decodeURL(url, unescapeSpace);
66      }
67  
68      public static String encodeURL(String url) {
69          return getHttp().encodeURL(url);
70      }
71  
72      public static String encodeURL(String url, boolean escapeSpaces) {
73          return getHttp().encodeURL(url, escapeSpaces);
74      }
75  
76      public static String getCompleteURL(HttpServletRequest request) {
77          return getHttp().getCompleteURL(request);
78      }
79  
80      public static Cookie[] getCookies() {
81          return getHttp().getCookies();
82      }
83  
84      public static String getDomain(String url) {
85          return getHttp().getDomain(url);
86      }
87  
88      public static String getIpAddress(String url) {
89          return getHttp().getIpAddress(url);
90      }
91  
92      public static Http getHttp() {
93          return _http;
94      }
95  
96      public static String getParameter(String url, String name) {
97          return getHttp().getParameter(url, name);
98      }
99  
100     public static String getParameter(
101         String url, String name, boolean escaped) {
102 
103         return getHttp().getParameter(url, name, escaped);
104     }
105 
106     public static Map<String, String[]> getParameterMap(String queryString) {
107         return getHttp().getParameterMap(queryString);
108     }
109 
110     public static String getProtocol(ActionRequest actionRequest) {
111         return getHttp().getProtocol(actionRequest);
112     }
113 
114     public static String getProtocol(boolean secure) {
115         return getHttp().getProtocol(secure);
116     }
117 
118     public static String getProtocol(HttpServletRequest request) {
119         return getHttp().getProtocol(request);
120     }
121 
122     public static String getProtocol(RenderRequest renderRequest) {
123         return getHttp().getProtocol(renderRequest);
124     }
125 
126     public static String getProtocol(String url) {
127         return getHttp().getProtocol(url);
128     }
129 
130     public static String getQueryString(String url) {
131         return getHttp().getQueryString(url);
132     }
133 
134     public static String getRequestURL(HttpServletRequest request) {
135         return getHttp().getRequestURL(request);
136     }
137 
138     public static boolean hasDomain(String url) {
139         return getHttp().hasDomain(url);
140     }
141 
142     public static boolean hasProtocol(String url) {
143         return getHttp().hasProtocol(url);
144     }
145 
146     public static boolean hasProxyConfig() {
147         return getHttp().hasProxyConfig();
148     }
149 
150     public static boolean isNonProxyHost(String host) {
151         return getHttp().isNonProxyHost(host);
152     }
153 
154     public static boolean isProxyHost(String host) {
155         return getHttp().isProxyHost(host);
156     }
157 
158     public static Map<String, String[]> parameterMapFromString(
159         String queryString) {
160 
161         return getHttp().parameterMapFromString(queryString);
162     }
163 
164     public static String parameterMapToString(
165         Map<String, String[]> parameterMap) {
166 
167         return getHttp().parameterMapToString(parameterMap);
168     }
169 
170     public static String parameterMapToString(
171         Map<String, String[]> parameterMap, boolean addQuestion) {
172 
173         return getHttp().parameterMapToString(parameterMap, addQuestion);
174     }
175 
176     public static String protocolize(String url, ActionRequest actionRequest) {
177         return getHttp().protocolize(url, actionRequest);
178     }
179 
180     public static String protocolize(String url, boolean secure) {
181         return getHttp().protocolize(url, secure);
182     }
183 
184     public static String protocolize(String url, HttpServletRequest request) {
185         return getHttp().protocolize(url, request);
186     }
187 
188     public static String protocolize(String url, RenderRequest renderRequest) {
189         return getHttp().protocolize(url, renderRequest);
190     }
191 
192     public static String removeDomain(String url) {
193         return getHttp().removeDomain(url);
194     }
195 
196     public static String removeParameter(String url, String name) {
197         return getHttp().removeParameter(url, name);
198     }
199 
200     public static String removeProtocol(String url) {
201         return getHttp().removeProtocol(url);
202     }
203 
204     public static String setParameter(String url, String name, boolean value) {
205         return getHttp().setParameter(url, name, value);
206     }
207 
208     public static String setParameter(String url, String name, double value) {
209         return getHttp().setParameter(url, name, value);
210     }
211 
212     public static String setParameter(String url, String name, int value) {
213         return getHttp().setParameter(url, name, value);
214     }
215 
216     public static String setParameter(String url, String name, long value) {
217         return getHttp().setParameter(url, name, value);
218     }
219 
220     public static String setParameter(String url, String name, short value) {
221         return getHttp().setParameter(url, name, value);
222     }
223 
224     public static String setParameter(String url, String name, String value) {
225         return getHttp().setParameter(url, name, value);
226     }
227 
228     public static byte[] URLtoByteArray(Http.Options options)
229         throws IOException {
230 
231         return getHttp().URLtoByteArray(options);
232     }
233 
234     public static byte[] URLtoByteArray(String location) throws IOException {
235         return getHttp().URLtoByteArray(location);
236     }
237 
238     public static byte[] URLtoByteArray(String location, boolean post)
239         throws IOException {
240 
241         return getHttp().URLtoByteArray(location, post);
242     }
243 
244     public static String URLtoString(Http.Options options) throws IOException {
245         return getHttp().URLtoString(options);
246     }
247 
248     public static String URLtoString(String location) throws IOException {
249         return getHttp().URLtoString(location);
250     }
251 
252     public static String URLtoString(String location, boolean post)
253         throws IOException {
254 
255         return getHttp().URLtoString(location, post);
256     }
257 
258     /**
259      * This method only uses the default Commons HttpClient implementation when
260      * the URL object represents a HTTP resource. The URL object could also
261      * represent a file or some JNDI resource. In that case, the default Java
262      * implementation is used.
263      *
264      * @return A string representation of the resource referenced by the URL
265      *         object
266      */
267     public static String URLtoString(URL url) throws IOException {
268         return getHttp().URLtoString(url);
269     }
270 
271     public void setHttp(Http http) {
272         _http = http;
273     }
274 
275     private static Http _http;
276 
277 }