Package com.taskadapter.redmineapi.internal.comm.redmine

Source Code of com.taskadapter.redmineapi.internal.comm.redmine.RedmineAuthenticator

package com.taskadapter.redmineapi.internal.comm.redmine;

import java.io.UnsupportedEncodingException;

import com.taskadapter.redmineapi.internal.comm.Communicator;
import com.taskadapter.redmineapi.internal.comm.ContentHandler;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpRequest;
import com.taskadapter.redmineapi.RedmineException;
import com.taskadapter.redmineapi.RedmineInternalError;

public class RedmineAuthenticator<K> implements Communicator<K> {
  /**
   * Header value.
   */
  private String authKey;

  /**
   * Used charset.
   */
  private final String charset;

  /**
   * Peer communicator.
   */
  private final Communicator<K> peer;

  public RedmineAuthenticator(Communicator<K> peer, String charset) {
    this.peer = peer;
    this.charset = charset;
  }

  public void setCredentials(String login, String password) {
    if (login == null) {
      authKey = null;
      return;
    }
    try {
      authKey = "Basic "
          + Base64.encodeBase64String(
              (login + ':' + password).getBytes(charset)).trim();
    } catch (UnsupportedEncodingException e) {
      throw new RedmineInternalError(e);
    }
  }

  @Override
  public <R> R sendRequest(HttpRequest request, ContentHandler<K, R> handler)
      throws RedmineException {
    if (authKey != null)
      request.addHeader("Authorization", authKey);
    return peer.sendRequest(request, handler);
  }

}
TOP

Related Classes of com.taskadapter.redmineapi.internal.comm.redmine.RedmineAuthenticator

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.