1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.util;
24  
25  import java.io.IOException;
26  
27  import java.net.URL;
28  
29  import java.util.HashMap;
30  import java.util.Map;
31  
32  import javax.portlet.ActionRequest;
33  import javax.portlet.RenderRequest;
34  
35  import javax.servlet.http.Cookie;
36  import javax.servlet.http.HttpServletRequest;
37  
38  /**
39   * <a href="Http.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   *
43   */
44  public interface Http {
45  
46      public static final String HTTP = "http";
47  
48      public static final int HTTP_PORT = 80;
49  
50      public static final String HTTP_WITH_SLASH = "http://";
51  
52      public static final String HTTPS = "https";
53  
54      public static final int HTTPS_PORT = 443;
55  
56      public static final String HTTPS_WITH_SLASH = "https://";
57  
58      public static final String PROTOCOL_DELIMITER = "://";
59  
60      public String addParameter(String url, String name, boolean value);
61  
62      public String addParameter(String url, String name, double value);
63  
64      public String addParameter(String url, String name, int value);
65  
66      public String addParameter(String url, String name, long value);
67  
68      public String addParameter(String url, String name, short value);
69  
70      public String addParameter(String url, String name, String value);
71  
72      public String decodeURL(String url);
73  
74      public String decodeURL(String url, boolean unescapeSpace);
75  
76      public String encodeURL(String url);
77  
78      public String encodeURL(String url, boolean escapeSpaces);
79  
80      public String getCompleteURL(HttpServletRequest request);
81  
82      public Cookie[] getCookies();
83  
84      public String getDomain(String url);
85  
86      public String getParameter(String url, String name);
87  
88      public String getParameter(String url, String name, boolean escaped);
89  
90      public Map<String, String[]> getParameterMap(String queryString);
91  
92      public String getProtocol(ActionRequest actionRequest);
93  
94      public String getProtocol(boolean secure);
95  
96      public String getProtocol(HttpServletRequest request);
97  
98      public String getProtocol(RenderRequest renderRequest);
99  
100     public String getProtocol(String url);
101 
102     public String getQueryString(String url);
103 
104     public String getRequestURL(HttpServletRequest request);
105 
106     public boolean hasDomain(String url);
107 
108     public boolean hasProtocol(String url);
109 
110     public boolean hasProxyConfig();
111 
112     public boolean isNonProxyHost(String host);
113 
114     public boolean isProxyHost(String host);
115 
116     public Map<String, String[]> parameterMapFromString(String queryString);
117 
118     public String parameterMapToString(Map<String, String[]> parameterMap);
119 
120     public String parameterMapToString(
121         Map<String, String[]> parameterMap, boolean addQuestion);
122 
123     public String protocolize(String url, ActionRequest actionRequest);
124 
125     public String protocolize(String url, boolean secure);
126 
127     public String protocolize(String url, HttpServletRequest request);
128 
129     public String protocolize(String url, RenderRequest renderRequest);
130 
131     public String removeDomain(String url);
132 
133     public String removeParameter(String url, String name);
134 
135     public String removeProtocol(String url);
136 
137     public String setParameter(String url, String name, boolean value);
138 
139     public String setParameter(String url, String name, double value);
140 
141     public String setParameter(String url, String name, int value);
142 
143     public String setParameter(String url, String name, long value);
144 
145     public String setParameter(String url, String name, short value);
146 
147     public String setParameter(String url, String name, String value);
148 
149     /**
150      * @deprecated
151      */
152     public void submit(String location) throws IOException;
153 
154     /**
155      * @deprecated
156      */
157     public void submit(String location, boolean post) throws IOException;
158 
159     /**
160      * @deprecated
161      */
162     public void submit(String location, Cookie[] cookies) throws IOException;
163 
164     /**
165      * @deprecated
166      */
167     public void submit(String location, Cookie[] cookies, boolean post)
168         throws IOException;
169 
170     /**
171      * @deprecated
172      */
173     public void submit(
174             String location, Cookie[] cookies, Http.Body body, boolean post)
175         throws IOException;
176 
177     /**
178      * @deprecated
179      */
180     public void submit(
181             String location, Cookie[] cookies, Map<String, String> parts,
182             boolean post)
183         throws IOException;
184 
185     public byte[] URLtoByteArray(Http.Options options) throws IOException;
186 
187     public byte[] URLtoByteArray(String location) throws IOException;
188 
189     public byte[] URLtoByteArray(String location, boolean post)
190         throws IOException;
191 
192     /**
193      * @deprecated
194      */
195     public byte[] URLtoByteArray(String location, Cookie[] cookies)
196         throws IOException;
197 
198     /**
199      * @deprecated
200      */
201     public byte[] URLtoByteArray(
202             String location, Cookie[] cookies, boolean post)
203         throws IOException;
204 
205     /**
206      * @deprecated
207      */
208     public byte[] URLtoByteArray(
209             String location, Cookie[] cookies, Http.Auth auth, Http.Body body,
210             boolean post)
211         throws IOException;
212 
213     /**
214      * @deprecated
215      */
216     public byte[] URLtoByteArray(
217             String location, Cookie[] cookies, Http.Auth auth,
218             Map<String, String> parts, boolean post)
219         throws IOException;
220 
221     /**
222      * @deprecated
223      */
224     public byte[] URLtoByteArray(
225             String location, Cookie[] cookies, Http.Body body, boolean post)
226         throws IOException;
227 
228     /**
229      * @deprecated
230      */
231     public byte[] URLtoByteArray(
232             String location, Cookie[] cookies, Map<String, String> parts,
233             boolean post)
234         throws IOException;
235 
236     public String URLtoString(Http.Options options) throws IOException;
237 
238     public String URLtoString(String location) throws IOException;
239 
240     public String URLtoString(String location, boolean post) throws IOException;
241 
242     /**
243      * @deprecated
244      */
245     public String URLtoString(String location, Cookie[] cookies)
246         throws IOException;
247 
248     /**
249      * @deprecated
250      */
251     public String URLtoString(String location, Cookie[] cookies, boolean post)
252         throws IOException;
253 
254     /**
255      * @deprecated
256      */
257     public String URLtoString(
258             String location, Cookie[] cookies, Http.Auth auth, Http.Body body,
259             boolean post)
260         throws IOException;
261 
262     /**
263      * @deprecated
264      */
265     public String URLtoString(
266             String location, Cookie[] cookies, Http.Auth auth,
267             Map<String, String> parts, boolean post)
268         throws IOException;
269 
270     /**
271      * @deprecated
272      */
273     public String URLtoString(
274             String location, Cookie[] cookies, Http.Body body, boolean post)
275         throws IOException;
276 
277     /**
278      * @deprecated
279      */
280     public String URLtoString(
281             String location, Cookie[] cookies, Map<String, String> parts,
282             boolean post)
283         throws IOException;
284 
285     /**
286      * @deprecated
287      */
288     public String URLtoString(
289             String location, String host, int port, String realm,
290             String username, String password)
291         throws IOException;
292 
293     /**
294      * This method only uses the default Commons HttpClient implementation when
295      * the URL object represents a HTTP resource. The URL object could also
296      * represent a file or some JNDI resource. In that case, the default Java
297      * implementation is used.
298      *
299      * @param       url URL object
300      * @return      A string representation of the resource referenced by the
301      *              URL object
302      * @throws      IOException
303      */
304     public String URLtoString(URL url) throws IOException;
305 
306     public class Auth {
307 
308         public Auth(
309             String host, int port, String realm, String username,
310             String password) {
311 
312             _host = host;
313             _port = port;
314             _realm = realm;
315             _username = username;
316             _password = password;
317         }
318 
319         public String getHost() {
320             return _host;
321         }
322 
323         public String getPassword() {
324             return _password;
325         }
326 
327         public int getPort() {
328             return _port;
329         }
330 
331         public String getRealm() {
332             return _realm;
333         }
334 
335         public String getUsername() {
336             return _username;
337         }
338 
339         private String _host;
340         private String _password;
341         private int _port;
342         private String _realm;
343         private String _username;
344 
345     }
346 
347     public class Body {
348 
349         public Body(String content, String contentType, String charset) {
350             _content = content;
351             _contentType = contentType;
352             _charset = charset;
353         }
354 
355         public String getCharset() {
356             return _charset;
357         }
358 
359         public String getContent() {
360             return _content;
361         }
362 
363         public String getContentType() {
364             return _contentType;
365         }
366 
367         private String _charset;
368         private String _content;
369         private String _contentType;
370 
371     }
372 
373     public enum Method {
374 
375         DELETE, GET, POST, PUT
376     }
377 
378     public class Options {
379 
380         public void addHeader(String name, String value) {
381             if (_headers == null) {
382                 _headers = new HashMap<String, String>();
383             }
384 
385             _headers.put(name, value);
386         }
387 
388         public void addPart(String name, String value) {
389             if (_body != null) {
390                 throw new IllegalArgumentException(
391                     "Part cannot be added because a body has already been set");
392             }
393 
394             if (_parts == null) {
395                 _parts = new HashMap<String, String>();
396             }
397 
398             _parts.put(name, value);
399         }
400 
401         public Auth getAuth() {
402             return _auth;
403         }
404 
405         public Body getBody() {
406             return _body;
407         }
408 
409         public Cookie[] getCookies() {
410             return _cookies;
411         }
412 
413         public Map<String, String> getHeaders() {
414             return _headers;
415         }
416 
417         public String getLocation() {
418             return _location;
419         }
420 
421         public Method getMethod() {
422             return _method;
423         }
424 
425         public Map<String, String> getParts() {
426             return _parts;
427         }
428 
429         public boolean isDelete() {
430             if (_method == Method.DELETE) {
431                 return true;
432             }
433             else {
434                 return false;
435             }
436         }
437 
438         public boolean isGet() {
439             if (_method == Method.GET) {
440                 return true;
441             }
442             else {
443                 return false;
444             }
445         }
446 
447         public boolean isPost() {
448             if (_method == Method.POST) {
449                 return true;
450             }
451             else {
452                 return false;
453             }
454         }
455 
456         public boolean isPut() {
457             if (_method == Method.PUT) {
458                 return true;
459             }
460             else {
461                 return false;
462             }
463         }
464 
465         public void setAuth(Http.Auth auth) {
466             setAuth(
467                 auth.getHost(), auth.getPort(), auth.getRealm(),
468                 auth.getUsername(), auth.getPassword());
469         }
470 
471         public void setAuth(
472             String host, int port, String realm, String username,
473             String password) {
474 
475             _auth = new Auth(host, port, realm, username, password);
476         }
477 
478         public void setBody(Http.Body body) {
479             setBody(
480                 body.getContent(), body.getContentType(), body.getCharset());
481         }
482 
483         public void setBody(
484             String content, String contentType, String charset) {
485 
486             if (_parts != null) {
487                 throw new IllegalArgumentException(
488                     "Body cannot be set because a part has already been added");
489             }
490 
491             _body = new Body(content, contentType, charset);
492         }
493 
494         public void setCookies(Cookie[] cookies) {
495             _cookies = cookies;
496         }
497 
498         public void setDelete(boolean delete) {
499             if (delete) {
500                 _method = Method.DELETE;
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 setPost(boolean post) {
520             if (post) {
521                 _method = Method.POST;
522             }
523             else {
524                 _method = Method.GET;
525             }
526         }
527 
528         public void setPut(boolean put) {
529             if (put) {
530                 _method = Method.PUT;
531             }
532             else {
533                 _method = Method.GET;
534             }
535         }
536 
537         private Auth _auth;
538         private Body _body;
539         private Cookie[] _cookies;
540         private Map<String, String> _headers;
541         private String _location;
542         private Map<String, String> _parts;
543         private Method _method = Method.GET;
544 
545     }
546 
547 }