com.taskadapter.redmineapi.internal.comm.redmine.RedmineAuthenticator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of redmine-java-api Show documentation
Show all versions of redmine-java-api Show documentation
Free open-source Java API for Redmine and Chiliproject bug/task management systems.
This project was originally a part of Task Adapter application (http://www.taskadapter.com)
and then was open-sourced.
The newest version!
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 implements Communicator {
/**
* Header value.
*/
private String authKey;
/**
* Used charset.
*/
private final String charset;
/**
* Peer communicator.
*/
private final Communicator peer;
public RedmineAuthenticator(Communicator 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 sendRequest(HttpRequest request, ContentHandler handler)
throws RedmineException {
if (authKey != null)
request.addHeader("Authorization", authKey);
return peer.sendRequest(request, handler);
}
}