private void applySslSettings ( ) { try { TrustManager [ ] trustManagers = null ; HostnameVerifier hostnameVerifier = null ; if ( ! verifyingSsl ) { TrustManager trustAll = new X509TrustManager ( ) { @ Override public void checkClientTrusted ( X509Certificate [ ] chain , String authType ) throws CertificateException { } @ Override public void checkServerTrusted ( X509Certificate [ ] chain , String authType ) throws CertificateException { } @ Override public X509Certificate [ ] getAcceptedIssuers ( ) { return null ; } } ; SSLContext sslContext = SSLContext . getInstance ( "TLS" ) ; trustManagers = new TrustManager [ ] { trustAll } ; hostnameVerifier = new HostnameVerifier ( ) { @ Override public boolean verify ( String hostname , SSLSession session ) { return true ; } } ; } else if ( sslCaCert != null ) { char [ ] password = null ; // Any password will work. CertificateFactory certificateFactory = CertificateFactory . getInstance ( "X.509" ) ; Collection < ? extends Certificate > certificates = certificateFactory . generateCertificates ( sslCaCert ) ; if ( certificates . isEmpty ( ) ) { throw new IllegalArgumentException ( "expected non-empty set of trusted certificates" ) ; } KeyStore caKeyStore = newEmptyKeyStore ( password ) ; int index = 0 ; for ( Certificate certificate : certificates ) { String certificateAlias = "ca" + Integer . toString ( index ++ ) ; caKeyStore . setCertificateEntry ( certificateAlias , certificate ) ; } TrustManagerFactory trustManagerFactory = TrustManagerFactory . getInstance ( TrustManagerFactory . getDefaultAlgorithm ( ) ) ; trustManagerFactory . init ( caKeyStore ) ; trustManagers = trustManagerFactory . getTrustManagers ( ) ; } if ( keyManagers != null || trustManagers != null ) { SSLContext sslContext = SSLContext . getInstance ( "TLS" ) ; sslContext . init ( keyManagers , trustManagers , new SecureRandom ( ) ) ; httpClient . setSslSocketFactory ( sslContext . getSocketFactory ( ) ) ; } else { httpClient . setSslSocketFactory ( null ) ; } httpClient . setHostnameVerifier ( hostnameVerifier ) ; } catch ( GeneralSecurityException e ) { throw new RuntimeException ( e ) ; } }
Apply SSL related settings to httpClient according to the current values of verifyingSsl and sslCaCert .