Skip to content

Commit

Permalink
Added support for Apache HTTP Client ConnectionKeepAliveStrategy and …
Browse files Browse the repository at this point in the history
…ConnectionReuseStrategy (#4202)

* Added support for Apache HTTP Client ConnectionKeepAliveStrategy and
ConnectionReuseStrategy

Signed-off-by: Richard A Sand <rsand@idfconnect.com>
  • Loading branch information
rsandidfc authored and senivam committed Aug 12, 2019
1 parent 3dd8549 commit 8e04007
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2019 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -134,6 +134,28 @@ public final class ApacheClientProperties {
*/
public static final String RETRY_HANDLER = "jersey.config.apache.client.retryHandler";

/**
* ConnectionReuseStrategy for the {@link org.apache.http.client.HttpClient}.
* <p/>
* The value MUST be an instance of {@link org.apache.http.impl.ConnectionReuseStrategy}.
* <p/>
* If the property is absent the default reuse strategy of the Apache HTTP library will be used
* <p/>
* The name of the configuration property is <tt>{@value}</tt>.
*/
public static final String REUSE_STRATEGY = "jersey.config.apache.client.reuseStrategy";

/**
* ConnectionKeepAliveStrategy for the {@link org.apache.http.client.HttpClient}.
* <p/>
* The value MUST be an instance of {@link org.apache.http.conn.ConnectionKeepAliveStrategy}.
* <p/>
* If the property is absent the default keepalive strategy of the Apache HTTP library will be used
* <p/>
* The name of the configuration property is <tt>{@value}</tt>.
*/
public static final String KEEPALIVE_STRATEGY = "jersey.config.apache.client.keepAliveStrategy";

/**
* Get the value of the specified property.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2019 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -56,7 +56,7 @@
import org.glassfish.jersey.message.internal.OutboundMessageContext;
import org.glassfish.jersey.message.internal.ReaderWriter;
import org.glassfish.jersey.message.internal.Statuses;

import org.apache.http.ConnectionReuseStrategy;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
Expand All @@ -76,6 +76,7 @@
import org.apache.http.config.ConnectionConfig;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.ConnectionKeepAliveStrategy;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.conn.ManagedHttpClientConnection;
import org.apache.http.conn.routing.HttpRoute;
Expand Down Expand Up @@ -111,12 +112,14 @@
* <li>{@link ApacheClientProperties#REQUEST_CONFIG}</li>
* <li>{@link ApacheClientProperties#CREDENTIALS_PROVIDER}</li>
* <li>{@link ApacheClientProperties#DISABLE_COOKIES}</li>
* <li>{@link ClientProperties#PROXY_URI}</li>
* <li>{@link ClientProperties#PROXY_USERNAME}</li>
* <li>{@link ClientProperties#PROXY_PASSWORD}</li>
* <li>{@link ClientProperties#REQUEST_ENTITY_PROCESSING} - default value is {@link RequestEntityProcessing#CHUNKED}</li>
* <li>{@link ApacheClientProperties#KEEPALIVE_STRATEGY}</li>
* <li>{@link org.glassfish.jersey.client.ClientProperties#PROXY_URI}</li>
* <li>{@link org.glassfish.jersey.client.ClientProperties#PROXY_USERNAME}</li>
* <li>{@link org.glassfish.jersey.client.ClientProperties#PROXY_PASSWORD}</li>
* <li>{@link org.glassfish.jersey.client.ClientProperties#REQUEST_ENTITY_PROCESSING} - default value is {@link org.glassfish.jersey.client.RequestEntityProcessing#CHUNKED}</li>
* <li>{@link ApacheClientProperties#PREEMPTIVE_BASIC_AUTHENTICATION}</li>
* <li>{@link ApacheClientProperties#RETRY_HANDLER}</li>
* <li>{@link ApacheClientProperties#REUSE_STRATEGY}</li>
* </ul>
* <p>
* This connector uses {@link RequestEntityProcessing#CHUNKED chunked encoding} as a default setting. This can
Expand Down Expand Up @@ -197,6 +200,34 @@ class ApacheConnector implements Connector {
}
}

Object keepAliveStrategy = config.getProperties().get(ApacheClientProperties.KEEPALIVE_STRATEGY);
if (keepAliveStrategy != null) {
if (!(keepAliveStrategy instanceof ConnectionKeepAliveStrategy)) {
LOGGER.log(
Level.WARNING,
LocalizationMessages.IGNORING_VALUE_OF_PROPERTY(
ApacheClientProperties.KEEPALIVE_STRATEGY,
keepAliveStrategy.getClass().getName(),
ConnectionKeepAliveStrategy.class.getName())
);
keepAliveStrategy = null;
}
}

Object reuseStrategy = config.getProperties().get(ApacheClientProperties.REUSE_STRATEGY);
if (reuseStrategy != null) {
if (!(reuseStrategy instanceof ConnectionReuseStrategy)) {
LOGGER.log(
Level.WARNING,
LocalizationMessages.IGNORING_VALUE_OF_PROPERTY(
ApacheClientProperties.REUSE_STRATEGY,
reuseStrategy.getClass().getName(),
ConnectionReuseStrategy.class.getName())
);
reuseStrategy = null;
}
}

Object reqConfig = config.getProperties().get(ApacheClientProperties.REQUEST_CONFIG);
if (reqConfig != null) {
if (!(reqConfig instanceof RequestConfig)) {
Expand All @@ -217,7 +248,13 @@ class ApacheConnector implements Connector {
clientBuilder.setConnectionManager(getConnectionManager(client, config, sslContext));
clientBuilder.setConnectionManagerShared(
PropertiesHelper.getValue(config.getProperties(), ApacheClientProperties.CONNECTION_MANAGER_SHARED, false, null));
clientBuilder.setSslcontext(sslContext);
clientBuilder.setSSLContext(sslContext);
if (keepAliveStrategy != null) {
clientBuilder.setKeepAliveStrategy((ConnectionKeepAliveStrategy) keepAliveStrategy);
}
if (reuseStrategy != null) {
clientBuilder.setConnectionReuseStrategy((ConnectionReuseStrategy) reuseStrategy);
}

final RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2019 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -37,13 +37,15 @@
* <li>{@link ApacheClientProperties#REQUEST_CONFIG}</li>
* <li>{@link ApacheClientProperties#CREDENTIALS_PROVIDER}</li>
* <li>{@link ApacheClientProperties#DISABLE_COOKIES}</li>
* <li>{@link ApacheClientProperties#KEEPALIVE_STRATEGY}</li>
* <li>{@link org.glassfish.jersey.client.ClientProperties#PROXY_URI}</li>
* <li>{@link org.glassfish.jersey.client.ClientProperties#PROXY_USERNAME}</li>
* <li>{@link org.glassfish.jersey.client.ClientProperties#PROXY_PASSWORD}</li>
* <li>{@link org.glassfish.jersey.client.ClientProperties#REQUEST_ENTITY_PROCESSING}
* - default value is {@link org.glassfish.jersey.client.RequestEntityProcessing#CHUNKED}</li>
* <li>{@link ApacheClientProperties#PREEMPTIVE_BASIC_AUTHENTICATION}</li>
* <li>{@link ApacheClientProperties#RETRY_HANDLER}</li>
* <li>{@link ApacheClientProperties#REUSE_STRATEGY}</li>
* </ul>
* </p>
* <p>
Expand Down

0 comments on commit 8e04007

Please sign in to comment.