2525import java .io .InputStream ;
2626import java .net .URI ;
2727import java .net .URISyntaxException ;
28+ import java .security .NoSuchAlgorithmException ;
29+ import java .security .KeyManagementException ;
30+ import java .security .KeyStoreException ;
31+ import java .util .Date ;
32+ import java .util .Objects ;
2833import java .util .HashMap ;
2934import java .util .List ;
3035import java .util .Map ;
3338import java .util .concurrent .TimeoutException ;
3439import java .util .regex .Matcher ;
3540import java .util .regex .Pattern ;
36- import java .util .Date ;
37- import java .util .Objects ;
3841
3942import com .qcloud .cos .ClientConfig ;
4043import com .qcloud .cos .Headers ;
8083import org .apache .http .client .methods .HttpPut ;
8184import org .apache .http .client .methods .HttpRequestBase ;
8285import org .apache .http .client .protocol .HttpClientContext ;
86+ import org .apache .http .config .Registry ;
87+ import org .apache .http .config .RegistryBuilder ;
88+ import org .apache .http .conn .socket .ConnectionSocketFactory ;
89+ import org .apache .http .conn .socket .PlainConnectionSocketFactory ;
90+ import org .apache .http .conn .ssl .NoopHostnameVerifier ;
91+ import org .apache .http .ssl .SSLContextBuilder ;
92+ import org .apache .http .conn .ssl .SSLConnectionSocketFactory ;
8393import org .apache .http .entity .InputStreamEntity ;
8494import org .apache .http .impl .client .HttpClientBuilder ;
8595import org .apache .http .impl .client .HttpClients ;
8898import org .slf4j .Logger ;
8999import org .slf4j .LoggerFactory ;
90100
101+ import javax .net .ssl .SSLContext ;
91102
92103public class DefaultCosHttpClient implements CosHttpClient {
93104
@@ -107,7 +118,29 @@ public DefaultCosHttpClient(ClientConfig clientConfig) {
107118 super ();
108119 this .errorResponseHandler = new CosErrorResponseHandler ();
109120 this .clientConfig = clientConfig ;
110- this .connectionManager = new PoolingHttpClientConnectionManager ();
121+
122+ if (clientConfig .isCheckSSLCertificate ()) {
123+ this .connectionManager = new PoolingHttpClientConnectionManager ();
124+ } else {
125+ try {
126+ SSLContext sslContext = SSLContextBuilder .create ().loadTrustMaterial ((chain , authType ) -> true ).build ();
127+
128+ SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory (sslContext , NoopHostnameVerifier .INSTANCE );
129+ Registry <ConnectionSocketFactory > socketFactoryRegistry = RegistryBuilder .<ConnectionSocketFactory > create ()
130+ .register ("http" , PlainConnectionSocketFactory .getSocketFactory ())
131+ .register ("https" , sslSocketFactory ).build ();
132+ this .connectionManager = new PoolingHttpClientConnectionManager (socketFactoryRegistry );
133+ } catch (NoSuchAlgorithmException e ) {
134+ log .error ("fail to init http client: " , e );
135+ throw new RuntimeException (e );
136+ } catch (KeyStoreException e ) {
137+ log .error ("fail to init http client: " , e );
138+ throw new RuntimeException (e );
139+ } catch (KeyManagementException e ) {
140+ log .error ("fail to init http client: " , e );
141+ throw new RuntimeException (e );
142+ }
143+ }
111144 this .maxErrorRetry = clientConfig .getMaxErrorRetry ();
112145 this .retryPolicy = ValidationUtils .assertNotNull (clientConfig .getRetryPolicy (), "retry policy" );
113146 this .backoffStrategy = ValidationUtils .assertNotNull (clientConfig .getBackoffStrategy (), "backoff strategy" );
0 commit comments