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