Left: | ||
Right: |
OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2010 Google Inc. | 2 * Copyright (c) 2010 Google Inc. |
3 * | 3 * |
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not u se this file except | 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not u se this file except |
5 * in compliance with the License. You may obtain a copy of the License at | 5 * in compliance with the License. You may obtain a copy of the License at |
6 * | 6 * |
7 * http://www.apache.org/licenses/LICENSE-2.0 | 7 * http://www.apache.org/licenses/LICENSE-2.0 |
8 * | 8 * |
9 * Unless required by applicable law or agreed to in writing, software distribut ed under the License | 9 * Unless required by applicable law or agreed to in writing, software distribut ed under the License |
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY K IND, either express | 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY K IND, either express |
(...skipping 22 matching lines...) Expand all Loading... | |
33 import java.util.HashMap; | 33 import java.util.HashMap; |
34 import java.util.List; | 34 import java.util.List; |
35 import java.util.logging.Level; | 35 import java.util.logging.Level; |
36 import java.util.logging.Logger; | 36 import java.util.logging.Logger; |
37 import java.util.zip.GZIPInputStream; | 37 import java.util.zip.GZIPInputStream; |
38 | 38 |
39 /** | 39 /** |
40 * HTTP response. | 40 * HTTP response. |
41 * | 41 * |
42 * <p> | 42 * <p> |
43 * Callers should call {@link #disconnect} when the HTTP response object is no l onger needed. Also, | |
yanivi
2012/05/22 13:01:19
Good, but please give an example of calling discon
yanivi
2012/05/22 13:01:19
We should really test this out with both NetHttpTr
rmistry
2012/05/22 14:37:55
Done.
| |
44 * consider modifying the alive property on {@link HttpTransport} to control whe ther the socket | |
yanivi
2012/05/22 13:01:19
There is no alive property on HttpTransport. This
rmistry
2012/05/22 14:37:55
Done.
| |
45 * should be returned to a pool of connected sockets. | |
46 * </p> | |
47 * | |
48 * <p> | |
43 * Implementation is not thread-safe. | 49 * Implementation is not thread-safe. |
44 * </p> | 50 * </p> |
45 * | 51 * |
46 * @since 1.0 | 52 * @since 1.0 |
47 * @author Yaniv Inbar | 53 * @author Yaniv Inbar |
48 */ | 54 */ |
49 public final class HttpResponse { | 55 public final class HttpResponse { |
50 | 56 |
51 /** HTTP response content or {@code null} before {@link #getContent()}. */ | 57 /** HTTP response content or {@code null} before {@link #getContent()}. */ |
52 private InputStream content; | 58 private InputStream content; |
53 | 59 |
54 /** Content encoding or {@code null}. */ | 60 /** Content encoding or {@code null}. */ |
55 private final String contentEncoding; | 61 private final String contentEncoding; |
56 | 62 |
57 /** | 63 /** |
58 * Content length or less than zero if not known. May be reset by {@link #getC ontent} if response | 64 * Content length or less than zero if not known. May be reset by {@link #getC ontent} if response |
59 * had GZip compression. | 65 * had GZip compression. |
60 */ | 66 */ |
61 private long contentLength; | 67 private long contentLength; |
62 | 68 |
63 /** Content type or {@code null} for none. */ | 69 /** Content type or {@code null} for none. */ |
64 private final String contentType; | 70 private final String contentType; |
65 | 71 |
66 /** HTTP headers. */ | 72 /** HTTP headers. */ |
67 private final HttpHeaders headers; | 73 private final HttpHeaders headers; |
68 | 74 |
69 /** Low-level HTTP response. */ | 75 /** Low-level HTTP response. */ |
70 private LowLevelHttpResponse response; | 76 LowLevelHttpResponse response; |
71 | 77 |
72 /** Status code. */ | 78 /** Status code. */ |
73 private final int statusCode; | 79 private final int statusCode; |
74 | 80 |
75 /** Status message or {@code null}. */ | 81 /** Status message or {@code null}. */ |
76 private final String statusMessage; | 82 private final String statusMessage; |
77 | 83 |
78 /** HTTP transport. */ | 84 /** HTTP transport. */ |
79 private final HttpTransport transport; | 85 private final HttpTransport transport; |
80 | 86 |
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
376 | 382 |
377 /** | 383 /** |
378 * Returns the content of the HTTP response. | 384 * Returns the content of the HTTP response. |
379 * <p> | 385 * <p> |
380 * The result is cached, so subsequent calls will be fast. | 386 * The result is cached, so subsequent calls will be fast. |
381 * | 387 * |
382 * @return input stream content of the HTTP response or {@code null} for none | 388 * @return input stream content of the HTTP response or {@code null} for none |
383 * @throws IOException I/O exception | 389 * @throws IOException I/O exception |
384 */ | 390 */ |
385 public InputStream getContent() throws IOException { | 391 public InputStream getContent() throws IOException { |
386 LowLevelHttpResponse response = this.response; | 392 if (content != null) { |
yanivi
2012/05/22 13:01:19
Can't do that because response.getContent() may be
rmistry
2012/05/22 14:37:55
Done.
| |
387 if (response == null) { | |
388 return content; | 393 return content; |
389 } | 394 } |
390 InputStream content = this.response.getContent(); | 395 InputStream content = this.response.getContent(); |
391 this.response = null; | |
392 if (content != null) { | 396 if (content != null) { |
393 // gzip encoding (wrap content with GZipInputStream) | 397 // gzip encoding (wrap content with GZipInputStream) |
394 String contentEncoding = this.contentEncoding; | 398 String contentEncoding = this.contentEncoding; |
395 if (contentEncoding != null && contentEncoding.contains("gzip")) { | 399 if (contentEncoding != null && contentEncoding.contains("gzip")) { |
396 content = new GZIPInputStream(content); | 400 content = new GZIPInputStream(content); |
397 contentLength = -1; | 401 contentLength = -1; |
398 } | 402 } |
399 // logging (wrap content with LoggingInputStream) | 403 // logging (wrap content with LoggingInputStream) |
400 Logger logger = HttpTransport.LOGGER; | 404 Logger logger = HttpTransport.LOGGER; |
401 if (loggingEnabled && logger.isLoggable(Level.CONFIG)) { | 405 if (loggingEnabled && logger.isLoggable(Level.CONFIG)) { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
446 * Closes the content of the HTTP response from {@link #getContent()}, ignorin g any content. | 450 * Closes the content of the HTTP response from {@link #getContent()}, ignorin g any content. |
447 */ | 451 */ |
448 public void ignore() throws IOException { | 452 public void ignore() throws IOException { |
449 InputStream content = getContent(); | 453 InputStream content = getContent(); |
450 if (content != null) { | 454 if (content != null) { |
451 content.close(); | 455 content.close(); |
452 } | 456 } |
453 } | 457 } |
454 | 458 |
455 /** | 459 /** |
456 * Disconnect using {@link LowLevelHttpResponse#disconnect()}. | 460 * Close the HTTP response content and disconnect using {@link LowLevelHttpRes ponse#disconnect()}. |
461 * | |
462 * <p> | |
463 * Upgrade warning: since version 1.10 {@link #disconnect} now closes the HTTP response content | |
464 * input stream | |
yanivi
2012/05/22 13:01:19
missing period.
also, make it a bit more clear th
rmistry
2012/05/22 14:37:55
Done.
| |
465 * </p> | |
457 * | 466 * |
458 * @since 1.4 | 467 * @since 1.4 |
459 */ | 468 */ |
460 public void disconnect() throws IOException { | 469 public void disconnect() throws IOException { |
470 ignore(); | |
461 response.disconnect(); | 471 response.disconnect(); |
yanivi
2012/05/22 13:01:19
[aside] Wow, thanks for fixing this NullPointerExc
| |
462 } | 472 } |
463 | 473 |
464 /** | 474 /** |
465 * Returns the HTTP response content parser to use for the content type of thi s HTTP response or | 475 * Returns the HTTP response content parser to use for the content type of thi s HTTP response or |
466 * {@code null} for none. | 476 * {@code null} for none. |
467 */ | 477 */ |
468 public HttpParser getParser() { | 478 public HttpParser getParser() { |
469 return request.getParser(contentType); | 479 return request.getParser(contentType); |
470 } | 480 } |
471 | 481 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
517 } | 527 } |
518 System.arraycopy(tmp, 0, buffer, length, bytesRead); | 528 System.arraycopy(tmp, 0, buffer, length, bytesRead); |
519 length += bytesRead; | 529 length += bytesRead; |
520 } | 530 } |
521 return new String(buffer, 0, length, "UTF-8"); | 531 return new String(buffer, 0, length, "UTF-8"); |
522 } finally { | 532 } finally { |
523 content.close(); | 533 content.close(); |
524 } | 534 } |
525 } | 535 } |
526 } | 536 } |
OLD | NEW |