com.assertthat.plugins.standalone.OkHttpClientBuilder Maven / Gradle / Ivy
package com.assertthat.plugins.standalone;
import okhttp3.*;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.logging.Logger;
class OkHttpClientBuilder {
private final static Logger LOGGER = Logger.getLogger(APIUtil.class.getName());
private OkHttpClient.Builder builder = new OkHttpClient.Builder();
OkHttpClientBuilder authenticated(String username, String password) {
builder.addInterceptor(new BasicAuthInterceptor(username, password));
return this;
}
OkHttpClientBuilder ignoringCertificate() {
try {
// Create a trust manager that does not validate certificate chains
final TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) {
}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) {
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[]{};
}
}
};
// Install the all-trusting trust manager
final SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
// Create an ssl socket factory with our all-trusting manager
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
builder.sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0]);
builder.hostnameVerifier((hostname, session) -> true);
} catch (Exception e) {
LOGGER.severe("Error configuring ssl socket factory: " + e.getMessage());
}
return this;
}
OkHttpClientBuilder withProxy(String proxyHost, int proxyPort) {
builder.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)));
return this;
}
OkHttpClientBuilder withProxyAuth(String username, String password) {
Authenticator proxyAuthenticator = (route, response) -> {
String credential = Credentials.basic(username, password);
return response.request().newBuilder()
.header("Proxy-Authorization", credential)
.build();
};
builder.proxyAuthenticator(proxyAuthenticator);
return this;
}
OkHttpClient build(){
return builder.build();
}
class BasicAuthInterceptor implements Interceptor {
private String credentials;
BasicAuthInterceptor(String user, String password) {
this.credentials = Credentials.basic(user, password);
}
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Request authenticatedRequest = request.newBuilder()
.header("Authorization", credentials).build();
return chain.proceed(authenticatedRequest);
}
}
}