001    /**
002     * Copyright (c) 2000-2013 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 com.liferay.portal.kernel.security.pacl.permission.PortalRuntimePermission;
018    import com.liferay.portal.kernel.security.pacl.permission.PortalSocketPermission;
019    
020    import java.io.IOException;
021    
022    import java.net.URL;
023    
024    import java.util.Map;
025    
026    import javax.portlet.ActionRequest;
027    import javax.portlet.RenderRequest;
028    
029    import javax.servlet.http.Cookie;
030    import javax.servlet.http.HttpServletRequest;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    public class HttpUtil {
036    
037            public static String addParameter(String url, String name, boolean value) {
038                    return getHttp().addParameter(url, name, value);
039            }
040    
041            public static String addParameter(String url, String name, double value) {
042                    return getHttp().addParameter(url, name, value);
043            }
044    
045            public static String addParameter(String url, String name, int value) {
046                    return getHttp().addParameter(url, name, value);
047            }
048    
049            public static String addParameter(String url, String name, long value) {
050                    return getHttp().addParameter(url, name, value);
051            }
052    
053            public static String addParameter(String url, String name, short value) {
054                    return getHttp().addParameter(url, name, value);
055            }
056    
057            public static String addParameter(String url, String name, String value) {
058                    return getHttp().addParameter(url, name, value);
059            }
060    
061            public static String decodePath(String path) {
062                    return getHttp().decodePath(path);
063            }
064    
065            public static String decodeURL(String url) {
066                    return getHttp().decodeURL(url);
067            }
068    
069            public static String decodeURL(String url, boolean unescapeSpaces) {
070                    return getHttp().decodeURL(url, unescapeSpaces);
071            }
072    
073            public static String encodeParameters(String url) {
074                    return getHttp().encodeParameters(url);
075            }
076    
077            public static String encodePath(String path) {
078                    return getHttp().encodePath(path);
079            }
080    
081            public static String encodeURL(String url) {
082                    return getHttp().encodeURL(url);
083            }
084    
085            public static String encodeURL(String url, boolean escapeSpaces) {
086                    return getHttp().encodeURL(url, escapeSpaces);
087            }
088    
089            public static String fixPath(String path) {
090                    return getHttp().fixPath(path);
091            }
092    
093            public static String fixPath(
094                    String path, boolean leading, boolean trailing) {
095    
096                    return getHttp().fixPath(path, leading, trailing);
097            }
098    
099            public static String getCompleteURL(HttpServletRequest request) {
100                    return getHttp().getCompleteURL(request);
101            }
102    
103            public static Cookie[] getCookies() {
104                    return getHttp().getCookies();
105            }
106    
107            public static String getDomain(String url) {
108                    return getHttp().getDomain(url);
109            }
110    
111            public static Http getHttp() {
112                    PortalRuntimePermission.checkGetBeanProperty(HttpUtil.class);
113    
114                    return _http;
115            }
116    
117            public static String getIpAddress(String url) {
118                    return getHttp().getIpAddress(url);
119            }
120    
121            public static String getParameter(String url, String name) {
122                    return getHttp().getParameter(url, name);
123            }
124    
125            public static String getParameter(
126                    String url, String name, boolean escaped) {
127    
128                    return getHttp().getParameter(url, name, escaped);
129            }
130    
131            public static Map<String, String[]> getParameterMap(String queryString) {
132                    return getHttp().getParameterMap(queryString);
133            }
134    
135            public static String getPath(String url) {
136                    return getHttp().getPath(url);
137            }
138    
139            public static String getProtocol(ActionRequest actionRequest) {
140                    return getHttp().getProtocol(actionRequest);
141            }
142    
143            public static String getProtocol(boolean secure) {
144                    return getHttp().getProtocol(secure);
145            }
146    
147            public static String getProtocol(HttpServletRequest request) {
148                    return getHttp().getProtocol(request);
149            }
150    
151            public static String getProtocol(RenderRequest renderRequest) {
152                    return getHttp().getProtocol(renderRequest);
153            }
154    
155            public static String getProtocol(String url) {
156                    return getHttp().getProtocol(url);
157            }
158    
159            public static String getQueryString(String url) {
160                    return getHttp().getQueryString(url);
161            }
162    
163            public static String getRequestURL(HttpServletRequest request) {
164                    return getHttp().getRequestURL(request);
165            }
166    
167            public static boolean hasDomain(String url) {
168                    return getHttp().hasDomain(url);
169            }
170    
171            public static boolean hasProtocol(String url) {
172                    return getHttp().hasProtocol(url);
173            }
174    
175            public static boolean hasProxyConfig() {
176                    return getHttp().hasProxyConfig();
177            }
178    
179            public static boolean isNonProxyHost(String host) {
180                    return getHttp().isNonProxyHost(host);
181            }
182    
183            public static boolean isProxyHost(String host) {
184                    return getHttp().isProxyHost(host);
185            }
186    
187            public static Map<String, String[]> parameterMapFromString(
188                    String queryString) {
189    
190                    return getHttp().parameterMapFromString(queryString);
191            }
192    
193            public static String parameterMapToString(
194                    Map<String, String[]> parameterMap) {
195    
196                    return getHttp().parameterMapToString(parameterMap);
197            }
198    
199            public static String parameterMapToString(
200                    Map<String, String[]> parameterMap, boolean addQuestion) {
201    
202                    return getHttp().parameterMapToString(parameterMap, addQuestion);
203            }
204    
205            public static String protocolize(String url, ActionRequest actionRequest) {
206                    return getHttp().protocolize(url, actionRequest);
207            }
208    
209            public static String protocolize(String url, boolean secure) {
210                    return getHttp().protocolize(url, secure);
211            }
212    
213            public static String protocolize(String url, HttpServletRequest request) {
214                    return getHttp().protocolize(url, request);
215            }
216    
217            public static String protocolize(String url, RenderRequest renderRequest) {
218                    return getHttp().protocolize(url, renderRequest);
219            }
220    
221            public static String removeDomain(String url) {
222                    return getHttp().removeDomain(url);
223            }
224    
225            public static String removeParameter(String url, String name) {
226                    return getHttp().removeParameter(url, name);
227            }
228    
229            public static String removeProtocol(String url) {
230                    return getHttp().removeProtocol(url);
231            }
232    
233            public static String setParameter(String url, String name, boolean value) {
234                    return getHttp().setParameter(url, name, value);
235            }
236    
237            public static String setParameter(String url, String name, double value) {
238                    return getHttp().setParameter(url, name, value);
239            }
240    
241            public static String setParameter(String url, String name, int value) {
242                    return getHttp().setParameter(url, name, value);
243            }
244    
245            public static String setParameter(String url, String name, long value) {
246                    return getHttp().setParameter(url, name, value);
247            }
248    
249            public static String setParameter(String url, String name, short value) {
250                    return getHttp().setParameter(url, name, value);
251            }
252    
253            public static String setParameter(String url, String name, String value) {
254                    return getHttp().setParameter(url, name, value);
255            }
256    
257            public static byte[] URLtoByteArray(Http.Options options)
258                    throws IOException {
259    
260                    PortalSocketPermission.checkConnect(options);
261    
262                    return getHttp().URLtoByteArray(options);
263            }
264    
265            public static byte[] URLtoByteArray(String location) throws IOException {
266                    PortalSocketPermission.checkConnect(location);
267    
268                    return getHttp().URLtoByteArray(location);
269            }
270    
271            public static byte[] URLtoByteArray(String location, boolean post)
272                    throws IOException {
273    
274                    PortalSocketPermission.checkConnect(location);
275    
276                    return getHttp().URLtoByteArray(location, post);
277            }
278    
279            public static String URLtoString(Http.Options options) throws IOException {
280                    PortalSocketPermission.checkConnect(options);
281    
282                    return getHttp().URLtoString(options);
283            }
284    
285            public static String URLtoString(String location) throws IOException {
286                    PortalSocketPermission.checkConnect(location);
287    
288                    return getHttp().URLtoString(location);
289            }
290    
291            public static String URLtoString(String location, boolean post)
292                    throws IOException {
293    
294                    PortalSocketPermission.checkConnect(location);
295    
296                    return getHttp().URLtoString(location, post);
297            }
298    
299            /**
300             * This method only uses the default Commons HttpClient implementation when
301             * the URL object represents a HTTP resource. The URL object could also
302             * represent a file or some JNDI resource. In that case, the default Java
303             * implementation is used.
304             *
305             * @return A string representation of the resource referenced by the URL
306             *         object
307             */
308            public static String URLtoString(URL url) throws IOException {
309                    PortalSocketPermission.checkConnect(url);
310    
311                    return getHttp().URLtoString(url);
312            }
313    
314            public void setHttp(Http http) {
315                    PortalRuntimePermission.checkSetBeanProperty(getClass());
316    
317                    _http = http;
318            }
319    
320            private static Http _http;
321    
322    }