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