Skip to content

Instantly share code, notes, and snippets.

@DisasteR
Forked from juddflamm/gist:5391938
Last active August 29, 2015 14:15
Show Gist options
  • Save DisasteR/442d1f3eeb0ad3fa7bed to your computer and use it in GitHub Desktop.
Save DisasteR/442d1f3eeb0ad3fa7bed to your computer and use it in GitHub Desktop.
//First create the httpClient in Dropwizard's run method as documented
final HttpClient httpClient = new HttpClientBuilder().using(configuration.getHttpClient()).build();
try {
//Create KeyStore obejcts for both the keystore and truststore
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
KeyStore truststore = KeyStore.getInstance(KeyStore.getDefaultType());
//Then load the actual keystore/truststore file(s), they are the same file in my case
keystore.load(new FileInputStream(configuration.getKeyStore()), configuration.getKeyStorePassword().toCharArray());
truststore.load(new FileInputStream(configuration.getKeyStore()), configuration.getKeyStorePassword().toCharArray());
//Then register a Scheme for HTTPS, in the httpClient, using your loaded keystore, keyPassword, and truststore
//The keypassword, second argument, is the password of your key, not the keystore.
httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", 443,
new SSLSocketFactory(keystore, configuration.getKeyStorePassword(), truststore)));
} catch (Throwable t) {
//If something goes wrong, just kill Dropwizard with a RuntimeException
throw new RuntimeException("Couldn't register the HTTPS scheme in HttpClient", t);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment