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