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 Response
get(String path
) throws IOException
{
34 GetMethod method
= new GetMethod(makeUrl(path
));
36 method
.setFollowRedirects(false);
37 addHeaders(method
, getHeadersForGet());
38 httpClient
.executeMethod(method
);
39 return createResponse(method
);
43 Response
post(String path
, String mimeType
, byte[] body
)
45 PostMethod post
= new PostMethod(makeUrl(path
));
47 post
.setFollowRedirects(false);
48 addHeaders(post
, getHeadersForPost(mimeType
));
49 post
.setRequestEntity(new ByteArrayRequestEntity(body
));
50 httpClient
.executeMethod(post
);
51 return createResponse(post
);
54 private void addHeaders(HttpMethodBase method
, List
<String
[]> headers
) {
55 for (String
[] headerPair
: headers
) {
56 method
.addRequestHeader(headerPair
[0], headerPair
[1]);
60 private Response
createResponse(HttpMethodBase method
) throws IOException
{
61 byte[] body
= method
.getResponseBody(getMaxResponseSize());
62 return new Response(method
.getStatusCode(),
63 body
, method
.getResponseCharSet());