From d12e42e8d51aabc18b1876d1ed8ba2379652179b Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Wed, 28 Nov 2018 19:48:08 +0100 Subject: [PATCH] Polish Closes gh-15211 --- .../pom.xml | 5 ++++ ...hRestHealthIndicatorAutoConfiguration.java | 2 +- .../main/resources/META-INF/spring.factories | 1 + .../ElasticsearchRestHealthIndicator.java | 29 +++++++++++-------- 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/pom.xml b/spring-boot-project/spring-boot-actuator-autoconfigure/pom.xml index 9eb4274d76f..654c17a4a45 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/pom.xml +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/pom.xml @@ -264,6 +264,11 @@ jetty-server true + + org.elasticsearch + elasticsearch + true + org.elasticsearch.client elasticsearch-rest-client diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/elasticsearch/ElasticSearchRestHealthIndicatorAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/elasticsearch/ElasticSearchRestHealthIndicatorAutoConfiguration.java index c47b934e6de..edfee5e5ed4 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/elasticsearch/ElasticSearchRestHealthIndicatorAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/elasticsearch/ElasticSearchRestHealthIndicatorAutoConfiguration.java @@ -40,7 +40,7 @@ import org.springframework.context.annotation.Configuration; * {@link ElasticsearchRestHealthIndicator} using the {@link RestClient}. * * @author Artsiom Yudovin - * @since 2.1.0 + * @since 2.1.1 */ @Configuration diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/spring.factories b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/spring.factories index 36341472ca7..725355b6b96 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/spring.factories +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/spring.factories @@ -15,6 +15,7 @@ org.springframework.boot.actuate.autoconfigure.couchbase.CouchbaseHealthIndicato org.springframework.boot.actuate.autoconfigure.couchbase.CouchbaseReactiveHealthIndicatorAutoConfiguration,\ org.springframework.boot.actuate.autoconfigure.elasticsearch.ElasticSearchClientHealthIndicatorAutoConfiguration,\ org.springframework.boot.actuate.autoconfigure.elasticsearch.ElasticSearchJestHealthIndicatorAutoConfiguration,\ +org.springframework.boot.actuate.autoconfigure.elasticsearch.ElasticSearchRestHealthIndicatorAutoConfiguration,\ org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration,\ org.springframework.boot.actuate.autoconfigure.endpoint.jmx.JmxEndpointAutoConfiguration,\ org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointAutoConfiguration,\ diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchRestHealthIndicator.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchRestHealthIndicator.java index fb542496afa..267e0fd8ff8 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchRestHealthIndicator.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchRestHealthIndicator.java @@ -16,11 +16,10 @@ package org.springframework.boot.actuate.elasticsearch; -import java.io.InputStreamReader; +import java.io.InputStream; import java.nio.charset.StandardCharsets; +import java.util.Map; -import com.google.gson.JsonElement; -import com.google.gson.JsonParser; import org.apache.http.HttpStatus; import org.elasticsearch.client.Request; import org.elasticsearch.client.Response; @@ -29,22 +28,29 @@ import org.elasticsearch.client.RestClient; import org.springframework.boot.actuate.health.AbstractHealthIndicator; import org.springframework.boot.actuate.health.Health; import org.springframework.boot.actuate.health.HealthIndicator; +import org.springframework.boot.json.JsonParser; +import org.springframework.boot.json.JsonParserFactory; +import org.springframework.util.StreamUtils; /** - * {@link HealthIndicator} for an Elasticsearch cluster by REST. + * {@link HealthIndicator} for an Elasticsearch cluster using a {@link RestClient}. * * @author Artsiom Yudovin - * @since 2.1.0 + * @author Brian Clozel + * @since 2.1.1 */ public class ElasticsearchRestHealthIndicator extends AbstractHealthIndicator { + private static final String RED_STATUS = "red"; + private final RestClient client; - private final JsonParser jsonParser = new JsonParser(); + private final JsonParser jsonParser; public ElasticsearchRestHealthIndicator(RestClient client) { super("Elasticsearch health check failed"); this.client = client; + this.jsonParser = JsonParserFactory.getJsonParser(); } @Override @@ -56,12 +62,11 @@ public class ElasticsearchRestHealthIndicator extends AbstractHealthIndicator { builder.down(); } else { - try (InputStreamReader reader = new InputStreamReader( - response.getEntity().getContent(), StandardCharsets.UTF_8)) { - JsonElement root = this.jsonParser.parse(reader); - JsonElement status = root.getAsJsonObject().get("status"); - if (status.getAsString() - .equals(io.searchbox.cluster.Health.Status.RED.getKey())) { + try (InputStream is = response.getEntity().getContent()) { + Map root = this.jsonParser + .parseMap(StreamUtils.copyToString(is, StandardCharsets.UTF_8)); + String status = (String) root.get("status"); + if (status.equals(RED_STATUS)) { builder.outOfService(); } else {