Package org.openbankdata.core.client

Source Code of org.openbankdata.core.client.AbstractBankClient

package org.openbankdata.core.client;

import org.openbankdata.core.client.exception.AuthenticationException;

import com.github.kevinsawicki.http.HttpRequest;

import java.net.CookieHandler;
import java.net.CookieManager;
import java.util.Map.Entry;

/**
* An abstract client class used for interacting with a bank's website or API.
*/
public abstract class AbstractBankClient implements BankClient {

  protected boolean mActiveSession;

  protected static final String USER_AGENT = "OpenBankDataJava/1.0";

  private Cache mCache;

  private String mUsername;
  private String mPassword;

  protected AbstractBankClient() {
    CookieHandler.setDefault(new CookieManager());
  }

  @Override
  public BankClient setCredentials(String pUsername, String pPassword) {
    mUsername = pUsername;
    mPassword = pPassword;
    return this;
  }

  protected HttpRequest createRequest(BankRequest pBankRequest, String pMethod) {
    HttpRequest vHttpRequest = null;
    if (HttpRequest.METHOD_POST.equals(pMethod)) {
      vHttpRequest = new HttpRequest(pBankRequest.getUri(), pMethod);
    } else {
      vHttpRequest = new HttpRequest(pBankRequest.generateUri(), pMethod);
    }
    vHttpRequest.userAgent(USER_AGENT);
    modifyRequest(pBankRequest);
    for (Entry<String, String> header : pBankRequest.getHeaders().entrySet()) {
      vHttpRequest.header(header.getKey(), header.getValue());
    }
    return vHttpRequest;
  }

  @Override
  public BankResponse post(BankRequest pBankRequest) {
    return sendRequest(pBankRequest, HttpRequest.METHOD_POST);
  }

  @Override
  public BankResponse get(BankRequest pBankRequest) {
    return sendRequest(pBankRequest, HttpRequest.METHOD_GET);
  }

  private BankResponse sendRequest(BankRequest pBankRequest, String pMethod) {
    if (!pBankRequest.skipAuthentication()) {
      if (!isSessionActive()) {
        getCache().clear();
        setSessionActive(activateSession());
        if (!isSessionActive()) {
          throw new AuthenticationException("Not logged in");
        }
      }
    }

    if (getCache().exists(pBankRequest)) {
      return getCache().get(pBankRequest);
    }
    HttpRequest request = createRequest(pBankRequest, pMethod);
    if (HttpRequest.METHOD_POST.equals(pMethod)) {
      request.form(pBankRequest.getParams());
    }
    request.code();
    BankResponse response = new DefaultBankResponse(request);
    getCache().put(pBankRequest, response);
    return response;
  }

  @Override
  public Cache getCache() {
    if (mCache == null) {
      mCache = new Cache();
    }
    return mCache;
  }

  @Override
  public void clearCache() {
    getCache().clear();
  }

  protected BankRequest modifyRequest(BankRequest pBankRequest) {
    return pBankRequest;
  }

  protected void setSessionActive(boolean pActiveSession) {
    mActiveSession = pActiveSession;
  }

  protected boolean isSessionActive() {
    return mActiveSession;
  }

  protected abstract boolean activateSession();

  protected String getUsername() {
    return mUsername;
  }

  protected String getPassword() {
    return mPassword;
  }

}
TOP

Related Classes of org.openbankdata.core.client.AbstractBankClient

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.