You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by cs...@apache.org on 2021/05/07 19:52:53 UTC

[maven-resolver] 01/01: [MRESOLVER-103] Move off from HttpClient deprecated classes

This is an automated email from the ASF dual-hosted git repository.

cstamas pushed a commit to branch MRESOLVER-103-move-off-from-deprecated-classes
in repository https://gitbox.apache.org/repos/asf/maven-resolver.git

commit a9f867378d0844705a90f14114c974b2386a7520
Author: Tamas Cservenak <ta...@cservenak.net>
AuthorDate: Fri May 7 21:51:26 2021 +0200

    [MRESOLVER-103] Move off from HttpClient deprecated classes
    
    Just refresh the codebase to latest 4.x classes of HttpClient.
---
 .../aether/transport/http/AuthSchemePool.java      |  4 +-
 .../eclipse/aether/transport/http/GlobalState.java | 35 ++++-----
 .../aether/transport/http/HttpTransporter.java     | 89 +++++++++++++++-------
 .../eclipse/aether/transport/http/LocalState.java  |  9 +--
 .../aether/transport/http/SharingHttpContext.java  | 10 +--
 .../aether/transport/http/SslSocketFactory.java    | 11 ++-
 .../http/X509HostnameVerifierAdapter.java          | 79 -------------------
 7 files changed, 94 insertions(+), 143 deletions(-)

diff --git a/maven-resolver-transport-http/src/main/java/org/eclipse/aether/transport/http/AuthSchemePool.java b/maven-resolver-transport-http/src/main/java/org/eclipse/aether/transport/http/AuthSchemePool.java
index 1957688..384227f 100644
--- a/maven-resolver-transport-http/src/main/java/org/eclipse/aether/transport/http/AuthSchemePool.java
+++ b/maven-resolver-transport-http/src/main/java/org/eclipse/aether/transport/http/AuthSchemePool.java
@@ -22,7 +22,7 @@ package org.eclipse.aether.transport.http;
 import java.util.LinkedList;
 
 import org.apache.http.auth.AuthScheme;
