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 boolean isSecure(String url) {
188                    return getHttp().isSecure(url);
189            }
190    
191            public static Map<String, String[]> parameterMapFromString(
192                    String queryString) {
193    
194                    return getHttp().parameterMapFromString(queryString);
195            }
196    
197            public static String parameterMapToString(
198                    Map<String, String[]> parameterMap) {
199    
200                    return getHttp().parameterMapToString(parameterMap);
201            }
202    
203            public static String parameterMapToString(
204                    Map<String, String[]> parameterMap, boolean addQuestion) {
205    
206                    return getHttp().parameterMapToString(parameterMap, addQuestion);
207            }
208    
209            public static String protocolize(String url, ActionRequest actionRequest) {
210                    return getHttp().protocolize(url, actionRequest);
211            }
212    
213            public static String protocolize(String url, boolean secure) {
214                    return getHttp().protocolize(url, secure);
215            }
216    
217            public static String protocolize(String url, HttpServletRequest request) {
218                    return getHttp().protocolize(url, request);
219            }
220    
221            public static String protocolize(String url, int port, boolean secure) {
222                    return getHttp().protocolize(url, port, secure);
223            }
224    
225            public static String protocolize(String url, RenderRequest renderRequest) {
226                    return getHttp().protocolize(url, renderRequest);
227            }
228    
229            public static String removeDomain(String url) {
230                    return getHttp().removeDomain(url);
231            }
232    
233            public static String removeParameter(String url, String name) {
234                    return getHttp().removeParameter(url, name);
235            }
236    
237            public static String removeProtocol(String url) {
238                    return getHttp().removeProtocol(url);
239            }
240    
241            public static String sanitizeHeader(String header) {
242                    return getHttp().sanitizeHeader(header);
243            }
244    
245            public static String setParameter(String url, String name, boolean value) {
246                    return getHttp().setParameter(url, name, value);
247            }
248    
249            public static String setParameter(String url, String name, double value) {
250                    return getHttp().setParameter(url, name, value);
251            }
252    
253            public static String setParameter(String url, String name, int value) {
254                    return getHttp().setParameter(url, name, value);
255            }
256    
257            public static String setParameter(String url, String name, long value) {
258                    return getHttp().setParameter(url, name, value);
259            }
260    
261            public static String setParameter(String url, String name, short value) {
262                    return getHttp().setParameter(url, name, value);
263            }
264    
265            public static String setParameter(String url, String name, String value) {
266                    return getHttp().setParameter(url, name, value);
267            }
268    
269            public static byte[] URLtoByteArray(Http.Options options)
270                    throws IOException {
271    
272                    PortalSocketPermission.checkConnect(options);
273    
274                    return getHttp().URLtoByteArray(options);
275            }
276    
277            public static byte[] URLtoByteArray(String location) throws IOException {
278                    PortalSocketPermission.checkConnect(location);
279    
280                    return getHttp().URLtoByteArray(location);
281            }
282    
283            public static byte[] URLtoByteArray(String location, boolean post)
284                    throws IOException {
285    
286                    PortalSocketPermission.checkConnect(location);
287    
288                    return getHttp().URLtoByteArray(location, post);
289            }
290    
291            public static String URLtoString(Http.Options options) throws IOException {
292                    PortalSocketPermission.checkConnect(options);
293    
294                    return getHttp().URLtoString(options);
295            }
296    
297            public static String URLtoString(String location) throws IOException {
298                    PortalSocketPermission.checkConnect(location);
299    
300                    return getHttp().URLtoString(location);
301            }
302    
303            public static String URLtoString(String location, boolean post)
304                    throws IOException {
305    
306                    PortalSocketPermission.checkConnect(location);
307    
308                    return getHttp().URLtoString(location, post);
309            }
310    
311            /**
312             * This method only uses the default Commons HttpClient implementation when
313             * the URL object represents a HTTP resource. The URL object could also
314             * represent a file or some JNDI resource. In that case, the default Java
315             * implementation is used.
316             *
317             * @param  url the URL
318             * @return A string representation of the resource referenced by the URL
319             *         object
320             * @throws IOException if an IO Exception occurred
321             */
322            public static String URLtoString(URL url) throws IOException {
323                    PortalSocketPermission.checkConnect(url);
324    
325                    return getHttp().URLtoString(url);
326            }
327    
328            public void setHttp(Http http) {
329                    PortalRuntimePermission.checkSetBeanProperty(getClass());
330    
331                    _http = http;
332            }
333    
334            private static Http _http;
335    
336    }