Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / tools / remoteapi / AppEngineClient.java
blobdbaffac93c348945faaa94590624ce0d81b6b8c1
1 // Copyright 2010 Google Inc. All Rights Reserved.
3 package com.google.appengine.tools.remoteapi;
5 import org.apache.commons.httpclient.Cookie;
6 import org.apache.commons.httpclient.util.EncodingUtil;
8 import java.io.IOException;
9 import java.util.List;
11 /**
12 * Abstract class that handles making HTTP requests to App Engine using
13 * cookie-based authentication. The actual mechanism by which the HTTP requests
14 * are made is left to subclasses to implement.
16 * <p>This class is thread-safe.</p>
19 abstract class AppEngineClient extends BaseRemoteApiClient {
20 private final String userEmail;
21 private final Cookie[] authCookies;
22 private final int maxResponseSize;
24 AppEngineClient(RemoteApiOptions options,
25 List<Cookie> authCookies, String appId) {
26 super(options, appId);
27 if (options == null) {
28 throw new IllegalArgumentException("options not set");
30 if (authCookies == null) {
31 throw new IllegalArgumentException("authCookies not set");
33 this.userEmail = options.getUserEmail();
34 this.authCookies = authCookies.toArray(new Cookie[0]);
35 this.maxResponseSize = options.getMaxHttpResponseSize();
38 @Override
39 public String serializeCredentials() {
40 StringBuilder out = new StringBuilder();
41 out.append("host=" + getHostname() + "\n");
42 out.append("email=" + userEmail + "\n");
43 for (Cookie cookie : authCookies) {
44 out.append("cookie=" + cookie.getName() + "=" + cookie.getValue() + "\n");
46 return out.toString();
49 /**
50 * @return the {@link Cookie} objects that should be used for authentication
52 Cookie[] getAuthCookies() {
53 return authCookies;
56 int getMaxResponseSize() {
57 return maxResponseSize;
60 List<String[]> getHeadersForPost(String mimeType) {
61 List<String[]> headers = getHeadersBase();
62 headers.add(new String[]{"Content-type", mimeType});
63 return headers;
66 List<String[]> getHeadersForGet() {
67 return getHeadersBase();
70 /**
71 * Simple class representing an HTTP response.
73 static class Response {
74 private final int statusCode;
75 private final byte[] responseBody;
76 private final String responseCharSet;
78 Response(int statusCode, byte[] responseBody, String responseCharSet) {
79 this.statusCode = statusCode;
80 this.responseBody = responseBody;
81 this.responseCharSet = responseCharSet;
84 int getStatusCode() {
85 return statusCode;
88 byte[] getBodyAsBytes() {
89 return responseBody;
92 String getBodyAsString() {
93 return EncodingUtil.getString(responseBody, responseCharSet);
97 abstract LegacyResponse legacyPost(String path, String mimeType, byte[] body) throws IOException;
98 abstract LegacyResponse legacyGet(String path) throws IOException;