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