View Javadoc
1   package lv.id.jc.piglatin.actuator;
2   
3   import java.io.IOException;
4   import java.net.URI;
5   import java.net.http.HttpClient;
6   import java.net.http.HttpRequest;
7   import java.net.http.HttpResponse;
8   import java.util.function.IntSupplier;
9   
10  import org.springframework.beans.factory.annotation.Value;
11  import org.springframework.stereotype.Component;
12  
13  
14  @Component
15  public class StatusCodeSupplier implements IntSupplier {
16      private final HttpClient httpClient;
17  
18      @Value("${blog.url:'https://jc.id.lv'}")
19      private String blogUrl;
20  
21      public StatusCodeSupplier(HttpClient httpClient) {
22          this.httpClient = httpClient;
23      }
24  
25      /**
26       * Returns the HTTP response code obtained by making a GET request to the specified base URL.
27       *
28       * @return The HTTP response code.
29       * @throws RuntimeException if an IOException or InterruptedException occurs during the execution of the GET request.
30       */
31      @Override
32      public int getAsInt() {
33          try {
34              var request = HttpRequest.newBuilder()
35                  .uri(URI.create(blogUrl))
36                  .build();
37              return httpClient.send(request, HttpResponse.BodyHandlers.ofString()).statusCode();
38          } catch (IOException | InterruptedException e) {
39              throw new RuntimeException(e);
40          }
41      }
42  }