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    
019    import java.net.URL;
020    
021    import java.util.ArrayList;
022    import java.util.HashMap;
023    import java.util.List;
024    import java.util.Map;
025    
026    import javax.portlet.ActionRequest;
027    import javax.portlet.PortletRequest;
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 Map<String, String[]> parameterMapFromString(String queryString);
132    
133            public String parameterMapToString(Map<String, String[]> parameterMap);
134    
135            public String parameterMapToString(
136                    Map<String, String[]> parameterMap, boolean addQuestion);
137    
138            public String protocolize(String url, ActionRequest actionRequest);
139    
140            public String protocolize(String url, boolean secure);
141    
142            public String protocolize(String url, HttpServletRequest request);
143    
144            public String protocolize(String url, int port, boolean secure);
145    
146            public String protocolize(String url, RenderRequest renderRequest);
147    
148            public String removeDomain(String url);
149    
150            public String removeParameter(String url, String name);
151    
152            public String removePathParameters(String uri);
153    
154            public String removeProtocol(String url);
155    
156            public String sanitizeHeader(String header);
157    
158            public String setParameter(String url, String name, boolean value);
159    
160            public String setParameter(String url, String name, double value);
161    
162            public String setParameter(String url, String name, int value);
163    
164            public String setParameter(String url, String name, long value);
165    
166            public String setParameter(String url, String name, short value);
167    
168            public String setParameter(String url, String name, String value);
169    
170            public String shortenURL(String url, int count);
171    
172            public byte[] URLtoByteArray(Http.Options options) throws IOException;
173    
174            public byte[] URLtoByteArray(String location) throws IOException;
175    
176            public byte[] URLtoByteArray(String location, boolean post)
177                    throws IOException;
178    
179            public String URLtoString(Http.Options options) throws IOException;
180    
181            public String URLtoString(String location) throws IOException;
182    
183            public String URLtoString(String location, boolean post) throws IOException;
184    
185            /**
186             * This method only uses the default Commons HttpClient implementation when
187             * the URL object represents a HTTP resource. The URL object could also
188             * represent a file or some JNDI resource. In that case, the default Java
189             * implementation is used.
190             *
191             * @param  url the URL
192             * @return A string representation of the resource referenced by the URL
193             *         object
194             * @throws IOException if an IO exception occurred
195             */
196            public String URLtoString(URL url) throws IOException;
197    
198            public class Auth {
199    
200                    public Auth(
201                            String host, int port, String realm, String username,
202                            String password) {
203    
204                            _host = host;
205                            _port = port;
206                            _realm = realm;
207                            _username = username;
208                            _password = password;
209                    }
210    
211                    public String getHost() {
212                            return _host;
213                    }
214    
215                    public String getPassword() {
216                            return _password;
217                    }
218    
219                    public int getPort() {
220                            return _port;
221                    }
222    
223                    public String getRealm() {
224                            return _realm;
225                    }
226    
227                    public String getUsername() {
228                            return _username;
229                    }
230    
231                    private final String _host;
232                    private final String _password;
233                    private final int _port;
234                    private final String _realm;
235                    private final String _username;
236    
237            }
238    
239            public class Body {
240    
241                    public Body(String content, String contentType, String charset) {
242                            _content = content;
243                            _contentType = contentType;
244                            _charset = charset;
245                    }
246    
247                    public String getCharset() {
248                            return _charset;
249                    }
250    
251                    public String getContent() {
252                            return _content;
253                    }
254    
255                    public String getContentType() {
256                            return _contentType;
257                    }
258    
259                    private final String _charset;
260                    private final String _content;
261                    private String _contentType;
262    
263            }
264    
265            public class FilePart {
266    
267                    public FilePart(
268                            String name, String fileName, byte[] value, String contentType,
269                            String charSet) {
270    
271                            _name = name;
272                            _fileName = fileName;
273                            _value = value;
274                            _contentType = contentType;
275                            _charSet = charSet;
276                    }
277    
278                    public String getCharSet() {
279                            return _charSet;
280                    }
281    
282                    public String getContentType() {
283                            return _contentType;
284                    }
285    
286                    public String getFileName() {
287                            return _fileName;
288                    }
289    
290                    public String getName() {
291                            return _name;
292                    }
293    
294                    public byte[] getValue() {
295                            return _value;
296                    }
297    
298                    private final String _charSet;
299                    private String _contentType;
300                    private final String _fileName;
301                    private final String _name;
302                    private final byte[] _value;
303    
304            }
305    
306            public enum Method {
307    
308                    DELETE, GET, HEAD, POST, PUT
309    
310            }
311    
312            public class Options {
313    
314                    public void addFilePart(
315                            String name, String fileName, byte[] value, String contentType,
316                            String charSet) {
317    
318                            if (_body != null) {
319                                    throw new IllegalArgumentException (
320                                            "File part cannot be added because a body has already " +
321                                                    "been set");
322                            }
323    
324                            if (_fileParts == null) {
325                                    _fileParts = new ArrayList<FilePart>();
326                            }
327    
328                            FilePart filePart = new FilePart(
329                                    name, fileName, value, contentType, charSet);
330    
331                            _fileParts.add(filePart);
332                    }
333    
334                    public void addHeader(String name, String value) {
335                            if (_headers == null) {
336                                    _headers = new HashMap<String, String>();
337                            }
338    
339                            _headers.put(name, value);
340                    }
341    
342                    public void addPart(String name, String value) {
343                            if (_body != null) {
344                                    throw new IllegalArgumentException(
345                                            "Part cannot be added because a body has already been set");
346                            }
347    
348                            if (_parts == null) {
349                                    _parts = new HashMap<String, String>();
350                            }
351    
352                            _parts.put(name, value);
353                    }
354    
355                    public Auth getAuth() {
356                            return _auth;
357                    }
358    
359                    public Body getBody() {
360                            return _body;
361                    }
362    
363                    public Cookie[] getCookies() {
364                            return _cookies;
365                    }
366    
367                    public List<FilePart> getFileParts() {
368                            return _fileParts;
369                    }
370    
371                    public Map<String, String> getHeaders() {
372                            return _headers;
373                    }
374    
375                    public String getLocation() {
376                            return _location;
377                    }
378    
379                    public Method getMethod() {
380                            return _method;
381                    }
382    
383                    public Map<String, String> getParts() {
384                            return _parts;
385                    }
386    
387                    public PortletRequest getPortletRequest() {
388                            return _portletRequest;
389                    }
390    
391                    public String getProgressId() {
392                            return _progressId;
393                    }
394    
395                    public Response getResponse() {
396                            return _response;
397                    }
398    
399                    public boolean isDelete() {
400                            if (_method == Method.DELETE) {
401                                    return true;
402                            }
403                            else {
404                                    return false;
405                            }
406                    }
407    
408                    public boolean isFollowRedirects() {
409                            return _followRedirects;
410                    }
411    
412                    public boolean isGet() {
413                            if (_method == Method.GET) {
414                                    return true;
415                            }
416                            else {
417                                    return false;
418                            }
419                    }
420    
421                    public boolean isHead() {
422                            if (_method == Method.HEAD) {
423                                    return true;
424                            }
425                            else {
426                                    return false;
427                            }
428                    }
429    
430                    public boolean isPost() {
431                            if (_method == Method.POST) {
432                                    return true;
433                            }
434                            else {
435                                    return false;
436                            }
437                    }
438    
439                    public boolean isPut() {
440                            if (_method == Method.PUT) {
441                                    return true;
442                            }
443                            else {
444                                    return false;
445                            }
446                    }
447    
448                    public void setAuth(Http.Auth auth) {
449                            setAuth(
450                                    auth.getHost(), auth.getPort(), auth.getRealm(),
451                                    auth.getUsername(), auth.getPassword());
452                    }
453    
454                    public void setAuth(
455                            String host, int port, String realm, String username,
456                            String password) {
457    
458                            _auth = new Auth(host, port, realm, username, password);
459                    }
460    
461                    public void setBody(Http.Body body) {
462                            setBody(
463                                    body.getContent(), body.getContentType(), body.getCharset());
464                    }
465    
466                    public void setBody(
467                            String content, String contentType, String charset) {
468    
469                            if (_parts != null) {
470                                    throw new IllegalArgumentException(
471                                            "Body cannot be set because a part has already been added");
472                            }
473    
474                            _body = new Body(content, contentType, charset);
475                    }
476    
477                    public void setCookies(Cookie[] cookies) {
478                            _cookies = cookies;
479                    }
480    
481                    public void setDelete(boolean delete) {
482                            if (delete) {
483                                    _method = Method.DELETE;
484                            }
485                            else {
486                                    _method = Method.GET;
487                            }
488                    }
489    
490                    public void setFileParts(List<FilePart> fileParts) {
491                            _fileParts = fileParts;
492                    }
493    
494                    public void setFollowRedirects(boolean followRedirects) {
495                            _followRedirects = followRedirects;
496                    }
497    
498                    public void setHead(boolean head) {
499                            if (head) {
500                                    _method = Method.HEAD;
501                            }
502                            else {
503                                    _method = Method.GET;
504                            }
505                    }
506    
507                    public void setHeaders(Map<String, String> headers) {
508                            _headers = headers;
509                    }
510    
511                    public void setLocation(String location) {
512                            _location = location;
513                    }
514    
515                    public void setParts(Map<String, String> parts) {
516                            _parts = parts;
517                    }
518    
519                    public void setPortletRequest(PortletRequest portletRequest) {
520                            _portletRequest = portletRequest;
521                    }
522    
523                    public void setPost(boolean post) {
524                            if (post) {
525                                    _method = Method.POST;
526                            }
527                            else {
528                                    _method = Method.GET;
529                            }
530                    }
531    
532                    public void setProgressId(String progressId) {
533                            _progressId = progressId;
534                    }
535    
536                    public void setPut(boolean put) {
537                            if (put) {
538                                    _method = Method.PUT;
539                            }
540                            else {
541                                    _method = Method.GET;
542                            }
543                    }
544    
545                    public void setResponse(Response response) {
546                            _response = response;
547                    }
548    
549                    private Auth _auth;
550                    private Body _body;
551                    private Cookie[] _cookies;
552                    private List<FilePart> _fileParts;
553                    private boolean _followRedirects = true;
554                    private Map<String, String> _headers;
555                    private String _location;
556                    private Method _method = Method.GET;
557                    private Map<String, String> _parts;
558                    private PortletRequest _portletRequest;
559                    private String _progressId;
560                    private Response _response = new Response();
561    
562            }
563    
564            public class Response {
565    
566                    public void addHeader(String name, String value) {
567                            if (_headers == null) {
568                                    _headers = new HashMap<String, String>();
569                            }
570    
571                            _headers.put(StringUtil.toLowerCase(name), value);
572                    }
573    
574                    public int getContentLength() {
575                            return _contentLength;
576                    }
577    
578                    public String getContentType() {
579                            return _contentType;
580                    }
581    
582                    public String getHeader(String name) {
583                            if (_headers == null) {
584                                    return null;
585                            }
586                            else {
587                                    return _headers.get(StringUtil.toLowerCase(name));
588                            }
589                    }
590    
591                    public Map<String, String> getHeaders() {
592                            return _headers;
593                    }
594    
595                    public String getRedirect() {
596                            return _redirect;
597                    }
598    
599                    public int getResponseCode() {
600                            return _responseCode;
601                    }
602    
603                    public void setContentLength(int contentLength) {
604                            _contentLength = contentLength;
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 String _contentType;
625                    private Map<String, String> _headers;
626                    private String _redirect;
627                    private int _responseCode = -1;
628    
629            }
630    
631    }