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