1
14
15 package com.liferay.portal.kernel.util;
16
17 import java.io.IOException;
18
19 import java.net.URL;
20
21 import java.util.HashMap;
22 import java.util.Map;
23
24 import javax.portlet.ActionRequest;
25 import javax.portlet.RenderRequest;
26
27 import javax.servlet.http.Cookie;
28 import javax.servlet.http.HttpServletRequest;
29
30
35 public interface Http {
36
37 public static final String HTTP = "http";
38
39 public static final int HTTP_PORT = 80;
40
41 public static final String HTTP_WITH_SLASH = "http://";
42
43 public static final String HTTPS = "https";
44
45 public static final int HTTPS_PORT = 443;
46
47 public static final String HTTPS_WITH_SLASH = "https://";
48
49 public static final String PROTOCOL_DELIMITER = "://";
50
51 public String addParameter(String url, String name, boolean value);
52
53 public String addParameter(String url, String name, double value);
54
55 public String addParameter(String url, String name, int value);
56
57 public String addParameter(String url, String name, long value);
58
59 public String addParameter(String url, String name, short value);
60
61 public String addParameter(String url, String name, String value);
62
63 public String decodeURL(String url);
64
65 public String decodeURL(String url, boolean unescapeSpace);
66
67 public String encodeURL(String url);
68
69 public String encodeURL(String url, boolean escapeSpaces);
70
71 public String getCompleteURL(HttpServletRequest request);
72
73 public Cookie[] getCookies();
74
75 public String getDomain(String url);
76
77 public String getIpAddress(String url);
78
79 public String getParameter(String url, String name);
80
81 public String getParameter(String url, String name, boolean escaped);
82
83 public Map<String, String[]> getParameterMap(String queryString);
84
85 public String getProtocol(ActionRequest actionRequest);
86
87 public String getProtocol(boolean secure);
88
89 public String getProtocol(HttpServletRequest request);
90
91 public String getProtocol(RenderRequest renderRequest);
92
93 public String getProtocol(String url);
94
95 public String getQueryString(String url);
96
97 public String getRequestURL(HttpServletRequest request);
98
99 public boolean hasDomain(String url);
100
101 public boolean hasProtocol(String url);
102
103 public boolean hasProxyConfig();
104
105 public boolean isNonProxyHost(String host);
106
107 public boolean isProxyHost(String host);
108
109 public Map<String, String[]> parameterMapFromString(String queryString);
110
111 public String parameterMapToString(Map<String, String[]> parameterMap);
112
113 public String parameterMapToString(
114 Map<String, String[]> parameterMap, boolean addQuestion);
115
116 public String protocolize(String url, ActionRequest actionRequest);
117
118 public String protocolize(String url, boolean secure);
119
120 public String protocolize(String url, HttpServletRequest request);
121
122 public String protocolize(String url, RenderRequest renderRequest);
123
124 public String removeDomain(String url);
125
126 public String removeParameter(String url, String name);
127
128 public String removeProtocol(String url);
129
130 public String setParameter(String url, String name, boolean value);
131
132 public String setParameter(String url, String name, double value);
133
134 public String setParameter(String url, String name, int value);
135
136 public String setParameter(String url, String name, long value);
137
138 public String setParameter(String url, String name, short value);
139
140 public String setParameter(String url, String name, String value);
141
142 public byte[] URLtoByteArray(Http.Options options) throws IOException;
143
144 public byte[] URLtoByteArray(String location) throws IOException;
145
146 public byte[] URLtoByteArray(String location, boolean post)
147 throws IOException;
148
149 public String URLtoString(Http.Options options) throws IOException;
150
151 public String URLtoString(String location) throws IOException;
152
153 public String URLtoString(String location, boolean post) throws IOException;
154
155
164 public String URLtoString(URL url) throws IOException;
165
166 public class Auth {
167
168 public Auth(
169 String host, int port, String realm, String username,
170 String password) {
171
172 _host = host;
173 _port = port;
174 _realm = realm;
175 _username = username;
176 _password = password;
177 }
178
179 public String getHost() {
180 return _host;
181 }
182
183 public String getPassword() {
184 return _password;
185 }
186
187 public int getPort() {
188 return _port;
189 }
190
191 public String getRealm() {
192 return _realm;
193 }
194
195 public String getUsername() {
196 return _username;
197 }
198
199 private String _host;
200 private String _password;
201 private int _port;
202 private String _realm;
203 private String _username;
204
205 }
206
207 public class Body {
208
209 public Body(String content, String contentType, String charset) {
210 _content = content;
211 _contentType = contentType;
212 _charset = charset;
213 }
214
215 public String getCharset() {
216 return _charset;
217 }
218
219 public String getContent() {
220 return _content;
221 }
222
223 public String getContentType() {
224 return _contentType;
225 }
226
227 private String _charset;
228 private String _content;
229 private String _contentType;
230
231 }
232
233 public enum Method {
234
235 DELETE, GET, HEAD, POST, PUT
236
237 }
238
239 public class Options {
240
241 public void addHeader(String name, String value) {
242 if (_headers == null) {
243 _headers = new HashMap<String, String>();
244 }
245
246 _headers.put(name, value);
247 }
248
249 public void addPart(String name, String value) {
250 if (_body != null) {
251 throw new IllegalArgumentException(
252 "Part cannot be added because a body has already been set");
253 }
254
255 if (_parts == null) {
256 _parts = new HashMap<String, String>();
257 }
258
259 _parts.put(name, value);
260 }
261
262 public Auth getAuth() {
263 return _auth;
264 }
265
266 public Body getBody() {
267 return _body;
268 }
269
270 public Cookie[] getCookies() {
271 return _cookies;
272 }
273
274 public Map<String, String> getHeaders() {
275 return _headers;
276 }
277
278 public String getLocation() {
279 return _location;
280 }
281
282 public Method getMethod() {
283 return _method;
284 }
285
286 public Map<String, String> getParts() {
287 return _parts;
288 }
289
290 public Response getResponse() {
291 return _response;
292 }
293
294 public boolean isDelete() {
295 if (_method == Method.DELETE) {
296 return true;
297 }
298 else {
299 return false;
300 }
301 }
302
303 public boolean isFollowRedirects() {
304 return _followRedirects;
305 }
306
307 public boolean isGet() {
308 if (_method == Method.GET) {
309 return true;
310 }
311 else {
312 return false;
313 }
314 }
315
316 public boolean isHead() {
317 if (_method == Method.HEAD) {
318 return true;
319 }
320 else {
321 return false;
322 }
323 }
324
325 public boolean isPost() {
326 if (_method == Method.POST) {
327 return true;
328 }
329 else {
330 return false;
331 }
332 }
333
334 public boolean isPut() {
335 if (_method == Method.PUT) {
336 return true;
337 }
338 else {
339 return false;
340 }
341 }
342
343 public void setAuth(Http.Auth auth) {
344 setAuth(
345 auth.getHost(), auth.getPort(), auth.getRealm(),
346 auth.getUsername(), auth.getPassword());
347 }
348
349 public void setAuth(
350 String host, int port, String realm, String username,
351 String password) {
352
353 _auth = new Auth(host, port, realm, username, password);
354 }
355
356 public void setBody(Http.Body body) {
357 setBody(
358 body.getContent(), body.getContentType(), body.getCharset());
359 }
360
361 public void setBody(
362 String content, String contentType, String charset) {
363
364 if (_parts != null) {
365 throw new IllegalArgumentException(
366 "Body cannot be set because a part has already been added");
367 }
368
369 _body = new Body(content, contentType, charset);
370 }
371
372 public void setCookies(Cookie[] cookies) {
373 _cookies = cookies;
374 }
375
376 public void setDelete(boolean delete) {
377 if (delete) {
378 _method = Method.DELETE;
379 }
380 else {
381 _method = Method.GET;
382 }
383 }
384
385 public void setFollowRedirects(boolean followRedirects) {
386 _followRedirects = followRedirects;
387 }
388
389 public void setHead(boolean head) {
390 if (head) {
391 _method = Method.HEAD;
392 }
393 else {
394 _method = Method.GET;
395 }
396 }
397
398 public void setHeaders(Map<String, String> headers) {
399 _headers = headers;
400 }
401
402 public void setLocation(String location) {
403 _location = location;
404 }
405
406 public void setParts(Map<String, String> parts) {
407 _parts = parts;
408 }
409
410 public void setPost(boolean post) {
411 if (post) {
412 _method = Method.POST;
413 }
414 else {
415 _method = Method.GET;
416 }
417 }
418
419 public void setPut(boolean put) {
420 if (put) {
421 _method = Method.PUT;
422 }
423 else {
424 _method = Method.GET;
425 }
426 }
427
428 public void setResponse(Response response) {
429 _response = response;
430 }
431
432 private Auth _auth;
433 private Body _body;
434 private Cookie[] _cookies;
435 private boolean _followRedirects = true;
436 private Map<String, String> _headers;
437 private String _location;
438 private Method _method = Method.GET;
439 private Map<String, String> _parts;
440 private Response _response = new Response();
441
442 }
443
444 public class Response {
445
446 public void addHeader(String name, String value) {
447 if (_headers == null) {
448 _headers = new HashMap<String, String>();
449 }
450
451 _headers.put(name, value);
452 }
453
454 public int getContentLength() {
455 return _contentLength;
456 }
457
458 public String getContentType() {
459 return _contentType;
460 }
461
462 public String getHeader(String name) {
463 if (_headers == null) {
464 return null;
465 }
466 else {
467 return _headers.get(name);
468 }
469 }
470
471 public Map<String, String> getHeaders() {
472 return _headers;
473 }
474
475 public String getRedirect() {
476 return _redirect;
477 }
478
479 public void setContentLength(int contentLength) {
480 _contentLength = contentLength;
481 }
482
483 public void setContentType(String contentType) {
484 _contentType = contentType;
485 }
486
487 public void setHeaders(Map<String, String> headers) {
488 _headers = headers;
489 }
490
491 public void setRedirect(String redirect) {
492 _redirect = redirect;
493 }
494
495 private int _contentLength = -1;
496 private String _contentType;
497 private Map<String, String> _headers;
498 private String _redirect;
499
500 }
501
502 }