@Test public void testAllowSelfSignedCerts() throws Exception { final SSLConfigurationCallback cb = SSLConfigurationCallback.Builder. createCustom(sslContextBuilderMock). allowSelfSignedCertificates(). build(); cb.customizeHttpClient(httpAsyncClientBuilderMock); final ArgumentCaptor<TrustStrategy> trustStrategyCaptor = ArgumentCaptor.forClass(TrustStrategy.class); verify(sslContextBuilderMock).loadTrustMaterial(trustStrategyCaptor.capture()); verify(sslContextBuilderMock).build(); verify(httpAsyncClientBuilderMock).setSSLContext(sslContextMock); verifyNoMoreInteractions(sslContextMock, sslContextBuilderMock, httpAsyncClientBuilderMock); assertEquals(1, trustStrategyCaptor.getAllValues().size()); final TrustStrategy trustStrategy = trustStrategyCaptor.getValue(); assertTrue(trustStrategy instanceof TrustSelfSignedStrategy); }
@Override public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { final SSLContext sslcontext; final TrustStrategy trustStrategy = allowSelfSignedCertificates ? new TrustSelfSignedStrategy() : null; try { if (StringUtils.isNotEmpty(trustStoreFile)) { sslContextBuilder.loadTrustMaterial(new File(trustStoreFile), trustStorePassword.toCharArray(), trustStrategy); } else { sslContextBuilder.loadTrustMaterial(trustStrategy); } } catch (KeyStoreException | CertificateException | NoSuchAlgorithmException e ) { throw new RuntimeException("Invalid trust store file " + trustStoreFile, e); } catch (IOException e) { throw new RuntimeException("Unable to load trust store data from " + trustStoreFile, e); } try { if (StringUtils.isNotEmpty(keyStoreFile)) { sslContextBuilder.loadKeyMaterial(new File(keyStoreFile), keyStorePassword.toCharArray(), keyPassword.toCharArray()); } } catch (KeyStoreException | CertificateException | NoSuchAlgorithmException | UnrecoverableKeyException e ) { throw new RuntimeException("Invalid key store file " + keyStoreFile, e); } catch (IOException e) { throw new RuntimeException("Unable to load key store data from " + keyStoreFile, e); } try { sslcontext = sslContextBuilder.build(); } catch (KeyManagementException | NoSuchAlgorithmException e) { throw new RuntimeException("SSL context initialization failed", e); } httpClientBuilder.setSSLContext(sslcontext); if (disableHostNameVerification) { httpClientBuilder.setSSLHostnameVerifier(new NoopHostnameVerifier()); } return httpClientBuilder; }
SSLConfigurationCallback implements HttpClientConfigCallback { @Override public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { final SSLContext sslcontext; final TrustStrategy trustStrategy = allowSelfSignedCertificates ? new TrustSelfSignedStrategy() : null; try { if (StringUtils.isNotEmpty(trustStoreFile)) { sslContextBuilder.loadTrustMaterial(new File(trustStoreFile), trustStorePassword.toCharArray(), trustStrategy); } else { sslContextBuilder.loadTrustMaterial(trustStrategy); } } catch (KeyStoreException | CertificateException | NoSuchAlgorithmException e ) { throw new RuntimeException("Invalid trust store file " + trustStoreFile, e); } catch (IOException e) { throw new RuntimeException("Unable to load trust store data from " + trustStoreFile, e); } try { if (StringUtils.isNotEmpty(keyStoreFile)) { sslContextBuilder.loadKeyMaterial(new File(keyStoreFile), keyStorePassword.toCharArray(), keyPassword.toCharArray()); } } catch (KeyStoreException | CertificateException | NoSuchAlgorithmException | UnrecoverableKeyException e ) { throw new RuntimeException("Invalid key store file " + keyStoreFile, e); } catch (IOException e) { throw new RuntimeException("Unable to load key store data from " + keyStoreFile, e); } try { sslcontext = sslContextBuilder.build(); } catch (KeyManagementException | NoSuchAlgorithmException e) { throw new RuntimeException("SSL context initialization failed", e); } httpClientBuilder.setSSLContext(sslcontext); if (disableHostNameVerification) { httpClientBuilder.setSSLHostnameVerifier(new NoopHostnameVerifier()); } return httpClientBuilder; } }
SSLConfigurationCallback implements HttpClientConfigCallback { @Override public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { final SSLContext sslcontext; final TrustStrategy trustStrategy = allowSelfSignedCertificates ? new TrustSelfSignedStrategy() : null; try { if (StringUtils.isNotEmpty(trustStoreFile)) { sslContextBuilder.loadTrustMaterial(new File(trustStoreFile), trustStorePassword.toCharArray(), trustStrategy); } else { sslContextBuilder.loadTrustMaterial(trustStrategy); } } catch (KeyStoreException | CertificateException | NoSuchAlgorithmException e ) { throw new RuntimeException("Invalid trust store file " + trustStoreFile, e); } catch (IOException e) { throw new RuntimeException("Unable to load trust store data from " + trustStoreFile, e); } try { if (StringUtils.isNotEmpty(keyStoreFile)) { sslContextBuilder.loadKeyMaterial(new File(keyStoreFile), keyStorePassword.toCharArray(), keyPassword.toCharArray()); } } catch (KeyStoreException | CertificateException | NoSuchAlgorithmException | UnrecoverableKeyException e ) { throw new RuntimeException("Invalid key store file " + keyStoreFile, e); } catch (IOException e) { throw new RuntimeException("Unable to load key store data from " + keyStoreFile, e); } try { sslcontext = sslContextBuilder.build(); } catch (KeyManagementException | NoSuchAlgorithmException e) { throw new RuntimeException("SSL context initialization failed", e); } httpClientBuilder.setSSLContext(sslcontext); if (disableHostNameVerification) { httpClientBuilder.setSSLHostnameVerifier(new NoopHostnameVerifier()); } return httpClientBuilder; } private  SSLConfigurationCallback(final SSLContextBuilder sslContextBuilder,
            final String trustStoreFile,
            final String trustStorePassword,
            final String keyStoreFile,
            final String keyStorePassword,
            final String keyPassword,
            final boolean disableHostNameVerification,
            final boolean allowSelfSignedCertificates); }
SSLConfigurationCallback implements HttpClientConfigCallback { @Override public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { final SSLContext sslcontext; final TrustStrategy trustStrategy = allowSelfSignedCertificates ? new TrustSelfSignedStrategy() : null; try { if (StringUtils.isNotEmpty(trustStoreFile)) { sslContextBuilder.loadTrustMaterial(new File(trustStoreFile), trustStorePassword.toCharArray(), trustStrategy); } else { sslContextBuilder.loadTrustMaterial(trustStrategy); } } catch (KeyStoreException | CertificateException | NoSuchAlgorithmException e ) { throw new RuntimeException("Invalid trust store file " + trustStoreFile, e); } catch (IOException e) { throw new RuntimeException("Unable to load trust store data from " + trustStoreFile, e); } try { if (StringUtils.isNotEmpty(keyStoreFile)) { sslContextBuilder.loadKeyMaterial(new File(keyStoreFile), keyStorePassword.toCharArray(), keyPassword.toCharArray()); } } catch (KeyStoreException | CertificateException | NoSuchAlgorithmException | UnrecoverableKeyException e ) { throw new RuntimeException("Invalid key store file " + keyStoreFile, e); } catch (IOException e) { throw new RuntimeException("Unable to load key store data from " + keyStoreFile, e); } try { sslcontext = sslContextBuilder.build(); } catch (KeyManagementException | NoSuchAlgorithmException e) { throw new RuntimeException("SSL context initialization failed", e); } httpClientBuilder.setSSLContext(sslcontext); if (disableHostNameVerification) { httpClientBuilder.setSSLHostnameVerifier(new NoopHostnameVerifier()); } return httpClientBuilder; } private  SSLConfigurationCallback(final SSLContextBuilder sslContextBuilder,
            final String trustStoreFile,
            final String trustStorePassword,
            final String keyStoreFile,
            final String keyStorePassword,
            final String keyPassword,
            final boolean disableHostNameVerification,
            final boolean allowSelfSignedCertificates); @Override HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder); }
SSLConfigurationCallback implements HttpClientConfigCallback { @Override public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { final SSLContext sslcontext; final TrustStrategy trustStrategy = allowSelfSignedCertificates ? new TrustSelfSignedStrategy() : null; try { if (StringUtils.isNotEmpty(trustStoreFile)) { sslContextBuilder.loadTrustMaterial(new File(trustStoreFile), trustStorePassword.toCharArray(), trustStrategy); } else { sslContextBuilder.loadTrustMaterial(trustStrategy); } } catch (KeyStoreException | CertificateException | NoSuchAlgorithmException e ) { throw new RuntimeException("Invalid trust store file " + trustStoreFile, e); } catch (IOException e) { throw new RuntimeException("Unable to load trust store data from " + trustStoreFile, e); } try { if (StringUtils.isNotEmpty(keyStoreFile)) { sslContextBuilder.loadKeyMaterial(new File(keyStoreFile), keyStorePassword.toCharArray(), keyPassword.toCharArray()); } } catch (KeyStoreException | CertificateException | NoSuchAlgorithmException | UnrecoverableKeyException e ) { throw new RuntimeException("Invalid key store file " + keyStoreFile, e); } catch (IOException e) { throw new RuntimeException("Unable to load key store data from " + keyStoreFile, e); } try { sslcontext = sslContextBuilder.build(); } catch (KeyManagementException | NoSuchAlgorithmException e) { throw new RuntimeException("SSL context initialization failed", e); } httpClientBuilder.setSSLContext(sslcontext); if (disableHostNameVerification) { httpClientBuilder.setSSLHostnameVerifier(new NoopHostnameVerifier()); } return httpClientBuilder; } private  SSLConfigurationCallback(final SSLContextBuilder sslContextBuilder,
            final String trustStoreFile,
            final String trustStorePassword,
            final String keyStoreFile,
            final String keyStorePassword,
            final String keyPassword,
            final boolean disableHostNameVerification,
            final boolean allowSelfSignedCertificates); @Override HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder);  }