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