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

devonfw / IDEasy / 24069879495

07 Apr 2026 07:32AM UTC coverage: 70.609% (-0.03%) from 70.643%
24069879495

Pull #1782

github

web-flow
Merge 2040cb5c4 into 420453a8c
Pull Request #1782: #1724: Create GUI commandlet

4196 of 6560 branches covered (63.96%)

Branch coverage included in aggregate %.

10889 of 14804 relevant lines covered (73.55%)

3.1 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.Redirect;
7
import java.net.http.HttpClient.Version;
8
import java.net.http.HttpRequest;
9
import java.net.http.HttpRequest.BodyPublisher;
10
import java.net.http.HttpRequest.Builder;
11
import java.net.http.HttpResponse;
12
import java.util.function.Consumer;
13

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

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

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

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

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

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

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

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

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

43
  protected static HttpClient createHttpClient() {
44

45
    return HttpClient.newBuilder().followRedirects(Redirect.ALWAYS).build();
5✔
46
  }
47

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

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

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

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

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

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

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

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

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

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

140
}
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