1 // Copyright 2010 Google Inc. All Rights Reserved.
3 package com
.google
.appengine
.tools
.remoteapi
;
5 import org
.apache
.commons
.httpclient
.Cookie
;
6 import org
.apache
.commons
.httpclient
.HttpClient
;
7 import org
.apache
.commons
.httpclient
.methods
.GetMethod
;
8 import org
.apache
.commons
.httpclient
.methods
.PostMethod
;
10 import java
.io
.IOException
;
11 import java
.util
.ArrayList
;
12 import java
.util
.Arrays
;
13 import java
.util
.List
;
16 * {@link ClientLogin} implementation for use inside a standalone Java app. We
17 * use {@link HttpClient} to issue HTTP requests.
20 class StandaloneClientLogin
extends ClientLogin
{
21 private static final int MAX_RESPONSE_SIZE
= 1024 * 1024;
23 StandaloneClientLogin() {}
26 PostResponse
executePost(String url
, List
<String
[]> postParams
) throws IOException
{
27 PostMethod post
= new PostMethod(url
);
28 for (String
[] param
: postParams
) {
29 post
.addParameter(param
[0], param
[1]);
31 HttpClient client
= new HttpClient();
32 client
.executeMethod(post
);
33 String body
= post
.getResponseBodyAsString(MAX_RESPONSE_SIZE
);
34 return new PostResponse(post
.getStatusCode(), body
);
38 List
<Cookie
> getAppEngineLoginCookies(String url
) throws IOException
{
39 GetMethod get
= new GetMethod(url
);
40 get
.setFollowRedirects(false);
42 HttpClient client
= new HttpClient();
43 client
.executeMethod(get
);
45 if (get
.getStatusCode() == 302) {
46 return new ArrayList
<Cookie
>(Arrays
.asList(client
.getState().getCookies()));
48 throw new LoginException("unexpected response from app engine: " + get
.getStatusCode());