Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use UpdateSite implementation for connect and preValidate #10172

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions core/src/main/java/hudson/model/UpdateCenter.java
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,9 @@
* @throws IOException if the validation fails
*/
public void preValidate(DownloadJob job, URL src) throws IOException {
if(job.site != null) {

Check warning on line 1276 in core/src/main/java/hudson/model/UpdateCenter.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 1276 is only partially covered, one branch is missing
job.site.preValidate(src);
}
}

/**
Expand Down Expand Up @@ -1410,6 +1413,10 @@
* how the connection gets established.
*/
protected URLConnection connect(DownloadJob job, URL src) throws IOException {
if(job.site != null) {

Check warning on line 1416 in core/src/main/java/hudson/model/UpdateCenter.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 1416 is only partially covered, one branch is missing
return job.site.connect(src);
}
// fall back to just using the normal ProxyConfiguration if the site is null
return ProxyConfiguration.open(src);
}

Expand Down
23 changes: 23 additions & 0 deletions core/src/main/java/hudson/model/UpdateSite.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem ideal ProxyConfiguration.open is deprecated:

public static URLConnection open(URL url) throws IOException {

You could potentially use this instead:

public static HttpRequest.Builder newHttpRequestBuilder(URI uri) {
HttpRequest.Builder httpRequestBuilder = HttpRequest.newBuilder(uri);
if (JenkinsJVM.isJenkinsJVM() && !UserAgentURLConnectionDecorator.DISABLED) {
httpRequestBuilder.setHeader("User-Agent", UserAgentURLConnectionDecorator.getUserAgent());
}
return httpRequestBuilder;
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree its not ideal, but the UpdateCenter.connect method returns a URLConnection. Is there a way to get a URLConnection from an HttpRequest.Builder?

}

/**
* 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
Expand Down
Loading