private CloseableHttpClient prepareAuthenticatingClient() throws Exception {
// install host name verifier that always approves host names
AllowAllHostnameVerifier hostnameVerifier = new AllowAllHostnameVerifier();
// for SSL requests we should accept self-signed host certificates
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
SSLContextBuilder sslContextBuilder = SSLContexts.custom()
.loadTrustMaterial(trustStore, new TrustSelfSignedStrategy());
// first attempt to prepare a https client with certificate credentials
if (this.certificateCredentials != null) {
String keystorePath = this.certificateCredentials.getKeystorePath();
String keystorePassword = this.certificateCredentials
.getKeystorePassword();
// fall back to keystore password if key password is missing
String keyPassword = this.certificateCredentials.getKeyPassword()
.or(keystorePassword);
KeyStore keyStore = KeyStore.getInstance(KEYSTORE_TYPE);
keyStore.load(new FileInputStream(keystorePath),
keystorePassword.toCharArray());
sslContextBuilder.loadKeyMaterial(keyStore,
keyPassword.toCharArray());
}
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
if (this.basicCredentials != null) {
String username = this.basicCredentials.getUsername();
String password = this.basicCredentials.getPassword();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(username, password));
}
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credentialsProvider)
.setSslcontext(sslContextBuilder.build())
.setHostnameVerifier(hostnameVerifier).build();
return httpclient;
}