Skip to content

Commit 667e5ca

Browse files
committed
Polish
1 parent 98a0e07 commit 667e5ca

File tree

6 files changed

+102
-101
lines changed

6 files changed

+102
-101
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/Sanitizer.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,38 @@ public class Sanitizer {
6565
DEFAULT_KEYS_TO_SANITIZE.addAll(URI_USERINFO_KEYS);
6666
}
6767

68+
/**
69+
* Create a new {@link Sanitizer} instance with a default set of keys to sanitize.
70+
*/
6871
public Sanitizer() {
6972
this(DEFAULT_KEYS_TO_SANITIZE.toArray(new String[0]));
7073
}
7174

75+
/**
76+
* Create a new {@link Sanitizer} instance with specific keys to sanitize.
77+
* @param keysToSanitize the keys to sanitize
78+
*/
7279
public Sanitizer(String... keysToSanitize) {
7380
this(Collections.emptyList(), keysToSanitize);
7481
}
7582

83+
/**
84+
* Create a new {@link Sanitizer} instance with a default set of keys to sanitize and
85+
* additional sanitizing functions.
86+
* @param sanitizingFunctions the sanitizing functions to apply
87+
* @since 2.6.0
88+
*/
7689
public Sanitizer(Iterable<SanitizingFunction> sanitizingFunctions) {
7790
this(sanitizingFunctions, DEFAULT_KEYS_TO_SANITIZE.toArray(new String[0]));
7891
}
7992

93+
/**
94+
* Create a new {@link Sanitizer} instance with specific keys to sanitize and
95+
* additional sanitizing functions.
96+
* @param sanitizingFunctions the sanitizing functions to apply
97+
* @param keysToSanitize the keys to sanitize
98+
* @since 2.6.0
99+
*/
80100
public Sanitizer(Iterable<SanitizingFunction> sanitizingFunctions, String... keysToSanitize) {
81101
sanitizingFunctions.forEach(this.sanitizingFunctions::add);
82102
this.sanitizingFunctions.add(getDefaultSanitizingFunction());

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/AbstractHealthIndicator.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,13 @@ public final Health health() {
8484
catch (Exception ex) {
8585
builder.down(ex);
8686
}
87-
logExceptionIfPresent(builder);
87+
logExceptionIfPresent(builder.getException());
8888
return builder.build();
8989
}
9090

91-
private void logExceptionIfPresent(Builder builder) {
92-
Throwable ex = builder.getException();
91+
private void logExceptionIfPresent(Throwable ex) {
9392
if (ex != null && this.logger.isWarnEnabled()) {
94-
String message = null;
95-
if (ex instanceof Exception) {
96-
message = this.healthCheckFailedMessage.apply((Exception) ex);
97-
}
93+
String message = (ex instanceof Exception) ? this.healthCheckFailedMessage.apply((Exception) ex) : null;
9894
this.logger.warn(StringUtils.hasText(message) ? message : DEFAULT_MESSAGE, ex);
9995
}
10096
}

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/Health.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -323,15 +323,6 @@ public Builder status(Status status) {
323323
return this;
324324
}
325325

326-
/**
327-
* Return the {@link Exception}.
328-
* @return the exception or {@code null} if the builder has no exception
329-
* @since 2.6.0
330-
*/
331-
public Throwable getException() {
332-
return this.exception;
333-
}
334-
335326
/**
336327
* Create a new {@link Health} instance with the previously specified code and
337328
* details.
@@ -341,6 +332,14 @@ public Health build() {
341332
return new Health(this);
342333
}
343334

335+
/**
336+
* Return the {@link Exception}.
337+
* @return the exception or {@code null} if the builder has no exception
338+
*/
339+
Throwable getException() {
340+
return this.exception;
341+
}
342+
344343
}
345344

346345
}

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/AbstractHealthIndicatorTests.java

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.actuate.health;
1818

19+
import java.util.function.Consumer;
20+
1921
import org.junit.jupiter.api.Test;
2022
import org.junit.jupiter.api.extension.ExtendWith;
2123

