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
;
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();
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();
49 * @return the {@link Cookie} objects that should be used for authentication
51 Cookie
[] getAuthCookies() {
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
});
65 List
<String
[]> getHeadersForGet() {
66 return getHeadersBase();
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
;
87 byte[] getBodyAsBytes() {
91 String
getBodyAsString() {
92 return EncodingUtil
.getString(responseBody
, responseCharSet
);