jettyhttp2alpn

Check if ALPN is supported


I'm evaluating Jetty alpn-boot for OpenJDK 8 to add support for ALPN (and HTTP/2). However, problem is that the jar file must be put in boot classpath, and I can't ensure it is always present for the webapp in all environments (like, a server at customer).

So I would need a fallback mechanism like:

if (isAlpnSupported()) {
    /* use HTTP/2 protocol */
} else {
    /* fallback legacy code */
}

Can I check if ALPN is supported without making an actual connection to a HTTP/2 server?


Solution

  • Checking whether ALPN is present is a simple check for the existence of the class:

    boolean isALPNPresent() {
        try {
            ClassLoader.getSystemClassLoader().loadClass("org.eclipse.jetty.alpn.ALPN");
            return true;
        } catch (Throwable x) {
            return false;
        }
    }
    

    This would be only useful if your web application performs HTTP requests to another server that supports both HTTP/1.1 and HTTP/2.

    With this check you can configure the HTTP client within your web application to perform the requests with the right protocol.