Skip to content

Commit

Permalink
Provide an Apache HttpClientBuilder configuration callback (#4238)
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Supol <jan.supol@oracle.com>
  • Loading branch information
jansupol authored and senivam committed Sep 5, 2019
1 parent 8c56a39 commit 8e01ccc
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicLong;
Expand Down Expand Up @@ -138,6 +139,12 @@
* be set to {@code true}.
* </p>
* <p>
* Registration of {@link ApacheHttpClientBuilderConfigurator} instance on the
* {@link javax.ws.rs.client.Client#register(Object) Client} is supported. A configuration provided by
* {@link ApacheHttpClientBuilderConfigurator} will override the {@link org.apache.http.impl.client.HttpClientBuilder}
* configuration set by using the properties.
* </p>
* <p>
* If a {@link org.glassfish.jersey.client.ClientResponse} is obtained and an
* entity is not read from the response then
* {@link org.glassfish.jersey.client.ClientResponse#close()} MUST be called
Expand Down Expand Up @@ -317,7 +324,15 @@ class ApacheConnector implements Connector {
this.cookieStore = null;
}
clientBuilder.setDefaultRequestConfig(requestConfig);
this.client = clientBuilder.build();

Optional<Object> contract = config.getInstances().stream()
.filter(a -> ApacheHttpClientBuilderConfigurator.class.isInstance(a)).findFirst();

final HttpClientBuilder configuredBuilder = contract.isPresent()
? ((ApacheHttpClientBuilderConfigurator) contract.get()).configure(clientBuilder)
: null;

this.client = configuredBuilder != null ? configuredBuilder.build() : clientBuilder.build();
}

private HttpClientConnectionManager getConnectionManager(final Client client,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@
* connection-based resources.
* </p>
* <p>
* Registration of {@link ApacheHttpClientBuilderConfigurator} instance on the
* {@link javax.ws.rs.client.Client#register(Object) Client} is supported. A configuration provided by
* {@link ApacheHttpClientBuilderConfigurator} will override the {@link org.apache.http.impl.client.HttpClientBuilder}
* configuration set by using the properties.
* </p>
* <p>
* If a response entity is obtained that is an instance of {@link java.io.Closeable}
* then the instance MUST be closed after processing the entity to release
* connection-based resources.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 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
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.apache.connector;

import org.apache.http.impl.client.HttpClientBuilder;
import org.glassfish.jersey.spi.Contract;

/**
* A callback interface used to configure {@link org.apache.http.impl.client.HttpClientBuilder}. It is called immediately before
* the {@link ApacheConnectorProvider} creates {@link org.apache.http.client.HttpClient}, after the
* {@link org.apache.http.impl.client.HttpClientBuilder} is configured using the properties.
*/
@Contract
public interface ApacheHttpClientBuilderConfigurator {
/**
* A callback method to configure the {@link org.apache.http.impl.client.HttpClientBuilder}
* @param httpClientBuilder {@link org.apache.http.impl.client.HttpClientBuilder} object to be further configured
* @return the configured {@link org.apache.http.impl.client.HttpClientBuilder}. If {@code null} is returned the
* {@code httpClientBuilder} is used by {@link ApacheConnectorProvider} instead.
*/
HttpClientBuilder configure(HttpClientBuilder httpClientBuilder);
}
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 @@ -33,6 +33,7 @@

import javax.inject.Singleton;

import org.apache.http.impl.client.HttpClientBuilder;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
import org.glassfish.jersey.client.authentication.ResponseAuthenticationException;
Expand Down Expand Up @@ -570,6 +571,25 @@ public void testAuthGetQueryParamsDigest() {
.queryParam("param1", "value1")
.queryParam("param2", "value2");
assertEquals("GET 3", r.request().get(String.class));
}

@Test
public void testAuthGetWithConfigurator() {
CredentialsProvider credentialsProvider = new org.apache.http.impl.client.BasicCredentialsProvider();
credentialsProvider.setCredentials(
AuthScope.ANY,
new UsernamePasswordCredentials("name", "password")
);
ApacheHttpClientBuilderConfigurator apacheHttpClientBuilderConfigurator = (httpClientBuilder) -> {
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
};

ClientConfig cc = new ClientConfig();
cc.register(apacheHttpClientBuilderConfigurator);
cc.connectorProvider(new ApacheConnectorProvider());
Client client = ClientBuilder.newClient(cc);
WebTarget r = client.target(getBaseUri()).path("test");

assertEquals("GET", r.request().get(String.class));
}
}

0 comments on commit 8e01ccc

Please sign in to comment.