Skip to content

Instantly share code, notes, and snippets.

@lounagen
Last active May 6, 2020 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lounagen/e6de7cb6b37cf45b26a2563505dfb666 to your computer and use it in GitHub Desktop.
Save lounagen/e6de7cb6b37cf45b26a2563505dfb666 to your computer and use it in GitHub Desktop.
Configure proxy on Jersey 2.x with Apache connector and JVM properties

Configure Jersey to support proxy, taking into account the regular JVM properties

The issue with the standard Jersey 2.x proxy configuration is it doesn't allow nonProxyHosts option. It doesn't allow to separate http and https calls too, but these limitations were ok for me.

In maven3 pom.xml:

  <properties>
    <jersey.version>2.30.1</jersey.version>
  </properties>
  <dependency>
    <groupId>org.glassfish.jersey.connectors</groupId>
    <artifactId>jersey-apache-connector</artifactId>
    <version>${jersey.version}</version>
  </dependency>
ClientConfig config = new ClientConfig()

    // Apache connector to active the proxy settings
    // EDIT: comment this line as it seems to be useless when using ApacheHttpClientBuilderConfigurator (below) and it provokes random hangs
    //.connectorProvider(new ApacheConnectorProvider())

    // Register specific features and black-magic Jersey behaviors
    .register(JacksonFeature.class)

    // By registering this magic lambda (Found after debugging both Jersey and HttpClient)
    // We fallback on the regular JVM proxy settings properties, and avoid the restricted
    // jersey properties.
    //
    // Jersey proxy properties are restrictive because they ignore nonProxyHosts.
    // Jersey properties:
    // .property(ClientProperties.PROXY_URI, "http://host:port")
    // .property(ClientProperties.PROXY_USERNAME, "myProxyUser")
    // .property(ClientProperties.PROXY_PASSWORD, "myProxyPassword")
    //
    // To be able to take into account regular JVM proxy properties:
    // For HTTP: -Dhttp.proxyHost=http.proxy.example.com -Dhttp.proxyPort=10080
    // For HTTPS: -Dhttps.proxyHost=https.proxy.example.com -Dhttps.proxyPort=10443
    // Common for BOTH http and https: -Dhttp.nonProxyHosts=foo.example.com|bar.example.com|*baz.example.com
    // Auth NTLM: -Dhttp.proxyUser=MyDomain/username or -Dhttp.auth.ntlm.domain=MyDomain
    // Auth Basic: -Dhttp.proxyUser=username or -Dhttp.proxyPassword=password
    .register(
        ((ApacheHttpClientBuilderConfigurator)
            httpClientBuilder -> {
              RequestConfig requestConfig =
                  RequestConfig.custom()
                      .setConnectTimeout(5000)
                      .setConnectionRequestTimeout(5000)
                      .setSocketTimeout(5000)
                      .build();
              httpClientBuilder.setDefaultRequestConfig(requestConfig);

              httpClientBuilder.useSystemProperties();
              return httpClientBuilder;
            }))

    // Register specific properties
    .property(ClientProperties.CONNECT_TIMEOUT, 5000)
    .property(ClientProperties.READ_TIMEOUT, 5000)
    .property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment