Search code examples
javassltomcathttp2apache-httpclient-5.x

How to make Conscrypt SSL Provider with HttpClient 5 work in Tomcat web app


I'm using Apache HttpClient 5 along with Conscrypt to perform simultaneous HTTP 2.0 requests over SSL as shown below:

final SSLContext sslContext;
    try {
        sslContext = SSLContexts.custom()
                .setProvider(Conscrypt.newProvider())
                .build();
    } catch (Exception e) {
         // ... omitted for brevity
    }

    final PoolingAsyncClientConnectionManager cm = PoolingAsyncClientConnectionManagerBuilder.create()
            .setTlsStrategy(new ConscriptClientTlsStrategy(sslContext))
            .build();

    final CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom()
            .setVersionPolicy(HttpVersionPolicy.NEGOTIATE)
            .setConnectionManager(cm)
            .build();

    try {
        httpclient.start();
        HttpHost host = new HttpHost("www.wikidata.org");
        final HttpClientContext clientContext = HttpClientContext.create();

        final SimpleHttpRequest request = SimpleHttpRequests.GET.create(host, "/w/api.php?action=wbsearchentities&search=Washington");
            request.addHeader("Accept-Charset", charset);
            FutureCallback<SimpleHttpResponse> callback = // ... omitted for brevity
            httpclient.execute(SimpleRequestProducer.create(request),
                    SimpleResponseConsumer.create(),
                    clientContext,
                    callback);
     }
     catch (Exception e) {
         // ... omitted for brevity
     }

Running the code in a unit test is successful. However, if run as part of a web application running in Tomcat v8, it throws the following exception at the call Conscrypt.newProvider(): message: java.lang.UnsatisfiedLinkError: Failed creating temp file (null). It looks like a privilege problem. Could someone point out what in this context should I configure to solve the problem?


Solution

  • You need to set conscrypt-openjdk-uber-1.4.2.jar in classpath instead of conscrypt-openjdk-1.4.2.jar, hope this solves your problem as uber jar will have all the dependencies required for conscrypt.

    Also use Http2AsyncClientBuilder instead of HttpAsyncClients for making http2 multiplexing.