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