From 78b85ddc70bb0fd2fb23bced8da1f3cde3ad30f1 Mon Sep 17 00:00:00 2001 From: Alex Earl Date: Fri, 17 Jan 2025 05:23:47 -0700 Subject: [PATCH] Allow UpdateSite to create the URLConnection Different UpdateSites may need to connect to URLs differently, this adds a method to UpdateSite which creates the URLConnection that will be used for connecting. This allows an implementation of a password protected UpdateSite which can add the authorization information to the connection prior to returning it. --- .../main/java/hudson/model/UpdateCenter.java | 7 ++++++ .../main/java/hudson/model/UpdateSite.java | 23 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/core/src/main/java/hudson/model/UpdateCenter.java b/core/src/main/java/hudson/model/UpdateCenter.java index 437444feb434..5d0f1d0ef125 100644 --- a/core/src/main/java/hudson/model/UpdateCenter.java +++ b/core/src/main/java/hudson/model/UpdateCenter.java @@ -1273,6 +1273,9 @@ static URL toUpdateCenterCheckUrl(String updateCenterUrl) throws MalformedURLExc * @throws IOException if the validation fails */ public void preValidate(DownloadJob job, URL src) throws IOException { + if(job.site != null) { + job.site.preValidate(src); + } } /** @@ -1410,6 +1413,10 @@ private static String getSourceUrl(@NonNull URL src, @CheckForNull URLConnection * how the connection gets established. */ protected URLConnection connect(DownloadJob job, URL src) throws IOException { + if(job.site != null) { + return job.site.connect(src); + } + // fall back to just using the normal ProxyConfiguration if the site is null return ProxyConfiguration.open(src); } diff --git a/core/src/main/java/hudson/model/UpdateSite.java b/core/src/main/java/hudson/model/UpdateSite.java index 22764fd403d0..b0c30e861e3e 100644 --- a/core/src/main/java/hudson/model/UpdateSite.java +++ b/core/src/main/java/hudson/model/UpdateSite.java @@ -37,6 +37,7 @@ import hudson.ExtensionList; import hudson.PluginManager; import hudson.PluginWrapper; +import hudson.ProxyConfiguration; import hudson.Util; import hudson.lifecycle.Lifecycle; import hudson.model.UpdateCenter.UpdateCenterJob; @@ -48,6 +49,7 @@ import java.io.IOException; import java.net.URI; import java.net.URL; +import java.net.URLConnection; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.time.Instant; @@ -202,6 +204,27 @@ public long getDataTimestamp() { } } + /** + * Opens a connection to the given URL + * @param src the url to connect to + * @return A {@code URLConnection} for the given src URL + * @since TODO + */ + public URLConnection connect(URL src) throws IOException { + return ProxyConfiguration.open(src); + } + + /** + * Validate the URL of the resource before downloading it. + * + * @param src The location of the resource on the network + * @throws IOException if the validation fails + * @since TODO + */ + public void preValidate(URL src) throws IOException { + // no validation needed in the default setup + } + /** * Forces an update of the data file from the configured URL, irrespective of the last time the data was retrieved. * @return A {@code FormValidation} indicating the if the update metadata was successfully downloaded from the configured update site