1 // Copyright 2011 Google. All Rights Reserved.
2 package com
.google
.appengine
.tools
.remoteapi
;
4 import com
.google
.appengine
.api
.urlfetch
.HTTPHeader
;
5 import com
.google
.appengine
.api
.urlfetch
.HTTPMethod
;
6 import com
.google
.appengine
.api
.urlfetch
.HTTPRequest
;
7 import com
.google
.appengine
.api
.urlfetch
.HTTPResponse
;
8 import com
.google
.appengine
.api
.urlfetch
.URLFetchService
;
9 import com
.google
.appengine
.api
.urlfetch
.URLFetchServiceFactory
;
11 import org
.apache
.commons
.httpclient
.Cookie
;
12 import org
.apache
.commons
.httpclient
.Header
;
13 import org
.apache
.commons
.httpclient
.HttpMethodBase
;
15 import java
.io
.IOException
;
17 import java
.util
.List
;
20 * An {@link AppEngineClient} implementation that uses {@link URLFetchService}.
21 * This implementation must be used when the client is an App Engine container
22 * since URLFetchService is the only way to make HTTP requests in this
26 class HostedAppEngineClient
extends AppEngineClient
{
28 private final URLFetchService urlFetch
= URLFetchServiceFactory
.getURLFetchService();
30 HostedAppEngineClient(RemoteApiOptions options
, List
<Cookie
> authCookies
,
32 super(options
, authCookies
, appId
);
35 private void addCookies(HTTPRequest req
) {
36 for (Cookie cookie
: getAuthCookies()) {
38 new HTTPHeader("Cookie", String
.format("%s=%s", cookie
.getName(), cookie
.getValue())));
43 Response
get(String path
) throws IOException
{
44 HTTPRequest req
= new HTTPRequest(new URL(makeUrl(path
)), HTTPMethod
.GET
);
45 req
.getFetchOptions().doNotFollowRedirects();
46 for (String
[] headerPair
: getHeadersForGet()) {
47 req
.addHeader(new HTTPHeader(headerPair
[0], headerPair
[1]));
50 HTTPResponse resp
= urlFetch
.fetch(req
);
51 return createResponse(resp
);
55 Response
post(String path
, String mimeType
, byte[] body
) throws IOException
{
56 HTTPRequest req
= new HTTPRequest(new URL(makeUrl(path
)), HTTPMethod
.POST
);
57 req
.getFetchOptions().doNotFollowRedirects();
58 for (String
[] headerPair
: getHeadersForPost(mimeType
)) {
59 req
.addHeader(new HTTPHeader(headerPair
[0], headerPair
[1]));
63 HTTPResponse resp
= urlFetch
.fetch(req
);
64 return createResponse(resp
);
68 * A small hack to get access to
69 * {@link HttpMethodBase#getContentCharSet(Header)} (which really ought to be
70 * static) so that we don't need to write code to extract the charset out of
71 * the Content-Type header.
73 private static class DummyMethod
extends HttpMethodBase
{
74 private static final DummyMethod INSTANCE
= new DummyMethod();
75 static String
getCharset(HTTPHeader header
) {
76 Header apacheHeader
= new Header(header
.getName(), header
.getValue());
77 return INSTANCE
.getContentCharSet(apacheHeader
);
81 public String
getName() {
86 static Response
createResponse(HTTPResponse resp
) {
87 byte[] body
= resp
.getContent();
88 String charset
= null;
89 for (HTTPHeader header
: resp
.getHeaders()) {
90 if (header
.getName().toLowerCase().equals("content-type")) {
91 charset
= DummyMethod
.getCharset(header
);
95 return new Response(resp
.getResponseCode(), body
, charset
);