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