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 java.io.IOException;
018    import java.io.InputStream;
019    
020    import java.net.URL;
021    
022    import java.util.ArrayList;
023    import java.util.HashMap;
024    import java.util.List;
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     * @author Hugo Huijser
036     */
037    public interface Http {
038    
039            public static final String HTTP = "http";
040    
041            public static final int HTTP_PORT = 80;
042    
043            public static final String HTTP_WITH_SLASH = "http://";
044    
045            public static final String HTTPS = "https";
046    
047            public static final int HTTPS_PORT = 443;
048    
049            public static final String HTTPS_WITH_SLASH = "https://";
050    
051            public static final String PROTOCOL_DELIMITER = "://";
052    
053            public static final int URL_MAXIMUM_LENGTH = 2083;
054    
055            public String addParameter(String url, String name, boolean value);
056    
057            public String addParameter(String url, String name, double value);
058    
059            public String addParameter(String url, String name, int value);
060    
061            public String addParameter(String url, String name, long value);
062    
063            public String addParameter(String url, String name, short value);
064    
065            public String addParameter(String url, String name, String value);
066    
067            public String decodePath(String path);
068    
069            public String decodeURL(String url);
070    
071            /**
072             * @deprecated As of 7.0.0, replaced by {@link #decodeURL(String)}
073             */
074            @Deprecated
075            public String decodeURL(String url, boolean unescapeSpaces);
076    
077            public String encodeParameters(String url);
078    
079            public String encodePath(String path);
080    
081            public String encodeURL(String url);
082    
083            public String encodeURL(String url, boolean escapeSpaces);
084    
085            public String fixPath(String path);
086    
087            public String fixPath(String path, boolean leading, boolean trailing);
088    
089            public String getCompleteURL(HttpServletRequest request);
090    
091            public Cookie[] getCookies();
092    
093            public String getDomain(String url);
094    
095            public String getIpAddress(String url);
096    
097            public String getParameter(String url, String name);
098    
099            public String getParameter(String url, String name, boolean escaped);
100    
101            public Map<String, String[]> getParameterMap(String queryString);
102    
103            public String getPath(String url);
104    
105            public String getProtocol(ActionRequest actionRequest);
106    
107            public String getProtocol(boolean secure);
108    
109            public String getProtocol(HttpServletRequest request);
110    
111            public String getProtocol(RenderRequest renderRequest);
112    
113            public String getProtocol(String url);
114    
115            public String getQueryString(String url);
116    
117            public String getRequestURL(HttpServletRequest request);
118    
119            public boolean hasDomain(String url);
120    
121            public boolean hasProtocol(String url);
122    
123            public boolean hasProxyConfig();
124    
125            public boolean isNonProxyHost(String host);
126    
127            public boolean isProxyHost(String host);
128    
129            public boolean isSecure(String url);
130    
131            public String normalizePath(String uri);
132    
133            public Map<String, String[]> parameterMapFromString(String queryString);
134    
135            public String parameterMapToString(Map<String, String[]> parameterMap);
136    
137            public String parameterMapToString(
138                    Map<String, String[]> parameterMap, boolean addQuestion);
139    
140            public String protocolize(String url, ActionRequest actionRequest);
141    
142            public String protocolize(String url, boolean secure);
143    
144            public String protocolize(String url, HttpServletRequest request);
145    
146            public String protocolize(String url, int port, boolean secure);
147    
148            public String protocolize(String url, RenderRequest renderRequest);
149    
150            public String removeDomain(String url);
151    
152            public String removeParameter(String url, String name);
153    
154            public String removePathParameters(String uri);
155    
156            public String removeProtocol(String url);
157    
158            public String sanitizeHeader(String header);
159    
160            public String setParameter(String url, String name, boolean value);
161    
162            public String setParameter(String url, String name, double value);
163    
164            public String setParameter(String url, String name, int value);
165    
166            public String setParameter(String url, String name, long value);
167    
168            public String setParameter(String url, String name, short value);
169    
170            public String setParameter(String url, String name, String value);
171    
172            public String shortenURL(String url, int count);
173    
174            public byte[] URLtoByteArray(Http.Options options) throws IOException;
175    
176            public byte[] URLtoByteArray(String location) throws IOException;
177    
178            public byte[] URLtoByteArray(String location, boolean post)
179                    throws IOException;
180    
181            public InputStream URLtoInputStream(Http.Options options)
182                    throws IOException;
183    
184            public InputStream URLtoInputStream(String location) throws IOException;
185    
186            public InputStream URLtoInputStream(String location, boolean post)
187                    throws IOException;
188    
189            public String URLtoString(Http.Options options) throws IOException;
190    
191            public String URLtoString(String location) throws IOException;
192    
193            public String URLtoString(String location, boolean post) throws IOException;
194    
195            /**
196             * This method only uses the default Commons HttpClient implementation when
197             * the URL object represents a HTTP resource. The URL object could also
198             * represent a file or some JNDI resource. In that case, the default Java
199             * implementation is used.
200             *
201             * @param  url the URL
202             * @return A string representation of the resource referenced by the URL
203             *         object
204             * @throws IOException if an IO exception occurred
205             */
206            public String URLtoString(URL url) throws IOException;
207    
208            public class Auth {
209    
210                    public Auth(
211                            String host, int port, String realm, String username,
212                            String password) {
213    
214                            _host = host;
215                            _port = port;
216                            _realm = realm;
217                            _username = username;
218                            _password = password;
219                    }
220    
221                    public String getHost() {
222                            return _host;
223                    }
224    
225                    public String getPassword() {
226                            return _password;
227                    }
228    
229                    public int getPort() {
230                            return _port;
231                    }
232    
233                    public String getRealm() {
234                            return _realm;
235                    }
236    
237                    public String getUsername() {
238                            return _username;
239                    }
240    
241                    private final String _host;
242                    private final String _password;
243                    private final int _port;
244                    private final String _realm;
245                    private final String _username;
246    
247            }
248    
249            public class Body {
250    
251                    public Body(String content, String contentType, String charset) {
252                            _content = content;
253                            _contentType = contentType;
254                            _charset = charset;
255                    }
256    
257                    public String getCharset() {
258                            return _charset;
259                    }
260    
261                    public String getContent() {
262                            return _content;
263                    }
264    
265                    public String getContentType() {
266                            return _contentType;
267                    }
268    
269                    private final String _charset;
270                    private final String _content;
271                    private String _contentType;
272    
273            }
274    
275            public class FilePart {
276    
277                    public FilePart(
278                            String name, String fileName, byte[] value, String contentType,
279                            String charSet) {
280    
281                            _name = name;
282                            _fileName = fileName;
283                            _value = value;
284                            _contentType = contentType;
285                            _charSet = charSet;
286                    }
287    
288                    public String getCharSet() {
289                            return _charSet;
290                    }
291    
292                    public String getContentType() {
293                            return _contentType;
294                    }
295    
296                    public String getFileName() {
297                            return _fileName;
298                    }
299    
300                    public String getName() {
301                            return _name;
302                    }
303    
304                    public byte[] getValue() {
305                            return _value;
306                    }
307    
308                    private final String _charSet;
309                    private String _contentType;
310                    private final String _fileName;
311                    private final String _name;
312                    private final byte[] _value;
313    
314            }
315    
316            public enum Method {
317    
318                    DELETE, GET, HEAD, POST, PUT
319    
320            }
321    
322            public class Options {
323    
324                    public void addFilePart(
325                            String name, String fileName, byte[] value, String contentType,
326                            String charSet) {
327    
328                            if (_body != null) {
329                                    throw new IllegalArgumentException (
330                                            "File part cannot be added because a body has already " +
331                                                    "been set");
332                            }
333    
334                            if (_fileParts == null) {
335                                    _fileParts = new ArrayList<>();
336                            }
337    
338                            FilePart filePart = new FilePart(
339                                    name, fileName, value, contentType, charSet);
340    
341                            _fileParts.add(filePart);
342                    }
343    
344                    public void addHeader(String name, String value) {
345                            if (_headers == null) {
346                                    _headers = new HashMap<>();
347                            }
348    
349                            _headers.put(name, value);
350                    }
351    
352                    public void addPart(String name, String value) {
353                            if (_body != null) {
354                                    throw new IllegalArgumentException(
355                                            "Part cannot be added because a body has already been set");
356                            }
357    
358                            if (_parts == null) {
359                                    _parts = new HashMap<>();
360                            }
361    
362                            _parts.put(name, value);
363                    }
364    
365                    public Auth getAuth() {
366                            return _auth;
367                    }
368    
369                    public Body getBody() {
370                            return _body;
371                    }
372    
373                    public Cookie[] getCookies() {
374                            return _cookies;
375                    }
376    
377                    public List<FilePart> getFileParts() {
378                            return _fileParts;
379                    }
380    
381                    public Map<String, String> getHeaders() {
382                            return _headers;
383                    }
384    
385                    public String getLocation() {
386                            return _location;
387                    }
388    
389                    public Method getMethod() {
390                            return _method;
391                    }
392    
393                    public Map<String, String> getParts() {
394                            return _parts;
395                    }
396    
397                    public Response getResponse() {
398                            return _response;
399                    }
400    
401                    public boolean isDelete() {
402                            if (_method == Method.DELETE) {
403                                    return true;
404                            }
405                            else {
406                                    return false;
407                            }
408                    }
409    
410                    public boolean isFollowRedirects() {
411                            return _followRedirects;
412                    }
413    
414                    public boolean isGet() {
415                            if (_method == Method.GET) {
416                                    return true;
417                            }
418                            else {
419                                    return false;
420                            }
421                    }
422    
423                    public boolean isHead() {
424                            if (_method == Method.HEAD) {
425                                    return true;
426                            }
427                            else {
428                                    return false;
429                            }
430                    }
431    
432                    public boolean isPost() {
433                            if (_method == Method.POST) {
434                                    return true;
435                            }
436                            else {
437                                    return false;
438                            }
439                    }
440    
441                    public boolean isPut() {
442                            if (_method == Method.PUT) {
443                                    return true;
444                            }
445                            else {
446                                    return false;
447                            }
448                    }
449    
450                    public void setAuth(Http.Auth auth) {
451                            setAuth(
452                                    auth.getHost(), auth.getPort(), auth.getRealm(),
453                                    auth.getUsername(), auth.getPassword());
454                    }
455    
456                    public void setAuth(
457                            String host, int port, String realm, String username,
458                            String password) {
459    
460                            _auth = new Auth(host, port, realm, username, password);
461                    }
462    
463                    public void setBody(Http.Body body) {
464                            setBody(
465                                    body.getContent(), body.getContentType(), body.getCharset());
466                    }
467    
468                    public void setBody(
469                            String content, String contentType, String charset) {
470    
471                            if (_parts != null) {
472                                    throw new IllegalArgumentException(
473                                            "Body cannot be set because a part has already been added");
474                            }
475    
476                            _body = new Body(content, contentType, charset);
477                    }
478    
479                    public void setCookies(Cookie[] cookies) {
480                            _cookies = cookies;
481                    }
482    
483                    public void setDelete(boolean delete) {
484                            if (delete) {
485                                    _method = Method.DELETE;
486                            }
487                            else {
488                                    _method = Method.GET;
489                            }
490                    }
491    
492                    public void setFileParts(List<FilePart> fileParts) {
493                            _fileParts = fileParts;
494                    }
495    
496                    public void setFollowRedirects(boolean followRedirects) {
497                            _followRedirects = followRedirects;
498                    }
499    
500                    public void setHead(boolean head) {
501                            if (head) {
502                                    _method = Method.HEAD;
503                            }
504                            else {
505                                    _method = Method.GET;
506                            }
507                    }
508    
509                    public void setHeaders(Map<String, String> headers) {
510                            _headers = headers;
511                    }
512    
513                    public void setLocation(String location) {
514                            _location = location;
515                    }
516    
517                    public void setParts(Map<String, String> parts) {
518                            _parts = parts;
519                    }
520    
521                    public void setPost(boolean post) {
522                            if (post) {
523                                    _method = Method.POST;
524                            }
525                            else {
526                                    _method = Method.GET;
527                            }
528                    }
529    
530                    public void setPut(boolean put) {
531                            if (put) {
532                                    _method = Method.PUT;
533                            }
534                            else {
535                                    _method = Method.GET;
536                            }
537                    }
538    
539                    public void setResponse(Response response) {
540                            _response = response;
541                    }
542    
543                    private Auth _auth;
544                    private Body _body;
545                    private Cookie[] _cookies;
546                    private List<FilePart> _fileParts;
547                    private boolean _followRedirects = true;
548                    private Map<String, String> _headers;
549                    private String _location;
550                    private Method _method = Method.GET;
551                    private Map<String, String> _parts;
552                    private Response _response = new Response();
553    
554            }
555    
556            public class Response {
557    
558                    public void addHeader(String name, String value) {
559                            if (_headers == null) {
560                                    _headers = new HashMap<>();
561                            }
562    
563                            _headers.put(StringUtil.toLowerCase(name), value);
564                    }
565    
566                    public int getContentLength() {
567                            return _contentLength;
568                    }
569    
570                    public long getContentLengthLong() {
571                            return _contentLengthLong;
572                    }
573    
574                    public String getContentType() {
575                            return _contentType;
576                    }
577    
578                    public String getHeader(String name) {
579                            if (_headers == null) {
580                                    return null;
581                            }
582                            else {
583                                    return _headers.get(StringUtil.toLowerCase(name));
584                            }
585                    }
586    
587                    public Map<String, String> getHeaders() {
588                            return _headers;
589                    }
590    
591                    public String getRedirect() {
592                            return _redirect;
593                    }
594    
595                    public int getResponseCode() {
596                            return _responseCode;
597                    }
598    
599                    public void setContentLength(int contentLength) {
600                            _contentLength = contentLength;
601                    }
602    
603                    public void setContentLengthLong(long contentLengthLong) {
604                            _contentLengthLong = contentLengthLong;
605                    }
606    
607                    public void setContentType(String contentType) {
608                            _contentType = contentType;
609                    }
610    
611                    public void setHeaders(Map<String, String> headers) {
612                            _headers = headers;
613                    }
614    
615                    public void setRedirect(String redirect) {
616                            _redirect = redirect;
617                    }
618    
619                    public void setResponseCode(int responseCode) {
620                            _responseCode = responseCode;
621                    }
622    
623                    private int _contentLength = -1;
624                    private long _contentLengthLong = -1;
625                    private String _contentType;
626                    private Map<String, String> _headers;
627                    private String _redirect;
628                    private int _responseCode = -1;
629    
630            }
631    
632    }