19
19
* under the License.
20
20
*/
21
21
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
-
64
22
import java .io .BufferedReader ;
65
23
import java .io .File ;
66
24
import java .io .FileInputStream ;
101
59
import java .util .regex .Pattern ;
102
60
import java .util .regex .PatternSyntaxException ;
103
61
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
+
104
109
/**
105
110
* Set of utilities methods for Javadoc.
106
111
*
@@ -1535,10 +1540,9 @@ protected static URL getRedirectUrl( URL url, Settings settings )
1535
1540
{
1536
1541
return url ;
1537
1542
}
1538
- HttpClient httpClient = null ;
1539
- try
1543
+
1544
+ try ( CloseableHttpClient httpClient = createHttpClient ( settings , url ) )
1540
1545
{
1541
- httpClient = createHttpClient ( settings , url );
1542
1546
HttpClientContext httpContext = HttpClientContext .create ();
1543
1547
HttpGet httpMethod = new HttpGet ( url .toString () );
1544
1548
HttpResponse response = httpClient .execute ( httpMethod , httpContext );
@@ -1552,13 +1556,6 @@ protected static URL getRedirectUrl( URL url, Settings settings )
1552
1556
List <URI > redirects = httpContext .getRedirectLocations ();
1553
1557
return isEmpty ( redirects ) ? url : redirects .get ( redirects .size () - 1 ).toURL ();
1554
1558
}
1555
- finally
1556
- {
1557
- if ( httpClient != null )
1558
- {
1559
- httpClient .getConnectionManager ().shutdown ();
1560
- }
1561
- }
1562
1559
}
1563
1560
1564
1561
/**
@@ -1639,7 +1636,7 @@ private static BufferedReader getReader( URL url, Settings settings ) throws IOE
1639
1636
else
1640
1637
{
1641
1638
// http, https...
1642
- final HttpClient httpClient = createHttpClient ( settings , url );
1639
+ final CloseableHttpClient httpClient = createHttpClient ( settings , url );
1643
1640
1644
1641
final HttpGet httpMethod = new HttpGet ( url .toString () );
1645
1642
@@ -1693,7 +1690,7 @@ public void close()
1693
1690
}
1694
1691
if ( httpClient != null )
1695
1692
{
1696
- httpClient .getConnectionManager (). shutdown ();
1693
+ httpClient .close ();
1697
1694
}
1698
1695
}
1699
1696
};
@@ -1751,22 +1748,29 @@ private static boolean isValidClassName( String str )
1751
1748
* @see #DEFAULT_TIMEOUT
1752
1749
* @since 2.8
1753
1750
*/
1754
- private static HttpClient createHttpClient ( Settings settings , URL url )
1751
+ private static CloseableHttpClient createHttpClient ( Settings settings , URL url )
1755
1752
{
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
+
1761
1769
// 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)" );
1764
1771
1765
1772
// 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 , "*/*" ) ) );
1770
1774
1771
1775
if ( settings != null && settings .getActiveProxy () != null )
1772
1776
{
@@ -1779,19 +1783,20 @@ private static HttpClient createHttpClient( Settings settings, URL url )
1779
1783
&& ( url == null || !ProxyUtils .validateNonProxyHosts ( proxyInfo , url .getHost () ) ) )
1780
1784
{
1781
1785
HttpHost proxy = new HttpHost ( activeProxy .getHost (), activeProxy .getPort () );
1782
- httpClient . getParams (). setParameter ( ConnRoutePNames . DEFAULT_PROXY , proxy );
1786
+ builder . setProxy ( proxy );
1783
1787
1784
1788
if ( StringUtils .isNotEmpty ( activeProxy .getUsername () ) && activeProxy .getPassword () != null )
1785
1789
{
1786
1790
Credentials credentials =
1787
1791
new UsernamePasswordCredentials ( activeProxy .getUsername (), activeProxy .getPassword () );
1788
1792
1789
- httpClient .getCredentialsProvider ().setCredentials ( AuthScope .ANY , credentials );
1793
+ CredentialsProvider credentialsProvider = new BasicCredentialsProvider ();
1794
+ credentialsProvider .setCredentials ( AuthScope .ANY , credentials );
1795
+ builder .setDefaultCredentialsProvider ( credentialsProvider );
1790
1796
}
1791
1797
}
1792
1798
}
1793
-
1794
- return httpClient ;
1799
+ return builder .build ();
1795
1800
}
1796
1801
1797
1802
static boolean equalsIgnoreCase ( String value , String ... strings )
0 commit comments