1
1
/*
2
- * Copyright 2012-2020 the original author or authors.
2
+ * Copyright 2012-2021 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
28
28
import org .springframework .data .elasticsearch .client .ClientConfiguration ;
29
29
import org .springframework .data .elasticsearch .client .reactive .ReactiveElasticsearchClient ;
30
30
import org .springframework .data .elasticsearch .client .reactive .ReactiveRestClients ;
31
- import org .springframework .http .HttpHeaders ;
32
31
import org .springframework .util .unit .DataSize ;
33
32
import org .springframework .web .reactive .function .client .ExchangeStrategies ;
34
33
import org .springframework .web .reactive .function .client .WebClient ;
@@ -50,36 +49,25 @@ public class ReactiveElasticsearchRestClientAutoConfiguration {
50
49
public ClientConfiguration clientConfiguration (ReactiveElasticsearchRestClientProperties properties ) {
51
50
ClientConfiguration .MaybeSecureClientConfigurationBuilder builder = ClientConfiguration .builder ()
52
51
.connectedTo (properties .getEndpoints ().toArray (new String [0 ]));
53
- if (properties .isUseSsl ()) {
54
- builder .usingSsl ();
55
- }
56
- configureTimeouts (builder , properties );
57
- configureExchangeStrategies (builder , properties );
52
+ PropertyMapper map = PropertyMapper .get ().alwaysApplyingWhenNonNull ();
53
+ map .from (properties .isUseSsl ()).whenTrue ().toCall (builder ::usingSsl );
54
+ map .from (properties .getUsername ()).whenHasText ()
55
+ .to ((username ) -> builder .withBasicAuth (username , properties .getPassword ()));
56
+ map .from (properties .getConnectionTimeout ()).to (builder ::withConnectTimeout );
57
+ map .from (properties .getSocketTimeout ()).to (builder ::withSocketTimeout );
58
+ configureExchangeStrategies (map , builder , properties );
58
59
return builder .build ();
59
60
}
60
61
61
- private void configureTimeouts (ClientConfiguration .TerminalClientConfigurationBuilder builder ,
62
+ private void configureExchangeStrategies (PropertyMapper map ,
63
+ ClientConfiguration .TerminalClientConfigurationBuilder builder ,
62
64
ReactiveElasticsearchRestClientProperties properties ) {
63
- PropertyMapper map = PropertyMapper .get ();
64
- map .from (properties .getConnectionTimeout ()).whenNonNull ().to (builder ::withConnectTimeout );
65
- map .from (properties .getSocketTimeout ()).whenNonNull ().to (builder ::withSocketTimeout );
66
- map .from (properties .getUsername ()).whenHasText ().to ((username ) -> {
67
- HttpHeaders headers = new HttpHeaders ();
68
- headers .setBasicAuth (username , properties .getPassword ());
69
- builder .withDefaultHeaders (headers );
70
- });
71
- }
72
-
73
- private void configureExchangeStrategies (ClientConfiguration .TerminalClientConfigurationBuilder builder ,
74
- ReactiveElasticsearchRestClientProperties properties ) {
75
- PropertyMapper map = PropertyMapper .get ();
76
- builder .withWebClientConfigurer ((webClient ) -> {
77
- ExchangeStrategies exchangeStrategies = ExchangeStrategies .builder ()
78
- .codecs ((configurer ) -> map .from (properties .getMaxInMemorySize ()).whenNonNull ()
79
- .asInt (DataSize ::toBytes )
80
- .to ((maxInMemorySize ) -> configurer .defaultCodecs ().maxInMemorySize (maxInMemorySize )))
81
- .build ();
82
- return webClient .mutate ().exchangeStrategies (exchangeStrategies ).build ();
65
+ map .from (properties .getMaxInMemorySize ()).asInt (DataSize ::toBytes ).to ((maxInMemorySize ) -> {
66
+ builder .withWebClientConfigurer ((webClient ) -> {
67
+ ExchangeStrategies exchangeStrategies = ExchangeStrategies .builder ()
68
+ .codecs ((configurer ) -> configurer .defaultCodecs ().maxInMemorySize (maxInMemorySize )).build ();
69
+ return webClient .mutate ().exchangeStrategies (exchangeStrategies ).build ();
70
+ });
83
71
});
84
72
}
85
73
0 commit comments