-
Notifications
You must be signed in to change notification settings - Fork 751
Expand file tree
/
Copy pathDefaultHttpFetcher.java
More file actions
67 lines (53 loc) · 2.25 KB
/
Copy pathDefaultHttpFetcher.java
File metadata and controls
67 lines (53 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package io.jenkins.plugins.casc.fetcher;
import static java.lang.Thread.currentThread;
import hudson.Extension;
import hudson.ProxyConfiguration;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.Collections;
@Extension(ordinal = -100)
public class DefaultHttpFetcher implements CasCConfigFetcher {
@Override
public boolean supports(String location) {
return location != null && (location.startsWith("http://") || location.startsWith("https://"));
}
@Override
public FetchResult fetch(String location, FetchCredentials credentials) throws IOException {
URI uri;
try {
uri = new URI(location);
} catch (URISyntaxException e) {
throw new IOException("Invalid URL: " + location, e);
}
String path = uri.getPath();
String fileName =
(path != null && path.contains("/")) ? path.substring(path.lastIndexOf('/') + 1) : "casc.yaml";
if (fileName.isEmpty()) {
fileName = "casc.yaml";
}
HttpClient client = ProxyConfiguration.newHttpClient();
HttpRequest request = ProxyConfiguration.newHttpRequestBuilder(uri)
.GET()
.timeout(Duration.ofSeconds(30))
.build();
byte[] yamlBytes;
try {
HttpResponse<byte[]> response = client.send(request, HttpResponse.BodyHandlers.ofByteArray());
if (response.statusCode() < 200 || response.statusCode() >= 300) {
throw new IOException("Failed to fetch configuration from " + location + ". HTTP status code: "
+ response.statusCode());
}
yamlBytes = response.body();
} catch (InterruptedException e) {
currentThread().interrupt();
throw new IOException("Interrupted while fetching configuration from: " + location, e);
}
ResolvedYaml resolved = new ResolvedYaml(fileName, () -> new java.io.ByteArrayInputStream(yamlBytes));
return new FetchResult(Collections.singletonList(resolved), (AutoCloseable) null);
}
}