App Engine Python SDK version 1.7.4 (2)
[gae.git] / java / src / main / com / google / appengine / api / urlfetch / HTTPResponse.java
blob25945c7de592c8e78c2023b308f25e0c52cde16e
1 // Copyright 2007 Google Inc. All rights reserved.
3 package com.google.appengine.api.urlfetch;
5 import java.net.URL;
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;
11 import java.util.Map;
13 /**
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;
25 private URL finalUrl;
27 HTTPResponse(int responseCode) {
28 this.responseCode = responseCode;
29 this.headers = new ArrayList<HTTPHeader>();
30 this.combinedHeadersMap = new HashMap<String, String>();
31 this.finalUrl = null;
34 /**
35 * Returns the HTTP response code from the request (e.g. 200, 500,
36 * etc.).
38 public int getResponseCode() {
39 return responseCode;
42 /**
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() {
47 return content;
50 /**
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
53 * this will be null.
55 public URL getFinalUrl() {
56 return finalUrl;
59 /**
60 * Returns a {@code List} of HTTP response headers that were
61 * returned by the remote server. These are not combined for
62 * repeated values.
64 public List<HTTPHeader> getHeadersUncombined() {
65 return Collections.unmodifiableList(headers);
68 /**
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
72 * values.
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);
87 } else {
88 combinedHeadersMap.put(name, combinedValue + ", " + value);
92 void setContent(byte[] content) {
93 this.content = content;
96 void setFinalUrl(URL finalUrl) {
97 this.finalUrl = finalUrl;