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