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 decodePath(String path) {
61          return getHttp().decodePath(path);
62      }
63  
64      public static String decodeURL(String url) {
65          return getHttp().decodeURL(url);
66      }
67  
68      public static String decodeURL(String url, boolean unescapeSpace) {
69          return getHttp().decodeURL(url, unescapeSpace);
70      }
71  
72      public static String encodePath(String path) {
73          return getHttp().encodePath(path);
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 fixPath(String path) {
85          return getHttp().fixPath(path);
86      }
87  
88      public static String fixPath(
89          String path, boolean leading, boolean trailing) {
90  
91          return getHttp().fixPath(path, leading, trailing);
92      }
93  
94      public static String getCompleteURL(HttpServletRequest request) {
95          return getHttp().getCompleteURL(request);
96      }
97  
98      public static Cookie[] getCookies() {
99          return getHttp().getCookies();
100     }
101 
102     public static String getDomain(String url) {
103         return getHttp().getDomain(url);
104     }
105 
106     public static String getIpAddress(String url) {
107         return getHttp().getIpAddress(url);
108     }
109 
110     public static Http getHttp() {
111         return _http;
112     }
113 
114     public static String getParameter(String url, String name) {
115         return getHttp().getParameter(url, name);
116     }
117 
118     public static String getParameter(
119         String url, String name, boolean escaped) {
120 
121         return getHttp().getParameter(url, name, escaped);
122     }
123 
124     public static Map<String, String[]> getParameterMap(String queryString) {
125         return getHttp().getParameterMap(queryString);
126     }
127 
128     public static String getProtocol(ActionRequest actionRequest) {
129         return getHttp().getProtocol(actionRequest);
130     }
131 
132     public static String getProtocol(boolean secure) {
133         return getHttp().getProtocol(secure);
134     }
135 
136     public static String getProtocol(HttpServletRequest request) {
137         return getHttp().getProtocol(request);
138     }
139 
140     public static String getProtocol(RenderRequest renderRequest) {
141         return getHttp().getProtocol(renderRequest);
142     }
143 
144     public static String getProtocol(String url) {
145         return getHttp().getProtocol(url);
146     }
147 
148     public static String getQueryString(String url) {
149         return getHttp().getQueryString(url);
150     }
151 
152     public static String getRequestURL(HttpServletRequest request) {
153         return getHttp().getRequestURL(request);
154     }
155 
156     public static boolean hasDomain(String url) {
157         return getHttp().hasDomain(url);
158     }
159 
160     public static boolean hasProtocol(String url) {
161         return getHttp().hasProtocol(url);
162     }
163 
164     public static boolean hasProxyConfig() {
165         return getHttp().hasProxyConfig();
166     }
167 
168     public static boolean isNonProxyHost(String host) {
169         return getHttp().isNonProxyHost(host);
170     }
171 
172     public static boolean isProxyHost(String host) {
173         return getHttp().isProxyHost(host);
174     }
175 
176     public static Map<String, String[]> parameterMapFromString(
177         String queryString) {
178 
179         return getHttp().parameterMapFromString(queryString);
180     }
181 
182     public static String parameterMapToString(
183         Map<String, String[]> parameterMap) {
184 
185         return getHttp().parameterMapToString(parameterMap);
186     }
187 
188     public static String parameterMapToString(
189         Map<String, String[]> parameterMap, boolean addQuestion) {
190 
191         return getHttp().parameterMapToString(parameterMap, addQuestion);
192     }
193 
194     public static String protocolize(String url, ActionRequest actionRequest) {
195         return getHttp().protocolize(url, actionRequest);
196     }
197 
198     public static String protocolize(String url, boolean secure) {
199         return getHttp().protocolize(url, secure);
200     }
201 
202     public static String protocolize(String url, HttpServletRequest request) {
203         return getHttp().protocolize(url, request);
204     }
205 
206     public static String protocolize(String url, RenderRequest renderRequest) {
207         return getHttp().protocolize(url, renderRequest);
208     }
209 
210     public static String removeDomain(String url) {
211         return getHttp().removeDomain(url);
212     }
213 
214     public static String removeParameter(String url, String name) {
215         return getHttp().removeParameter(url, name);
216     }
217 
218     public static String removeProtocol(String url) {
219         return getHttp().removeProtocol(url);
220     }
221 
222     public static String setParameter(String url, String name, boolean value) {
223         return getHttp().setParameter(url, name, value);
224     }
225 
226     public static String setParameter(String url, String name, double value) {
227         return getHttp().setParameter(url, name, value);
228     }
229 
230     public static String setParameter(String url, String name, int value) {
231         return getHttp().setParameter(url, name, value);
232     }
233 
234     public static String setParameter(String url, String name, long value) {
235         return getHttp().setParameter(url, name, value);
236     }
237 
238     public static String setParameter(String url, String name, short value) {
239         return getHttp().setParameter(url, name, value);
240     }
241 
242     public static String setParameter(String url, String name, String value) {
243         return getHttp().setParameter(url, name, value);
244     }
245 
246     public static byte[] URLtoByteArray(Http.Options options)
247         throws IOException {
248 
249         return getHttp().URLtoByteArray(options);
250     }
251 
252     public static byte[] URLtoByteArray(String location) throws IOException {
253         return getHttp().URLtoByteArray(location);
254     }
255 
256     public static byte[] URLtoByteArray(String location, boolean post)
257         throws IOException {
258 
259         return getHttp().URLtoByteArray(location, post);
260     }
261 
262     public static String URLtoString(Http.Options options) throws IOException {
263         return getHttp().URLtoString(options);
264     }
265 
266     public static String URLtoString(String location) throws IOException {
267         return getHttp().URLtoString(location);
268     }
269 
270     public static String URLtoString(String location, boolean post)
271         throws IOException {
272 
273         return getHttp().URLtoString(location, post);
274     }
275 
276     /**
277      * This method only uses the default Commons HttpClient implementation when
278      * the URL object represents a HTTP resource. The URL object could also
279      * represent a file or some JNDI resource. In that case, the default Java
280      * implementation is used.
281      *
282      * @return A string representation of the resource referenced by the URL
283      *         object
284      */
285     public static String URLtoString(URL url) throws IOException {
286         return getHttp().URLtoString(url);
287     }
288 
289     public void setHttp(Http http) {
290         _http = http;
291     }
292 
293     private static Http _http;
294 
295 }