package com.github.chrisprice.phonegapbuild.api.managers;

import com.github.chrisprice.phonegapbuild.api.PatchedMultiPartWriter;
import com.github.chrisprice.phonegapbuild.api.data.me.MeResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.json.JSONConfiguration;
import com.sun.jersey.client.apache.ApacheHttpClient;
import com.sun.jersey.client.apache.config.DefaultApacheHttpClientConfig;

public class MeManagerImpl implements MeManager {


  private static class TokenResponse {
    private String token;

    public String getToken() {
      return token;
    }
  }

  private static final String TOKEN_PARAM = "auth_token";

  @Override
  public WebResource createRootWebResource(String username, String password) {
    return createRootWebResource(username, password, null);
  }

  @Override
  public WebResource createRootWebResource(String username, String password, String proxyUri) {
    // create a DefaultApacheHttpClientConfig (instead of just a ClientConfig)
    DefaultApacheHttpClientConfig config = new DefaultApacheHttpClientConfig();
    // configure it to use a proxy
    if (proxyUri != null) {
      config.getProperties().put(DefaultApacheHttpClientConfig.PROPERTY_PROXY_URI, proxyUri);
    }
    // configure it to parse JSON to POJOs
    config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
    // configure to follow re-directs (used for downloading packages)
    config.getProperties().put(DefaultApacheHttpClientConfig.PROPERTY_FOLLOW_REDIRECTS, Boolean.TRUE);
    // set http auth credentials
    config.getState().setCredentials(null, null, -1, username, password);
    // configure it to explicity use the patched MultiPartWriter
    config.getClasses().add(PatchedMultiPartWriter.class);
    // create an ApacheHttpClient (instead of just a Client)
    ApacheHttpClient c = ApacheHttpClient.create(config);
    // create a resource
    WebResource webResource = c.resource("https://build.phonegap.com");
    // fetch a token
    TokenResponse tokenResponse = requestToken(webResource);
    // associate the token with all future requests
    return webResource.queryParam(TOKEN_PARAM, tokenResponse.getToken());
  }

  private TokenResponse requestToken(WebResource resource) {
    return resource.path(TOKEN_PATH).post(TokenResponse.class);
  }

  /* (non-Javadoc)
   * @see com.github.chrisprice.phonegapbuild.api.managers.MeManager#requestMe(com.sun.jersey.api.client.WebResource)
   */
  @Override
  public MeResponse requestMe(WebResource resource) {
    return resource.path(API_V1_PATH).get(MeResponse.class);
  }
}

@@ -914,24 +914,248 @@
 proxyUri) %7B%0A
+%09  return createRootWebResource(username, password, proxyUri, null, null);%0A  %7D%0A%0A  @Override%0A  public WebResource createRootWebResource(String username, String password, String proxyUri, String proxyUser, String proxyPwd ) %7B%0A
     // creat
@@ -1760,24 +1760,197 @@
 lean.TRUE);%0A
+%0A    // set proxy auth credentials%0A  %09if ( proxyUser != null && proxyPwd != null ) %7B%0A%09%09  config.getState().setProxyCredentials(null, null, -1, proxyUser, proxyPwd );%0A    %7D%0A%0A
     // set h

Update MeManagerImpl.java