1
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
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 decodeURL(String url);
64
65 public String decodeURL(String url, boolean unescapeSpace);
66
67 public String encodeURL(String url);
68
69 public String encodeURL(String url, boolean escapeSpaces);
70
71 public String getCompleteURL(HttpServletRequest request);
72
73 public Cookie[] getCookies();
74
75 public String getDomain(String url);
76
77 public String getIpAddress(String url);
78
79 public String getParameter(String url, String name);
80
81 public String getParameter(String url, String name, boolean escaped);
82
83 public Map<String, String[]> getParameterMap(String queryString);
84
85 public String getProtocol(ActionRequest actionRequest);
86
87 public String getProtocol(boolean secure);
88
89 public String getProtocol(HttpServletRequest request);
90
91 public String getProtocol(RenderRequest renderRequest);
92
93 public String getProtocol(String url);
94
95 public String getQueryString(String url);
96
97 public String getRequestURL(HttpServletRequest request);
98
99 public boolean hasDomain(String url);
100
101 public boolean hasProtocol(String url);
102
103 public boolean hasProxyConfig();
104
105 public boolean isNonProxyHost(String host);
106
107 public boolean isProxyHost(String host);
108
109 public Map<String, String[]> parameterMapFromString(String queryString);
110
111 public String parameterMapToString(Map<String, String[]> parameterMap);
112
113 public String parameterMapToString(
114 Map<String, String[]> parameterMap, boolean addQuestion);
115
116 public String protocolize(String url, ActionRequest actionRequest);
117
118 public String protocolize(String url, boolean secure);
119
120 public String protocolize(String url, HttpServletRequest request);
121
122 public String protocolize(String url, RenderRequest renderRequest);
123
124 public String removeDomain(String url);
125
126 public String removeParameter(String url, String name);
127
128 public String removeProtocol(String url);
129
130 public String setParameter(String url, String name, boolean value);
131
132 public String setParameter(String url, String name, double value);
133
134 public String setParameter(String url, String name, int value);
135
136 public String setParameter(String url, String name, long value);
137
138 public String setParameter(String url, String name, short value);
139
140 public String setParameter(String url, String name, String value);
141
142
145 public void submit(String location) throws IOException;
146
147
150 public void submit(String location, boolean post) throws IOException;
151
152
155 public void submit(String location, Cookie[] cookies) throws IOException;
156
157
160 public void submit(String location, Cookie[] cookies, boolean post)
161 throws IOException;
162
163
166 public void submit(
167 String location, Cookie[] cookies, Http.Body body, boolean post)
168 throws IOException;
169
170
173 public void submit(
174 String location, Cookie[] cookies, Map<String, String> parts,
175 boolean post)
176 throws IOException;
177
178 public byte[] URLtoByteArray(Http.Options options) throws IOException;
179
180 public byte[] URLtoByteArray(String location) throws IOException;
181
182 public byte[] URLtoByteArray(String location, boolean post)
183 throws IOException;
184
185
188 public byte[] URLtoByteArray(String location, Cookie[] cookies)
189 throws IOException;
190
191
194 public byte[] URLtoByteArray(
195 String location, Cookie[] cookies, boolean post)
196 throws IOException;
197
198
201 public byte[] URLtoByteArray(
202 String location, Cookie[] cookies, Http.Auth auth, Http.Body body,
203 boolean post)
204 throws IOException;
205
206
209 public byte[] URLtoByteArray(
210 String location, Cookie[] cookies, Http.Auth auth,
211 Map<String, String> parts, boolean post)
212 throws IOException;
213
214
217 public byte[] URLtoByteArray(
218 String location, Cookie[] cookies, Http.Body body, boolean post)
219 throws IOException;
220
221
224 public byte[] URLtoByteArray(
225 String location, Cookie[] cookies, Map<String, String> parts,
226 boolean post)
227 throws IOException;
228
229 public String URLtoString(Http.Options options) throws IOException;
230
231 public String URLtoString(String location) throws IOException;
232
233 public String URLtoString(String location, boolean post) throws IOException;
234
235
238 public String URLtoString(String location, Cookie[] cookies)
239 throws IOException;
240
241
244 public String URLtoString(String location, Cookie[] cookies, boolean post)
245 throws IOException;
246
247
250 public String URLtoString(
251 String location, Cookie[] cookies, Http.Auth auth, Http.Body body,
252 boolean post)
253 throws IOException;
254
255
258 public String URLtoString(
259 String location, Cookie[] cookies, Http.Auth auth,
260 Map<String, String> parts, boolean post)
261 throws IOException;
262
263
266 public String URLtoString(
267 String location, Cookie[] cookies, Http.Body body, boolean post)
268 throws IOException;
269
270
273 public String URLtoString(
274 String location, Cookie[] cookies, Map<String, String> parts,
275 boolean post)
276 throws IOException;
277
278
281 public String URLtoString(
282 String location, String host, int port, String realm,
283 String username, String password)
284 throws IOException;
285
286
296 public String URLtoString(URL url) throws IOException;
297
298 public class Auth {
299
300 public Auth(
301 String host, int port, String realm, String username,
302 String password) {
303
304 _host = host;
305 _port = port;
306 _realm = realm;
307 _username = username;
308 _password = password;
309 }
310
311 public String getHost() {
312 return _host;
313 }
314
315 public String getPassword() {
316 return _password;
317 }
318
319 public int getPort() {
320 return _port;
321 }
322
323 public String getRealm() {
324 return _realm;
325 }
326
327 public String getUsername() {
328 return _username;
329 }
330
331 private String _host;
332 private String _password;
333 private int _port;
334 private String _realm;
335 private String _username;
336
337 }
338
339 public class Body {
340
341 public Body(String content, String contentType, String charset) {
342 _content = content;
343 _contentType = contentType;
344 _charset = charset;
345 }
346
347 public String getCharset() {
348 return _charset;
349 }
350
351 public String getContent() {
352 return _content;
353 }
354
355 public String getContentType() {
356 return _contentType;
357 }
358
359 private String _charset;
360 private String _content;
361 private String _contentType;
362
363 }
364
365 public enum Method {
366
367 DELETE, GET, HEAD, POST, PUT
368
369 }
370
371 public class Options {
372
373 public void addHeader(String name, String value) {
374 if (_headers == null) {
375 _headers = new HashMap<String, String>();
376 }
377
378 _headers.put(name, value);
379 }
380
381 public void addPart(String name, String value) {
382 if (_body != null) {
383 throw new IllegalArgumentException(
384 "Part cannot be added because a body has already been set");
385 }
386
387 if (_parts == null) {
388 _parts = new HashMap<String, String>();
389 }
390
391 _parts.put(name, value);
392 }
393
394 public Auth getAuth() {
395 return _auth;
396 }
397
398 public Body getBody() {
399 return _body;
400 }
401
402 public Cookie[] getCookies() {
403 return _cookies;
404 }
405
406 public Map<String, String> getHeaders() {
407 return _headers;
408 }
409
410 public String getLocation() {
411 return _location;
412 }
413
414 public Method getMethod() {
415 return _method;
416 }
417
418 public Map<String, String> getParts() {
419 return _parts;
420 }
421
422 public Response getResponse() {
423 return _response;
424 }
425
426 public boolean isDelete() {
427 if (_method == Method.DELETE) {
428 return true;
429 }
430 else {
431 return false;
432 }
433 }
434
435 public boolean isFollowRedirects() {
436 return _followRedirects;
437 }
438
439 public boolean isGet() {
440 if (_method == Method.GET) {
441 return true;
442 }
443 else {
444 return false;
445 }
446 }
447
448 public boolean isHead() {
449 if (_method == Method.HEAD) {
450 return true;
451 }
452 else {
453 return false;
454 }
455 }
456
457 public boolean isPost() {
458 if (_method == Method.POST) {
459 return true;
460 }
461 else {
462 return false;
463 }
464 }
465
466 public boolean isPut() {
467 if (_method == Method.PUT) {
468 return true;
469 }
470 else {
471 return false;
472 }
473 }
474
475 public void setAuth(Http.Auth auth) {
476 setAuth(
477 auth.getHost(), auth.getPort(), auth.getRealm(),
478 auth.getUsername(), auth.getPassword());
479 }
480
481 public void setAuth(
482 String host, int port, String realm, String username,
483 String password) {
484
485 _auth = new Auth(host, port, realm, username, password);
486 }
487
488 public void setBody(Http.Body body) {
489 setBody(
490 body.getContent(), body.getContentType(), body.getCharset());
491 }
492
493 public void setBody(
494 String content, String contentType, String charset) {
495
496 if (_parts != null) {
497 throw new IllegalArgumentException(
498 "Body cannot be set because a part has already been added");
499 }
500
501 _body = new Body(content, contentType, charset);
502 }
503
504 public void setCookies(Cookie[] cookies) {
505 _cookies = cookies;
506 }
507
508 public void setDelete(boolean delete) {
509 if (delete) {
510 _method = Method.DELETE;
511 }
512 else {
513 _method = Method.GET;
514 }
515 }
516
517 public void setFollowRedirects(boolean followRedirects) {
518 _followRedirects = followRedirects;
519 }
520
521 public void setHead(boolean head) {
522 if (head) {
523 _method = Method.HEAD;
524 }
525 else {
526 _method = Method.GET;
527 }
528 }
529
530 public void setHeaders(Map<String, String> headers) {
531 _headers = headers;
532 }
533
534 public void setLocation(String location) {
535 _location = location;
536 }
537
538 public void setParts(Map<String, String> parts) {
539 _parts = parts;
540 }
541
542 public void setPost(boolean post) {
543 if (post) {
544 _method = Method.POST;
545 }
546 else {
547 _method = Method.GET;
548 }
549 }
550
551 public void setPut(boolean put) {
552 if (put) {
553 _method = Method.PUT;
554 }
555 else {
556 _method = Method.GET;
557 }
558 }
559
560 public void setResponse(Response response) {
561 _response = response;
562 }
563
564 private Auth _auth;
565 private Body _body;
566 private Cookie[] _cookies;
567 private boolean _followRedirects = true;
568 private Map<String, String> _headers;
569 private String _location;
570 private Method _method = Method.GET;
571 private Map<String, String> _parts;
572 private Response _response = new Response();
573
574 }
575
576 public class Response {
577
578 public void addHeader(String name, String value) {
579 if (_headers == null) {
580 _headers = new HashMap<String, String>();
581 }
582
583 _headers.put(name, value);
584 }
585
586 public int getContentLength() {
587 return _contentLength;
588 }
589
590 public String getContentType() {
591 return _contentType;
592 }
593
594 public String getHeader(String name) {
595 if (_headers == null) {
596 return null;
597 }
598 else {
599 return _headers.get(name);
600 }
601 }
602
603 public Map<String, String> getHeaders() {
604 return _headers;
605 }
606
607 public String getRedirect() {
608 return _redirect;
609 }
610
611 public void setContentLength(int contentLength) {
612 _contentLength = contentLength;
613 }
614
615 public void setContentType(String contentType) {
616 _contentType = contentType;
617 }
618
619 public void setHeaders(Map<String, String> headers) {
620 _headers = headers;
621 }
622
623 public void setRedirect(String redirect) {
624 _redirect = redirect;
625 }
626
627 private int _contentLength = -1;
628 private String _contentType;
629 private Map<String, String> _headers;
630 private String _redirect;
631
632 }
633
634 }