org.rx.net.http.AuthenticProxy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rxlib Show documentation
Show all versions of rxlib Show documentation
A set of utilities for Java
package org.rx.net.http;
import io.netty.handler.codec.http.HttpHeaderNames;
import lombok.Getter;
import lombok.Setter;
import okhttp3.Authenticator;
import okhttp3.Credentials;
import java.net.Proxy;
import java.net.SocketAddress;
@Getter
public class AuthenticProxy extends Proxy {
private final Authenticator authenticator;
@Setter
private boolean directOnFail;
public AuthenticProxy(Type type, SocketAddress sa) {
this(type, sa, null, null);
}
public AuthenticProxy(Type type, SocketAddress sa, String username, String password) {
super(type, sa);
authenticator = (route, response) -> {
String name = HttpHeaderNames.PROXY_AUTHORIZATION.toString();
if (directOnFail && response.request().header(name) != null) {
return null;
}
String credential = Credentials.basic(username, password);
return response.request().newBuilder()
.header(name, credential)
.build();
};
directOnFail = true;
}
}