All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.payu.sdk.api.http.ApiAuthenticator Maven / Gradle / Ivy

The newest version!
package com.payu.sdk.api.http;

import com.squareup.okhttp.Authenticator;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import java.io.IOException;
import java.net.Proxy;
import java.util.logging.Logger;

public class ApiAuthenticator extends PayUAuthenticator implements Authenticator {

  private static final int MAX_REDIRECTS = 2;
  private static final Logger logger = Logger.getLogger(ApiAuthenticator.class.getName());

  public ApiAuthenticator(String apiLogin, String apiKey, String fingerprint) {
    super(apiLogin, apiKey, fingerprint);
  }

  @Override public Request authenticate(Proxy proxy, Response response) throws IOException {
    if (responseCount(response) >= MAX_REDIRECTS) {
      return null; //Up, many redirects, no authorization...
    }
    return authRequest(response.request().newBuilder());
  }

  @Override public Request authenticateProxy(Proxy proxy, Response response) throws IOException {
    return null;
  }

  private int responseCount(Response response) {
    int result = 1;
    while ((response = response.priorResponse()) != null) {
      result++;
    }
    logger.info(String.format("Number of redirects: %s\n", result));
    return result;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy