001
014
015 package com.liferay.portal.kernel.util;
016
017 import java.io.IOException;
018 import java.io.InputStream;
019
020 import java.net.URL;
021
022 import java.util.ArrayList;
023 import java.util.HashMap;
024 import java.util.List;
025 import java.util.Map;
026
027 import javax.portlet.ActionRequest;
028 import javax.portlet.RenderRequest;
029
030 import javax.servlet.http.Cookie;
031 import javax.servlet.http.HttpServletRequest;
032
033
037 public interface Http {
038
039 public static final String HTTP = "http";
040
041 public static final int HTTP_PORT = 80;
042
043 public static final String HTTP_WITH_SLASH = "http:
044
045 public static final String HTTPS = "https";
046
047 public static final int HTTPS_PORT = 443;
048
049 public static final String HTTPS_WITH_SLASH = "https:
050
051 public static final String PROTOCOL_DELIMITER = ":
052
053 public static final int URL_MAXIMUM_LENGTH = 2083;
054
055 public String addParameter(String url, String name, boolean value);
056
057 public String addParameter(String url, String name, double value);
058
059 public String addParameter(String url, String name, int value);
060
061 public String addParameter(String url, String name, long value);
062
063 public String addParameter(String url, String name, short value);
064
065 public String addParameter(String url, String name, String value);
066
067 public String decodePath(String path);
068
069 public String decodeURL(String url);
070
071
074 @Deprecated
075 public String decodeURL(String url, boolean unescapeSpaces);
076
077 public String encodeParameters(String url);
078
079 public String encodePath(String path);
080
081 public String encodeURL(String url);
082
083 public String encodeURL(String url, boolean escapeSpaces);
084
085 public String fixPath(String path);
086
087 public String fixPath(String path, boolean leading, boolean trailing);
088
089 public String getCompleteURL(HttpServletRequest request);
090
091 public Cookie[] getCookies();
092
093 public String getDomain(String url);
094
095 public String getIpAddress(String url);
096
097 public String getParameter(String url, String name);
098
099 public String getParameter(String url, String name, boolean escaped);
100
101 public Map<String, String[]> getParameterMap(String queryString);
102
103 public String getPath(String url);
104
105 public String getProtocol(ActionRequest actionRequest);
106
107 public String getProtocol(boolean secure);
108
109 public String getProtocol(HttpServletRequest request);
110
111 public String getProtocol(RenderRequest renderRequest);
112
113 public String getProtocol(String url);
114
115 public String getQueryString(String url);
116
117 public String getRequestURL(HttpServletRequest request);
118
119 public boolean hasDomain(String url);
120
121 public boolean hasProtocol(String url);
122
123 public boolean hasProxyConfig();
124
125 public boolean isNonProxyHost(String host);
126
127 public boolean isProxyHost(String host);
128
129 public boolean isSecure(String url);
130
131 public Map<String, String[]> parameterMapFromString(String queryString);
132
133 public String parameterMapToString(Map<String, String[]> parameterMap);
134
135 public String parameterMapToString(
136 Map<String, String[]> parameterMap, boolean addQuestion);
137
138 public String protocolize(String url, ActionRequest actionRequest);
139
140 public String protocolize(String url, boolean secure);
141
142 public String protocolize(String url, HttpServletRequest request);
143
144 public String protocolize(String url, int port, boolean secure);
145
146 public String protocolize(String url, RenderRequest renderRequest);
147
148 public String removeDomain(String url);
149
150 public String removeParameter(String url, String name);
151
152 public String removePathParameters(String uri);
153
154 public String removeProtocol(String url);
155
156 public String sanitizeHeader(String header);
157
158 public String setParameter(String url, String name, boolean value);
159
160 public String setParameter(String url, String name, double value);
161
162 public String setParameter(String url, String name, int value);
163
164 public String setParameter(String url, String name, long value);
165
166 public String setParameter(String url, String name, short value);
167
168 public String setParameter(String url, String name, String value);
169
170 public String shortenURL(String url, int count);
171
172 public byte[] URLtoByteArray(Http.Options options) throws IOException;
173
174 public byte[] URLtoByteArray(String location) throws IOException;
175
176 public byte[] URLtoByteArray(String location, boolean post)
177 throws IOException;
178
179 public InputStream URLtoInputStream(Http.Options options)
180 throws IOException;
181
182 public InputStream URLtoInputStream(String location) throws IOException;
183
184 public InputStream URLtoInputStream(String location, boolean post)
185 throws IOException;
186
187 public String URLtoString(Http.Options options) throws IOException;
188
189 public String URLtoString(String location) throws IOException;
190
191 public String URLtoString(String location, boolean post) throws IOException;
192
193
204 public String URLtoString(URL url) throws IOException;
205
206 public class Auth {
207
208 public Auth(
209 String host, int port, String realm, String username,
210 String password) {
211
212 _host = host;
213 _port = port;
214 _realm = realm;
215 _username = username;
216 _password = password;
217 }
218
219 public String getHost() {
220 return _host;
221 }
222
223 public String getPassword() {
224 return _password;
225 }
226
227 public int getPort() {
228 return _port;
229 }
230
231 public String getRealm() {
232 return _realm;
233 }
234
235 public String getUsername() {
236 return _username;
237 }
238
239 private final String _host;
240 private final String _password;
241 private final int _port;
242 private final String _realm;
243 private final String _username;
244
245 }
246
247 public class Body {
248
249 public Body(String content, String contentType, String charset) {
250 _content = content;
251 _contentType = contentType;
252 _charset = charset;
253 }
254
255 public String getCharset() {
256 return _charset;
257 }
258
259 public String getContent() {
260 return _content;
261 }
262
263 public String getContentType() {
264 return _contentType;
265 }
266
267 private final String _charset;
268 private final String _content;
269 private String _contentType;
270
271 }
272
273 public class FilePart {
274
275 public FilePart(
276 String name, String fileName, byte[] value, String contentType,
277 String charSet) {
278
279 _name = name;
280 _fileName = fileName;
281 _value = value;
282 _contentType = contentType;
283 _charSet = charSet;
284 }
285
286 public String getCharSet() {
287 return _charSet;
288 }
289
290 public String getContentType() {
291 return _contentType;
292 }
293
294 public String getFileName() {
295 return _fileName;
296 }
297
298 public String getName() {
299 return _name;
300 }
301
302 public byte[] getValue() {
303 return _value;
304 }
305
306 private final String _charSet;
307 private String _contentType;
308 private final String _fileName;
309 private final String _name;
310 private final byte[] _value;
311
312 }
313
314 public enum Method {
315
316 DELETE, GET, HEAD, POST, PUT
317
318 }
319
320 public class Options {
321
322 public void addFilePart(
323 String name, String fileName, byte[] value, String contentType,
324 String charSet) {
325
326 if (_body != null) {
327 throw new IllegalArgumentException (
328 "File part cannot be added because a body has already " +
329 "been set");
330 }
331
332 if (_fileParts == null) {
333 _fileParts = new ArrayList<FilePart>();
334 }
335
336 FilePart filePart = new FilePart(
337 name, fileName, value, contentType, charSet);
338
339 _fileParts.add(filePart);
340 }
341
342 public void addHeader(String name, String value) {
343 if (_headers == null) {
344 _headers = new HashMap<String, String>();
345 }
346
347 _headers.put(name, value);
348 }
349
350 public void addPart(String name, String value) {
351 if (_body != null) {
352 throw new IllegalArgumentException(
353 "Part cannot be added because a body has already been set");
354 }
355
356 if (_parts == null) {
357 _parts = new HashMap<String, String>();
358 }
359
360 _parts.put(name, value);
361 }
362
363 public Auth getAuth() {
364 return _auth;
365 }
366
367 public Body getBody() {
368 return _body;
369 }
370
371 public Cookie[] getCookies() {
372 return _cookies;
373 }
374
375 public List<FilePart> getFileParts() {
376 return _fileParts;
377 }
378
379 public Map<String, String> getHeaders() {
380 return _headers;
381 }
382
383 public String getLocation() {
384 return _location;
385 }
386
387 public Method getMethod() {
388 return _method;
389 }
390
391 public Map<String, String> getParts() {
392 return _parts;
393 }
394
395 public Response getResponse() {
396 return _response;
397 }
398
399 public boolean isDelete() {
400 if (_method == Method.DELETE) {
401 return true;
402 }
403 else {
404 return false;
405 }
406 }
407
408 public boolean isFollowRedirects() {
409 return _followRedirects;
410 }
411
412 public boolean isGet() {
413 if (_method == Method.GET) {
414 return true;
415 }
416 else {
417 return false;
418 }
419 }
420
421 public boolean isHead() {
422 if (_method == Method.HEAD) {
423 return true;
424 }
425 else {
426 return false;
427 }
428 }
429
430 public boolean isPost() {
431 if (_method == Method.POST) {
432 return true;
433 }
434 else {
435 return false;
436 }
437 }
438
439 public boolean isPut() {
440 if (_method == Method.PUT) {
441 return true;
442 }
443 else {
444 return false;
445 }
446 }
447
448 public void setAuth(Http.Auth auth) {
449 setAuth(
450 auth.getHost(), auth.getPort(), auth.getRealm(),
451 auth.getUsername(), auth.getPassword());
452 }
453
454 public void setAuth(
455 String host, int port, String realm, String username,
456 String password) {
457
458 _auth = new Auth(host, port, realm, username, password);
459 }
460
461 public void setBody(Http.Body body) {
462 setBody(
463 body.getContent(), body.getContentType(), body.getCharset());
464 }
465
466 public void setBody(
467 String content, String contentType, String charset) {
468
469 if (_parts != null) {
470 throw new IllegalArgumentException(
471 "Body cannot be set because a part has already been added");
472 }
473
474 _body = new Body(content, contentType, charset);
475 }
476
477 public void setCookies(Cookie[] cookies) {
478 _cookies = cookies;
479 }
480
481 public void setDelete(boolean delete) {
482 if (delete) {
483 _method = Method.DELETE;
484 }
485 else {
486 _method = Method.GET;
487 }
488 }
489
490 public void setFileParts(List<FilePart> fileParts) {
491 _fileParts = fileParts;
492 }
493
494 public void setFollowRedirects(boolean followRedirects) {
495 _followRedirects = followRedirects;
496 }
497
498 public void setHead(boolean head) {
499 if (head) {
500 _method = Method.HEAD;
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 public void setResponse(Response response) {
538 _response = response;
539 }
540
541 private Auth _auth;
542 private Body _body;
543 private Cookie[] _cookies;
544 private List<FilePart> _fileParts;
545 private boolean _followRedirects = true;
546 private Map<String, String> _headers;
547 private String _location;
548 private Method _method = Method.GET;
549 private Map<String, String> _parts;
550 private Response _response = new Response();
551
552 }
553
554 public class Response {
555
556 public void addHeader(String name, String value) {
557 if (_headers == null) {
558 _headers = new HashMap<String, String>();
559 }
560
561 _headers.put(StringUtil.toLowerCase(name), value);
562 }
563
564 public int getContentLength() {
565 return _contentLength;
566 }
567
568 public long getContentLengthLong() {
569 return _contentLengthLong;
570 }
571
572 public String getContentType() {
573 return _contentType;
574 }
575
576 public String getHeader(String name) {
577 if (_headers == null) {
578 return null;
579 }
580 else {
581 return _headers.get(StringUtil.toLowerCase(name));
582 }
583 }
584
585 public Map<String, String> getHeaders() {
586 return _headers;
587 }
588
589 public String getRedirect() {
590 return _redirect;
591 }
592
593 public int getResponseCode() {
594 return _responseCode;
595 }
596
597 public void setContentLength(int contentLength) {
598 _contentLength = contentLength;
599 }
600
601 public void setContentLengthLong(long contentLengthLong) {
602 _contentLengthLong = contentLengthLong;
603 }
604
605 public void setContentType(String contentType) {
606 _contentType = contentType;
607 }
608
609 public void setHeaders(Map<String, String> headers) {
610 _headers = headers;
611 }
612
613 public void setRedirect(String redirect) {
614 _redirect = redirect;
615 }
616
617 public void setResponseCode(int responseCode) {
618 _responseCode = responseCode;
619 }
620
621 private int _contentLength = -1;
622 private long _contentLengthLong = -1;
623 private String _contentType;
624 private Map<String, String> _headers;
625 private String _redirect;
626 private int _responseCode = -1;
627
628 }
629
630 }