1313import co .elastic .clients .json .JsonData ;
1414import co .elastic .clients .json .jackson .JacksonJsonpMapper ;
1515import co .elastic .clients .transport .ElasticsearchTransport ;
16- import co .elastic .clients .transport .rest_client .RestClientTransport ;
16+ import co .elastic .clients .transport .rest5_client .Rest5ClientTransport ;
17+ import co .elastic .clients .transport .rest5_client .low_level .Rest5Client ;
18+ import co .elastic .clients .transport .rest5_client .low_level .Rest5ClientBuilder ;
1719import com .fasterxml .jackson .databind .DeserializationFeature ;
1820import com .fasterxml .jackson .databind .SerializationFeature ;
1921import com .fasterxml .jackson .datatype .jsr310 .JavaTimeModule ;
2426import java .io .IOException ;
2527import java .lang .reflect .Field ;
2628import java .lang .reflect .ParameterizedType ;
29+ import java .net .URISyntaxException ;
2730import java .security .cert .X509Certificate ;
2831import java .time .Instant ;
2932import java .util .HashMap ;
3437import javax .net .ssl .SSLContext ;
3538import lombok .RequiredArgsConstructor ;
3639import org .apache .commons .lang3 .ArrayUtils ;
37- import org .apache .http .HttpHost ;
38- import org .apache .http .auth .AuthScope ;
39- import org .apache .http .auth .UsernamePasswordCredentials ;
40- import org .apache .http .conn .ssl .NoopHostnameVerifier ;
41- import org .apache .http .impl .client .BasicCredentialsProvider ;
42- import org .apache .http .impl .nio .client .HttpAsyncClientBuilder ;
43- import org .apache .http .ssl .SSLContextBuilder ;
44- import org .elasticsearch .client .RestClient ;
45- import org .elasticsearch .client .RestClientBuilder ;
40+ import org .apache .hc .client5 .http .auth .AuthScope ;
41+ import org .apache .hc .client5 .http .auth .UsernamePasswordCredentials ;
42+ import org .apache .hc .client5 .http .impl .async .HttpAsyncClientBuilder ;
43+ import org .apache .hc .client5 .http .impl .auth .BasicCredentialsProvider ;
44+ import org .apache .hc .client5 .http .impl .nio .PoolingAsyncClientConnectionManager ;
45+ import org .apache .hc .client5 .http .impl .nio .PoolingAsyncClientConnectionManagerBuilder ;
46+ import org .apache .hc .client5 .http .ssl .DefaultClientTlsStrategy ;
47+ import org .apache .hc .client5 .http .ssl .NoopHostnameVerifier ;
48+ import org .apache .hc .core5 .http .HttpHost ;
49+ import org .apache .hc .core5 .ssl .SSLContextBuilder ;
4650import org .springframework .beans .factory .annotation .Autowired ;
4751import org .springframework .context .annotation .Bean ;
4852import org .springframework .retry .annotation .Backoff ;
@@ -68,14 +72,15 @@ public void setEsEngine(EsEngine esEngine) {
6872 this .esEngine = esEngine ;
6973 }
7074
71- public ElasticsearchClient getElasticClient () {
72- RestClientBuilder restClientBuilder = RestClient .builder (HttpHost .create (config .getUrl ()));
75+ public ElasticsearchClient getElasticClient () throws URISyntaxException {
76+ Rest5ClientBuilder restClientBuilder = Rest5Client .builder (HttpHost .create (config .getUrl ()));
7377 HttpAsyncClientBuilder clientBuilder = HttpAsyncClientBuilder .create ();
7478 if (config .getUsername () != null ) {
7579 BasicCredentialsProvider credsProv = new BasicCredentialsProvider ();
7680 credsProv .setCredentials (
77- AuthScope .ANY ,
78- new UsernamePasswordCredentials (config .getUsername (), config .getPassword ()));
81+ new AuthScope (null , -1 ),
82+ new UsernamePasswordCredentials (
83+ config .getUsername (), config .getPassword ().toCharArray ()));
7984 clientBuilder .setDefaultCredentialsProvider (credsProv );
8085 }
8186 if (!config .isRejectUnauthorized ()) {
@@ -85,20 +90,24 @@ public ElasticsearchClient getElasticClient() {
8590 SSLContextBuilder .create ()
8691 .loadTrustMaterial (null , (X509Certificate [] chain , String authType ) -> true )
8792 .build ();
88- clientBuilder
89- .setSSLContext (sslContext )
90- .setSSLHostnameVerifier (NoopHostnameVerifier .INSTANCE );
93+ PoolingAsyncClientConnectionManager connectionManager =
94+ PoolingAsyncClientConnectionManagerBuilder .create ()
95+ .setTlsStrategy (
96+ new DefaultClientTlsStrategy (sslContext , NoopHostnameVerifier .INSTANCE ))
97+ .build ();
98+
99+ clientBuilder .setConnectionManager (connectionManager );
91100 } catch (Exception e ) {
92101 throw new RuntimeException (e );
93102 }
94103 }
95- restClientBuilder .setHttpClientConfigCallback ( hc -> clientBuilder );
96- RestClient restClient = restClientBuilder .build ();
104+ restClientBuilder .setHttpClient ( clientBuilder . build () );
105+ Rest5Client restClient = restClientBuilder .build ();
97106 JacksonJsonpMapper jsonpMapper = new JacksonJsonpMapper ();
98107 jsonpMapper .objectMapper ().registerModule (new JavaTimeModule ());
99108 jsonpMapper .objectMapper ().configure (SerializationFeature .WRITE_DATES_AS_TIMESTAMPS , false );
100109 jsonpMapper .objectMapper ().configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
101- ElasticsearchTransport transport = new RestClientTransport (restClient , jsonpMapper );
110+ ElasticsearchTransport transport = new Rest5ClientTransport (restClient , jsonpMapper );
102111 return new ElasticsearchClient (transport );
103112 }
104113
0 commit comments