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