maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gb...@apache.org
Subject [maven-javadoc-plugin] 01/01: [MJAVADOC-516] Replace usage of deprecated HttpClient code
Date Fri, 02 Mar 2018 19:35:21 GMT
This is an automated email from the ASF dual-hosted git repository.

gboue pushed a commit to branch MJAVADOC-516
in repository https://gitbox.apache.org/repos/asf/maven-javadoc-plugin.git

commit 14c837c46f391eba23e5bf99536133c1ae177053
Author: Guillaume Boué <gboue@apache.org>
AuthorDate: Fri Mar 2 20:34:47 2018 +0100

    [MJAVADOC-516] Replace usage of deprecated HttpClient code
    
    Instead of creating a mutable DefaultHttpClient, we now use builder
    classes and the new API introduced in 4.3, which deprecates the old one.
    The configuration of the HttpClient stays the same.
    
    Since the new API is guaranteeing a CloseableHttpClient,
    try-with-resources can used.
---
 .../apache/maven/plugins/javadoc/JavadocUtil.java  | 76 ++++++++--------------
 1 file changed, 28 insertions(+), 48 deletions(-)

diff --git a/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java b/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java
index e60699f..090b480 100644
--- a/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java
+++ b/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java
@@ -26,15 +26,16 @@ import org.apache.http.HttpStatus;
 import org.apache.http.auth.AuthScope;
 import org.apache.http.auth.Credentials;
 import org.apache.http.auth.UsernamePasswordCredentials;
-import org.apache.http.client.HttpClient;
+import org.apache.http.client.CredentialsProvider;
+import org.apache.http.client.config.RequestConfig;
 import org.apache.http.client.methods.HttpGet;
-import org.apache.http.client.params.ClientPNames;
 import org.apache.http.client.protocol.HttpClientContext;
-import org.apache.http.conn.params.ConnRoutePNames;
-import org.apache.http.impl.client.DefaultHttpClient;
-import org.apache.http.impl.conn.PoolingClientConnectionManager;
-import org.apache.http.params.CoreConnectionPNames;
-import org.apache.http.params.CoreProtocolPNames;
+import org.apache.http.impl.client.BasicCredentialsProvider;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
 import org.apache.maven.plugin.logging.Log;
 import org.apache.maven.project.MavenProject;
 import org.apache.maven.settings.Proxy;
@@ -72,7 +73,6 @@ import java.io.OutputStreamWriter;
 import java.io.PrintStream;
 import java.io.UnsupportedEncodingException;
 import java.lang.reflect.Modifier;
-import java.net.SocketTimeoutException;
 import java.net.URI;
 import java.net.URL;
 import java.net.URLClassLoader;
@@ -1657,10 +1657,8 @@ public class JavadocUtil
         {
             return url;
         }
-        HttpClient httpClient = null;
-        try
+        try ( CloseableHttpClient httpClient = createHttpClient( settings, url ) )
         {
-            httpClient = createHttpClient( settings, url );
             HttpClientContext httpContext = HttpClientContext.create();
             HttpGet httpMethod = new HttpGet( url.toString() );
             HttpResponse response = httpClient.execute( httpMethod, httpContext );
@@ -1674,13 +1672,6 @@ public class JavadocUtil
             List<URI> redirects = httpContext.getRedirectLocations();
             return redirects.isEmpty() ? url : redirects.get( redirects.size() - 1 ).toURL();
         }
