001    /**
002     * Copyright (c) 2000-2010 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 unescapeSpace) {
067                    return getHttp().decodeURL(url, unescapeSpace);
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 String getIpAddress(String url) {
105                    return getHttp().getIpAddress(url);
106            }
107    
108            public static Http getHttp() {
109                    return _http;
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 getProtocol(ActionRequest actionRequest) {
127                    return getHttp().getProtocol(actionRequest);
128            }
129    
130            public static String getProtocol(boolean secure) {
131                    return getHttp().getProtocol(secure);
132            }
133    
134            public static String getProtocol(HttpServletRequest request) {
135                    return getHttp().getProtocol(request);
136            }
137    
138            public static String getProtocol(RenderRequest renderRequest) {
139                    return getHttp().getProtocol(renderRequest);
140            }
141    
142            public static String getProtocol(String url) {
143                    return getHttp().getProtocol(url);
144            }
145    
146            public static String getQueryString(String url) {
147                    return getHttp().getQueryString(url);
148            }
149    
150            public static String getRequestURL(HttpServletRequest request) {
151                    return getHttp().getRequestURL(request);
152            }
153    
154            public static boolean hasDomain(String url) {
155                    return getHttp().hasDomain(url);
156            }
157    
158            public static boolean hasProtocol(String url) {
159                    return getHttp().hasProtocol(url);
160            }
161    
162            public static boolean hasProxyConfig() {
163                    return getHttp().hasProxyConfig();
164            }
165    
166            public static boolean isNonProxyHost(String host) {
167                    return getHttp().isNonProxyHost(host);
168            }
169    
170            public static boolean isProxyHost(String host) {
171                    return getHttp().isProxyHost(host);
172            }
173    
174            public static Map<String, String[]> parameterMapFromString(
175                    String queryString) {
176    
177                    return getHttp().parameterMapFromString(queryString);
178            }
179    
180            public static String parameterMapToString(
181                    Map<String, String[]> parameterMap) {
182    
183                    return getHttp().parameterMapToString(parameterMap);
184            }
185    
186            public static String parameterMapToString(
187                    Map<String, String[]> parameterMap, boolean addQuestion) {
188    
189                    return getHttp().parameterMapToString(parameterMap, addQuestion);
190            }
191    
192            public static String protocolize(String url, ActionRequest actionRequest) {
193                    return getHttp().protocolize(url, actionRequest);
194            }
195    
196            public static String protocolize(String url, boolean secure) {
197                    return getHttp().protocolize(url, secure);
198            }
199    
200            public static String protocolize(String url, HttpServletRequest request) {
201                    return getHttp().protocolize(url, request);
202            }
203    
204            public static String protocolize(String url, RenderRequest renderRequest) {
205                    return getHttp().protocolize(url, renderRequest);
206            }
207    
208            public static String removeDomain(String url) {
209                    return getHttp().removeDomain(url);
210            }
211    
212            public static String removeParameter(String url, String name) {
213                    return getHttp().removeParameter(url, name);
214            }
215    
216            public static String removeProtocol(String url) {
217                    return getHttp().removeProtocol(url);
218            }
219    
220            public static String setParameter(String url, String name, boolean value) {
221                    return getHttp().setParameter(url, name, value);
222            }
223    
224            public static String setParameter(String url, String name, double value) {
225                    return getHttp().setParameter(url, name, value);
226            }
227    
228            public static String setParameter(String url, String name, int value) {
229                    return getHttp().setParameter(url, name, value);
230            }
231    
232            public static String setParameter(String url, String name, long value) {
233                    return getHttp().setParameter(url, name, value);
234            }
235    
236            public static String setParameter(String url, String name, short value) {
237                    return getHttp().setParameter(url, name, value);
238            }
239    
240            public static String setParameter(String url, String name, String value) {
241                    return getHttp().setParameter(url, name, value);
242            }
243    
244            public static byte[] URLtoByteArray(Http.Options options)
245                    throws IOException {
246    
247                    return getHttp().URLtoByteArray(options);
248            }
249    
250            public static byte[] URLtoByteArray(String location) throws IOException {
251                    return getHttp().URLtoByteArray(location);
252            }
253    
254            public static byte[] URLtoByteArray(String location, boolean post)
255                    throws IOException {
256    
257                    return getHttp().URLtoByteArray(location, post);
258            }
259    
260            public static String URLtoString(Http.Options options) throws IOException {
261                    return getHttp().URLtoString(options);
262            }
263    
264            public static String URLtoString(String location) throws IOException {
265                    return getHttp().URLtoString(location);
266            }
267    
268            public static String URLtoString(String location, boolean post)
269                    throws IOException {
270    
271                    return getHttp().URLtoString(location, post);
272            }
273    
274            /**
275             * This method only uses the default Commons HttpClient implementation when
276             * the URL object represents a HTTP resource. The URL object could also
277             * represent a file or some JNDI resource. In that case, the default Java
278             * implementation is used.
279             *
280             * @return A string representation of the resource referenced by the URL
281             *                 object
282             */
283            public static String URLtoString(URL url) throws IOException {
284                    return getHttp().URLtoString(url);
285            }
286    
287            public void setHttp(Http http) {
288                    _http = http;
289            }
290    
291            private static Http _http;
292    
293    }