So I am trying to create a REST API server using Grizzly / Jersey and it is working fine but I am not able to figure out how to enable http2. Documentation on this subject is all but non-existent. This is what I have:
private static SSLContext getSSLContext(final String keystore) throws Exception {
SSLContext sslContext = SSLContext.getInstance("TLSv1");
final char[] passphrase = "changeit".toCharArray();
final KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
final KeyStore ks = KeyStore.getInstance("JKS");
try (InputStream stream = Main.class.getResourceAsStream(keystore)) {
ks.load(stream, passphrase);
}
kmf.init(ks, passphrase);
sslContext.init(kmf.getKeyManagers(), null, null);
return sslContext;
}
final ResourceConfig rc = new ResourceConfig().packages("path.to.package");
final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(uri, rc, false);
final NetworkListener listener = server.getListeners().iterator().next();
listener.registerAddOn(new Http2AddOn());
listener.setSecure(true);
final SSLEngineConfigurator configurator =
new SSLEngineConfigurator(
getSSLContext("keystore.jks"),
false,
false,
false
);
listener.setSSLEngineConfig(configurator);
And, as far as I know, I have all the appropriate dependencies in my pom.xml file:
<dependency>
<groupId>org.glassfish.grizzly</groupId>
<artifactId>grizzly-http2</artifactId>
<version>2.3.24</version>
</dependency>
<dependency>
<groupId>org.glassfish.grizzly</groupId>
<artifactId>grizzly-npn-bootstrap</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.grizzly</groupId>
<artifactId>grizzly-npn-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-http</artifactId>
</dependency>
When I test the server with curl -v --http2 ..., I get
Does someone know what is missing ?
I found what was the problem. It is explained here: http://unrestful.io/2015/10/09/alpn-java.html
Java 8 does not support ALPN. grizzly-npm-bootstrap is there to override system classes to provide that support. As such, it should be added to Xbootclasspath like so:
java -Xbootclasspath/p:/path/to/grizzly-npn-bootstrap.jar
And since it overrides system classes, it needs to be adapted to new versions of the JRE. Since I am using Java 8 and grizzly-npn-bootstrap.jar is only available for OpenJDK 7, I am out of luck.
One option is to switch to Jetty which provides alpn-boot for most (all ?) releases of OpenJDK.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With