Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / tools / remoteapi / HostedAppEngineClient.java
blobefa9143baea479d89eafeae20f70598d03aa7150
1 // Copyright 2011 Google. All Rights Reserved.
2 package com.google.appengine.tools.remoteapi;
4 import com.google.appengine.api.urlfetch.HTTPHeader;
5 import com.google.appengine.api.urlfetch.HTTPMethod;
6 import com.google.appengine.api.urlfetch.HTTPRequest;
7 import com.google.appengine.api.urlfetch.HTTPResponse;
8 import com.google.appengine.api.urlfetch.URLFetchService;
9 import com.google.appengine.api.urlfetch.URLFetchServiceFactory;
11 import org.apache.commons.httpclient.Cookie;
12 import org.apache.commons.httpclient.Header;
13 import org.apache.commons.httpclient.HttpMethodBase;
15 import java.io.IOException;
16 import java.net.URL;
17 import java.util.List;
19 /**
20 * An {@link AppEngineClient} implementation that uses {@link URLFetchService}.
21 * This implementation must be used when the client is an App Engine container
22 * since URLFetchService is the only way to make HTTP requests in this
23 * environment.
26 class HostedAppEngineClient extends AppEngineClient {
28 private final URLFetchService urlFetch = URLFetchServiceFactory.getURLFetchService();
30 HostedAppEngineClient(RemoteApiOptions options, List<Cookie> authCookies,
31 String appId) {
32 super(options, authCookies, appId);
35 private void addCookies(HTTPRequest req) {
36 for (Cookie cookie : getAuthCookies()) {
37 req.addHeader(
38 new HTTPHeader("Cookie", String.format("%s=%s", cookie.getName(), cookie.getValue())));
42 @Override
43 public Response get(String path) throws IOException {
44 return createResponse(doGet(path));
47 private HTTPResponse doGet(String path) throws IOException {
48 HTTPRequest req = new HTTPRequest(new URL(makeUrl(path)), HTTPMethod.GET);
49 req.getFetchOptions().doNotFollowRedirects();
50 for (String[] headerPair : getHeadersForGet()) {
51 req.addHeader(new HTTPHeader(headerPair[0], headerPair[1]));
53 addCookies(req);
54 return urlFetch.fetch(req);
57 @Override
58 public Response post(String path, String mimeType, byte[] body) throws IOException {
59 return createResponse(doPost(path, mimeType, body));
62 private HTTPResponse doPost(String path, String mimeType, byte[] body)
63 throws IOException {
64 HTTPRequest req = new HTTPRequest(new URL(makeUrl(path)), HTTPMethod.POST);
65 req.getFetchOptions().doNotFollowRedirects();
66 for (String[] headerPair : getHeadersForPost(mimeType)) {
67 req.addHeader(new HTTPHeader(headerPair[0], headerPair[1]));
69 addCookies(req);
70 req.setPayload(body);
71 return urlFetch.fetch(req);
74 @Override
75 public LegacyResponse legacyGet(String path) throws IOException {
76 return createLegacyResponse(doGet(path));
79 @Override
80 public LegacyResponse legacyPost(String path, String mimeType, byte[] body)
81 throws IOException {
82 return createLegacyResponse(doPost(path, mimeType, body));
85 /**
86 * A small hack to get access to
87 * {@link HttpMethodBase#getContentCharSet(Header)} (which really ought to be
88 * static) so that we don't need to write code to extract the charset out of
89 * the Content-Type header.
91 private static class DummyMethod extends HttpMethodBase {
92 private static final DummyMethod INSTANCE = new DummyMethod();
93 static String getCharset(HTTPHeader header) {
94 Header apacheHeader = new Header(header.getName(), header.getValue());
95 return INSTANCE.getContentCharSet(apacheHeader);
98 @Override
99 public String getName() {
100 return "dummy";
104 static Response createResponse(HTTPResponse resp) {
105 return new Response(resp.getResponseCode(), resp.getContent(), getCharset(resp));
108 static LegacyResponse createLegacyResponse(HTTPResponse resp) {
109 return new LegacyResponse(resp.getResponseCode(), resp.getContent(), getCharset(resp));
112 static String getCharset(HTTPResponse resp) {
113 String charset = null;
114 for (HTTPHeader header : resp.getHeaders()) {
115 if (header.getName().toLowerCase().equals("content-type")) {
116 charset = DummyMethod.getCharset(header);
117 break;
120 return charset;