Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedded Java server using Grizzly: How do you enable http2

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

  • ...
  • ALPN/NPN, server did not agree to a protocol
  • SSL connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
  • ...

Does someone know what is missing ?

like image 705
Pierre-Luc Carmel Biron Avatar asked Oct 19 '22 09:10

Pierre-Luc Carmel Biron


1 Answers

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.

like image 184
Pierre-Luc Carmel Biron Avatar answered Oct 21 '22 02:10

Pierre-Luc Carmel Biron