Android Open Source - dashclock-gerrit Basic Auth With Cookie Authentication Provider






From Project

Back to project page dashclock-gerrit.

License

The source code is released under:

Apache License

If you think the Android project dashclock-gerrit listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.plusonelabs.dashclock.gerrit.auth;
/*from w  w  w .j  av  a2s .  c  o  m*/
import static com.plusonelabs.dashclock.gerrit.util.UrlUtil.*;

import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;

import com.github.kevinsawicki.http.HttpRequest;
import com.plusonelabs.dashclock.gerrit.GerritEndpoint;

public class BasicAuthWithCookieAuthenticationProvider implements AuthenticationProvider {

  @Override
  public void preRequest(GerritEndpoint endpoint) {
    initCookieManager();
    performAuthenticationRequest(endpoint);
  }

  private void initCookieManager() {
    CookieManager manager = new CookieManager();
    manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    CookieHandler.setDefault(manager);
  }

  private void performAuthenticationRequest(GerritEndpoint endpoint) {
    String loginUrl = appendPath(endpoint.getUrl(), "login/");
    HttpRequest request = createRequest(endpoint, loginUrl);
    processRequest(endpoint, request);
  }

  private HttpRequest createRequest(GerritEndpoint endpoint, String loginUrl) {
    HttpRequest request = HttpRequest.get(loginUrl);
    request.acceptGzipEncoding().uncompress(true).trustAllCerts().trustAllHosts();
    request.basic(endpoint.getUsername(), endpoint.getPassword());
    return request;
  }

  private void processRequest(GerritEndpoint endpoint, HttpRequest request) {
    if (!request.ok()) {
      throw new IllegalStateException("Could not authenticate at Gerrit server "
          + endpoint.getUrl());
    }
  }

  @Override
  public void supplyCredentials(HttpRequest request, GerritEndpoint endpoint) {
    request.basic(endpoint.getUsername(), endpoint.getPassword());
  }

  @Override
  public boolean isAnonymous() {
    return false;
  }
}




Java Source Code List

com.plusonelabs.dashclock.gerrit.Change.java
com.plusonelabs.dashclock.gerrit.GerritDashClockExtension.java
com.plusonelabs.dashclock.gerrit.GerritEndpoint.java
com.plusonelabs.dashclock.gerrit.Gerrit.java
com.plusonelabs.dashclock.gerrit.QueryBuilder.java
com.plusonelabs.dashclock.gerrit.SettingsActivity.java
com.plusonelabs.dashclock.gerrit.TestActivity.java
com.plusonelabs.dashclock.gerrit.auth.AnonymousAuthenticationProvider.java
com.plusonelabs.dashclock.gerrit.auth.AuthenticationProvider.java
com.plusonelabs.dashclock.gerrit.auth.BasicAuthWithCookieAuthenticationProvider.java
com.plusonelabs.dashclock.gerrit.prefs.AboutFragment.java
com.plusonelabs.dashclock.gerrit.prefs.DisplayFragment.java
com.plusonelabs.dashclock.gerrit.prefs.GerritPreferences.java
com.plusonelabs.dashclock.gerrit.prefs.SecurePreferences.java
com.plusonelabs.dashclock.gerrit.prefs.ServerFragment.java
com.plusonelabs.dashclock.gerrit.util.ParamCheck.java
com.plusonelabs.dashclock.gerrit.util.StringUtil.java
com.plusonelabs.dashclock.gerrit.util.UrlUtil.java