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