1 // Copyright 2011 Google. All Rights Reserved.
2 package com
.google
.appengine
.tools
.remoteapi
;
4 import org
.apache
.commons
.httpclient
.Cookie
;
5 import org
.apache
.commons
.httpclient
.HttpClient
;
6 import org
.apache
.commons
.httpclient
.HttpMethodBase
;
7 import org
.apache
.commons
.httpclient
.MultiThreadedHttpConnectionManager
;
8 import org
.apache
.commons
.httpclient
.methods
.ByteArrayRequestEntity
;
9 import org
.apache
.commons
.httpclient
.methods
.GetMethod
;
10 import org
.apache
.commons
.httpclient
.methods
.PostMethod
;
12 import java
.io
.IOException
;
13 import java
.util
.List
;
16 * An {@link AppEngineClient} implementation that uses apache's
17 * {@link HttpClient}. This implementation must be used when the client is
18 * not an App Engine container, since it cannot not rely on the availability of
19 * the local urlfetch service.
22 class StandaloneAppEngineClient
extends AppEngineClient
{
23 private final HttpClient httpClient
;
25 StandaloneAppEngineClient(RemoteApiOptions options
, List
<Cookie
> authCookies
, String appId
) {
26 super(options
, authCookies
, appId
);
27 HttpClient httpClient
= new HttpClient(new MultiThreadedHttpConnectionManager());
28 httpClient
.getState().addCookies(getAuthCookies());
29 this.httpClient
= httpClient
;
33 public Response
get(String path
) throws IOException
{
34 return createResponse(doGet(path
));
37 private HttpMethodBase
doGet(String path
) throws IOException
{
38 GetMethod method
= new GetMethod(makeUrl(path
));
40 method
.setFollowRedirects(false);
41 addHeaders(method
, getHeadersForGet());
42 httpClient
.executeMethod(method
);
47 public Response
post(String path
, String mimeType
, byte[] body
) throws IOException
{
48 return createResponse(doPost(path
, mimeType
, body
));
51 private HttpMethodBase
doPost(String path
, String mimeType
, byte[] body
) throws IOException
{
52 PostMethod post
= new PostMethod(makeUrl(path
));
54 post
.setFollowRedirects(false);
55 addHeaders(post
, getHeadersForPost(mimeType
));
56 post
.setRequestEntity(new ByteArrayRequestEntity(body
));
57 httpClient
.executeMethod(post
);
62 public LegacyResponse
legacyGet(String path
) throws IOException
{
63 return createLegacyResponse(doGet(path
));
67 public LegacyResponse
legacyPost(String path
, String mimeType
, byte[] body
)
69 return createLegacyResponse(doPost(path
, mimeType
, body
));
72 private void addHeaders(HttpMethodBase method
, List
<String
[]> headers
) {
73 for (String
[] headerPair
: headers
) {
74 method
.addRequestHeader(headerPair
[0], headerPair
[1]);
78 private Response
createResponse(HttpMethodBase method
) throws IOException
{
79 byte[] body
= method
.getResponseBody(getMaxResponseSize());
80 return new Response(method
.getStatusCode(),
81 body
, method
.getResponseCharSet());
84 private LegacyResponse
createLegacyResponse(HttpMethodBase method
) throws IOException
{
85 byte[] body
= method
.getResponseBody(getMaxResponseSize());
86 return new LegacyResponse(method
.getStatusCode(),
87 body
, method
.getResponseCharSet());