• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

devonfw / IDEasy / 29005741803

09 Jul 2026 08:44AM UTC coverage: 72.471% (+0.3%) from 72.18%
29005741803

Pull #2138

github

web-flow
Merge a1365b48d into 8ed88cfeb
Pull Request #2138: #2137: fix fix-vpn-tls-problem not detecting TLS certificate issues behind HTTP redirects

4898 of 7468 branches covered (65.59%)

Branch coverage included in aggregate %.

12635 of 16725 relevant lines covered (75.55%)

3.19 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

62.79
cli/src/main/java/com/devonfw/tools/ide/io/HttpDownloader.java
1
package com.devonfw.tools.ide.io;
2

3
import java.io.InputStream;
4
import java.net.URI;
5
import java.net.http.HttpClient;
6
import java.net.http.HttpClient.Version;
7
import java.net.http.HttpRequest;
8
import java.net.http.HttpRequest.BodyPublisher;
9
import java.net.http.HttpRequest.Builder;
10
import java.net.http.HttpResponse;
11
import java.util.function.Consumer;
12

13
/**
14
 * Base class providing support for HTTP downloads.
15
 */
16
public abstract class HttpDownloader {
3✔
17

18
  /** @see Builder#GET() */
19
  protected static final String HTTP_METHOD_GET = "GET";
20

21
  /** @see Builder#POST(BodyPublisher) */
22
  protected static final String HTTP_METHOD_POST = "POST";
23

24
  /** @see Builder#PUT(BodyPublisher) */
25
  protected static final String HTTP_METHOD_PUT = "PUT";
26

27
  /** @see Builder#DELETE() */
28
  protected static final String HTTP_METHOD_DELETE = "DELETE";
29

30
  /** @see Builder#HEAD() */
31
  protected static final String HTTP_METHOD_HEAD = "HEAD";
32

33
  /** HTTP method: {@value} */
34
  protected static final String HTTP_METHOD_OPTIONS = "OPTIONS";
35

36
  /** HTTP method: {@value} */
37
  protected static final String HTTP_METHOD_TRACE = "TRACE";
38

39
  /** HTTP method: {@value} */
40
  protected static final String HTTP_METHOD_PATCH = "PATCH";
41

42
  protected static HttpClient createHttpClient() {
43

44
    return HttpClientFactory.create();
2✔
45
  }
46

47
  /**
48
   * Retrieves the {@link HttpResponse} body from a given URL as {@link String}.
49
   *
50
   * @param url the URL to retrieve the response body from.
51
   * @return a string representing the response body.
52
   * @throws IllegalStateException if the response body could not be retrieved.
53
   */
54
  protected static String httpGetAsString(String url) {
55

56
    try (HttpClient client = createHttpClient()) {
2✔
57
      HttpRequest request = createGetRequest(url);
3✔
58
      HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
5✔
59
      if (response.statusCode() == 200) {
4!
60
        return response.body();
6✔
61
      }
62
      throw new IllegalStateException("Unexpected response code " + response.statusCode() + ":" + response.body());
×
63
    } catch (Exception e) {
×
64
      throw new IllegalStateException("Failed to retrieve response body from url: " + url, e);
×
65
    }
66
  }
67

68
  /**
69
   * Retrieves the {@link HttpResponse} body as {@link InputStream} and processes it by the given {@link Consumer}.
70
   *
71
   * @param url the URL to retrieve the response body from.
72
   * @param httpVersion the HTTP {@link Version} to use.
73
   * @param bodyConsumer the {@link Consumer} to process the response body as {@link
74
   * @throws IllegalStateException if the response body could not be retrieved.
75
   */
76
  protected static void httpGet(String url, Version httpVersion, Consumer<HttpResponse<InputStream>> bodyConsumer) {
77

78
    try (HttpClient client = createHttpClient()) {
2✔
79
      HttpRequest request = createGetRequest(url, httpVersion);
4✔
80
      HttpResponse<InputStream> response = client.send(request, HttpResponse.BodyHandlers.ofInputStream());
5✔
81
      int statusCode = response.statusCode();
3✔
82
      if (statusCode == 200) {
3!
83
        bodyConsumer.accept(response);
4✔
84
      } else {
85
        throw new IllegalStateException("Download failed with status code " + statusCode);
×
86
      }
87
    } catch (Exception e) {
×
88
      throw new IllegalStateException("Failed to stream response body from url: " + url, e);
×
89
    }
1✔
90
  }
1✔
91

92
  /**
93
   * Creates a {@link #HTTP_METHOD_GET GET} {@link HttpRequest request}.
94
   *
95
   * @param url the URL.
96
   * @return the {@link HttpRequest}.
97
   */
98
  protected static HttpRequest createGetRequest(String url) {
99

100
    return createRequest(url, null, HTTP_METHOD_GET, null);
6✔
101
  }
102

103
  /**
104
   * Creates a {@link #HTTP_METHOD_GET GET} {@link HttpRequest request}.
105
   *
106
   * @param url the URL.
107
   * @param httpVersion the HTTP {@link Version}.
108
   * @return the {@link HttpRequest}.
109
   */
110
  protected static HttpRequest createGetRequest(String url, Version httpVersion) {
111

112
    return createRequest(url, httpVersion, HTTP_METHOD_GET, null);
6✔
113
  }
114

115
  /**
116
   * Creates a HTTP {@link HttpRequest request}.
117
   *
118
   * @param url the URL.
119
   * @param method the HTTP method. Please only provide constants such as {@link #HTTP_METHOD_DELETE}.
120
   * @param httpVersion the HTTP {@link Version}.
121
   * @return the {@link HttpRequest}.
122
   */
123
  protected static HttpRequest createRequest(String url, Version httpVersion, String method, BodyPublisher bodyPublisher) {
124

125
    Builder builder = HttpRequest.newBuilder()
2✔
126
        .uri(URI.create(url));
3✔
127
    builder = switch (method) {
9!
128
      case HTTP_METHOD_GET -> builder.GET();
3✔
129
      case HTTP_METHOD_DELETE -> builder.DELETE();
×
130
      case HTTP_METHOD_HEAD -> builder.HEAD();
×
131
      default -> builder.method(method, bodyPublisher);
×
132
    };
133
    if (httpVersion != null) {
2!
134
      builder.version(httpVersion);
×
135
    }
136
    return builder.build();
3✔
137
  }
138

139
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc