Merge pull request #376 from coheigea/httpclient

Update HTTPClient and replace deprecated calls
This commit is contained in:
Jean-Baptiste Onofré 2019-07-26 11:23:02 +02:00 committed by GitHub
commit 3ab1115e9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 43 additions and 107 deletions

View File

@ -38,7 +38,7 @@ import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -53,7 +53,7 @@ public class HTTPDiscoveryAgent implements DiscoveryAgent, Suspendable {
private static final Logger LOG = LoggerFactory.getLogger(HTTPDiscoveryAgent.class);
private String registryURL = "http://localhost:8080/discovery-registry/default";
private HttpClient httpClient = new DefaultHttpClient();
private HttpClient httpClient = HttpClientBuilder.create().build();
private AtomicBoolean running = new AtomicBoolean();
private final AtomicReference<DiscoveryListener> discoveryListener = new AtomicReference<DiscoveryListener>();
private final HashSet<String> registeredServices = new HashSet<String>();

View File

@ -40,26 +40,23 @@ import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpOptions;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.AbstractHttpMessage;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
@ -317,9 +314,10 @@ public class HttpClientTransport extends HttpTransportSupport {
}
protected HttpClient createHttpClient() {
DefaultHttpClient client = new DefaultHttpClient(createClientConnectionManager());
HttpClientBuilder clientBuilder = HttpClientBuilder.create();
clientBuilder.setConnectionManager(createClientConnectionManager());
if (useCompression) {
client.addRequestInterceptor( new HttpRequestInterceptor() {
clientBuilder.addInterceptorLast(new HttpRequestInterceptor() {
@Override
public void process(HttpRequest request, HttpContext context) {
// We expect to received a compression response that we un-gzip
@ -327,31 +325,30 @@ public class HttpClientTransport extends HttpTransportSupport {
}
});
}
RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
if (getProxyHost() != null) {
HttpHost proxy = new HttpHost(getProxyHost(), getProxyPort());
client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
requestConfigBuilder.setProxy(proxy);
if (client.getConnectionManager().getSchemeRegistry().get("http") == null) {
client.getConnectionManager().getSchemeRegistry().register(
new Scheme("http", getProxyPort(), PlainSocketFactory.getSocketFactory()));
}
if(getProxyUser() != null && getProxyPassword() != null) {
client.getCredentialsProvider().setCredentials(
if (getProxyUser() != null && getProxyPassword() != null) {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(
new AuthScope(getProxyHost(), getProxyPort()),
new UsernamePasswordCredentials(getProxyUser(), getProxyPassword()));
clientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
}
HttpParams params = client.getParams();
HttpConnectionParams.setSoTimeout(params, soTimeout);
HttpClientParams.setCookiePolicy(params, CookiePolicy.BROWSER_COMPATIBILITY);
requestConfigBuilder.setSocketTimeout(soTimeout);
requestConfigBuilder.setCookieSpec(CookieSpecs.DEFAULT);
clientBuilder.setDefaultRequestConfig(requestConfigBuilder.build());
return client;
return clientBuilder.build();
}
protected ClientConnectionManager createClientConnectionManager() {
return new PoolingClientConnectionManager();
protected HttpClientConnectionManager createClientConnectionManager() {
return new PoolingHttpClientConnectionManager();
}
protected void configureMethod(AbstractHttpMessage method) {

View File

@ -24,11 +24,13 @@ import org.apache.activemq.broker.SslContext;
import org.apache.activemq.transport.http.HttpClientTransport;
import org.apache.activemq.transport.util.TextWireFormat;
import org.apache.activemq.util.IOExceptionSupport;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.ssl.DefaultHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
public class HttpsClientTransport extends HttpClientTransport {
@ -37,19 +39,17 @@ public class HttpsClientTransport extends HttpClientTransport {
}
@Override
protected ClientConnectionManager createClientConnectionManager() {
PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager(createSchemeRegistry());
return connectionManager;
protected HttpClientConnectionManager createClientConnectionManager() {
return new PoolingHttpClientConnectionManager(createRegistry());
}
private SchemeRegistry createSchemeRegistry() {
private Registry<ConnectionSocketFactory> createRegistry() {
SchemeRegistry schemeRegistry = new SchemeRegistry();
RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.<ConnectionSocketFactory>create();
try {
SSLSocketFactory sslSocketFactory = new SSLSocketFactory(createSocketFactory(),
SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
schemeRegistry.register(new Scheme("https", getRemoteUrl().getPort(), sslSocketFactory));
return schemeRegistry;
SSLConnectionSocketFactory sslConnectionFactory = new SSLConnectionSocketFactory(createSocketFactory(), new DefaultHostnameVerifier());
registryBuilder.register("https", sslConnectionFactory);
return registryBuilder.build();
} catch (Exception e) {
throw new IllegalStateException("Failure trying to create scheme registry", e);
}

View File

@ -1,61 +0,0 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.transport.http;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.activemq.transport.util.TextWireFormat;
import org.apache.http.client.HttpClient;
import org.apache.http.client.params.HttpClientParams;
import org.junit.Before;
import org.junit.Test;
/**
* Test that {@link HttpClientTransport} sets a broad-range compatibility
* cookie policy.
*
* @see <a href="https://issues.apache.org/jira/browse/AMQ-6571">AMQ-6571: HttpClientTransport refuses to accept cookies using `Expires' header</a>
*/
@SuppressWarnings("deprecation")
public class HttpClientTransportCookiePolicyTest {
private HttpClientTransport transport;
/**
* Create the transport so we can inspect it.
* @throws URISyntaxException if something goes wrong.
*/
@Before
public void setUp() throws URISyntaxException {
transport = new HttpClientTransport(mock(TextWireFormat.class), new URI("http://localhost:8080/test"));
}
/**
* Create a new connection and check the connection properties.
*/
@Test
public void test() {
HttpClient client = transport.createHttpClient();
assertEquals("Cookie spec", org.apache.http.client.params.CookiePolicy.BROWSER_COMPATIBILITY, HttpClientParams.getCookiePolicy(client.getParams()));
}
}

View File

@ -57,14 +57,14 @@ public abstract class ClassLoaderSPIConnectionFactory implements SPIConnectionFa
} else {
LOG.info("Adding extension dir: " + f.getAbsolutePath());
urls.add(f.toURL());
urls.add(f.toURI().toURL());
File[] files = f.listFiles();
if (files != null) {
for (int j = 0; j < files.length; j++) {
if (files[j].getName().endsWith(".zip") || files[j].getName().endsWith(".jar")) {
LOG.info("Adding extension dir: " + files[j].getAbsolutePath());
urls.add(files[j].toURL());
urls.add(files[j].toURI().toURL());
}
}
}

View File

@ -69,8 +69,8 @@
<hawtdispatch-version>1.22</hawtdispatch-version>
<howl-version>0.1.8</howl-version>
<hsqldb-version>1.8.0.12</hsqldb-version>
<httpclient-version>4.5.6</httpclient-version>
<httpcore-version>4.4.10</httpcore-version>
<httpclient-version>4.5.9</httpclient-version>
<httpcore-version>4.4.11</httpcore-version>
<insight-version>1.2.0.Beta4</insight-version>
<jackson-version>2.9.9</jackson-version>
<jackson-databind-version>2.9.9.1</jackson-databind-version>