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