Introduce a high-level HTTP client API (#7398)

This commit is contained in:
Basil Crow authored and GitHub committed 2022-11-19 10:17:11 -08:00
1 parent a2af93b3b7
commit 9e3c7807f3
6 files changed
+240 -101

No files matched your search

+40 -23
View File
@@ -72,18 +72,21 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.lang.reflect.Method;
import java.net.HttpURLConnection;
import java.net.JarURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLConnection;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Paths;
import java.nio.file.attribute.FileTime;
import java.security.CodeSource;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@@ -1956,32 +1959,46 @@ public abstract class PluginManager extends AbstractModelObject implements OnMas
}
@Restricted(NoExternalUse.class)
@RequirePOST public FormValidation doCheckUpdateSiteUrl(StaplerRequest request, @QueryParameter String value) {
@RequirePOST public FormValidation doCheckUpdateSiteUrl(StaplerRequest request, @QueryParameter String value) throws InterruptedException {
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
if (StringUtils.isNotBlank(value)) {
try {
value += ((value.contains("?")) ? "&" : "?") + "version=" + Jenkins.VERSION + "&uctest";
URL url = new URL(value);
// Connect to the URL
HttpURLConnection conn = (HttpURLConnection) ProxyConfiguration.open(url);
conn.setRequestMethod("HEAD");
conn.setConnectTimeout(5000);
if (100 <= conn.getResponseCode() && conn.getResponseCode() <= 399) {
return FormValidation.ok();
} else {
LOGGER.log(Level.FINE, "Obtained a non OK ({0}) response from the update center",
new Object[]{conn.getResponseCode(), url});
return FormValidation.error(Messages.PluginManager_connectionFailed());
}
} catch (IOException e) {
LOGGER.log(Level.FINE, "Failed to check update site", e);
return FormValidation.error(Messages.PluginManager_connectionFailed());
}
} else {
value = Util.fixEmptyAndTrim(value);
if (value == null) {
return FormValidation.error(Messages.PluginManager_emptyUpdateSiteUrl());
}
value += ((value.contains("?")) ? "&" : "?") + "version=" + Jenkins.VERSION + "&uctest";
URI uri;
try {
uri = new URI(value);
} catch (URISyntaxException e) {
return FormValidation.error(e, Messages.PluginManager_invalidUrl());
}
HttpClient httpClient = ProxyConfiguration.newHttpClientBuilder()
.connectTimeout(Duration.ofSeconds(5))
.build();
HttpRequest httpRequest;
try {
httpRequest = ProxyConfiguration.newHttpRequestBuilder(uri)
.method("HEAD", HttpRequest.BodyPublishers.noBody())
.build();
} catch (IllegalArgumentException e) {
return FormValidation.error(e, Messages.PluginManager_invalidUrl());
}
try {
java.net.http.HttpResponse<Void> httpResponse = httpClient.send(
httpRequest, java.net.http.HttpResponse.BodyHandlers.discarding());
if (100 <= httpResponse.statusCode() && httpResponse.statusCode() <= 399) {
return FormValidation.ok();
}
LOGGER.log(Level.FINE, "Obtained a non OK ({0}) response from the update center",
new Object[] {httpResponse.statusCode(), uri});
return FormValidation.error(Messages.PluginManager_connectionFailed());
} catch (IOException e) {
LOGGER.log(Level.FINE, "Failed to check update site", e);
return FormValidation.error(e, Messages.PluginManager_connectionFailed());
}
}
@Restricted(NoExternalUse.class)