1 // Copyright 2007 Google Inc. All rights reserved.
3 package com
.google
.appengine
.api
.urlfetch
;
6 import java
.io
.Serializable
;
7 import java
.util
.ArrayList
;
8 import java
.util
.Collections
;
9 import java
.util
.HashMap
;
10 import java
.util
.List
;
14 * {@code HTTPResponse} encapsulates the results of a {@code
15 * HTTPRequest} made via the {@code URLFetchService}.
18 public class HTTPResponse
implements Serializable
{
19 static final long serialVersionUID
= -4270789523885950851L;
21 private final int responseCode
;
22 private final List
<HTTPHeader
> headers
;
23 private final HashMap
<String
, String
> combinedHeadersMap
;
24 private byte[] content
;
27 HTTPResponse(int responseCode
) {
28 this.responseCode
= responseCode
;
29 this.headers
= new ArrayList
<HTTPHeader
>();
30 this.combinedHeadersMap
= new HashMap
<String
, String
>();
35 * Returns the HTTP response code from the request (e.g. 200, 500,
38 public int getResponseCode() {
43 * Returns the content of the request, or null if there is no
44 * content present (e.g. in a HEAD request).
46 public byte[] getContent() {
51 * Returns the final URL the content came from if redirects were followed
52 * automatically in the request, if different than the input URL; otherwise
55 public URL
getFinalUrl() {
60 * Returns a {@code List} of HTTP response headers that were
61 * returned by the remote server. These are not combined for
64 public List
<HTTPHeader
> getHeadersUncombined() {
65 return Collections
.unmodifiableList(headers
);
69 * Returns a {@code List} of HTTP response headers that were
70 * returned by the remote server. Multi-valued headers are
71 * represented as a single {@code HTTPHeader} with comma-separated
74 public List
<HTTPHeader
> getHeaders() {
75 ArrayList
<HTTPHeader
> combinedHeaders
= new ArrayList
<HTTPHeader
>();
76 for (Map
.Entry
<String
, String
> entry
: combinedHeadersMap
.entrySet()) {
77 combinedHeaders
.add(new HTTPHeader(entry
.getKey(), entry
.getValue()));
79 return Collections
.unmodifiableList(combinedHeaders
);
82 void addHeader(String name
, String value
) {
83 headers
.add(new HTTPHeader(name
, value
));
84 String combinedValue
= combinedHeadersMap
.get(name
);
85 if (combinedValue
== null) {
86 combinedHeadersMap
.put(name
, value
);
88 combinedHeadersMap
.put(name
, combinedValue
+ ", " + value
);
92 void setContent(byte[] content
) {
93 this.content
= content
;
96 void setFinalUrl(URL finalUrl
) {
97 this.finalUrl
= finalUrl
;