001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.io.IOException;
018    
019    import java.net.URL;
020    
021    import java.util.Map;
022    
023    import javax.portlet.ActionRequest;
024    import javax.portlet.RenderRequest;
025    
026    import javax.servlet.http.Cookie;
027    import javax.servlet.http.HttpServletRequest;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     */
032    public class HttpUtil {
033    
034            public static String addParameter(String url, String name, boolean value) {
035                    return getHttp().addParameter(url, name, value);
036            }
037    
038            public static String addParameter(String url, String name, double value) {
039                    return getHttp().addParameter(url, name, value);
040            }
041    
042            public static String addParameter(String url, String name, int value) {
043                    return getHttp().addParameter(url, name, value);
044            }
045    
046            public static String addParameter(String url, String name, long value) {
047                    return getHttp().addParameter(url, name, value);
048            }
049    
050            public static String addParameter(String url, String name, short value) {
051                    return getHttp().addParameter(url, name, value);
052            }
053    
054            public static String addParameter(String url, String name, String value) {
055                    return getHttp().addParameter(url, name, value);
056            }
057    
058            public static String decodePath(String path) {
059                    return getHttp().decodePath(path);
060            }
061    
062            public static String decodeURL(String url) {
063                    return getHttp().decodeURL(url);
064            }
065    
066            public static String decodeURL(String url, boolean unescapeSpaces) {
067                    return getHttp().decodeURL(url, unescapeSpaces);
068            }
069    
070            public static String encodePath(String path) {
071                    return getHttp().encodePath(path);
072            }
073    
074            public static String encodeURL(String url) {
075                    return getHttp().encodeURL(url);
076            }
077    
078            public static String encodeURL(String url, boolean escapeSpaces) {
079                    return getHttp().encodeURL(url, escapeSpaces);
080            }
081    
082            public static String fixPath(String path) {
083                    return getHttp().fixPath(path);
084            }
085    
086            public static String fixPath(
087                    String path, boolean leading, boolean trailing) {
088    
089                    return getHttp().fixPath(path, leading, trailing);
090            }
091    
092            public static String getCompleteURL(HttpServletRequest request) {
093                    return getHttp().getCompleteURL(request);
094            }
095    
096            public static Cookie[] getCookies() {
097                    return getHttp().getCookies();
098            }
099    
100            public static String getDomain(String url) {
101                    return getHttp().getDomain(url);
102            }
103    
104            public static Http getHttp() {
105                    return _http;
106            }
107    
108            public static String getIpAddress(String url) {
109                    return getHttp().getIpAddress(url);
110            }
111    
112            public static String getParameter(String url, String name) {
113                    return getHttp().getParameter(url, name);
114            }
115    
116            public static String getParameter(
117                    String url, String name, boolean escaped) {
118    
119                    return getHttp().getParameter(url, name, escaped);
120            }
121    
122            public static Map<String, String[]> getParameterMap(String queryString) {
123                    return getHttp().getParameterMap(queryString);
124            }
125    
126            public static String getPath(String url) {
127                    return getHttp().getPath(url);
128            }
129    
130            public static String getProtocol(ActionRequest actionRequest) {
131                    return getHttp().getProtocol(actionRequest);
132            }
133    
134            public static String getProtocol(boolean secure) {
135                    return getHttp().getProtocol(secure);
136            }
137    
138            public static String getProtocol(HttpServletRequest request) {
139                    return getHttp().getProtocol(request);
140            }
141    
142            public static String getProtocol(RenderRequest renderRequest) {
143                    return getHttp().getProtocol(renderRequest);
144            }
145    
146            public static String getProtocol(String url) {
147                    return getHttp().getProtocol(url);
148            }
149    
150            public static String getQueryString(String url) {
151                    return getHttp().getQueryString(url);
152            }
153    
154            public static String getRequestURL(HttpServletRequest request) {
155                    return getHttp().getRequestURL(request);
156            }
157    
158            public static boolean hasDomain(String url) {
159                    return getHttp().hasDomain(url);
160            }
161    
162            public static boolean hasProtocol(String url) {
163                    return getHttp().hasProtocol(url);
164            }
165    
166            public static boolean hasProxyConfig() {
167                    return getHttp().hasProxyConfig();
168            }
169    
170            public static boolean isNonProxyHost(String host) {
171                    return getHttp().isNonProxyHost(host);
172            }
173    
174            public static boolean isProxyHost(String host) {
175                    return getHttp().isProxyHost(host);
176            }
177    
178            public static Map<String, String[]> parameterMapFromString(
179                    String queryString) {
180    
181                    return getHttp().parameterMapFromString(queryString);
182            }
183    
184            public static String parameterMapToString(
185                    Map<String, String[]> parameterMap) {
186    
187                    return getHttp().parameterMapToString(parameterMap);
188            }
189    
190            public static String parameterMapToString(
191                    Map<String, String[]> parameterMap, boolean addQuestion) {
192    
193                    return getHttp().parameterMapToString(parameterMap, addQuestion);
194            }
195    
196            public static String protocolize(String url, ActionRequest actionRequest) {
197                    return getHttp().protocolize(url, actionRequest);
198            }
199    
200            public static String protocolize(String url, boolean secure) {
201                    return getHttp().protocolize(url, secure);
202            }
203    
204            public static String protocolize(String url, HttpServletRequest request) {
205                    return getHttp().protocolize(url, request);
206            }
207    
208            public static String protocolize(String url, RenderRequest renderRequest) {
209                    return getHttp().protocolize(url, renderRequest);
210            }
211    
212            public static String removeDomain(String url) {
213                    return getHttp().removeDomain(url);
214            }
215    
216            public static String removeParameter(String url, String name) {
217                    return getHttp().removeParameter(url, name);
218            }
219    
220            public static String removeProtocol(String url) {
221                    return getHttp().removeProtocol(url);
222            }
223    
224            public static String setParameter(String url, String name, boolean value) {
225                    return getHttp().setParameter(url, name, value);
226            }
227    
228            public static String setParameter(String url, String name, double value) {
229                    return getHttp().setParameter(url, name, value);
230            }
231    
232            public static String setParameter(String url, String name, int value) {
233                    return getHttp().setParameter(url, name, value);
234            }
235    
236            public static String setParameter(String url, String name, long value) {
237                    return getHttp().setParameter(url, name, value);
238            }
239    
240            public static String setParameter(String url, String name, short value) {
241                    return getHttp().setParameter(url, name, value);
242            }
243    
244            public static String setParameter(String url, String name, String value) {
245                    return getHttp().setParameter(url, name, value);
246            }
247    
248            public static byte[] URLtoByteArray(Http.Options options)
249                    throws IOException {
250    
251                    return getHttp().URLtoByteArray(options);
252            }
253    
254            public static byte[] URLtoByteArray(String location) throws IOException {
255                    return getHttp().URLtoByteArray(location);
256            }
257    
258            public static byte[] URLtoByteArray(String location, boolean post)
259                    throws IOException {
260    
261                    return getHttp().URLtoByteArray(location, post);
262            }
263    
264            public static String URLtoString(Http.Options options) throws IOException {
265                    return getHttp().URLtoString(options);
266            }
267    
268            public static String URLtoString(String location) throws IOException {
269                    return getHttp().URLtoString(location);
270            }
271    
272            public static String URLtoString(String location, boolean post)
273                    throws IOException {
274    
275                    return getHttp().URLtoString(location, post);
276            }
277    
278            /**
279             * This method only uses the default Commons HttpClient implementation when
280             * the URL object represents a HTTP resource. The URL object could also
281             * represent a file or some JNDI resource. In that case, the default Java
282             * implementation is used.
283             *
284             * @return A string representation of the resource referenced by the URL
285             *         object
286             */
287            public static String URLtoString(URL url) throws IOException {
288                    return getHttp().URLtoString(url);
289            }
290    
291            public void setHttp(Http http) {
292                    _http = http;
293            }
294    
295            private static Http _http;
296    
297    }