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