1   /**
2    * Copyright (c) 2000-2008 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 com.liferay.portal.kernel.bean.BeanLocatorUtil;
26  
27  import java.io.IOException;
28  
29  import java.net.URL;
30  
31  import java.util.Map;
32  
33  import javax.portlet.ActionRequest;
34  import javax.portlet.RenderRequest;
35  
36  import javax.servlet.http.Cookie;
37  import javax.servlet.http.HttpServletRequest;
38  
39  /**
40   * <a href="HttpUtil.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   *
44   */
45  public class HttpUtil {
46  
47      public static String addParameter(String url, String name, boolean value) {
48          return getHttp().addParameter(url, name, value);
49      }
50  
51      public static String addParameter(String url, String name, double value) {
52          return getHttp().addParameter(url, name, value);
53      }
54  
55      public static String addParameter(String url, String name, int value) {
56          return getHttp().addParameter(url, name, value);
57      }
58  
59      public static String addParameter(String url, String name, long value) {
60          return getHttp().addParameter(url, name, value);
61      }
62  
63      public static String addParameter(String url, String name, short value) {
64          return getHttp().addParameter(url, name, value);
65      }
66  
67      public static String addParameter(String url, String name, String value) {
68          return getHttp().addParameter(url, name, value);
69      }
70  
71      public static String decodeURL(String url) {
72          return getHttp().decodeURL(url);
73      }
74  
75      public static String decodeURL(String url, boolean unescapeSpace) {
76          return getHttp().decodeURL(url, unescapeSpace);
77      }
78  
79      public static String encodeURL(String url) {
80          return getHttp().encodeURL(url);
81      }
82  
83      public static String encodeURL(String url, boolean escapeSpaces) {
84          return getHttp().encodeURL(url, escapeSpaces);
85      }
86  
87      public static String getCompleteURL(HttpServletRequest req) {
88          return getHttp().getCompleteURL(req);
89      }
90  
91      public static String getDomain(String url) {
92          return getHttp().getDomain(url);
93      }
94  
95      public static Http getHttp() {
96          return _getUtil()._http;
97      }
98  
99      public static String getParameter(String url, String name) {
100         return getHttp().getParameter(url, name);
101     }
102 
103     public static String getParameter(
104         String url, String name, boolean escaped) {
105 
106         return getHttp().getParameter(url, name, escaped);
107     }
108 
109     public static Map<String, String[]> getParameterMap(String queryString) {
110         return getHttp().getParameterMap(queryString);
111     }
112 
113     public static String getProtocol(boolean secure) {
114         return getHttp().getProtocol(secure);
115     }
116 
117     public static String getProtocol(String url) {
118         return getHttp().getProtocol(url);
119     }
120 
121     public static String getProtocol(HttpServletRequest req) {
122         return getHttp().getProtocol(req);
123     }
124 
125     public static String getProtocol(ActionRequest req) {
126         return getHttp().getProtocol(req);
127     }
128 
129     public static String getProtocol(RenderRequest req) {
130         return getHttp().getProtocol(req);
131     }
132 
133     public static String getQueryString(String url) {
134         return getHttp().getQueryString(url);
135     }
136 
137     public static String getRequestURL(HttpServletRequest req) {
138         return getHttp().getRequestURL(req);
139     }
140 
141     public static boolean hasProxyConfig() {
142         return getHttp().hasProxyConfig();
143     }
144 
145     public static boolean isNonProxyHost(String host) {
146         return getHttp().isNonProxyHost(host);
147     }
148 
149     public static boolean isProxyHost(String host) {
150         return getHttp().isProxyHost(host);
151     }
152 
153     public static Map<String, String[]> parameterMapFromString(
154         String queryString) {
155 
156         return getHttp().parameterMapFromString(queryString);
157     }
158 
159     public static String parameterMapToString(
160         Map<String, String[]> parameterMap) {
161 
162         return getHttp().parameterMapToString(parameterMap);
163     }
164 
165     public static String parameterMapToString(
166         Map<String, String[]> parameterMap, boolean addQuestion) {
167 
168         return getHttp().parameterMapToString(parameterMap, addQuestion);
169     }
170 
171     public static String protocolize(String url, boolean secure) {
172         return getHttp().protocolize(url, secure);
173     }
174 
175     public static String protocolize(String url, HttpServletRequest req) {
176         return getHttp().protocolize(url, req);
177     }
178 
179     public static String protocolize(String url, ActionRequest req) {
180         return getHttp().protocolize(url, req);
181     }
182 
183     public static String protocolize(String url, RenderRequest req) {
184         return getHttp().protocolize(url, req);
185     }
186 
187     public static String removeParameter(String url, String name) {
188         return getHttp().removeParameter(url, name);
189     }
190 
191     public static String removeProtocol(String url) {
192         return getHttp().removeProtocol(url);
193     }
194 
195     public static void submit(String location) throws IOException {
196         getHttp().submit(location);
197     }
198 
199     public static void submit(String location, Cookie[] cookies)
200         throws IOException {
201 
202         getHttp().submit(location, cookies);
203     }
204 
205     public static void submit(String location, boolean post)
206         throws IOException {
207 
208         getHttp().submit(location, post);
209     }
210 
211     public static void submit(String location, Cookie[] cookies, boolean post)
212         throws IOException {
213 
214         getHttp().submit(location, cookies, post);
215     }
216 
217     public static void submit(
218             String location, Cookie[] cookies, Map<String, String> parts,
219             boolean post)
220         throws IOException {
221 
222         getHttp().submit(location, cookies, parts, post);
223     }
224 
225     public static byte[] URLtoByteArray(String location) throws IOException {
226         return getHttp().URLtoByteArray(location);
227     }
228 
229     public static byte[] URLtoByteArray(String location, Cookie[] cookies)
230         throws IOException {
231 
232         return getHttp().URLtoByteArray(location, cookies);
233     }
234 
235     public static byte[] URLtoByteArray(String location, boolean post)
236         throws IOException {
237 
238         return getHttp().URLtoByteArray(location, post);
239     }
240 
241     public static byte[] URLtoByteArray(
242             String location, Cookie[] cookies, boolean post)
243         throws IOException {
244 
245         return getHttp().URLtoByteArray(location, cookies, post);
246     }
247 
248     public static byte[] URLtoByteArray(
249             String location, Cookie[] cookies, Map<String, String> parts,
250             boolean post)
251         throws IOException {
252 
253         return getHttp().URLtoByteArray(location, cookies, parts, post);
254     }
255 
256     public static String URLtoString(String location) throws IOException {
257         return getHttp().URLtoString(location);
258     }
259 
260     public static String URLtoString(String location, Cookie[] cookies)
261         throws IOException {
262 
263          return getHttp().URLtoString(location, cookies);
264     }
265 
266     public static String URLtoString(String location, boolean post)
267         throws IOException {
268 
269         return getHttp().URLtoString(location, post);
270     }
271 
272     public static String URLtoString(
273             String location, Cookie[] cookies, boolean post)
274         throws IOException {
275 
276         return getHttp().URLtoString(location, cookies, post);
277     }
278 
279     public static String URLtoString(
280             String location, Cookie[] cookies, Map<String, String> parts,
281             boolean post)
282         throws IOException {
283 
284         return getHttp().URLtoString(location, cookies, parts, post);
285     }
286 
287     public static String URLtoString(
288             String location, String host, int port, String realm,
289             String username, String password)
290         throws IOException {
291 
292         return getHttp().URLtoString(
293             location, host, port, realm, username, password);
294     }
295 
296     /**
297      * This method only uses the default Commons HttpClient implementation when
298      * the URL object represents a HTTP resource. The URL object could also
299      * represent a file or some JNDI resource. In that case, the default Java
300      * implementation is used.
301      *
302      * @param       url URL object
303      * @return      A string representation of the resource referenced by the
304      *              URL object
305      * @throws      IOException
306      */
307     public static String URLtoString(URL url) throws IOException {
308         return getHttp().URLtoString(url);
309     }
310 
311     public void setHttp(Http http) {
312         _http = http;
313     }
314 
315     private static HttpUtil _getUtil() {
316         if (_util == null) {
317             _util = (HttpUtil)BeanLocatorUtil.locate(_UTIL);
318         }
319 
320         return _util;
321     }
322 
323     private static final String _UTIL = HttpUtil.class.getName();
324 
325     private static HttpUtil _util;
326 
327     private Http _http;
328 
329 }