1
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
43 public interface Http {
44
45 public static final String HTTP = "http";
46
47 public static final int HTTP_PORT = 80;
48
49 public static final String HTTP_WITH_SLASH = "http://";
50
51 public static final String HTTPS = "https";
52
53 public static final int HTTPS_PORT = 443;
54
55 public static final String HTTPS_WITH_SLASH = "https://";
56
57 public static final String PROTOCOL_DELIMITER = "://";
58
59 public String addParameter(String url, String name, boolean value);
60
61 public String addParameter(String url, String name, double value);
62
63 public String addParameter(String url, String name, int value);
64
65 public String addParameter(String url, String name, long value);
66
67 public String addParameter(String url, String name, short value);
68
69 public String addParameter(String url, String name, String value);
70
71 public String decodeURL(String url);
72
73 public String decodeURL(String url, boolean unescapeSpace);
74
75 public String encodeURL(String url);
76
77 public String encodeURL(String url, boolean escapeSpaces);
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
153 public void submit(String location) throws IOException;
154
155
158 public void submit(String location, boolean post) throws IOException;
159
160
163 public void submit(String location, Cookie[] cookies) throws IOException;
164
165
168 public void submit(String location, Cookie[] cookies, boolean post)
169 throws IOException;
170
171
174 public void submit(
175 String location, Cookie[] cookies, Http.Body body, boolean post)
176 throws IOException;
177
178
181 public void submit(
182 String location, Cookie[] cookies, Map<String, String> parts,
183 boolean post)
184 throws IOException;
185
186 public byte[] URLtoByteArray(Http.Options options) throws IOException;
187
188 public byte[] URLtoByteArray(String location) throws IOException;
189
190 public byte[] URLtoByteArray(String location, boolean post)
191 throws IOException;
192
193
196 public byte[] URLtoByteArray(String location, Cookie[] cookies)
197 throws IOException;
198
199
202 public byte[] URLtoByteArray(
203 String location, Cookie[] cookies, boolean post)
204 throws IOException;
205
206
209 public byte[] URLtoByteArray(
210 String location, Cookie[] cookies, Http.Auth auth, Http.Body body,
211 boolean post)
212 throws IOException;
213
214
217 public byte[] URLtoByteArray(
218 String location, Cookie[] cookies, Http.Auth auth,
219 Map<String, String> parts, boolean post)
220 throws IOException;
221
222
225 public byte[] URLtoByteArray(
226 String location, Cookie[] cookies, Http.Body body, boolean post)
227 throws IOException;
228
229
232 public byte[] URLtoByteArray(
233 String location, Cookie[] cookies, Map<String, String> parts,
234 boolean post)
235 throws IOException;
236
237 public String URLtoString(Http.Options options) throws IOException;
238
239 public String URLtoString(String location) throws IOException;
240
241 public String URLtoString(String location, boolean post) throws IOException;
242
243
246 public String URLtoString(String location, Cookie[] cookies)
247 throws IOException;
248
249
252 public String URLtoString(String location, Cookie[] cookies, boolean post)
253 throws IOException;
254
255
258 public String URLtoString(
259 String location, Cookie[] cookies, Http.Auth auth, Http.Body body,
260 boolean post)
261 throws IOException;
262
263
266 public String URLtoString(
267 String location, Cookie[] cookies, Http.Auth auth,
268 Map<String, String> parts, boolean post)
269 throws IOException;
270
271
274 public String URLtoString(
275 String location, Cookie[] cookies, Http.Body body, boolean post)
276 throws IOException;
277
278
281 public String URLtoString(
282 String location, Cookie[] cookies, Map<String, String> parts,
283 boolean post)
284 throws IOException;
285
286
289 public String URLtoString(
290 String location, String host, int port, String realm,
291 String username, String password)
292 throws IOException;
293
294
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 }