-import org.apache.http.client.params.AuthPolicy;
+import org.apache.http.client.config.AuthSchemes;
 import org.apache.http.impl.auth.BasicScheme;
 
 /**
@@ -47,7 +47,7 @@ final class AuthSchemePool
         {
             authScheme = authSchemes.removeLast();
         }
-        else if ( AuthPolicy.BASIC.equalsIgnoreCase( schemeName ) )
+        else if ( AuthSchemes.BASIC.equalsIgnoreCase( schemeName ) )
         {
             authScheme = new BasicScheme();
         }
diff --git a/maven-resolver-transport-http/src/main/java/org/eclipse/aether/transport/http/GlobalState.java b/maven-resolver-transport-http/src/main/java/org/eclipse/aether/transport/http/GlobalState.java
index 29ef555..03cea76 100644
--- a/maven-resolver-transport-http/src/main/java/org/eclipse/aether/transport/http/GlobalState.java
+++ b/maven-resolver-transport-http/src/main/java/org/eclipse/aether/transport/http/GlobalState.java
@@ -27,11 +27,11 @@ import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 
 import org.apache.http.HttpHost;
-import org.apache.http.conn.ClientConnectionManager;
-import org.apache.http.conn.scheme.PlainSocketFactory;
-import org.apache.http.conn.scheme.Scheme;
-import org.apache.http.conn.scheme.SchemeRegistry;
-import org.apache.http.impl.conn.PoolingClientConnectionManager;
+import org.apache.http.config.RegistryBuilder;
+import org.apache.http.conn.HttpClientConnectionManager;
+import org.apache.http.conn.socket.ConnectionSocketFactory;
+import org.apache.http.conn.socket.PlainConnectionSocketFactory;
+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
 import org.eclipse.aether.RepositoryCache;
 import org.eclipse.aether.RepositorySystemSession;
 import org.eclipse.aether.util.ConfigUtils;
@@ -88,7 +88,7 @@ final class GlobalState
 
     private static final String CONFIG_PROP_CACHE_STATE = "aether.connector.http.cacheState";
 
-    private final ConcurrentMap<SslConfig, ClientConnectionManager> connectionManagers;
+    private final ConcurrentMap<SslConfig, HttpClientConnectionManager> connectionManagers;
 
     private final ConcurrentMap<CompoundKey, Object> userTokens;
 
@@ -141,21 +141,21 @@ final class GlobalState
 
     public void close()
     {
-        for ( Iterator<Map.Entry<SslConfig, ClientConnectionManager>> it = connectionManagers.entrySet().iterator();
+        for ( Iterator<Map.Entry<SslConfig, HttpClientConnectionManager>> it = connectionManagers.entrySet().iterator();
               it.hasNext(); )
         {
-            ClientConnectionManager connMgr = it.next().getValue();
+            HttpClientConnectionManager connMgr = it.next().getValue();
             it.remove();
             connMgr.shutdown();
         }
     }
 
-    public ClientConnectionManager getConnectionManager( SslConfig config )
+    public HttpClientConnectionManager getConnectionManager( SslConfig config )
     {
-        ClientConnectionManager manager = connectionManagers.get( config );
+        HttpClientConnectionManager manager = connectionManagers.get( config );
         if ( manager == null )
         {
-            ClientConnectionManager connMgr = newConnectionManager( config );
+            HttpClientConnectionManager connMgr = newConnectionManager( config );
             manager = connectionManagers.putIfAbsent( config, connMgr );
             if ( manager != null )
             {
@@ -170,13 +170,14 @@ final class GlobalState
     }
 
     @SuppressWarnings( "checkstyle:magicnumber" )
-    public static ClientConnectionManager newConnectionManager( SslConfig sslConfig )
+    public static HttpClientConnectionManager newConnectionManager( SslConfig sslConfig )
     {
-        SchemeRegistry schemeReg = new SchemeRegistry();
-        schemeReg.register( new Scheme( "http", 80, new PlainSocketFactory() ) );
-        schemeReg.register( new Scheme( "https", 443, new SslSocketFactory( sslConfig ) ) );
-
-        PoolingClientConnectionManager connMgr = new PoolingClientConnectionManager( schemeReg );
+        PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(
+                RegistryBuilder.<ConnectionSocketFactory>create()
+                               .register( "http", new PlainConnectionSocketFactory() )
+                               .register( "https", new SslSocketFactory( sslConfig ) )
+                               .build()
+        );
         connMgr.setMaxTotal( 100 );
         connMgr.setDefaultMaxPerRoute( 50 );
         return connMgr;
diff --git a/maven-resolver-transport-http/src/main/java/org/eclipse/aether/transport/http/HttpTransporter.java b/maven-resolver-transport-http/src/main/java/org/eclipse/aether/transport/http/HttpTransporter.java
index 15fa36a..94c5705 100644
--- a/maven-resolver-transport-http/src/main/java/org/eclipse/aether/transport/http/HttpTransporter.java
+++ b/maven-resolver-transport-http/src/main/java/org/eclipse/aether/transport/http/HttpTransporter.java
@@ -23,6 +23,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.InterruptedIOException;
 import java.io.OutputStream;
+import java.io.UncheckedIOException;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.util.Collections;
@@ -40,10 +41,9 @@ import org.apache.http.HttpHost;
 import org.apache.http.HttpResponse;
 import org.apache.http.HttpStatus;
 import org.apache.http.auth.AuthScope;
-import org.apache.http.auth.params.AuthParams;
 import org.apache.http.client.CredentialsProvider;
-import org.apache.http.client.HttpClient;
 import org.apache.http.client.HttpResponseException;
+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;
@@ -51,14 +51,11 @@ import org.apache.http.client.methods.HttpPut;
 import org.apache.http.client.methods.HttpUriRequest;
 import org.apache.http.client.utils.DateUtils;
 import org.apache.http.client.utils.URIUtils;
-import org.apache.http.conn.params.ConnRouteParams;
+import org.apache.http.config.SocketConfig;
 import org.apache.http.entity.AbstractHttpEntity;
 import org.apache.http.entity.ByteArrayEntity;
-import org.apache.http.impl.client.DecompressingHttpClient;
-import org.apache.http.impl.client.DefaultHttpClient;
-import org.apache.http.params.HttpConnectionParams;
-import org.apache.http.params.HttpParams;
-import org.apache.http.params.HttpProtocolParams;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
 import org.apache.http.util.EntityUtils;
 import org.eclipse.aether.ConfigurationProperties;
 import org.eclipse.aether.RepositorySystemSession;
@@ -98,7 +95,7 @@ final class HttpTransporter
 
     private final HttpHost proxy;
 
-    private final HttpClient client;
+    private final CloseableHttpClient client;
 
     private final Map<?, ?> headers;
 
@@ -140,13 +137,16 @@ final class HttpTransporter
             ConfigUtils.getMap( session, Collections.emptyMap(), ConfigurationProperties.HTTP_HEADERS + "."
                 + repository.getId(), ConfigurationProperties.HTTP_HEADERS );
 
-        DefaultHttpClient client = new DefaultHttpClient( state.getConnectionManager() );
+        HttpClientBuilder clientBuilder = HttpClientBuilder.create()
+                                 .setConnectionManager( state.getConnectionManager() )
+                                 .setConnectionManagerShared( true );
 
-        configureClient( client.getParams(), session, repository, proxy );
+        configureClient( clientBuilder, session, repository, proxy );
 
-        client.setCredentialsProvider( toCredentialsProvider( server, repoAuthContext, proxy, proxyAuthContext ) );
+        clientBuilder.setDefaultCredentialsProvider(
+                toCredentialsProvider( server, repoAuthContext, proxy, proxyAuthContext ) );
 
-        this.client = new DecompressingHttpClient( client );
+        this.client = clientBuilder.build();
     }
 
     private static HttpHost toHost( Proxy proxy )
@@ -159,23 +159,48 @@ final class HttpTransporter
         return host;
     }
 
-    private static void configureClient( HttpParams params, RepositorySystemSession session,
+    private static void configureClient( HttpClientBuilder builder, RepositorySystemSession session,
                                          RemoteRepository repository, HttpHost proxy )
     {
-        AuthParams.setCredentialCharset( params, ConfigUtils.getString( session,
-                ConfigurationProperties.DEFAULT_HTTP_CREDENTIAL_ENCODING,
-                ConfigurationProperties.HTTP_CREDENTIAL_ENCODING + "." + repository.getId(),
-                ConfigurationProperties.HTTP_CREDENTIAL_ENCODING ) );
-        ConnRouteParams.setDefaultProxy( params, proxy );
-        HttpConnectionParams.setConnectionTimeout( params, ConfigUtils.getInteger( session,
-                ConfigurationProperties.DEFAULT_CONNECT_TIMEOUT,
-                ConfigurationProperties.CONNECT_TIMEOUT + "." + repository.getId(),
-                ConfigurationProperties.CONNECT_TIMEOUT ) );
-        HttpConnectionParams.setSoTimeout( params, ConfigUtils.getInteger( session,
-                ConfigurationProperties.DEFAULT_REQUEST_TIMEOUT,
-                ConfigurationProperties.REQUEST_TIMEOUT + "." + repository.getId(),
-                ConfigurationProperties.REQUEST_TIMEOUT ) );
-        HttpProtocolParams.setUserAgent( params, ConfigUtils.getString( session,
+//
+//        AuthParams.setCredentialCharset( params, ConfigUtils.getString( session,
+//                ConfigurationProperties.DEFAULT_HTTP_CREDENTIAL_ENCODING,
+//                ConfigurationProperties.HTTP_CREDENTIAL_ENCODING + "." + repository.getId(),
+//                ConfigurationProperties.HTTP_CREDENTIAL_ENCODING ) );
+
+        SocketConfig.Builder socketConfig = SocketConfig.custom();
+        socketConfig.setSoTimeout(
+                ConfigUtils.getInteger( session,
+                        ConfigurationProperties.DEFAULT_REQUEST_TIMEOUT,
+                        ConfigurationProperties.REQUEST_TIMEOUT + "." + repository.getId(),
+                        ConfigurationProperties.REQUEST_TIMEOUT )
+        );
+        builder.setDefaultSocketConfig( socketConfig.build() );
+
+        RequestConfig.Builder requestConfig = RequestConfig.custom();
+        requestConfig.setProxy( proxy );
+        requestConfig.setContentCompressionEnabled( true );
+//        requestConfig.setConnectionRequestTimeout(
+//                ConfigUtils.getInteger( session,
+//                        ConfigurationProperties.DEFAULT_REQUEST_TIMEOUT,
+//                        ConfigurationProperties.REQUEST_TIMEOUT + "." + repository.getId(),
+//                        ConfigurationProperties.REQUEST_TIMEOUT )
+//        );
+        requestConfig.setSocketTimeout(
+                ConfigUtils.getInteger( session,
+                        ConfigurationProperties.DEFAULT_REQUEST_TIMEOUT,
+                        ConfigurationProperties.REQUEST_TIMEOUT + "." + repository.getId(),
+                        ConfigurationProperties.REQUEST_TIMEOUT )
+        );
+        requestConfig.setConnectTimeout(
+                ConfigUtils.getInteger( session,
+                        ConfigurationProperties.DEFAULT_CONNECT_TIMEOUT,
+                        ConfigurationProperties.CONNECT_TIMEOUT + "." + repository.getId(),
+                        ConfigurationProperties.CONNECT_TIMEOUT )
+        );
+        builder.setDefaultRequestConfig( requestConfig.build() );
+
+        builder.setUserAgent( ConfigUtils.getString( session,
                 ConfigurationProperties.DEFAULT_USER_AGENT,
                 ConfigurationProperties.USER_AGENT ) );
     }
@@ -476,6 +501,14 @@ final class HttpTransporter
     @Override
     protected void implClose()
     {
+        try
+        {
+            client.close();
+        }
+        catch ( IOException e )
+        {
+            throw new UncheckedIOException( e );
+        }
         AuthenticationContext.close( repoAuthContext );
         AuthenticationContext.close( proxyAuthContext );
         state.close();
diff --git a/maven-resolver-transport-http/src/main/java/org/eclipse/aether/transport/http/LocalState.java b/maven-resolver-transport-http/src/main/java/org/eclipse/aether/transport/http/LocalState.java
index ebc5bd5..9034a8e 100644
--- a/maven-resolver-transport-http/src/main/java/org/eclipse/aether/transport/http/LocalState.java
+++ b/maven-resolver-transport-http/src/main/java/org/eclipse/aether/transport/http/LocalState.java
@@ -19,13 +19,12 @@ package org.eclipse.aether.transport.http;
  * under the License.
  */
 
