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 public Response
get(String path
) throws IOException
{
44 return createResponse(doGet(path
));
47 private HTTPResponse
doGet(String path
) throws IOException
{
48 HTTPRequest req
= new HTTPRequest(new URL(makeUrl(path
)), HTTPMethod
.GET
);
49 req
.getFetchOptions().doNotFollowRedirects();
50 for (String
[] headerPair
: getHeadersForGet()) {
51 req
.addHeader(new HTTPHeader(headerPair
[0], headerPair
[1]));
54 return urlFetch
.fetch(req
);
58 public Response
post(String path
, String mimeType
, byte[] body
) throws IOException
{
59 return createResponse(doPost(path
, mimeType
, body
));
62 private HTTPResponse
doPost(String path
, String mimeType
, byte[] body
)
64 HTTPRequest req
= new HTTPRequest(new URL(makeUrl(path
)), HTTPMethod
.POST
);
65 req
.getFetchOptions().doNotFollowRedirects();
66 for (String
[] headerPair
: getHeadersForPost(mimeType
)) {
67 req
.addHeader(new HTTPHeader(headerPair
[0], headerPair
[1]));
71 return urlFetch
.fetch(req
);
75 public LegacyResponse
legacyGet(String path
) throws IOException
{
76 return createLegacyResponse(doGet(path
));
80 public LegacyResponse
legacyPost(String path
, String mimeType
, byte[] body
)
82 return createLegacyResponse(doPost(path
, mimeType
, body
));
86 * A small hack to get access to
87 * {@link HttpMethodBase#getContentCharSet(Header)} (which really ought to be
88 * static) so that we don't need to write code to extract the charset out of
89 * the Content-Type header.
91 private static class DummyMethod
extends HttpMethodBase
{
92 private static final DummyMethod INSTANCE
= new DummyMethod();
93 static String
getCharset(HTTPHeader header
) {
94 Header apacheHeader
= new Header(header
.getName(), header
.getValue());
95 return INSTANCE
.getContentCharSet(apacheHeader
);
99 public String
getName() {
104 static Response
createResponse(HTTPResponse resp
) {
105 return new Response(resp
.getResponseCode(), resp
.getContent(), getCharset(resp
));
108 static LegacyResponse
createLegacyResponse(HTTPResponse resp
) {
109 return new LegacyResponse(resp
.getResponseCode(), resp
.getContent(), getCharset(resp
));
112 static String
getCharset(HTTPResponse resp
) {
113 String charset
= null;
114 for (HTTPHeader header
: resp
.getHeaders()) {
115 if (header
.getName().toLowerCase().equals("content-type")) {
116 charset
= DummyMethod
.getCharset(header
);