001
014
015 package com.liferay.portal.kernel.util;
016
017 import java.io.IOException;
018
019 import java.net.URL;
020
021 import java.util.ArrayList;
022 import java.util.HashMap;
023 import java.util.List;
024 import java.util.Map;
025
026 import javax.portlet.ActionRequest;
027 import javax.portlet.PortletRequest;
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 public String decodeURL(String url, boolean unescapeSpaces);
072
073 public String encodeParameters(String url);
074
075 public String encodePath(String path);
076
077 public String encodeURL(String url);
078
079 public String encodeURL(String url, boolean escapeSpaces);
080
081 public String fixPath(String path);
082
083 public String fixPath(String path, boolean leading, boolean trailing);
084
085 public String getCompleteURL(HttpServletRequest request);
086
087 public Cookie[] getCookies();
088
089 public String getDomain(String url);
090
091 public String getIpAddress(String url);
092
093 public String getParameter(String url, String name);
094
095 public String getParameter(String url, String name, boolean escaped);
096
097 public Map<String, String[]> getParameterMap(String queryString);
098
099 public String getPath(String url);
100
101 public String getProtocol(ActionRequest actionRequest);
102
103 public String getProtocol(boolean secure);
104
105 public String getProtocol(HttpServletRequest request);
106
107 public String getProtocol(RenderRequest renderRequest);
108
109 public String getProtocol(String url);
110
111 public String getQueryString(String url);
112
113 public String getRequestURL(HttpServletRequest request);
114
115 public boolean hasDomain(String url);
116
117 public boolean hasProtocol(String url);
118
119 public boolean hasProxyConfig();
120
121 public boolean isNonProxyHost(String host);
122
123 public boolean isProxyHost(String host);
124
125 public boolean isSecure(String url);
126
127 public Map<String, String[]> parameterMapFromString(String queryString);
128
129 public String parameterMapToString(Map<String, String[]> parameterMap);
130
131 public String parameterMapToString(
132 Map<String, String[]> parameterMap, boolean addQuestion);
133
134 public String protocolize(String url, ActionRequest actionRequest);
135
136 public String protocolize(String url, boolean secure);
137
138 public String protocolize(String url, HttpServletRequest request);
139
140 public String protocolize(String url, int port, boolean secure);
141
142 public String protocolize(String url, RenderRequest renderRequest);
143
144 public String removeDomain(String url);
145
146 public String removeParameter(String url, String name);
147
148 public String removeProtocol(String url);
149
150 public String sanitizeHeader(String header);
151
152 public String setParameter(String url, String name, boolean value);
153
154 public String setParameter(String url, String name, double value);
155
156 public String setParameter(String url, String name, int value);
157
158 public String setParameter(String url, String name, long value);
159
160 public String setParameter(String url, String name, short value);
161
162 public String setParameter(String url, String name, String value);
163
164 public String shortenURL(String url, int count);
165
166 public byte[] URLtoByteArray(Http.Options options) throws IOException;
167
168 public byte[] URLtoByteArray(String location) throws IOException;
169
170 public byte[] URLtoByteArray(String location, boolean post)
171 throws IOException;
172
173 public String URLtoString(Http.Options options) throws IOException;
174
175 public String URLtoString(String location) throws IOException;
176
177 public String URLtoString(String location, boolean post) throws IOException;
178
179
190 public String URLtoString(URL url) throws IOException;
191
192 public class Auth {
193
194 public Auth(
195 String host, int port, String realm, String username,
196 String password) {
197
198 _host = host;
199 _port = port;
200 _realm = realm;
201 _username = username;
202 _password = password;
203 }
204
205 public String getHost() {
206 return _host;
207 }
208
209 public String getPassword() {
210 return _password;
211 }
212
213 public int getPort() {
214 return _port;
215 }
216
217 public String getRealm() {
218 return _realm;
219 }
220
221 public String getUsername() {
222 return _username;
223 }
224
225 private String _host;
226 private String _password;
227 private int _port;
228 private String _realm;
229 private String _username;
230
231 }
232
233 public class Body {
234
235 public Body(String content, String contentType, String charset) {
236 _content = content;
237 _contentType = contentType;
238 _charset = charset;
239 }
240
241 public String getCharset() {
242 return _charset;
243 }
244
245 public String getContent() {
246 return _content;
247 }
248
249 public String getContentType() {
250 return _contentType;
251 }
252
253 private String _charset;
254 private String _content;
255 private String _contentType;
256
257 }
258
259 public class FilePart {
260
261 public FilePart(
262 String name, String fileName, byte[] value, String contentType,
263 String charSet) {
264
265 _name = name;
266 _fileName = fileName;
267 _value = value;
268 _contentType = contentType;
269 _charSet = charSet;
270 }
271
272 public String getCharSet() {
273 return _charSet;
274 }
275
276 public String getContentType() {
277 return _contentType;
278 }
279
280 public String getFileName() {
281 return _fileName;
282 }
283
284 public String getName() {
285 return _name;
286 }
287
288 public byte[] getValue() {
289 return _value;
290 }
291
292 private String _charSet;
293 private String _contentType;
294 private String _fileName;
295 private String _name;
296 private byte[] _value;
297
298 }
299
300 public enum Method {
301
302 DELETE, GET, HEAD, POST, PUT
303
304 }
305
306 public class Options {
307
308 public void addFilePart(
309 String name, String fileName, byte[] value, String contentType,
310 String charSet) {
311
312 if (_body != null) {
313 throw new IllegalArgumentException (
314 "File part cannot be added because a body has already " +
315 "been set");
316 }
317
318 if (_fileParts == null) {
319 _fileParts = new ArrayList<FilePart>();
320 }
321
322 FilePart filePart = new FilePart(
323 name, fileName, value, contentType, charSet);
324
325 _fileParts.add(filePart);
326 }
327
328 public void addHeader(String name, String value) {
329 if (_headers == null) {
330 _headers = new HashMap<String, String>();
331 }
332
333 _headers.put(name, value);
334 }
335
336 public void addPart(String name, String value) {
337 if (_body != null) {
338 throw new IllegalArgumentException(
339 "Part cannot be added because a body has already been set");
340 }
341
342 if (_parts == null) {
343 _parts = new HashMap<String, String>();
344 }
345
346 _parts.put(name, value);
347 }
348
349 public Auth getAuth() {
350 return _auth;
351 }
352
353 public Body getBody() {
354 return _body;
355 }
356
357 public Cookie[] getCookies() {
358 return _cookies;
359 }
360
361 public List<FilePart> getFileParts() {
362 return _fileParts;
363 }
364
365 public Map<String, String> getHeaders() {
366 return _headers;
367 }
368
369 public String getLocation() {
370 return _location;
371 }
372
373 public Method getMethod() {
374 return _method;
375 }
376
377 public Map<String, String> getParts() {
378 return _parts;
379 }
380
381 public PortletRequest getPortletRequest() {
382 return _portletRequest;
383 }
384
385 public String getProgressId() {
386 return _progressId;
387 }
388
389 public Response getResponse() {
390 return _response;
391 }
392
393 public boolean isDelete() {
394 if (_method == Method.DELETE) {
395 return true;
396 }
397 else {
398 return false;
399 }
400 }
401
402 public boolean isFollowRedirects() {
403 return _followRedirects;
404 }
405
406 public boolean isGet() {
407 if (_method == Method.GET) {
408 return true;
409 }
410 else {
411 return false;
412 }
413 }
414
415 public boolean isHead() {
416 if (_method == Method.HEAD) {
417 return true;
418 }
419 else {
420 return false;
421 }
422 }
423
424 public boolean isPost() {
425 if (_method == Method.POST) {
426 return true;
427 }
428 else {
429 return false;
430 }
431 }
432
433 public boolean isPut() {
434 if (_method == Method.PUT) {
435 return true;
436 }
437 else {
438 return false;
439 }
440 }
441
442 public void setAuth(Http.Auth auth) {
443 setAuth(
444 auth.getHost(), auth.getPort(), auth.getRealm(),
445 auth.getUsername(), auth.getPassword());
446 }
447
448 public void setAuth(
449 String host, int port, String realm, String username,
450 String password) {
451
452 _auth = new Auth(host, port, realm, username, password);
453 }
454
455 public void setBody(Http.Body body) {
456 setBody(
457 body.getContent(), body.getContentType(), body.getCharset());
458 }
459
460 public void setBody(
461 String content, String contentType, String charset) {
462
463 if (_parts != null) {
464 throw new IllegalArgumentException(
465 "Body cannot be set because a part has already been added");
466 }
467
468 _body = new Body(content, contentType, charset);
469 }
470
471 public void setCookies(Cookie[] cookies) {
472 _cookies = cookies;
473 }
474
475 public void setDelete(boolean delete) {
476 if (delete) {
477 _method = Method.DELETE;
478 }
479 else {
480 _method = Method.GET;
481 }
482 }
483
484 public void setFileParts(List<FilePart> fileParts) {
485 _fileParts = fileParts;
486 }
487
488 public void setFollowRedirects(boolean followRedirects) {
489 _followRedirects = followRedirects;
490 }
491
492 public void setHead(boolean head) {
493 if (head) {
494 _method = Method.HEAD;
495 }
496 else {
497 _method = Method.GET;
498 }
499 }
500
501 public void setHeaders(Map<String, String> headers) {
502 _headers = headers;
503 }
504
505 public void setLocation(String location) {
506 _location = location;
507 }
508
509 public void setParts(Map<String, String> parts) {
510 _parts = parts;
511 }
512
513 public void setPortletRequest(PortletRequest portletRequest) {
514 _portletRequest = portletRequest;
515 }
516
517 public void setPost(boolean post) {
518 if (post) {
519 _method = Method.POST;
520 }
521 else {
522 _method = Method.GET;
523 }
524 }
525
526 public void setProgressId(String progressId) {
527 _progressId = progressId;
528 }
529
530 public void setPut(boolean put) {
531 if (put) {
532 _method = Method.PUT;
533 }
534 else {
535 _method = Method.GET;
536 }
537 }
538
539 public void setResponse(Response response) {
540 _response = response;
541 }
542
543 private Auth _auth;
544 private Body _body;
545 private Cookie[] _cookies;
546 private List<FilePart> _fileParts;
547 private boolean _followRedirects = true;
548 private Map<String, String> _headers;
549 private String _location;
550 private Method _method = Method.GET;
551 private Map<String, String> _parts;
552 private PortletRequest _portletRequest;
553 private String _progressId;
554 private Response _response = new Response();
555
556 }
557
558 public class Response {
559
560 public void addHeader(String name, String value) {
561 if (_headers == null) {
562 _headers = new HashMap<String, String>();
563 }
564
565 _headers.put(StringUtil.toLowerCase(name), value);
566 }
567
568 public int getContentLength() {
569 return _contentLength;
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 setContentType(String contentType) {
602 _contentType = contentType;
603 }
604
605 public void setHeaders(Map<String, String> headers) {
606 _headers = headers;
607 }
608
609 public void setRedirect(String redirect) {
610 _redirect = redirect;
611 }
612
613 public void setResponseCode(int responseCode) {
614 _responseCode = responseCode;
615 }
616
617 private int _contentLength = -1;
618 private String _contentType;
619 private Map<String, String> _headers;
620 private String _redirect;
621 private int _responseCode = -1;
622
623 }
624
625 }