-import java.io.Closeable;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 
 import org.apache.http.HttpHost;
 import org.apache.http.auth.AuthScheme;
-import org.apache.http.conn.ClientConnectionManager;
+import org.apache.http.conn.HttpClientConnectionManager;
 import org.eclipse.aether.RepositorySystemSession;
 import org.eclipse.aether.repository.RemoteRepository;
 import org.eclipse.aether.transport.http.GlobalState.CompoundKey;
@@ -35,12 +34,10 @@ import org.eclipse.aether.transport.http.GlobalState.CompoundKey;
  * communication with server.
  */
 final class LocalState
-    implements Closeable
 {
-
     private final GlobalState global;
 
-    private final ClientConnectionManager connMgr;
+    private final HttpClientConnectionManager connMgr;
 
     private final CompoundKey userTokenKey;
 
@@ -74,7 +71,7 @@ final class LocalState
         }
     }
 
-    public ClientConnectionManager getConnectionManager()
+    public HttpClientConnectionManager getConnectionManager()
     {
         return connMgr;
     }
diff --git a/maven-resolver-transport-http/src/main/java/org/eclipse/aether/transport/http/SharingHttpContext.java b/maven-resolver-transport-http/src/main/java/org/eclipse/aether/transport/http/SharingHttpContext.java
index 33b8b2f..e47b3d1 100644
--- a/maven-resolver-transport-http/src/main/java/org/eclipse/aether/transport/http/SharingHttpContext.java
+++ b/maven-resolver-transport-http/src/main/java/org/eclipse/aether/transport/http/SharingHttpContext.java
@@ -21,7 +21,7 @@ package org.eclipse.aether.transport.http;
 
 import java.io.Closeable;
 