@@ -36,76 +38,78 @@ class AbstractHealthIndicatorTests {
3638

3739
@Test
3840
void healthCheckWhenUpDoesNotLogHealthCheckFailedMessage(CapturedOutput output) {
39-
Health heath = new AbstractHealthIndicator("Test message") {
40-
@Override
41-
protected void doHealthCheck(Builder builder) {
42-
builder.up();
43-
}
44-
}.health();
41+
TestHealthIndicator indicator = new TestHealthIndicator("Test message", Builder::up);
42+
Health heath = indicator.health();
4543
assertThat(heath.getStatus()).isEqualTo(Status.UP);
4644
assertThat(output).doesNotContain("Test message");
4745
}
4846

4947
@Test
5048
void healthCheckWhenDownWithExceptionThrownDoesNotLogHealthCheckFailedMessage(CapturedOutput output) {
51-
Health heath = new AbstractHealthIndicator("Test message") {
52-
@Override
53-
protected void doHealthCheck(Builder builder) {
54-
throw new IllegalStateException("Test exception");
55-
}
56-
}.health();
49+
TestHealthIndicator indicator = new TestHealthIndicator("Test message", (builder) -> {
50+
throw new IllegalStateException("Test exception");
51+
});
52+
Health heath = indicator.health();
5753
assertThat(heath.getStatus()).isEqualTo(Status.DOWN);
5854
assertThat(output).contains("Test message").contains("Test exception");
5955
}
6056

6157
@Test
6258
void healthCheckWhenDownWithExceptionConfiguredDoesNotLogHealthCheckFailedMessage(CapturedOutput output) {
63-
Health heath = new AbstractHealthIndicator("Test message") {
64-
@Override
65-
protected void doHealthCheck(Builder builder) {
66-
builder.down().withException(new IllegalStateException("Test exception"));
67-
}
68-
}.health();
59+
Health heath = new TestHealthIndicator("Test message",
60+
(builder) -> builder.down().withException(new IllegalStateException("Test exception"))).health();
6961
assertThat(heath.getStatus()).isEqualTo(Status.DOWN);
7062
assertThat(output).contains("Test message").contains("Test exception");
7163
}
7264

7365
@Test
7466
void healthCheckWhenDownWithExceptionConfiguredDoesNotLogHealthCheckFailedMessageTwice(CapturedOutput output) {
75-
Health heath = new AbstractHealthIndicator("Test message") {
76-
@Override
77-
protected void doHealthCheck(Builder builder) {
78-
IllegalStateException ex = new IllegalStateException("Test exception");
79-
builder.down().withException(ex);
80-
throw ex;
81-
}
82-
}.health();
67+
TestHealthIndicator indicator = new TestHealthIndicator("Test message", (builder) -> {
68+
IllegalStateException ex = new IllegalStateException("Test exception");
69+
builder.down().withException(ex);
70+
throw ex;
71+
});
72+
Health heath = indicator.health();
8373
assertThat(heath.getStatus()).isEqualTo(Status.DOWN);
8474
assertThat(output).contains("Test message").containsOnlyOnce("Test exception");
8575
}
8676

8777
@Test
8878
void healthCheckWhenDownWithExceptionAndNoFailureMessageLogsDefaultMessage(CapturedOutput output) {
89-
Health heath = new AbstractHealthIndicator() {
90-
@Override
91-
protected void doHealthCheck(Builder builder) {
92-
builder.down().withException(new IllegalStateException("Test exception"));
93-
}
94-
}.health();
79+
TestHealthIndicator indicator = new TestHealthIndicator(
80+
(builder) -> builder.down().withException(new IllegalStateException("Test exception")));
81+
Health heath = indicator.health();
9582
assertThat(heath.getStatus()).isEqualTo(Status.DOWN);
9683
assertThat(output).contains("Health check failed").contains("Test exception");
9784
}
9885

9986
@Test
10087
void healthCheckWhenDownWithErrorLogsDefaultMessage(CapturedOutput output) {
101-
Health heath = new AbstractHealthIndicator("Test Message") {
102-
@Override
103-
protected void doHealthCheck(Builder builder) {
104-
builder.down().withException(new Error("Test error"));
105-
}
106-
}.health();
88+
TestHealthIndicator indicator = new TestHealthIndicator("Test Message",
89+
(builder) -> builder.down().withException(new Error("Test error")));
90+
Health heath = indicator.health();
10791
assertThat(heath.getStatus()).isEqualTo(Status.DOWN);
10892
assertThat(output).contains("Health check failed").contains("Test error");
10993
}
11094

95+
static class TestHealthIndicator extends AbstractHealthIndicator {
96+
97+
private Consumer<Builder> action;
98+
99+
TestHealthIndicator(String message, Consumer<Builder> action) {
100+
super(message);
101+
this.action = action;
102+
}
103+
104+
TestHealthIndicator(Consumer<Builder> action) {
105+
this.action = action;
106+
}
107+
108+
@Override
109+
protected void doHealthCheck(Builder builder) throws Exception {
110+
this.action.accept(builder);
111+
}
112+
113+
}
114+
111115
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/elasticsearch/ReactiveElasticsearchRestClientAutoConfiguration.java

Lines changed: 29 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.springframework.data.elasticsearch.client.reactive.ReactiveRestClients;
3838
import org.springframework.data.elasticsearch.client.reactive.ReactiveRestClients.WebClientConfigurationCallback;
3939
import org.springframework.util.Assert;
40+
import org.springframework.util.ObjectUtils;
4041
import org.springframework.util.unit.DataSize;
4142
import org.springframework.web.reactive.function.client.ExchangeStrategies;
4243
import org.springframework.web.reactive.function.client.WebClient;
@@ -120,7 +121,11 @@ private List<String> getEndpoints() {
120121
if (this.deprecatedProperties.isCustomized()) {
121122
return this.deprecatedProperties.getEndpoints();
122123
}
123-
return this.uris.stream().map((uri) -> uri.getHost() + ":" + uri.getPort()).collect(Collectors.toList());
124+
return this.uris.stream().map(this::getEndpoint).collect(Collectors.toList());
125+
}
126+
127+
private String getEndpoint(URI uri) {
128+
return uri.getHost() + ":" + uri.getPort();
124129
}
125130

126131
private Credentials getCredentials() {
@@ -132,13 +137,10 @@ private Credentials getCredentials() {
132137
if (uriCredentials == null) {
133138
return propertyCredentials;
134139
}
135-
if (propertyCredentials != null && !uriCredentials.equals(propertyCredentials)) {
136-
throw new IllegalArgumentException(
137-
"Credentials from URI user info do not match those from spring.elasticsearch.username and "
138-
+ "spring.elasticsearch.password");
139-
}
140+
Assert.isTrue(propertyCredentials == null || uriCredentials.equals(propertyCredentials),
141+
"Credentials from URI user info do not match those from spring.elasticsearch.username and "
142+
+ "spring.elasticsearch.password");
140143
return uriCredentials;
141-
142144
}
143145

144146
private Duration getConnectionTimeout() {
@@ -155,8 +157,8 @@ private boolean isUseSsl() {
155157
if (this.deprecatedProperties.isCustomized()) {
156158
return this.deprecatedProperties.isUseSsl();
157159
}
158-
Set<String> schemes = this.uris.stream().map((uri) -> uri.getScheme()).collect(Collectors.toSet());
159-
Assert.isTrue(schemes.size() == 1, () -> "Configured Elasticsearch URIs have varying schemes");
160+
Set<String> schemes = this.uris.stream().map(URI::getScheme).collect(Collectors.toSet());
161+
Assert.isTrue(schemes.size() == 1, "Configured Elasticsearch URIs have varying schemes");
160162
return schemes.iterator().next().equals("https");
161163
}
162164

@@ -189,29 +191,28 @@ private String getPassword() {
189191
}
190192

191193
private static Credentials from(List<String> uris) {
192-
Set<String> userInfos = uris.stream().map(URI::create).map((uri) -> uri.getUserInfo())
194+
Set<String> userInfos = uris.stream().map(URI::create).map(URI::getUserInfo)
193195
.collect(Collectors.toSet());
194-
Assert.isTrue(userInfos.size() == 1, () -> "Configured Elasticsearch URIs have varying user infos");
196+
Assert.isTrue(userInfos.size() == 1, "Configured Elasticsearch URIs have varying user infos");
195197
String userInfo = userInfos.iterator().next();
196-
if (userInfo != null) {
197-
String[] parts = userInfo.split(":");
198-
return new Credentials(parts[0], (parts.length == 2) ? parts[1] : "");
198+
if (userInfo == null) {
199+
return null;
199200
}
200-
return null;
201+
String[] parts = userInfo.split(":");
202+
String username = parts[0];
203+
String password = (parts.length != 2) ? "" : parts[1];
204+
return new Credentials(username, password);
201205
}
202206

203207
private static Credentials from(ElasticsearchProperties properties) {
204-
String username = properties.getUsername();
205-
String password = properties.getPassword();
206-
if (username == null && password == null) {
207-
return null;
208-
}
209-
return new Credentials(username, password);
208+
return getCredentials(properties.getUsername(), properties.getPassword());
210209
}
211210

212211
private static Credentials from(DeprecatedReactiveElasticsearchRestClientProperties properties) {
213-
String username = properties.getUsername();
214-
String password = properties.getPassword();
212+
return getCredentials(properties.getUsername(), properties.getPassword());
213+
}
214+
215+
private static Credentials getCredentials(String username, String password) {
215216
if (username == null && password == null) {
216217
return null;
217218
}
@@ -223,38 +224,20 @@ public boolean equals(Object obj) {
223224
if (this == obj) {
224225
return true;
225226
}
226-
if (obj == null) {
227-
return false;
228-
}
229-
if (getClass() != obj.getClass()) {
227+
if (obj == null || getClass() != obj.getClass()) {
230228
return false;
231229
}
232230
Credentials other = (Credentials) obj;
233-
if (this.password == null) {
234-
if (other.password != null) {
235-
return false;
236-
}
237-
}
238-
else if (!this.password.equals(other.password)) {
239-
return false;
240-
}
241-
if (this.username == null) {
242-
if (other.username != null) {
243-
return false;
244-
}
245-
}
246-
else if (!this.username.equals(other.username)) {
247-
return false;
248-
}
249-
return true;
231+
return ObjectUtils.nullSafeEquals(this.username, other.username)
232+
&& ObjectUtils.nullSafeEquals(this.password, other.password);
250233
}
251234

252235
@Override
253236
public int hashCode() {
254237
final int prime = 31;
255238
int result = 1;
256-
result = prime * result + ((this.password == null) ? 0 : this.password.hashCode());
257-
result = prime * result + ((this.username == null) ? 0 : this.username.hashCode());
239+
result = prime * result + ObjectUtils.nullSafeHashCode(this.username);
240+
result = prime * result + ObjectUtils.nullSafeHashCode(this.password);
258241
return result;
259242
}
260243

spring-boot-project/spring-boot-docs/src/docs/asciidoc/actuator/metrics.adoc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1081,7 +1081,6 @@ Long task timers require a separate metric name and can be stacked with a short
10811081

10821082
[[actuator.metrics.supported.redis]]
10831083
==== Redis Metrics
1084-
10851084
Auto-configuration registers a `MicrometerCommandLatencyRecorder` for the auto-configured `LettuceConnectionFactory`.
10861085
For more details refer to the {lettuce-docs}#command.latency.metrics.micrometer[Micrometer Metrics section] of the Lettuce documentation.
10871086

0 commit comments

Comments
 (0)