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 ;
2527import java .io .IOException ;
2628import java .lang .reflect .Field ;
2729import java .lang .reflect .ParameterizedType ;
30+ import java .net .URISyntaxException ;
2831import java .security .cert .X509Certificate ;
2932import java .time .Instant ;
3033import java .util .*;
3134import javax .net .ssl .SSLContext ;
3235import lombok .RequiredArgsConstructor ;
3336import lombok .extern .slf4j .Slf4j ;
3437import org .apache .commons .lang3 .ArrayUtils ;
35- import org .apache .http .HttpHost ;
36- import org .apache .http .auth .AuthScope ;
37- import org .apache .http .auth .UsernamePasswordCredentials ;
38- import org .apache .http .conn .ssl .NoopHostnameVerifier ;
39- import org .apache .http .impl .client .BasicCredentialsProvider ;
40- import org .apache .http .impl .nio .client .HttpAsyncClientBuilder ;
41- import org .apache .http .ssl .SSLContextBuilder ;
42- import org .elasticsearch .client .RestClient ;
43- import org .elasticsearch .client .RestClientBuilder ;
38+ import org .apache .hc .client5 .http .auth .AuthScope ;
39+ import org .apache .hc .client5 .http .auth .UsernamePasswordCredentials ;
40+ import org .apache .hc .client5 .http .impl .async .HttpAsyncClientBuilder ;
41+ import org .apache .hc .client5 .http .impl .auth .BasicCredentialsProvider ;
42+ import org .apache .hc .client5 .http .impl .nio .PoolingAsyncClientConnectionManager ;
43+ import org .apache .hc .client5 .http .impl .nio .PoolingAsyncClientConnectionManagerBuilder ;
44+ import org .apache .hc .client5 .http .ssl .DefaultClientTlsStrategy ;
45+ import org .apache .hc .client5 .http .ssl .NoopHostnameVerifier ;
46+ import org .apache .hc .core5 .http .HttpHost ;
47+ import org .apache .hc .core5 .ssl .SSLContextBuilder ;
4448import org .springframework .beans .factory .annotation .Autowired ;
4549import org .springframework .stereotype .Component ;
4650
@@ -61,14 +65,15 @@ public void setSearchEngine(EngineContext searchEngine) {
6165 this .searchEngine = searchEngine ;
6266 }
6367
64- private ElasticsearchClient getElasticClient () {
65- RestClientBuilder restClientBuilder = RestClient .builder (HttpHost .create (config .getUrl ()));
68+ private ElasticsearchClient getElasticClient () throws URISyntaxException {
69+ Rest5ClientBuilder restClientBuilder = Rest5Client .builder (HttpHost .create (config .getUrl ()));
6670 HttpAsyncClientBuilder clientBuilder = HttpAsyncClientBuilder .create ();
6771 if (config .getUsername () != null ) {
6872 BasicCredentialsProvider credsProv = new BasicCredentialsProvider ();
6973 credsProv .setCredentials (
70- AuthScope .ANY ,
71- new UsernamePasswordCredentials (config .getUsername (), config .getPassword ()));
74+ new AuthScope (null , -1 ),
75+ new UsernamePasswordCredentials (
76+ config .getUsername (), config .getPassword ().toCharArray ()));
7277 clientBuilder .setDefaultCredentialsProvider (credsProv );
7378 }
7479 if (!config .isRejectUnauthorized ()) {
@@ -78,20 +83,24 @@ private ElasticsearchClient getElasticClient() {
7883 SSLContextBuilder .create ()
7984 .loadTrustMaterial (null , (X509Certificate [] chain , String authType ) -> true )
8085 .build ();
81- clientBuilder
82- .setSSLContext (sslContext )
83- .setSSLHostnameVerifier (NoopHostnameVerifier .INSTANCE );
86+ PoolingAsyncClientConnectionManager connectionManager =
87+ PoolingAsyncClientConnectionManagerBuilder .create ()
88+ .setTlsStrategy (
89+ new DefaultClientTlsStrategy (sslContext , NoopHostnameVerifier .INSTANCE ))
90+ .build ();
91+
92+ clientBuilder .setConnectionManager (connectionManager );
8493 } catch (Exception e ) {
8594 throw new RuntimeException (e );
8695 }
8796 }
88- restClientBuilder .setHttpClientConfigCallback ( hc -> clientBuilder );
89- RestClient restClient = restClientBuilder .build ();
97+ restClientBuilder .setHttpClient ( clientBuilder . build () );
98+ Rest5Client restClient = restClientBuilder .build ();
9099 JacksonJsonpMapper jsonpMapper = new JacksonJsonpMapper ();
91100 jsonpMapper .objectMapper ().registerModule (new JavaTimeModule ());
92101 jsonpMapper .objectMapper ().configure (SerializationFeature .WRITE_DATES_AS_TIMESTAMPS , false );
93102 jsonpMapper .objectMapper ().configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
94- ElasticsearchTransport transport = new RestClientTransport (restClient , jsonpMapper );
103+ ElasticsearchTransport transport = new Rest5ClientTransport (restClient , jsonpMapper );
95104 return new ElasticsearchClient (transport );
96105 }
97106
0 commit comments