-import org.apache.http.client.protocol.ClientContext;
+import org.apache.http.client.protocol.HttpClientContext;
 import org.apache.http.protocol.BasicHttpContext;
 
 /**
@@ -43,13 +43,13 @@ final class SharingHttpContext
     {
         this.state = state;
         authCache = new SharingAuthCache( state );
-        super.setAttribute( ClientContext.AUTH_CACHE, authCache );
+        super.setAttribute( HttpClientContext.AUTH_CACHE, authCache );
     }
 
     @Override
     public Object getAttribute( String id )
     {
-        if ( ClientContext.USER_TOKEN.equals( id ) )
+        if ( HttpClientContext.USER_TOKEN.equals( id ) )
         {
             return state.getUserToken();
         }
@@ -59,7 +59,7 @@ final class SharingHttpContext
     @Override
     public void setAttribute( String id, Object obj )
     {
-        if ( ClientContext.USER_TOKEN.equals( id ) )
+        if ( HttpClientContext.USER_TOKEN.equals( id ) )
         {
             state.setUserToken( obj );
         }
@@ -72,7 +72,7 @@ final class SharingHttpContext
     @Override
     public Object removeAttribute( String id )
     {
-        if ( ClientContext.USER_TOKEN.equals( id ) )
+        if ( HttpClientContext.USER_TOKEN.equals( id ) )
         {
             state.setUserToken( null );
             return null;
diff --git a/maven-resolver-transport-http/src/main/java/org/eclipse/aether/transport/http/SslSocketFactory.java b/maven-resolver-transport-http/src/main/java/org/eclipse/aether/transport/http/SslSocketFactory.java
index 4426da4..e74e577 100644
--- a/maven-resolver-transport-http/src/main/java/org/eclipse/aether/transport/http/SslSocketFactory.java
+++ b/maven-resolver-transport-http/src/main/java/org/eclipse/aether/transport/http/SslSocketFactory.java
@@ -26,7 +26,7 @@ import javax.net.ssl.SSLContext;
 import javax.net.ssl.SSLSocket;
 import javax.net.ssl.SSLSocketFactory;
 
-import org.apache.http.conn.ssl.X509HostnameVerifier;
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
 
 /**
  * Specialized SSL socket factory to more closely resemble the JRE's HttpsClient and respect well-known SSL-related
@@ -36,7 +36,7 @@ import org.apache.http.conn.ssl.X509HostnameVerifier;
  *      Reference Guide, Customization</a>
  */
 final class SslSocketFactory
-    extends org.apache.http.conn.ssl.SSLSocketFactory
+    extends org.apache.http.conn.ssl.SSLConnectionSocketFactory
 {
 
     private final String[] cipherSuites;
@@ -54,13 +54,12 @@ final class SslSocketFactory
         return ( context != null ) ? context.getSocketFactory() : (SSLSocketFactory) SSLSocketFactory.getDefault();
     }
 
-    private static X509HostnameVerifier getHostnameVerifier( HostnameVerifier verifier )
+    private static HostnameVerifier getHostnameVerifier( HostnameVerifier verifier )
     {
-        return ( verifier != null ) ? X509HostnameVerifierAdapter.adapt( verifier )
-                        : org.apache.http.conn.ssl.SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER;
+        return ( verifier != null ) ? verifier : SSLConnectionSocketFactory.getDefaultHostnameVerifier();
     }
 
-    private SslSocketFactory( SSLSocketFactory socketfactory, X509HostnameVerifier hostnameVerifier,
+    private SslSocketFactory( SSLSocketFactory socketfactory, HostnameVerifier hostnameVerifier,
                               String[] cipherSuites, String[] protocols )
     {
         super( socketfactory, hostnameVerifier );
diff --git a/maven-resolver-transport-http/src/main/java/org/eclipse/aether/transport/http/X509HostnameVerifierAdapter.java b/maven-resolver-transport-http/src/main/java/org/eclipse/aether/transport/http/X509HostnameVerifierAdapter.java
deleted file mode 100644
index ac0825b..0000000
--- a/maven-resolver-transport-http/src/main/java/org/eclipse/aether/transport/http/X509HostnameVerifierAdapter.java
+++ /dev/null
@@ -1,79 +0,0 @@
-package org.eclipse.aether.transport.http;
-
-/*
- * 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
- * 
- *  http://www.apache.org/licenses/LICENSE-2.0
- * 
- * 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.
- */
-
-import java.io.IOException;
-import java.security.cert.X509Certificate;
-
-import javax.net.ssl.HostnameVerifier;
-import javax.net.ssl.SSLException;
-import javax.net.ssl.SSLSession;
-import javax.net.ssl.SSLSocket;
-
-import org.apache.http.conn.ssl.X509HostnameVerifier;
-
-/**
- * Makes a standard hostname verifier compatible with Apache HttpClient's API.
- */
-final class X509HostnameVerifierAdapter
-    implements X509HostnameVerifier
-{
-
-    private final HostnameVerifier verifier;
-
-    public static X509HostnameVerifier adapt( HostnameVerifier verifier )
-    {
-        if ( verifier instanceof X509HostnameVerifier )
-        {
-            return (X509HostnameVerifier) verifier;
-        }
-        return new X509HostnameVerifierAdapter( verifier );
-    }
-
-    private X509HostnameVerifierAdapter( HostnameVerifier verifier )
-    {
-        this.verifier = verifier;
-    }
-
-    public boolean verify( String hostname, SSLSession session )
-    {
-        return verifier.verify( hostname, session );
-    }
-
-    public void verify( String host, SSLSocket socket )
-        throws IOException
-    {
-        if ( !verify( host, socket.getSession() ) )
-        {
-            throw new SSLException( "<" + host + "> does not pass hostname verification" );
-        }
-    }
-
-    public void verify( String host, X509Certificate cert )
-    {
-        throw new UnsupportedOperationException();
-    }
-
-    public void verify( String host, String[] cns, String[] subjectAlts )
-    {
-        throw new UnsupportedOperationException();
-    }
-
-}