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