package com.swookiee.tools.client;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
/**
* This Builder Class helps to create a {@link SwookieeClient} instance. You can configure target hostname, port,
* username, password and HTTPS settings. If you do not set any properties the default target
* <code>http://localhost:8080</code> with credentials <code>admin:admin123</code> will be used.
* <p>
* Note: Since this API is in a very early stage changes may occur.
*/
public final class SwookieClientBuilder {
private final String hostname;
private Integer port = 8080;
private String username = "admin";
private String password = "admin123";
private boolean enableHttps = false;
private final boolean enableSelfSigned = false;
private SwookieClientBuilder(final String hostname) {
this.hostname = hostname;
}
public static SwookieClientBuilder newTarget(final String host) {
return new SwookieClientBuilder(host);
}
public SwookieClientBuilder withPort(final Integer port) {
this.port = port;
return this;
}
public SwookieClientBuilder enableHttps() {
this.enableHttps = true;
return this;
}
public SwookieClientBuilder enableSelfSignedHttps() {
this.enableHttps = true;
enableHttps = true;
return this;
}
public SwookieClientBuilder withUsernamePassword(final String username, final String password) {
this.username = username;
this.password = password;
return this;
}
public SwookieeClient create() throws SwookieeClientException {
final HttpHost httpHost = getHttpHost();
final CloseableHttpClient httpclient = getHttpClient();
final BasicScheme basicAuth = new BasicScheme();
final AuthCache authCache = new BasicAuthCache();
authCache.put(httpHost, basicAuth);
final HttpClientContext swookieeContext = HttpClientContext.create();
swookieeContext.setAuthCache(authCache);
return new SwookieeClient(httpclient, swookieeContext, httpHost);
}
private CloseableHttpClient getHttpClient() throws SwookieeClientException {
final CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(this.hostname, this.port), new UsernamePasswordCredentials(
this.username, this.password));
final HttpClientBuilder httpClientBuilder = HttpClients.custom().setDefaultCredentialsProvider(credsProvider);
if (this.enableSelfSigned) {
try {
final SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
final CloseableHttpClient httpclient = httpClientBuilder.setSSLSocketFactory(sslsf).build();
} catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException ex) {
throw new SwookieeClientException("Could not initiate self signed certification", ex);
}
}
return httpClientBuilder.build();
}
private HttpHost getHttpHost() {
final HttpHost httpHost;
if (enableHttps) {
httpHost = new HttpHost(this.hostname, this.port, "https");
} else {
httpHost = new HttpHost(this.hostname, this.port);
}
return httpHost;
}
}