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.ArrayList;
025    import java.util.List;
026    import java.util.Map;
027    
028    import javax.portlet.ActionRequest;
029    import javax.portlet.RenderRequest;
030    
031    import javax.servlet.http.Cookie;
032    import javax.servlet.http.HttpServletRequest;
033    
034    /**
035     * @author Brian Wing Shun Chan
036     */
037    public class HttpUtil {
038    
039            public static String addParameter(String url, String name, boolean value) {
040                    return getHttp().addParameter(url, name, value);
041            }
042    
043            public static String addParameter(String url, String name, double value) {
044                    return getHttp().addParameter(url, name, value);
045            }
046    
047            public static String addParameter(String url, String name, int value) {
048                    return getHttp().addParameter(url, name, value);
049            }
050    
051            public static String addParameter(String url, String name, long value) {
052                    return getHttp().addParameter(url, name, value);
053            }
054    
055            public static String addParameter(String url, String name, short value) {
056                    return getHttp().addParameter(url, name, value);
057            }
058    
059            public static String addParameter(String url, String name, String value) {
060                    return getHttp().addParameter(url, name, value);
061            }
062    
063            public static String decodePath(String path) {
064                    return getHttp().decodePath(path);
065            }
066    
067            public static String decodeURL(String url) {
068                    return getHttp().decodeURL(url);
069            }
070    
071            public static String decodeURL(String url, boolean unescapeSpaces) {
072                    return getHttp().decodeURL(url, unescapeSpaces);
073            }
074    
075            public static String encodeParameters(String url) {
076                    return getHttp().encodeParameters(url);
077            }
078    
079            public static String encodePath(String path) {
080                    return getHttp().encodePath(path);
081            }
082    
083            public static String encodeURL(String url) {
084                    return getHttp().encodeURL(url);
085            }
086    
087            public static String encodeURL(String url, boolean escapeSpaces) {
088                    return getHttp().encodeURL(url, escapeSpaces);
089            }
090    
091            public static String fixPath(String path) {
092                    return getHttp().fixPath(path);
093            }
094    
095            public static String fixPath(
096                    String path, boolean leading, boolean trailing) {
097    
098                    return getHttp().fixPath(path, leading, trailing);
099            }
100    
101            public static String getCompleteURL(HttpServletRequest request) {
102                    return getHttp().getCompleteURL(request);
103            }
104    
105            public static Cookie[] getCookies() {
106                    return getHttp().getCookies();
107            }
108    
109            public static String getDomain(String url) {
110                    return getHttp().getDomain(url);
111            }
112    
113            public static Http getHttp() {
114                    PortalRuntimePermission.checkGetBeanProperty(HttpUtil.class);
115    
116                    return _http;
117            }
118    
119            public static String getIpAddress(String url) {
120                    return getHttp().getIpAddress(url);
121            }
122    
123            public static String getParameter(String url, String name) {
124                    return getHttp().getParameter(url, name);
125            }
126    
127            public static String getParameter(
128                    String url, String name, boolean escaped) {
129    
130                    return getHttp().getParameter(url, name, escaped);
131            }
132    
133            public static Map<String, String[]> getParameterMap(String queryString) {
134                    return getHttp().getParameterMap(queryString);
135            }
136    
137            public static String getPath(String url) {
138                    return getHttp().getPath(url);
139            }
140    
141            public static String getProtocol(ActionRequest actionRequest) {
142                    return getHttp().getProtocol(actionRequest);
143            }
144    
145            public static String getProtocol(boolean secure) {
146                    return getHttp().getProtocol(secure);
147            }
148    
149            public static String getProtocol(HttpServletRequest request) {
150                    return getHttp().getProtocol(request);
151            }
152    
153            public static String getProtocol(RenderRequest renderRequest) {
154                    return getHttp().getProtocol(renderRequest);
155            }
156    
157            public static String getProtocol(String url) {
158                    return getHttp().getProtocol(url);
159            }
160    
161            public static String getQueryString(String url) {
162                    return getHttp().getQueryString(url);
163            }
164    
165            public static String getRequestURL(HttpServletRequest request) {
166                    return getHttp().getRequestURL(request);
167            }
168    
169            public static boolean hasDomain(String url) {
170                    return getHttp().hasDomain(url);
171            }
172    
173            public static boolean hasProtocol(String url) {
174                    return getHttp().hasProtocol(url);
175            }
176    
177            public static boolean hasProxyConfig() {
178                    return getHttp().hasProxyConfig();
179            }
180    
181            public static boolean isNonProxyHost(String host) {
182                    return getHttp().isNonProxyHost(host);
183            }
184    
185            public static boolean isProxyHost(String host) {
186                    return getHttp().isProxyHost(host);
187            }
188    
189            public static boolean isSecure(String url) {
190                    return getHttp().isSecure(url);
191            }
192    
193            public static String normalizePath(String uri) {
194                    if (Validator.isNull(uri)) {
195                            return uri;
196                    }
197    
198                    uri = removePathParameters(uri);
199    
200                    String path = null;
201                    String queryString = null;
202    
203                    int pos = uri.indexOf('?');
204    
205                    if (pos != -1) {
206                            path = uri.substring(0, pos);
207                            queryString = uri.substring(pos + 1);
208                    }
209                    else {
210                            path = uri;
211                    }
212    
213                    String[] uriParts = StringUtil.split(
214                            path.substring(1), StringPool.SLASH);
215    
216                    List<String> parts = new ArrayList<String>(uriParts.length);
217    
218                    for (int i = 0; i < uriParts.length; i++) {
219                            String curUriPart = URLCodec.decodeURL(uriParts[i]);
220                            String prevUriPart = null;
221    
222                            if (i > 0) {
223                                    prevUriPart = URLCodec.decodeURL(uriParts[i - 1]);
224                            }
225    
226                            if (curUriPart.equals(StringPool.DOUBLE_PERIOD)) {
227                                    if (!prevUriPart.equals(StringPool.PERIOD)) {
228                                            parts.remove(parts.size() - 1);
229                                    }
230                            }
231                            else if ((curUriPart.length() > 0) &&
232                                             !curUriPart.equals(StringPool.PERIOD)) {
233    
234                                    parts.add(URLCodec.encodeURL(curUriPart));
235                            }
236                    }
237    
238                    if (parts.isEmpty()) {
239                            return StringPool.SLASH;
240                    }
241    
242                    StringBundler sb = new StringBundler(parts.size() * 2 + 2);
243    
244                    for (String part : parts) {
245                            sb.append(StringPool.SLASH);
246                            sb.append(part);
247                    }
248    
249                    if (Validator.isNotNull(queryString)) {
250                            sb.append(StringPool.QUESTION);
251                            sb.append(queryString);
252                    }
253    
254                    return sb.toString();
255            }
256    
257            public static Map<String, String[]> parameterMapFromString(
258                    String queryString) {
259    
260                    return getHttp().parameterMapFromString(queryString);
261            }
262    
263            public static String parameterMapToString(
264                    Map<String, String[]> parameterMap) {
265    
266                    return getHttp().parameterMapToString(parameterMap);
267            }
268    
269            public static String parameterMapToString(
270                    Map<String, String[]> parameterMap, boolean addQuestion) {
271    
272                    return getHttp().parameterMapToString(parameterMap, addQuestion);
273            }
274    
275            public static String protocolize(String url, ActionRequest actionRequest) {
276                    return getHttp().protocolize(url, actionRequest);
277            }
278    
279            public static String protocolize(String url, boolean secure) {
280                    return getHttp().protocolize(url, secure);
281            }
282    
283            public static String protocolize(String url, HttpServletRequest request) {
284                    return getHttp().protocolize(url, request);
285            }
286    
287            public static String protocolize(String url, int port, boolean secure) {
288                    return getHttp().protocolize(url, port, secure);
289            }
290    
291            public static String protocolize(String url, RenderRequest renderRequest) {
292                    return getHttp().protocolize(url, renderRequest);
293            }
294    
295            public static String removeDomain(String url) {
296                    return getHttp().removeDomain(url);
297            }
298    
299            public static String removeParameter(String url, String name) {
300                    return getHttp().removeParameter(url, name);
301            }
302    
303            public static String removePathParameters(String uri) {
304                    if (Validator.isNull(uri)) {
305                            return uri;
306                    }
307    
308                    int pos = uri.indexOf(StringPool.SEMICOLON);
309    
310                    if (pos == -1) {
311                            return uri;
312                    }
313    
314                    String[] uriParts = StringUtil.split(
315                            uri.substring(1), StringPool.SLASH);
316    
317                    StringBundler sb = new StringBundler(uriParts.length * 2);
318    
319                    for (String uriPart : uriParts) {
320                            pos = uriPart.indexOf(StringPool.SEMICOLON);
321    
322                            if (pos == -1) {
323                                    sb.append(StringPool.SLASH);
324                                    sb.append(uriPart);
325                            }
326                            else if (pos == 0) {
327                                    continue;
328                            }
329                            else {
330                                    sb.append(StringPool.SLASH);
331                                    sb.append(uriPart.substring(0, pos));
332                            }
333                    }
334    
335                    if (sb.length() == 0) {
336                            return StringPool.SLASH;
337                    }
338    
339                    return sb.toString();
340            }
341    
342            public static String removeProtocol(String url) {
343                    return getHttp().removeProtocol(url);
344            }
345    
346            public static String sanitizeHeader(String header) {
347                    return getHttp().sanitizeHeader(header);
348            }
349    
350            public static String setParameter(String url, String name, boolean value) {
351                    return getHttp().setParameter(url, name, value);
352            }
353    
354            public static String setParameter(String url, String name, double value) {
355                    return getHttp().setParameter(url, name, value);
356            }
357    
358            public static String setParameter(String url, String name, int value) {
359                    return getHttp().setParameter(url, name, value);
360            }
361    
362            public static String setParameter(String url, String name, long value) {
363                    return getHttp().setParameter(url, name, value);
364            }
365    
366            public static String setParameter(String url, String name, short value) {
367                    return getHttp().setParameter(url, name, value);
368            }
369    
370            public static String setParameter(String url, String name, String value) {
371                    return getHttp().setParameter(url, name, value);
372            }
373    
374            public static String shortenURL(String url, int count) {
375                    return getHttp().shortenURL(url, count);
376            }
377    
378            public static byte[] URLtoByteArray(Http.Options options)
379                    throws IOException {
380    
381                    PortalSocketPermission.checkConnect(options);
382    
383                    return getHttp().URLtoByteArray(options);
384            }
385    
386            public static byte[] URLtoByteArray(String location) throws IOException {
387                    PortalSocketPermission.checkConnect(location);
388    
389                    return getHttp().URLtoByteArray(location);
390            }
391    
392            public static byte[] URLtoByteArray(String location, boolean post)
393                    throws IOException {
394    
395                    PortalSocketPermission.checkConnect(location);
396    
397                    return getHttp().URLtoByteArray(location, post);
398            }
399    
400            public static String URLtoString(Http.Options options) throws IOException {
401                    PortalSocketPermission.checkConnect(options);
402    
403                    return getHttp().URLtoString(options);
404            }
405    
406            public static String URLtoString(String location) throws IOException {
407                    PortalSocketPermission.checkConnect(location);
408    
409                    return getHttp().URLtoString(location);
410            }
411    
412            public static String URLtoString(String location, boolean post)
413                    throws IOException {
414    
415                    PortalSocketPermission.checkConnect(location);
416    
417                    return getHttp().URLtoString(location, post);
418            }
419    
420            /**
421             * This method only uses the default Commons HttpClient implementation when
422             * the URL object represents a HTTP resource. The URL object could also
423             * represent a file or some JNDI resource. In that case, the default Java
424             * implementation is used.
425             *
426             * @param  url the URL
427             * @return A string representation of the resource referenced by the URL
428             *         object
429             * @throws IOException if an IO Exception occurred
430             */
431            public static String URLtoString(URL url) throws IOException {
432                    PortalSocketPermission.checkConnect(url);
433    
434                    return getHttp().URLtoString(url);
435            }
436    
437            public void setHttp(Http http) {
438                    PortalRuntimePermission.checkSetBeanProperty(getClass());
439    
440                    _http = http;
441            }
442    
443            private static Http _http;
444    
445    }