1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.util;
16  
17  import java.io.IOException;
18  
19  import java.net.URL;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import javax.portlet.ActionRequest;
25  import javax.portlet.RenderRequest;
26  
27  import javax.servlet.http.Cookie;
28  import javax.servlet.http.HttpServletRequest;
29  
30  /**
31   * <a href="Http.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   */
35  public interface Http {
36  
37      public static final String HTTP = "http";
38  
39      public static final int HTTP_PORT = 80;
40  
41      public static final String HTTP_WITH_SLASH = "http://";
42  
43      public static final String HTTPS = "https";
44  
45      public static final int HTTPS_PORT = 443;
46  
47      public static final String HTTPS_WITH_SLASH = "https://";
48  
49      public static final String PROTOCOL_DELIMITER = "://";
50  
51      public String addParameter(String url, String name, boolean value);
52  
53      public String addParameter(String url, String name, double value);
54  
55      public String addParameter(String url, String name, int value);
56  
57      public String addParameter(String url, String name, long value);
58  
59      public String addParameter(String url, String name, short value);
60  
61      public String addParameter(String url, String name, String value);
62  
63      public String decodePath(String path);
64  
65      public String decodeURL(String url);
66  
67      public String decodeURL(String url, boolean unescapeSpace);
68  
69      public String encodePath(String path);
70  
71      public String encodeURL(String url);
72  
73      public String encodeURL(String url, boolean escapeSpaces);
74  
75      public String fixPath(String path);
76  
77      public String fixPath(String path, boolean leading, boolean trailing);
78  
79      public String getCompleteURL(HttpServletRequest request);
80  
81      public Cookie[] getCookies();
82  
83      public String getDomain(String url);
84  
85      public String getIpAddress(String url);
86  
87      public String getParameter(String url, String name);
88  
89      public String getParameter(String url, String name, boolean escaped);
90  
91      public Map<String, String[]> getParameterMap(String queryString);
92  
93      public String getProtocol(ActionRequest actionRequest);
94  
95      public String getProtocol(boolean secure);
96  
97      public String getProtocol(HttpServletRequest request);
98  
99      public String getProtocol(RenderRequest renderRequest);
100 
101     public String getProtocol(String url);
102 
103     public String getQueryString(String url);
104 
105     public String getRequestURL(HttpServletRequest request);
106 
107     public boolean hasDomain(String url);
108 
109     public boolean hasProtocol(String url);
110 
111     public boolean hasProxyConfig();
112 
113     public boolean isNonProxyHost(String host);
114 
115     public boolean isProxyHost(String host);
116 
117     public Map<String, String[]> parameterMapFromString(String queryString);
118 
119     public String parameterMapToString(Map<String, String[]> parameterMap);
120 
121     public String parameterMapToString(
122         Map<String, String[]> parameterMap, boolean addQuestion);
123 
124     public String protocolize(String url, ActionRequest actionRequest);
125 
126     public String protocolize(String url, boolean secure);
127 
128     public String protocolize(String url, HttpServletRequest request);
129 
130     public String protocolize(String url, RenderRequest renderRequest);
131 
132     public String removeDomain(String url);
133 
134     public String removeParameter(String url, String name);
135 
136     public String removeProtocol(String url);
137 
138     public String setParameter(String url, String name, boolean value);
139 
140     public String setParameter(String url, String name, double value);
141 
142     public String setParameter(String url, String name, int value);
143 
144     public String setParameter(String url, String name, long value);
145 
146     public String setParameter(String url, String name, short value);
147 
148     public String setParameter(String url, String name, String value);
149 
150     public byte[] URLtoByteArray(Http.Options options) throws IOException;
151 
152     public byte[] URLtoByteArray(String location) throws IOException;
153 
154     public byte[] URLtoByteArray(String location, boolean post)
155         throws IOException;
156 
157     public String URLtoString(Http.Options options) throws IOException;
158 
159     public String URLtoString(String location) throws IOException;
160 
161     public String URLtoString(String location, boolean post) throws IOException;
162 
163     /**
164      * This method only uses the default Commons HttpClient implementation when
165      * the URL object represents a HTTP resource. The URL object could also
166      * represent a file or some JNDI resource. In that case, the default Java
167      * implementation is used.
168      *
169      * @return A string representation of the resource referenced by the URL
170      *         object
171      */
172     public String URLtoString(URL url) throws IOException;
173 
174     public class Auth {
175 
176         public Auth(
177             String host, int port, String realm, String username,
178             String password) {
179 
180             _host = host;
181             _port = port;
182             _realm = realm;
183             _username = username;
184             _password = password;
185         }
186 
187         public String getHost() {
188             return _host;
189         }
190 
191         public String getPassword() {
192             return _password;
193         }
194 
195         public int getPort() {
196             return _port;
197         }
198 
199         public String getRealm() {
200             return _realm;
201         }
202 
203         public String getUsername() {
204             return _username;
205         }
206 
207         private String _host;
208         private String _password;
209         private int _port;
210         private String _realm;
211         private String _username;
212 
213     }
214 
215     public class Body {
216 
217         public Body(String content, String contentType, String charset) {
218             _content = content;
219             _contentType = contentType;
220             _charset = charset;
221         }
222 
223         public String getCharset() {
224             return _charset;
225         }
226 
227         public String getContent() {
228             return _content;
229         }
230 
231         public String getContentType() {
232             return _contentType;
233         }
234 
235         private String _charset;
236         private String _content;
237         private String _contentType;
238 
239     }
240 
241     public enum Method {
242 
243         DELETE, GET, HEAD, POST, PUT
244 
245     }
246 
247     public class Options {
248 
249         public void addHeader(String name, String value) {
250             if (_headers == null) {
251                 _headers = new HashMap<String, String>();
252             }
253 
254             _headers.put(name, value);
255         }
256 
257         public void addPart(String name, String value) {
258             if (_body != null) {
259                 throw new IllegalArgumentException(
260                     "Part cannot be added because a body has already been set");
261             }
262 
263             if (_parts == null) {
264                 _parts = new HashMap<String, String>();
265             }
266 
267             _parts.put(name, value);
268         }
269 
270         public Auth getAuth() {
271             return _auth;
272         }
273 
274         public Body getBody() {
275             return _body;
276         }
277 
278         public Cookie[] getCookies() {
279             return _cookies;
280         }
281 
282         public Map<String, String> getHeaders() {
283             return _headers;
284         }
285 
286         public String getLocation() {
287             return _location;
288         }
289 
290         public Method getMethod() {
291             return _method;
292         }
293 
294         public Map<String, String> getParts() {
295             return _parts;
296         }
297 
298         public Response getResponse() {
299             return _response;
300         }
301 
302         public boolean isDelete() {
303             if (_method == Method.DELETE) {
304                 return true;
305             }
306             else {
307                 return false;
308             }
309         }
310 
311         public boolean isFollowRedirects() {
312             return _followRedirects;
313         }
314 
315         public boolean isGet() {
316             if (_method == Method.GET) {
317                 return true;
318             }
319             else {
320                 return false;
321             }
322         }
323 
324         public boolean isHead() {
325             if (_method == Method.HEAD) {
326                 return true;
327             }
328             else {
329                 return false;
330             }
331         }
332 
333         public boolean isPost() {
334             if (_method == Method.POST) {
335                 return true;
336             }
337             else {
338                 return false;
339             }
340         }
341 
342         public boolean isPut() {
343             if (_method == Method.PUT) {
344                 return true;
345             }
346             else {
347                 return false;
348             }
349         }
350 
351         public void setAuth(Http.Auth auth) {
352             setAuth(
353                 auth.getHost(), auth.getPort(), auth.getRealm(),
354                 auth.getUsername(), auth.getPassword());
355         }
356 
357         public void setAuth(
358             String host, int port, String realm, String username,
359             String password) {
360 
361             _auth = new Auth(host, port, realm, username, password);
362         }
363 
364         public void setBody(Http.Body body) {
365             setBody(
366                 body.getContent(), body.getContentType(), body.getCharset());
367         }
368 
369         public void setBody(
370             String content, String contentType, String charset) {
371 
372             if (_parts != null) {
373                 throw new IllegalArgumentException(
374                     "Body cannot be set because a part has already been added");
375             }
376 
377             _body = new Body(content, contentType, charset);
378         }
379 
380         public void setCookies(Cookie[] cookies) {
381             _cookies = cookies;
382         }
383 
384         public void setDelete(boolean delete) {
385             if (delete) {
386                 _method = Method.DELETE;
387             }
388             else {
389                 _method = Method.GET;
390             }
391         }
392 
393         public void setFollowRedirects(boolean followRedirects) {
394             _followRedirects = followRedirects;
395         }
396 
397         public void setHead(boolean head) {
398             if (head) {
399                 _method = Method.HEAD;
400             }
401             else {
402                 _method = Method.GET;
403             }
404         }
405 
406         public void setHeaders(Map<String, String> headers) {
407             _headers = headers;
408         }
409 
410         public void setLocation(String location) {
411             _location = location;
412         }
413 
414         public void setParts(Map<String, String> parts) {
415             _parts = parts;
416         }
417 
418         public void setPost(boolean post) {
419             if (post) {
420                 _method = Method.POST;
421             }
422             else {
423                 _method = Method.GET;
424             }
425         }
426 
427         public void setPut(boolean put) {
428             if (put) {
429                 _method = Method.PUT;
430             }
431             else {
432                 _method = Method.GET;
433             }
434         }
435 
436         public void setResponse(Response response) {
437             _response = response;
438         }
439 
440         private Auth _auth;
441         private Body _body;
442         private Cookie[] _cookies;
443         private boolean _followRedirects = true;
444         private Map<String, String> _headers;
445         private String _location;
446         private Method _method = Method.GET;
447         private Map<String, String> _parts;
448         private Response _response = new Response();
449 
450     }
451 
452     public class Response {
453 
454         public void addHeader(String name, String value) {
455             if (_headers == null) {
456                 _headers = new HashMap<String, String>();
457             }
458 
459             _headers.put(name, value);
460         }
461 
462         public int getContentLength() {
463             return _contentLength;
464         }
465 
466         public String getContentType() {
467             return _contentType;
468         }
469 
470         public String getHeader(String name) {
471             if (_headers == null) {
472                 return null;
473             }
474             else {
475                 return _headers.get(name);
476             }
477         }
478 
479         public Map<String, String> getHeaders() {
480             return _headers;
481         }
482 
483         public String getRedirect() {
484             return _redirect;
485         }
486 
487         public void setContentLength(int contentLength) {
488             _contentLength = contentLength;
489         }
490 
491         public void setContentType(String contentType) {
492             _contentType = contentType;
493         }
494 
495         public void setHeaders(Map<String, String> headers) {
496             _headers = headers;
497         }
498 
499         public void setRedirect(String redirect) {
500             _redirect = redirect;
501         }
502 
503         private int _contentLength = -1;
504         private String _contentType;
505         private Map<String, String> _headers;
506         private String _redirect;
507 
508     }
509 
510 }