1919 * under the License.
2020 */
2121
22- import org .apache .http .HttpHeaders ;
23- import org .apache .http .HttpHost ;
24- import org .apache .http .HttpResponse ;
25- import org .apache .http .HttpStatus ;
26- import org .apache .http .auth .AuthScope ;
27- import org .apache .http .auth .Credentials ;
28- import org .apache .http .auth .UsernamePasswordCredentials ;
29- import org .apache .http .client .HttpClient ;
30- import org .apache .http .client .methods .HttpGet ;
31- import org .apache .http .client .params .ClientPNames ;
32- import org .apache .http .client .params .CookiePolicy ;
33- import org .apache .http .client .protocol .HttpClientContext ;
34- import org .apache .http .conn .params .ConnRoutePNames ;
35- import org .apache .http .impl .client .DefaultHttpClient ;
36- import org .apache .http .impl .conn .PoolingClientConnectionManager ;
37- import org .apache .http .message .BasicHeader ;
38- import org .apache .http .params .CoreConnectionPNames ;
39- import org .apache .http .params .CoreProtocolPNames ;
40- import org .apache .maven .plugin .logging .Log ;
41- import org .apache .maven .project .MavenProject ;
42- import org .apache .maven .settings .Proxy ;
43- import org .apache .maven .settings .Settings ;
44- import org .apache .maven .shared .invoker .DefaultInvocationRequest ;
45- import org .apache .maven .shared .invoker .DefaultInvoker ;
46- import org .apache .maven .shared .invoker .InvocationOutputHandler ;
47- import org .apache .maven .shared .invoker .InvocationRequest ;
48- import org .apache .maven .shared .invoker .InvocationResult ;
49- import org .apache .maven .shared .invoker .Invoker ;
50- import org .apache .maven .shared .invoker .MavenInvocationException ;
51- import org .apache .maven .shared .invoker .PrintStreamHandler ;
52- import org .apache .maven .wagon .proxy .ProxyInfo ;
53- import org .apache .maven .wagon .proxy .ProxyUtils ;
54- import org .codehaus .plexus .languages .java .version .JavaVersion ;
55- import org .codehaus .plexus .util .DirectoryScanner ;
56- import org .codehaus .plexus .util .FileUtils ;
57- import org .codehaus .plexus .util .IOUtil ;
58- import org .codehaus .plexus .util .Os ;
59- import org .codehaus .plexus .util .StringUtils ;
60- import org .codehaus .plexus .util .cli .CommandLineException ;
61- import org .codehaus .plexus .util .cli .CommandLineUtils ;
62- import org .codehaus .plexus .util .cli .Commandline ;
63-
6422import java .io .BufferedReader ;
6523import java .io .File ;
6624import java .io .FileInputStream ;
10159import java .util .regex .Pattern ;
10260import java .util .regex .PatternSyntaxException ;
10361
62+ import org .apache .http .HttpHeaders ;
63+ import org .apache .http .HttpHost ;
64+ import org .apache .http .HttpResponse ;
65+ import org .apache .http .HttpStatus ;
66+ import org .apache .http .auth .AuthScope ;
67+ import org .apache .http .auth .Credentials ;
68+ import org .apache .http .auth .UsernamePasswordCredentials ;
69+ import org .apache .http .client .CredentialsProvider ;
70+ import org .apache .http .client .config .CookieSpecs ;
71+ import org .apache .http .client .config .RequestConfig ;
72+ import org .apache .http .client .methods .HttpGet ;
73+ import org .apache .http .client .protocol .HttpClientContext ;
74+ import org .apache .http .config .Registry ;
75+ import org .apache .http .config .RegistryBuilder ;
76+ import org .apache .http .conn .socket .ConnectionSocketFactory ;
77+ import org .apache .http .conn .socket .PlainConnectionSocketFactory ;
78+ import org .apache .http .conn .ssl .SSLConnectionSocketFactory ;
79+ import org .apache .http .impl .client .BasicCredentialsProvider ;
80+ import org .apache .http .impl .client .CloseableHttpClient ;
81+ import org .apache .http .impl .client .HttpClientBuilder ;
82+ import org .apache .http .impl .client .HttpClients ;
83+ import org .apache .http .impl .conn .PoolingHttpClientConnectionManager ;
84+ import org .apache .http .message .BasicHeader ;
85+ import org .apache .maven .plugin .logging .Log ;
86+ import org .apache .maven .project .MavenProject ;
87+ import org .apache .maven .settings .Proxy ;
88+ import org .apache .maven .settings .Settings ;
89+ import org .apache .maven .shared .invoker .DefaultInvocationRequest ;
90+ import org .apache .maven .shared .invoker .DefaultInvoker ;
91+ import org .apache .maven .shared .invoker .InvocationOutputHandler ;
92+ import org .apache .maven .shared .invoker .InvocationRequest ;
93+ import org .apache .maven .shared .invoker .InvocationResult ;
94+ import org .apache .maven .shared .invoker .Invoker ;
95+ import org .apache .maven .shared .invoker .MavenInvocationException ;
96+ import org .apache .maven .shared .invoker .PrintStreamHandler ;
97+ import org .apache .maven .wagon .proxy .ProxyInfo ;
98+ import org .apache .maven .wagon .proxy .ProxyUtils ;
99+ import org .codehaus .plexus .languages .java .version .JavaVersion ;
100+ import org .codehaus .plexus .util .DirectoryScanner ;
101+ import org .codehaus .plexus .util .FileUtils ;
102+ import org .codehaus .plexus .util .IOUtil ;
103+ import org .codehaus .plexus .util .Os ;
104+ import org .codehaus .plexus .util .StringUtils ;
105+ import org .codehaus .plexus .util .cli .CommandLineException ;
106+ import org .codehaus .plexus .util .cli .CommandLineUtils ;
107+ import org .codehaus .plexus .util .cli .Commandline ;
108+
104109/**
105110 * Set of utilities methods for Javadoc.
106111 *
@@ -1535,10 +1540,9 @@ protected static URL getRedirectUrl( URL url, Settings settings )
15351540 {
15361541 return url ;
15371542 }
1538- HttpClient httpClient = null ;
1539- try
1543+
1544+ try ( CloseableHttpClient httpClient = createHttpClient ( settings , url ) )
15401545 {
1541- httpClient = createHttpClient ( settings , url );
15421546 HttpClientContext httpContext = HttpClientContext .create ();
15431547 HttpGet httpMethod = new HttpGet ( url .toString () );
15441548 HttpResponse response = httpClient .execute ( httpMethod , httpContext );
@@ -1552,13 +1556,6 @@ protected static URL getRedirectUrl( URL url, Settings settings )
15521556 List <URI > redirects = httpContext .getRedirectLocations ();
15531557 return isEmpty ( redirects ) ? url : redirects .get ( redirects .size () - 1 ).toURL ();
15541558 }
1555- finally
1556- {
1557- if ( httpClient != null )
1558- {
1559- httpClient .getConnectionManager ().shutdown ();
1560- }
1561- }
15621559 }
15631560
15641561 /**
@@ -1639,7 +1636,7 @@ private static BufferedReader getReader( URL url, Settings settings ) throws IOE
16391636 else
16401637 {
16411638 // http, https...
1642- final HttpClient httpClient = createHttpClient ( settings , url );
1639+ final CloseableHttpClient httpClient = createHttpClient ( settings , url );
16431640
16441641 final HttpGet httpMethod = new HttpGet ( url .toString () );
16451642
@@ -1693,7 +1690,7 @@ public void close()
16931690 }
16941691 if ( httpClient != null )
16951692 {
1696- httpClient .getConnectionManager (). shutdown ();
1693+ httpClient .close ();
16971694 }
16981695 }
16991696 };
@@ -1751,22 +1748,29 @@ private static boolean isValidClassName( String str )
17511748 * @see #DEFAULT_TIMEOUT
17521749 * @since 2.8
17531750 */
1754- private static HttpClient createHttpClient ( Settings settings , URL url )
1751+ private static CloseableHttpClient createHttpClient ( Settings settings , URL url )
17551752 {
1756- DefaultHttpClient httpClient = new DefaultHttpClient ( new PoolingClientConnectionManager () );
1757- httpClient .getParams ().setIntParameter ( CoreConnectionPNames .SO_TIMEOUT , DEFAULT_TIMEOUT );
1758- httpClient .getParams ().setIntParameter ( CoreConnectionPNames .CONNECTION_TIMEOUT , DEFAULT_TIMEOUT );
1759- httpClient .getParams ().setBooleanParameter ( ClientPNames .ALLOW_CIRCULAR_REDIRECTS , true );
1760-
1753+ HttpClientBuilder builder = HttpClients .custom ();
1754+
1755+ Registry <ConnectionSocketFactory > csfRegistry =
1756+ RegistryBuilder .<ConnectionSocketFactory >create ()
1757+ .register ( "http" , PlainConnectionSocketFactory .getSocketFactory () )
1758+ .register ( "https" , SSLConnectionSocketFactory .getSystemSocketFactory () )
1759+ .build ();
1760+
1761+ builder .setConnectionManager ( new PoolingHttpClientConnectionManager ( csfRegistry ) );
1762+ builder .setDefaultRequestConfig ( RequestConfig .custom ()
1763+ .setSocketTimeout ( DEFAULT_TIMEOUT )
1764+ .setConnectTimeout ( DEFAULT_TIMEOUT )
1765+ .setCircularRedirectsAllowed ( true )
1766+ .setCookieSpec ( CookieSpecs .IGNORE_COOKIES )
1767+ .build () );
1768+
17611769 // Some web servers don't allow the default user-agent sent by httpClient
1762- httpClient .getParams ().setParameter ( CoreProtocolPNames .USER_AGENT ,
1763- "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" );
1770+ builder .setUserAgent ( "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" );
17641771
17651772 // Some server reject requests that do not have an Accept header
1766- httpClient .getParams ().setParameter ( ClientPNames .DEFAULT_HEADERS ,
1767- Arrays .asList ( new BasicHeader ( HttpHeaders .ACCEPT , "*/*" ) ) );
1768-
1769- httpClient .getParams ().setParameter ( ClientPNames .COOKIE_POLICY , CookiePolicy .BROWSER_COMPATIBILITY );
1773+ builder .setDefaultHeaders ( Arrays .asList ( new BasicHeader ( HttpHeaders .ACCEPT , "*/*" ) ) );
17701774
17711775 if ( settings != null && settings .getActiveProxy () != null )
17721776 {
@@ -1779,19 +1783,20 @@ private static HttpClient createHttpClient( Settings settings, URL url )
17791783 && ( url == null || !ProxyUtils .validateNonProxyHosts ( proxyInfo , url .getHost () ) ) )
17801784 {
17811785 HttpHost proxy = new HttpHost ( activeProxy .getHost (), activeProxy .getPort () );
1782- httpClient . getParams (). setParameter ( ConnRoutePNames . DEFAULT_PROXY , proxy );
1786+ builder . setProxy ( proxy );
17831787
17841788 if ( StringUtils .isNotEmpty ( activeProxy .getUsername () ) && activeProxy .getPassword () != null )
17851789 {
17861790 Credentials credentials =
17871791 new UsernamePasswordCredentials ( activeProxy .getUsername (), activeProxy .getPassword () );
17881792
1789- httpClient .getCredentialsProvider ().setCredentials ( AuthScope .ANY , credentials );
1793+ CredentialsProvider credentialsProvider = new BasicCredentialsProvider ();
1794+ credentialsProvider .setCredentials ( AuthScope .ANY , credentials );
1795+ builder .setDefaultCredentialsProvider ( credentialsProvider );
17901796 }
17911797 }
17921798 }
1793-
1794- return httpClient ;
1799+ return builder .build ();
17951800 }
17961801
17971802 static boolean equalsIgnoreCase ( String value , String ... strings )
0 commit comments