com.rt.storage.api.client.http.BasicAuthentication Maven / Gradle / Ivy
package com.rt.storage.api.client.http;
import com.rt.storage.api.client.util.Preconditions;
import java.io.IOException;
/**
* Basic authentication HTTP request initializer as specified in Basic Authentication Scheme
*
* Implementation is immutable and thread-safe. It can be used as either an HTTP request
* initializer or an HTTP request execute interceptor. {@link #initialize(HttpRequest)} only sets
* itself as the interceptor. Authentication is actually done in {@link #intercept(HttpRequest)},
* which is implemented using {@link HttpHeaders#setBasicAuthentication(String, String)}.
*
* @since 1.7
* @author Yaniv Inbar
*/
public final class BasicAuthentication implements HttpRequestInitializer, HttpExecuteInterceptor {
private final String username;
private final String password;
public BasicAuthentication(String username, String password) {
this.username = Preconditions.checkNotNull(username);
this.password = Preconditions.checkNotNull(password);
}
public void initialize(HttpRequest request) throws IOException {
request.setInterceptor(this);
}
public void intercept(HttpRequest request) throws IOException {
request.getHeaders().setBasicAuthentication(username, password);
}
/** Returns the username. */
public String getUsername() {
return username;
}
/** Returns the password. */
public String getPassword() {
return password;
}
}