diff --git a/core/src/main/java/hudson/FilePath.java b/core/src/main/java/hudson/FilePath.java index cdc057ef9c54..d7d306d2aa1a 100644 --- a/core/src/main/java/hudson/FilePath.java +++ b/core/src/main/java/hudson/FilePath.java @@ -83,6 +83,7 @@ import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URI; +import java.net.URISyntaxException; import java.net.URL; import java.net.URLConnection; import java.nio.charset.Charset; @@ -3338,7 +3339,13 @@ private int findSeparator(String pattern) { @Restricted(NoExternalUse.class) static class UrlFactory { public URL newURL(String location) throws MalformedURLException { - return new URL(location); + try { + return new URI(location).toURL(); + } catch (URISyntaxException e) { + MalformedURLException mex = new MalformedURLException(e.getMessage()); + mex.initCause(e); + throw mex; + } } } diff --git a/core/src/main/java/hudson/Functions.java b/core/src/main/java/hudson/Functions.java index 7f3411406780..5438423c6d7e 100644 --- a/core/src/main/java/hudson/Functions.java +++ b/core/src/main/java/hudson/Functions.java @@ -110,7 +110,6 @@ import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; -import java.net.URL; import java.net.URLDecoder; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; @@ -1939,11 +1938,11 @@ public String getServerName() { String url = Jenkins.get().getRootUrl(); try { if (url != null) { - String host = new URL(url).getHost(); + String host = new URI(url).toURL().getHost(); if (host != null) return host; } - } catch (MalformedURLException e) { + } catch (MalformedURLException | URISyntaxException e) { // fall back to HTTP request } return Stapler.getCurrentRequest().getServerName(); diff --git a/core/src/main/java/hudson/Main.java b/core/src/main/java/hudson/Main.java index 625fef0c52ee..3e065bf5d7b0 100644 --- a/core/src/main/java/hudson/Main.java +++ b/core/src/main/java/hudson/Main.java @@ -37,6 +37,7 @@ import java.io.Writer; import java.net.HttpRetryException; import java.net.HttpURLConnection; +import java.net.URI; import java.net.URL; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; @@ -99,11 +100,11 @@ public static int remotePost(String[] args) throws Exception { if (!home.endsWith("/")) home = home + '/'; // make sure it ends with '/' // check for authentication info - String auth = new URL(home).getUserInfo(); + String auth = new URI(home).toURL().getUserInfo(); if (auth != null) auth = "Basic " + new Base64Encoder().encode(auth.getBytes(StandardCharsets.UTF_8)); { // check if the home is set correctly - HttpURLConnection con = open(new URL(home)); + HttpURLConnection con = open(new URI(home).toURL()); if (auth != null) con.setRequestProperty("Authorization", auth); con.connect(); if (con.getResponseCode() != 200 @@ -113,7 +114,7 @@ public static int remotePost(String[] args) throws Exception { } } - URL jobURL = new URL(home + "job/" + Util.encode(projectName).replace("/", "/job/") + "/"); + URL jobURL = new URI(home + "job/" + Util.encode(projectName).replace("/", "/job/") + "/").toURL(); { // check if the job name is correct HttpURLConnection con = open(new URL(jobURL, "acceptBuildResult")); @@ -128,8 +129,8 @@ public static int remotePost(String[] args) throws Exception { // get a crumb to pass the csrf check String crumbField = null, crumbValue = null; try { - HttpURLConnection con = open(new URL(home + - "crumbIssuer/api/xml?xpath=concat(//crumbRequestField,\":\",//crumb)'")); + HttpURLConnection con = open(new URI(home + + "crumbIssuer/api/xml?xpath=concat(//crumbRequestField,\":\",//crumb)'").toURL()); if (auth != null) con.setRequestProperty("Authorization", auth); String line = IOUtils.readFirstLine(con.getInputStream(), "UTF-8"); String[] components = line.split(":"); @@ -193,7 +194,7 @@ public static int remotePost(String[] args) throws Exception { } catch (HttpRetryException e) { if (e.getLocation() != null) { // retry with the new location - location = new URL(e.getLocation()); + location = new URI(e.getLocation()).toURL(); continue; } // otherwise failed for reasons beyond us. diff --git a/core/src/main/java/hudson/PluginManager.java b/core/src/main/java/hudson/PluginManager.java index bd627324bb19..4bf5c4b9869f 100644 --- a/core/src/main/java/hudson/PluginManager.java +++ b/core/src/main/java/hudson/PluginManager.java @@ -1843,7 +1843,7 @@ static class UrlPluginCopier implements PluginCopier { @Override public void copy(File target) throws Exception { - try (InputStream input = ProxyConfiguration.getInputStream(new URL(url))) { + try (InputStream input = ProxyConfiguration.getInputStream(new URI(url).toURL())) { Files.copy(input, target.toPath()); } } @@ -1945,7 +1945,7 @@ public HttpResponse doUploadPlugin(StaplerRequest req) throws IOException, Servl @RequirePOST public FormValidation doCheckPluginUrl(StaplerRequest request, @QueryParameter String value) throws IOException { if (StringUtils.isNotBlank(value)) { try { - URL url = new URL(value); + URL url = new URI(value).toURL(); if (!url.getProtocol().startsWith("http")) { return FormValidation.error(Messages.PluginManager_invalidUrl()); } @@ -1953,7 +1953,7 @@ public HttpResponse doUploadPlugin(StaplerRequest req) throws IOException, Servl if (!url.getProtocol().equals("https")) { return FormValidation.warning(Messages.PluginManager_insecureUrl()); } - } catch (MalformedURLException e) { + } catch (MalformedURLException | URISyntaxException e) { return FormValidation.error(e.getMessage()); } } diff --git a/core/src/main/java/hudson/TcpSlaveAgentListener.java b/core/src/main/java/hudson/TcpSlaveAgentListener.java index 3936071de2ef..e55ff99b2d35 100644 --- a/core/src/main/java/hudson/TcpSlaveAgentListener.java +++ b/core/src/main/java/hudson/TcpSlaveAgentListener.java @@ -38,10 +38,10 @@ import java.io.SequenceInputStream; import java.net.BindException; import java.net.InetSocketAddress; -import java.net.MalformedURLException; import java.net.Socket; import java.net.SocketAddress; -import java.net.URL; +import java.net.URI; +import java.net.URISyntaxException; import java.nio.channels.ServerSocketChannel; import java.nio.charset.StandardCharsets; import java.security.interfaces.RSAPublicKey; @@ -85,8 +85,7 @@ public final class TcpSlaveAgentListener extends Thread { public final int configuredPort; /** - * @param port - * Use 0 to choose a random port. + * @param port Use 0 to choose a random port. */ public TcpSlaveAgentListener(int port) throws IOException { super("TCP agent listener port=" + port); @@ -120,6 +119,7 @@ public int getPort() { /** * Gets the TCP port number in which we are advertising. + * * @since 1.656 */ public int getAdvertisedPort() { @@ -128,21 +128,23 @@ public int getAdvertisedPort() { /** * Gets the host name that we advertise protocol clients to connect to. + * * @since 2.198 */ public String getAdvertisedHost() { if (CLI_HOST_NAME != null) { - return CLI_HOST_NAME; + return CLI_HOST_NAME; } try { - return new URL(Jenkins.get().getRootUrl()).getHost(); - } catch (MalformedURLException e) { + return new URI(Jenkins.get().getRootUrl()).getHost(); + } catch (URISyntaxException e) { throw new IllegalStateException("Could not get TcpSlaveAgentListener host name", e); } } /** * Gets the Base64 encoded public key that forms part of this instance's identity keypair. + * * @return the Base64 encoded public key * @since 2.16 */ @@ -165,6 +167,7 @@ public String getAgentProtocolNames() { /** * Gets Remoting minimum supported version to prevent unsupported agents from connecting + * * @since 2.171 */ public VersionNumber getRemotingMinimumVersion() { @@ -228,9 +231,9 @@ public void shutdown() { private final class ConnectionHandler extends Thread { private static final String DEFAULT_RESPONSE_404 = "HTTP/1.0 404 Not Found\r\n" + - "Content-Type: text/plain;charset=UTF-8\r\n" + - "\r\n" + - "Not Found\r\n"; + "Content-Type: text/plain;charset=UTF-8\r\n" + + "\r\n" + + "Not Found\r\n"; private final Socket s; /** * Unique number to identify this connection. Used in the log. @@ -407,8 +410,8 @@ public boolean connect(Socket socket) throws IOException { socket.getRemoteSocketAddress(), new String(ping, StandardCharsets.UTF_8), responseLength > 0 && responseLength <= response.length ? - new String(response, 0, responseLength, StandardCharsets.UTF_8) : - "bad response length " + responseLength, + new String(response, 0, responseLength, StandardCharsets.UTF_8) : + "bad response length " + responseLength, }); return false; } diff --git a/core/src/main/java/hudson/cli/InstallPluginCommand.java b/core/src/main/java/hudson/cli/InstallPluginCommand.java index cc9d3b59f91d..dfcd410522ee 100644 --- a/core/src/main/java/hudson/cli/InstallPluginCommand.java +++ b/core/src/main/java/hudson/cli/InstallPluginCommand.java @@ -33,6 +33,8 @@ import hudson.util.VersionNumber; import java.io.File; import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; import java.net.URL; import java.nio.file.Files; import java.nio.file.StandardCopyOption; @@ -104,7 +106,7 @@ protected int run() throws Exception { // is this an URL? try { - URL u = new URL(source); + URL u = new URI(source).toURL(); stdout.println(Messages.InstallPluginCommand_InstallingPluginFromUrl(u)); File f = getTmpFile(); FileUtils.copyURLToFile(u, f); // TODO JENKINS-58248 proxy @@ -113,7 +115,7 @@ protected int run() throws Exception { pm.dynamicLoad(f); } continue; - } catch (MalformedURLException e) { + } catch (MalformedURLException | URISyntaxException e) { // not an URL } diff --git a/core/src/main/java/hudson/model/DownloadService.java b/core/src/main/java/hudson/model/DownloadService.java index b9a5610c4e35..ce0e34e44b3d 100644 --- a/core/src/main/java/hudson/model/DownloadService.java +++ b/core/src/main/java/hudson/model/DownloadService.java @@ -44,6 +44,7 @@ import java.io.InputStream; import java.lang.reflect.Field; import java.net.HttpURLConnection; +import java.net.URI; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; @@ -79,6 +80,7 @@ public class DownloadService { * the prefix for the signature validator name */ private static final String signatureValidatorPrefix = "downloadable"; + /** * Builds up an HTML fragment that starts all the download jobs. * @@ -108,6 +110,7 @@ public Downloadable getById(String id) { * Confusingly, the JSONP files are given the {@code *.json} file extension, when they are really JavaScript and should be {@code *.js}. * This method extracts the JSON from a JSONP URL, since that is what we actually want when we download from the server. * (Currently the true JSON is not published separately, and extracting from the {@code *.json.html} is more work.) + * * @param src a URL to a JSONP file (typically including {@code id} and {@code version} query parameters) * @return the embedded JSON text * @throws IOException if either downloading or processing failed @@ -133,6 +136,7 @@ public static String loadJSON(URL src) throws IOException { /** * Loads JSON from a JSON-with-{@code postMessage} URL. + * * @param src a URL to a JSON HTML file (typically including {@code id} and {@code version} query parameters) * @return the embedded JSON text * @throws IOException if either downloading or processing failed @@ -215,14 +219,13 @@ public static class Downloadable implements ExtensionPoint { /** * Creates a new downloadable. * - * @param id The ID to use. - * @param url - * URL relative to {@link UpdateCenter#getDefaultBaseUrl()}. - * So if this string is "foo.json", the ultimate URL will be - * something like "http://updates.jenkins-ci.org/updates/foo.json" - * - * For security and privacy reasons, we don't allow the retrieval - * from random locations. + * @param id The ID to use. + * @param url URL relative to {@link UpdateCenter#getDefaultBaseUrl()}. + * So if this string is "foo.json", the ultimate URL will be + * something like "http://updates.jenkins-ci.org/updates/foo.json" + *
+ * For security and privacy reasons, we don't allow the retrieval
+ * from random locations.
* @param interval The interval, in milliseconds, between attempts to update this downloadable's data.
*/
public Downloadable(@NonNull String id, @NonNull String url, long interval) {
@@ -286,7 +289,6 @@ public String getId() {
*
* @param clazz The class to use to generate an ID.
* @return The ID generated based on the specified class.
- *
* @since 2.244
*/
@NonNull
@@ -322,8 +324,7 @@ public List