App Engine Java SDK version 1.7.0
[gae.git] / java / src / main / com / google / appengine / api / urlfetch / HTTPRequest.java
bloba9e3bcd3f22591cebec98854e46a9df7a5670907
1 // Copyright 2007 Google Inc. All rights reserved.
3 package com.google.appengine.api.urlfetch;
5 import java.io.Serializable;
6 import java.net.URL;
7 import java.util.ArrayList;
8 import java.util.Collections;
9 import java.util.LinkedHashMap;
10 import java.util.List;
12 /**
13 * {@code HTTPRequest} encapsulates a single HTTP request that is made
14 * via the {@code URLFetchService}.
17 public class HTTPRequest implements Serializable {
18 private final HTTPMethod method;
19 private final URL url;
20 private final LinkedHashMap<String, HTTPHeader> headers;
21 private final FetchOptions fetchOptions;
23 /**
24 * The payload of this request, or null if there is no payload
25 * present.
27 private byte[] payload = null;
29 /**
30 * Creates a {@code HTTPRequest} that represents a GET request to
31 * the specified URL.
33 public HTTPRequest(URL url) {
34 this(url, HTTPMethod.GET);
37 /**
38 * Creates a {@code HTTPRequest} that represents an HTTP request to
39 * the specified URL with the specified HTTP method (GET, POST, etc).
41 public HTTPRequest(URL url, HTTPMethod method) {
42 this(url, method, FetchOptions.Builder.withDefaults());
45 /**
46 * Creates a {@code HTTPRequest} that represents an HTTP request to
47 * the specified URL with the specified HTTP method (GET, POST, etc)
48 * and the specified {@link FetchOptions}.
50 public HTTPRequest(URL url, HTTPMethod method, FetchOptions fetchOptions) {
51 this.url = url;
52 this.method = method;
53 this.fetchOptions = fetchOptions;
54 this.headers = new LinkedHashMap<String, HTTPHeader>();
57 /**
58 * Gets the HTTP method for this request (GET, POST, etc).
60 public HTTPMethod getMethod() {
61 return method;
64 /**
65 * Gets the URL for this request.
67 public URL getURL() {
68 return url;
71 /**
72 * Gets the payload (such as POST body) for this request. Certain HTTP
73 * methods (e.g. GET) will not have any payload, and this method
74 * will return null.
76 public byte[] getPayload() {
77 return payload;
80 /**
81 * Sets the payload for this request. This method should not be
82 * called for certain HTTP methods (e.g. GET).
84 public void setPayload(byte[] payload) {
85 this.payload = payload;
88 /**
89 * Adds {@code header} to this request. If an {@code HTTPHeader} with
90 * the same {@code name} already exists for this request, it's values
91 * are merged with {@code header}.
93 * @param header a not {@code null} {@code HTTPHeader}
95 public void addHeader(HTTPHeader header) {
96 String name = header.getName();
97 HTTPHeader newHeader = headers.get(name);
98 if (newHeader == null) {
99 headers.put(name, new HTTPHeader(name, header.getValue()));
100 } else {
101 headers.put(name, new HTTPHeader(name, newHeader.getValue() + ", " + header.getValue()));
106 * Sets an {@code HTTPHeader} for this request. If an
107 * {@link HTTPHeader} with the same {@code name}
108 * already exists, its value is replaced.
110 public void setHeader(HTTPHeader header) {
111 headers.put(header.getName(), header);
115 * Returns an immutable {@code List} of {@code HTTPHeader} objects
116 * that have been added to this request.
118 public List<HTTPHeader> getHeaders() {
119 return Collections.unmodifiableList(new ArrayList<HTTPHeader>(headers.values()));
123 * Get the fetch options for this request.
125 public FetchOptions getFetchOptions() {
126 return fetchOptions;