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