io.github.wooenrico.http.common.HttpProxy Maven / Gradle / Ivy
package io.github.wooenrico.http.common;
import java.net.Proxy;
import java.net.SocketAddress;
import java.util.Base64;
/**
* HttpProxy Object
*/
public class HttpProxy extends Proxy {
/**
* Proxy-Authorization header name
*/
public final static String Proxy_Authorization_Header_Name = "Proxy-Authorization";
private String username;
private String password;
/**
* Creates an entry representing a PROXY connection.
* Certain combinations are illegal. For instance, for types Http, and
* Socks, a SocketAddress must be provided.
*
* Use the {@code Proxy.NO_PROXY} constant
* for representing a direct connection.
*
* @param type the {@code Type} of the proxy
* @param sa the {@code SocketAddress} for that proxy
* @throws IllegalArgumentException when the type and the address are
* incompatible
*/
public HttpProxy(Type type, SocketAddress sa) {
super(type, sa);
}
/**
* Creates an entry representing a PROXY connection.
* @param type the {@code Type} of the proxy
* @param sa the {@code SocketAddress} for that proxy
* @param username the username
* @param password the password
*/
public HttpProxy(Type type, SocketAddress sa, String username, String password) {
super(type, sa);
this.username = username;
this.password = password;
}
/**
* proxy auth header
*
* @return header value of Proxy-Authorization
*/
public String getBasicAuthorization() {
if (username == null && password == null) {
return null;
}
String encodeKey = username + ":" + password;
byte[] encode = Base64.getEncoder().encode(encodeKey.getBytes());
return "Basic" + " " + new String(encode);
}
}