|
| 1 | +package org.itech.ahb.health; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.net.http.HttpClient; |
| 5 | +import java.net.http.HttpRequest; |
| 6 | +import java.net.http.HttpRequest.Builder; |
| 7 | +import java.net.http.HttpResponse; |
| 8 | +import java.util.Base64; |
| 9 | +import lombok.extern.slf4j.Slf4j; |
| 10 | +import org.itech.ahb.config.properties.HTTPForwardServerConfigurationProperties; |
| 11 | +import org.springframework.boot.actuate.autoconfigure.health.ConditionalOnEnabledHealthIndicator; |
| 12 | +import org.springframework.boot.actuate.health.Health; |
| 13 | +import org.springframework.boot.actuate.health.HealthIndicator; |
| 14 | +import org.springframework.stereotype.Component; |
| 15 | + |
| 16 | +@Component("httpforward") |
| 17 | +@ConditionalOnEnabledHealthIndicator("httpforward") |
| 18 | +@Slf4j |
| 19 | +public class HTTPForwardServerHealthIndicator implements HealthIndicator { |
| 20 | + |
| 21 | + private final HTTPForwardServerConfigurationProperties properties; |
| 22 | + |
| 23 | + public HTTPForwardServerHealthIndicator(HTTPForwardServerConfigurationProperties properties) { |
| 24 | + this.properties = properties; |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public Health health() { |
| 29 | + if (properties.getHealthUri() == null) { |
| 30 | + return Health.unknown().build(); |
| 31 | + } |
| 32 | + HttpClient client = HttpClient.newHttpClient(); |
| 33 | + log.debug("creating request to test forward http server at " + properties.getHealthUri().toString()); |
| 34 | + Builder requestBuilder = HttpRequest.newBuilder() |
| 35 | + .method(properties.getHealthMethod().toString(), HttpRequest.BodyPublishers.ofString(properties.getHealthBody())) |
| 36 | + .uri(properties.getHealthUri()); |
| 37 | + if (!(properties.getUsername() == null || properties.getUsername().equals(""))) { |
| 38 | + log.debug( |
| 39 | + "using username '" + |
| 40 | + properties.getUsername() + |
| 41 | + "' to test forward http server at " + |
| 42 | + properties.getUri().toString().toString() |
| 43 | + ); |
| 44 | + requestBuilder.header( |
| 45 | + "Authorization", |
| 46 | + "Basic " + |
| 47 | + Base64.getEncoder() |
| 48 | + .encodeToString((properties.getUsername() + ":" + new String(properties.getPassword())).getBytes()) |
| 49 | + ); |
| 50 | + } |
| 51 | + HttpRequest request = requestBuilder.build(); |
| 52 | + |
| 53 | + try { |
| 54 | + log.debug("testing forward http server at " + properties.getHealthUri().toString()); |
| 55 | + HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); |
| 56 | + if (response.statusCode() == 200) { |
| 57 | + log.debug("testing forward http server at " + properties.getHealthUri().toString() + " success"); |
| 58 | + return Health.up().build(); |
| 59 | + } |
| 60 | + } catch (IOException | InterruptedException e) { |
| 61 | + log.debug("error occurred communicating with http server at " + properties.getHealthUri().toString(), e); |
| 62 | + } |
| 63 | + log.debug("testing forward http server at " + properties.getHealthUri().toString() + " failure"); |
| 64 | + return Health.down().build(); |
| 65 | + } |
| 66 | +} |
0 commit comments