From d2a78080987de1411f992632c8f3e35bde92e2ba Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 22 Jul 2020 17:25:04 -0700 Subject: [PATCH] Polish --- .../ElasticsearchReactiveHealthIndicator.java | 47 ++++++++++--------- .../spring-boot-docs/build.gradle | 2 +- .../transport/LocalHttpClientTransport.java | 6 ++- .../platform/io/ZipFileTarArchive.java | 2 +- 4 files changed, 31 insertions(+), 26 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchReactiveHealthIndicator.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchReactiveHealthIndicator.java index 1c4272c8e0..a71b0d199c 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchReactiveHealthIndicator.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchReactiveHealthIndicator.java @@ -23,8 +23,11 @@ import reactor.core.publisher.Mono; import org.springframework.boot.actuate.health.AbstractReactiveHealthIndicator; import org.springframework.boot.actuate.health.Health; import org.springframework.boot.actuate.health.HealthIndicator; +import org.springframework.boot.actuate.health.Status; import org.springframework.core.ParameterizedTypeReference; import org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient; +import org.springframework.web.reactive.function.client.ClientResponse; +import org.springframework.web.reactive.function.client.WebClient; /** * {@link HealthIndicator} for an Elasticsearch cluster using a @@ -50,28 +53,28 @@ public class ElasticsearchReactiveHealthIndicator extends AbstractReactiveHealth @Override protected Mono doHealthCheck(Health.Builder builder) { - return this.client.execute((callback) -> callback.get().uri("/_cluster/health/").exchange()) - .flatMap((response) -> { - if (response.statusCode().is2xxSuccessful()) { - return response.bodyToMono(STRING_OBJECT_MAP).map((body) -> { - String status = (String) body.get("status"); - if (RED_STATUS.equals(status)) { - builder.outOfService(); - } - else { - builder.up(); - } - builder.withDetails(body); - return builder.build(); - }); - } - else { - builder.down(); - builder.withDetail("statusCode", response.rawStatusCode()); - builder.withDetail("reasonPhrase", response.statusCode().getReasonPhrase()); - return response.releaseBody().thenReturn(builder.build()); - } - }); + return this.client.execute(this::getHealth).flatMap((response) -> doHealthCheck(builder, response)); + } + + private Mono getHealth(WebClient webClient) { + return webClient.get().uri("/_cluster/health/").exchange(); + } + + private Mono doHealthCheck(Health.Builder builder, ClientResponse response) { + if (response.statusCode().is2xxSuccessful()) { + return response.bodyToMono(STRING_OBJECT_MAP).map((body) -> getHealth(builder, body)); + } + builder.down(); + builder.withDetail("statusCode", response.rawStatusCode()); + builder.withDetail("reasonPhrase", response.statusCode().getReasonPhrase()); + return response.releaseBody().thenReturn(builder.build()); + } + + private Health getHealth(Health.Builder builder, Map body) { + String status = (String) body.get("status"); + builder.status(RED_STATUS.equals(status) ? Status.OUT_OF_SERVICE : Status.UP); + builder.withDetails(body); + return builder.build(); } } diff --git a/spring-boot-project/spring-boot-docs/build.gradle b/spring-boot-project/spring-boot-docs/build.gradle index c52cfaa211..a080ef8028 100644 --- a/spring-boot-project/spring-boot-docs/build.gradle +++ b/spring-boot-project/spring-boot-docs/build.gradle @@ -95,7 +95,7 @@ dependencies { testImplementation(project(":spring-boot-project:spring-boot-tools:spring-boot-test-support")) testImplementation("org.assertj:assertj-core") testImplementation("org.junit.jupiter:junit-jupiter") - + testRuntimeOnly(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-web")) testRuntimeOnly("com.h2database:h2") testRuntimeOnly("org.springframework:spring-jdbc") diff --git a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/transport/LocalHttpClientTransport.java b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/transport/LocalHttpClientTransport.java index fbe3914528..9861f4d45a 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/transport/LocalHttpClientTransport.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/transport/LocalHttpClientTransport.java @@ -69,8 +69,10 @@ final class LocalHttpClientTransport extends HttpClientTransport { private static String socketFilePath(Environment environment) { String host = environment.get(DOCKER_HOST); - return (host != null && host.startsWith(UNIX_SOCKET_PREFIX)) ? host.substring(UNIX_SOCKET_PREFIX.length()) - : host; + if (host != null && host.startsWith(UNIX_SOCKET_PREFIX)) { + return host.substring(UNIX_SOCKET_PREFIX.length()); + } + return host; } /** diff --git a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/io/ZipFileTarArchive.java b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/io/ZipFileTarArchive.java index 0555eb979a..90d543b0d6 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/io/ZipFileTarArchive.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/io/ZipFileTarArchive.java @@ -76,7 +76,7 @@ public class ZipFileTarArchive implements TarArchive { private void assertArchiveHasEntries(File jarFile) { try (ZipFile zipFile = new ZipFile(jarFile)) { - Assert.state(zipFile.getEntries().hasMoreElements(), "File '" + jarFile.toString() + Assert.state(zipFile.getEntries().hasMoreElements(), () -> "File '" + jarFile + "' is not compatible with buildpacks; ensure jar file is valid and launch script is not enabled"); } catch (IOException ex) {