Java Magazine, July/August 2017
ORACLE COM JAVAMAGAZINE JULY AUGUST 2017 43 java 9 name chosen for the sslContext method is confusing because it makes you think that the HttpClient will use the old and deprecated SSL instead of TLS to work with h2 It is important to avoid confusion about the usage of SSL and the various names The sslContext method will configure a TLS context not an SSL context I work with TLS version 12 TLSv12 in my next example I also use the Bouncy Castle libraries to make it easy to load certificates I end up generating an SSLContext instance configured for TLSv12 These Bouncy Castle libraries are very popular when developers are working with TLS in Java Using them I combine the new HTTP client with the usage of Bouncy Castle libraries to work with h2 in JShell When I was in the process of writing this article the latest version of the Bouncy Castle libraries was 157 and therefore I use the names for the JAR files that include this version number I wont be working with any specific build system I will continue to make the examples work with JShell However you can easily use the code as a baseline to configure any build system to work with the Bouncy Castle libraries Download the following JAR files from the Bouncy Castle site and save them in a folder bcmail jdk15on 157 jar bcpkix jdk15on 157 jar bcprov jdk15on 157 jar The following line launches JShell with the jdk incubator httpclient module specified as a value in the addmodules option and the previously enumerated JAR files specified as values for the class path option This way JShell will resolve the jdk incubator httpclient and load the specified class files that allow us to work with the Bouncy Castle libraries Make sure you launch JShell in the path in which you saved the JAR files If you dont have jshell included in the path you need to specify its full path jshell add modules jdk incubator httpclient classpath bcpkix jdk15on 157 jar bcmail jdk15on 157 jar bcprov jdk15on 157 jar The following code contains all the import statements Im also including many import statements that JShell runs by default to make it easy to run the code when you dont use JShell Import statements that duplicate ones JShell brings in automatically do not generate errors import jdk incubator http import java util concurrent CompletableFuture import java lang import java net URI import java net URISyntaxException import jdk incubator http import java security KeyFactory import java security KeyStore import java security KeyStoreException import java security NoSuchAlgorithmException import java security PrivateKey import java security SecureRandom import java security Security import java security UnrecoverableKeyException import java security cert Certificate import java security cert CertificateException import java security cert CertificateFactory import java security cert X509Certificate import java security spec InvalidKeySpecException For software development tasks you will find the new HTTP 2 client to be extremely useful especially when you need to work with an interactive REPL such as JShell
You must have JavaScript enabled to view digital editions.