-        finally
-        {
-            if ( httpClient != null )
-            {
-                httpClient.getConnectionManager().shutdown();
-            }
-        }
     }
 
     /**
@@ -1709,8 +1700,7 @@ public class JavadocUtil
         }
 
         BufferedReader reader = null;
-        HttpGet httpMethod = null;
-        HttpClient httpClient = null;
+        CloseableHttpClient httpClient = null;
 
         try
         {
@@ -1724,17 +1714,8 @@ public class JavadocUtil
                 // http, https...
                 httpClient = createHttpClient( settings, url );
 
-                httpMethod = new HttpGet( url.toString() );
-                HttpResponse response;
-                try
-                {
-                    response = httpClient.execute( httpMethod );
-                }
-                catch ( SocketTimeoutException e )
-                {
-                    // could be a sporadic failure, one more retry before we give up
-                    response = httpClient.execute( httpMethod );
-                }
+                HttpGet httpMethod = new HttpGet( url.toString() );
+                HttpResponse response = httpClient.execute( httpMethod );
 
                 int status = response.getStatusLine().getStatusCode();
                 if ( status != HttpStatus.SC_OK )
@@ -1767,13 +1748,9 @@ public class JavadocUtil
         {
             IOUtil.close( reader );
 
-            if ( httpMethod != null )
-            {
-                httpMethod.releaseConnection();
-            }
             if ( httpClient != null )
             {
-                httpClient.getConnectionManager().shutdown();
+                httpClient.close();
             }
         }
     }
@@ -1829,16 +1806,17 @@ public class JavadocUtil
      * @see #DEFAULT_TIMEOUT
      * @since 2.8
      */
-    private static HttpClient createHttpClient( Settings settings, URL url )
+    private static CloseableHttpClient createHttpClient( Settings settings, URL url )
     {
-        DefaultHttpClient httpClient = new DefaultHttpClient( new PoolingClientConnectionManager()
);
-        httpClient.getParams().setIntParameter( CoreConnectionPNames.SO_TIMEOUT, DEFAULT_TIMEOUT
);
-        httpClient.getParams().setIntParameter( CoreConnectionPNames.CONNECTION_TIMEOUT,
DEFAULT_TIMEOUT );
-        httpClient.getParams().setBooleanParameter( ClientPNames.ALLOW_CIRCULAR_REDIRECTS,
true );
-
-        // Some web servers don't allow the default user-agent sent by httpClient
-        httpClient.getParams().setParameter( CoreProtocolPNames.USER_AGENT,
-                                             "Mozilla/4.0 (compatible; MSIE 6.0; Windows
NT 5.0)" );
+        HttpClientBuilder builder = HttpClients.custom();
+        builder.setConnectionManager( new PoolingHttpClientConnectionManager() );
+        builder.setDefaultRequestConfig( RequestConfig.custom()
+                                           .setSocketTimeout( DEFAULT_TIMEOUT )
+                                           .setConnectTimeout( DEFAULT_TIMEOUT )
+                                           .setCircularRedirectsAllowed( true ).build()
+        );
+        builder.setUserAgent( "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" );
+        builder.setRetryHandler( new DefaultHttpRequestRetryHandler( 1, false ) );
 
         if ( settings != null && settings.getActiveProxy() != null )
         {
@@ -1851,19 +1829,21 @@ public class JavadocUtil
                  && ( url == null || !ProxyUtils.validateNonProxyHosts( proxyInfo,
url.getHost() ) ) )
             {
                 HttpHost proxy = new HttpHost( activeProxy.getHost(), activeProxy.getPort()
);
-                httpClient.getParams().setParameter( ConnRoutePNames.DEFAULT_PROXY, proxy
);
+                builder.setProxy( proxy );
 
                 if ( StringUtils.isNotEmpty( activeProxy.getUsername() ) && activeProxy.getPassword()
!= null )
                 {
                     Credentials credentials =
                         new UsernamePasswordCredentials( activeProxy.getUsername(), activeProxy.getPassword()
);
 
-                    httpClient.getCredentialsProvider().setCredentials( AuthScope.ANY, credentials
);
+                    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
+                    credentialsProvider.setCredentials( AuthScope.ANY, credentials );
+                    builder.setDefaultCredentialsProvider( credentialsProvider );
                 }
             }
         }
 
-        return httpClient;
+        return builder.build();
     }
 
     static boolean equalsIgnoreCase( String value, String... strings )

-- 
To stop receiving notification emails like this one, please contact
gboue@apache.org.

Mime
View raw message