3

Ref: https://jersey.java.net/documentation/latest/user-guide.html#d0e4337. I am trying to use the ApacheConnector as a Connector for the jersey-client. The client seemed to work fine in the 2.4.1 version of jersey-client and apache connector.

The usage docs on the site mentioned has a note:

This API has changed in Jersey 2.5, where the ConnectorProvider SPI has been introduced in order to decouple client initialization from the connector instantiation. Starting with Jersey 2.5 it is therefore not possible to directly register Connector instances in the Jersey ClientConfig. The new ConnectorProvider SPI must be used instead to configure a custom client-side transport connector.

public  Client configureDefaultJerseyClient(String host) throws Exception
{
    String certFilePath = InstallCert.doInstall(host,SSL_PORT,TRUST_STORE_PASSWORD);
    if(EMPTY_STRING.equals(certFilePath))
    {
        throw new Exception("Error while installing certificate for host " + host);
    }
    ClientConfig clientConfig = new ClientConfig();

    /* As the PoolingClientConnectionManager is a deprecated class, the client will
    not support the multithreaded requests. Commenting the code below to avoid using
    deprecated class. In order to test we would be instantiating multiple clients to
    serve the multithreaded requests.*/

    clientConfig.property(ApacheClientProperties.CONNECTION_MANAGER, new PoolingHttpClientConnectionManager());

    SslConfigurator sslConfig = defaultSslConfigurator(certFilePath);
    clientConfig.property(ApacheClientProperties.SSL_CONFIG, sslConfig);    

    SSLContext sslContext = sslConfig.createSSLContext();
    clientConfig.property(ApacheClientProperties.SSL_CONFIG, sslConfig);

    Client client = ClientBuilder.newBuilder().sslContext(sslContext).build();
    client.register(new MyFilter());
    client.register(new org.glassfish.jersey.filter.LoggingFilter());

    ApacheConnectorProvider provider = new ApacheConnectorProvider();
    provider.getConnector(client, clientConfig);

    return client;
}

But it seems the client always uses the default HttpUrlConnection as a connector. How do I use the connector configured for the client?

Vivek Jain
  • 3,811
  • 6
  • 30
  • 47
Varun
  • 86
  • 1
  • 1
  • 8

1 Answers1

6

Set the connector to the ClientConfig not other way around (ConnectorProvider#getConnector isn't supposed to be called by users but by Jersey Client, it's part of SPI):

ClientConfig clientConfig = new ClientConfig();
clientConfig.connectorProvider(new ApacheConnectorProvider());
Client client = ClientBuilder.newClient(clientConfig);

This is described in Jersey User Guide - Client Transport Connectors.

Michal Gajdos
  • 10,339
  • 2
  • 43
  • 42
  • Thanks... I was able to get it working :). I was not able to find as to how the default constructor was being called and thats the reason i tried to call provider.getConnector(client, clientConfig); . I debugged the code and got that at runtime the appropriate connectors are called. – Varun Feb 13 '14 at 08:15
  • how can I set in the provider the proxy server? – Dejell Mar 23 '15 at 17:44
  • Be careful if you're going to use Connection pooling through the ApacheConnectorProvider. It is broken for Jersey versions < 2.29 as the connection doesn't get released back to the pool. I spent 3 days trying to debug a connection leak and it turned out to be a bug in the connector. https://github.com/eclipse-ee4j/jersey/pull/3861 – isopropylcyanide Aug 03 '20 at 22:11
  • This is a great solution for this: https://stackoverflow.com/questions/22355235/patch-request-using-jersey-client – Bjqn Feb 24 '22 at 07:40