App Engine Java SDK version 1.9.14
[gae.git] / java / src / main / com / google / appengine / tools / remoteapi / AppEngineClient.java
blob7963d9054a4aa931388b1152ba0b887320153392
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.util.List;
10 /**
11 * Abstract class that handles making HTTP requests to App Engine using
12 * cookie-based authentication. The actual mechanism by which the HTTP requests
13 * are made is left to subclasses to implement.
15 * <p>This class is thread-safe.</p>
18 abstract class AppEngineClient extends BaseRemoteApiClient {
19 private final String userEmail;
20 private final Cookie[] authCookies;
21 private final int maxResponseSize;
23 AppEngineClient(RemoteApiOptions options,
24 List<Cookie> authCookies, String appId) {
25 super(options, appId);
26 if (options == null) {
27 throw new IllegalArgumentException("options not set");
29 if (authCookies == null) {
30 throw new IllegalArgumentException("authCookies not set");
32 this.userEmail = options.getUserEmail();
33 this.authCookies = authCookies.toArray(new Cookie[0]);
34 this.maxResponseSize = options.getMaxHttpResponseSize();
37 @Override
38 public String serializeCredentials() {
39 StringBuilder out = new StringBuilder();
40 out.append("host=" + getHostname() + "\n");
41 out.append("email=" + userEmail + "\n");
42 for (Cookie cookie : authCookies) {
43 out.append("cookie=" + cookie.getName() + "=" + cookie.getValue() + "\n");
45 return out.toString();
48 /**
49 * @return the {@link Cookie} objects that should be used for authentication
51 Cookie[] getAuthCookies() {
52 return authCookies;
55 int getMaxResponseSize() {
56 return maxResponseSize;
59 List<String[]> getHeadersForPost(String mimeType) {
60 List<String[]> headers = getHeadersBase();
61 headers.add(new String[]{"Content-type", mimeType});
62 return headers;
65 List<String[]> getHeadersForGet() {
66 return getHeadersBase();
69 /**
70 * Simple class representing an HTTP response.
72 static class Response {
73 private final int statusCode;
74 private final byte[] responseBody;
75 private final String responseCharSet;
77 Response(int statusCode, byte[] responseBody, String responseCharSet) {
78 this.statusCode = statusCode;
79 this.responseBody = responseBody;
80 this.responseCharSet = responseCharSet;
83 int getStatusCode() {
84 return statusCode;
87 byte[] getBodyAsBytes() {
88 return responseBody;
91 String getBodyAsString() {
92 return EncodingUtil.getString(responseBody, responseCharSet);