Version 1.7.4
[gae.git] / java / src / main / com / google / appengine / tools / remoteapi / StandaloneClientLogin.java
blob0f93b52e7457bbc39fb8d17534a4dc1fc313725a
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;
15 /**
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() {}
25 @Override
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);
37 @Override
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()));
47 } else {
48 throw new LoginException("unexpected response from app engine: " + get.getStatusCode());