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 decodePath(String path);
64
65 public String decodeURL(String url);
66
67 public String decodeURL(String url, boolean unescapeSpaces);
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 getCompleteURL(HttpServletRequest request);
76
77 public Cookie[] getCookies();
78
79 public String getDomain(String url);
80
81 public String getIpAddress(String url);
82
83 public String getParameter(String url, String name);
84
85 public String getParameter(String url, String name, boolean escaped);
86
87 public Map<String, String[]> getParameterMap(String queryString);
88
89 public String getProtocol(ActionRequest actionRequest);
90
91 public String getProtocol(boolean secure);
92
93 public String getProtocol(HttpServletRequest request);
94
95 public String getProtocol(RenderRequest renderRequest);
96
97 public String getProtocol(String url);
98
99 public String getQueryString(String url);
100
101 public String getRequestURL(HttpServletRequest request);
102
103 public boolean hasDomain(String url);
104
105 public boolean hasProtocol(String url);
106
107 public boolean hasProxyConfig();
108
109 public boolean isNonProxyHost(String host);
110
111 public boolean isProxyHost(String host);
112
113 public Map<String, String[]> parameterMapFromString(String queryString);
114
115 public String parameterMapToString(Map<String, String[]> parameterMap);
116
117 public String parameterMapToString(
118 Map<String, String[]> parameterMap, boolean addQuestion);
119
120 public String protocolize(String url, ActionRequest actionRequest);
121
122 public String protocolize(String url, boolean secure);
123
124 public String protocolize(String url, HttpServletRequest request);
125
126 public String protocolize(String url, RenderRequest renderRequest);
127
128 public String removeDomain(String url);
129
130 public String removeParameter(String url, String name);
131
132 public String removeProtocol(String url);
133
134 public String setParameter(String url, String name, boolean value);
135
136 public String setParameter(String url, String name, double value);
137
138 public String setParameter(String url, String name, int value);
139
140 public String setParameter(String url, String name, long value);
141
142 public String setParameter(String url, String name, short value);
143
144 public String setParameter(String url, String name, String value);
145
146 public byte[] URLtoByteArray(Http.Options options) throws IOException;
147
148 public byte[] URLtoByteArray(String location) throws IOException;
149
150 public byte[] URLtoByteArray(String location, boolean post)
151 throws IOException;
152
153
156 public byte[] URLtoByteArray(
157 String location, Cookie[] cookies, Http.Auth auth, Http.Body body,
158 boolean post)
159 throws IOException;
160
161
164 public byte[] URLtoByteArray(
165 String location, Cookie[] cookies, Http.Auth auth,
166 Map<String, String> parts, boolean post)
167 throws IOException;
168
169 public String URLtoString(Http.Options options) throws IOException;
170
171 public String URLtoString(String location) throws IOException;
172
173 public String URLtoString(String location, boolean post) throws IOException;
174
175
178 public String URLtoString(
179 String location, Cookie[] cookies, Http.Auth auth, Http.Body body,
180 boolean post)
181 throws IOException;
182
183
186 public String URLtoString(
187 String location, Cookie[] cookies, Http.Auth auth,
188 Map<String, String> parts, boolean post)
189 throws IOException;
190
191
201 public String URLtoString(URL url) throws IOException;
202
203 public class Auth {
204
205 public Auth(
206 String host, int port, String realm, String username,
207 String password) {
208
209 _host = host;
210 _port = port;
211 _realm = realm;
212 _username = username;
213 _password = password;
214 }
215
216 public String getHost() {
217 return _host;
218 }
219
220 public String getPassword() {
221 return _password;
222 }
223
224 public int getPort() {
225 return _port;
226 }
227
228 public String getRealm() {
229 return _realm;
230 }
231
232 public String getUsername() {
233 return _username;
234 }
235
236 private String _host;
237 private String _password;
238 private int _port;
239 private String _realm;
240 private String _username;
241
242 }
243
244 public class Body {
245
246 public Body(String content, String contentType, String charset) {
247 _content = content;
248 _contentType = contentType;
249 _charset = charset;
250 }
251
252 public String getCharset() {
253 return _charset;
254 }
255
256 public String getContent() {
257 return _content;
258 }
259
260 public String getContentType() {
261 return _contentType;
262 }
263
264 private String _charset;
265 private String _content;
266 private String _contentType;
267
268 }
269
270 public enum Method {
271
272 DELETE, GET, HEAD, POST, PUT
273
274 }
275
276 public class Options {
277
278 public void addHeader(String name, String value) {
279 if (_headers == null) {
280 _headers = new HashMap<String, String>();
281 }
282
283 _headers.put(name, value);
284 }
285
286 public void addPart(String name, String value) {
287 if (_body != null) {
288 throw new IllegalArgumentException(
289 "Part cannot be added because a body has already been set");
290 }
291
292 if (_parts == null) {
293 _parts = new HashMap<String, String>();
294 }
295
296 _parts.put(name, value);
297 }
298
299 public Auth getAuth() {
300 return _auth;
301 }
302
303 public Body getBody() {
304 return _body;
305 }
306
307 public Cookie[] getCookies() {
308 return _cookies;
309 }
310
311 public Map<String, String> getHeaders() {
312 return _headers;
313 }
314
315 public String getLocation() {
316 return _location;
317 }
318
319 public Method getMethod() {
320 return _method;
321 }
322
323 public Map<String, String> getParts() {
324 return _parts;
325 }
326
327 public Response getResponse() {
328 return _response;
329 }
330
331 public boolean isDelete() {
332 if (_method == Method.DELETE) {
333 return true;
334 }
335 else {
336 return false;
337 }
338 }
339
340 public boolean isFollowRedirects() {
341 return _followRedirects;
342 }
343
344 public boolean isGet() {
345 if (_method == Method.GET) {
346 return true;
347 }
348 else {
349 return false;
350 }
351 }
352
353 public boolean isHead() {
354 if (_method == Method.HEAD) {
355 return true;
356 }
357 else {
358 return false;
359 }
360 }
361
362 public boolean isPost() {
363 if (_method == Method.POST) {
364 return true;
365 }
366 else {
367 return false;
368 }
369 }
370
371 public boolean isPut() {
372 if (_method == Method.PUT) {
373 return true;
374 }
375 else {
376 return false;
377 }
378 }
379
380 public void setAuth(Http.Auth auth) {
381 setAuth(
382 auth.getHost(), auth.getPort(), auth.getRealm(),
383 auth.getUsername(), auth.getPassword());
384 }
385
386 public void setAuth(
387 String host, int port, String realm, String username,
388 String password) {
389
390 _auth = new Auth(host, port, realm, username, password);
391 }
392
393 public void setBody(Http.Body body) {
394 setBody(
395 body.getContent(), body.getContentType(), body.getCharset());
396 }
397
398 public void setBody(
399 String content, String contentType, String charset) {
400
401 if (_parts != null) {
402 throw new IllegalArgumentException(
403 "Body cannot be set because a part has already been added");
404 }
405
406 _body = new Body(content, contentType, charset);
407 }
408
409 public void setCookies(Cookie[] cookies) {
410 _cookies = cookies;
411 }
412
413 public void setDelete(boolean delete) {
414 if (delete) {
415 _method = Method.DELETE;
416 }
417 else {
418 _method = Method.GET;
419 }
420 }
421
422 public void setFollowRedirects(boolean followRedirects) {
423 _followRedirects = followRedirects;
424 }
425
426 public void setHead(boolean head) {
427 if (head) {
428 _method = Method.HEAD;
429 }
430 else {
431 _method = Method.GET;
432 }
433 }
434
435 public void setHeaders(Map<String, String> headers) {
436 _headers = headers;
437 }
438
439 public void setLocation(String location) {
440 _location = location;
441 }
442
443 public void setParts(Map<String, String> parts) {
444 _parts = parts;
445 }
446
447 public void setPost(boolean post) {
448 if (post) {
449 _method = Method.POST;
450 }
451 else {
452 _method = Method.GET;
453 }
454 }
455
456 public void setPut(boolean put) {
457 if (put) {
458 _method = Method.PUT;
459 }
460 else {
461 _method = Method.GET;
462 }
463 }
464
465 public void setResponse(Response response) {
466 _response = response;
467 }
468
469 private Auth _auth;
470 private Body _body;
471 private Cookie[] _cookies;
472 private boolean _followRedirects = true;
473 private Map<String, String> _headers;
474 private String _location;
475 private Method _method = Method.GET;
476 private Map<String, String> _parts;
477 private Response _response = new Response();
478
479 }
480
481 public class Response {
482
483 public void addHeader(String name, String value) {
484 if (_headers == null) {
485 _headers = new HashMap<String, String>();
486 }
487
488 _headers.put(name, value);
489 }
490
491 public int getContentLength() {
492 return _contentLength;
493 }
494
495 public String getContentType() {
496 return _contentType;
497 }
498
499 public String getHeader(String name) {
500 if (_headers == null) {
501 return null;
502 }
503 else {
504 return _headers.get(name);
505 }
506 }
507
508 public Map<String, String> getHeaders() {
509 return _headers;
510 }
511
512 public String getRedirect() {
513 return _redirect;
514 }
515
516 public void setContentLength(int contentLength) {
517 _contentLength = contentLength;
518 }
519
520 public void setContentType(String contentType) {
521 _contentType = contentType;
522 }
523
524 public void setHeaders(Map<String, String> headers) {
525 _headers = headers;
526 }
527
528 public void setRedirect(String redirect) {
529 _redirect = redirect;
530 }
531
532 private int _contentLength = -1;
533 private String _contentType;
534 private Map<String, String> _headers;
535 private String _redirect;
536
537 }
538
539 }