diff --git a/core/src/main/java/hudson/FilePath.java b/core/src/main/java/hudson/FilePath.java index d7d306d2aa1a..cdc057ef9c54 100644 --- a/core/src/main/java/hudson/FilePath.java +++ b/core/src/main/java/hudson/FilePath.java @@ -83,7 +83,6 @@ 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; @@ -3339,13 +3338,7 @@ private int findSeparator(String pattern) { @Restricted(NoExternalUse.class) static class UrlFactory { public URL newURL(String location) throws MalformedURLException { - try { - return new URI(location).toURL(); - } catch (URISyntaxException e) { - MalformedURLException mex = new MalformedURLException(e.getMessage()); - mex.initCause(e); - throw mex; - } + return new URL(location); } } diff --git a/core/src/main/java/hudson/Functions.java b/core/src/main/java/hudson/Functions.java index 5438423c6d7e..7f3411406780 100644 --- a/core/src/main/java/hudson/Functions.java +++ b/core/src/main/java/hudson/Functions.java @@ -110,6 +110,7 @@ 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; @@ -1938,11 +1939,11 @@ public String getServerName() { String url = Jenkins.get().getRootUrl(); try { if (url != null) { - String host = new URI(url).toURL().getHost(); + String host = new URL(url).getHost(); if (host != null) return host; } - } catch (MalformedURLException | URISyntaxException e) { + } catch (MalformedURLException 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 3e065bf5d7b0..625fef0c52ee 100644 --- a/core/src/main/java/hudson/Main.java +++ b/core/src/main/java/hudson/Main.java @@ -37,7 +37,6 @@ 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; @@ -100,11 +99,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 URI(home).toURL().getUserInfo(); + String auth = new URL(home).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 URI(home).toURL()); + HttpURLConnection con = open(new URL(home)); if (auth != null) con.setRequestProperty("Authorization", auth); con.connect(); if (con.getResponseCode() != 200 @@ -114,7 +113,7 @@ public static int remotePost(String[] args) throws Exception { } } - URL jobURL = new URI(home + "job/" + Util.encode(projectName).replace("/", "/job/") + "/").toURL(); + URL jobURL = new URL(home + "job/" + Util.encode(projectName).replace("/", "/job/") + "/"); { // check if the job name is correct HttpURLConnection con = open(new URL(jobURL, "acceptBuildResult")); @@ -129,8 +128,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 URI(home + - "crumbIssuer/api/xml?xpath=concat(//crumbRequestField,\":\",//crumb)'").toURL()); + HttpURLConnection con = open(new URL(home + + "crumbIssuer/api/xml?xpath=concat(//crumbRequestField,\":\",//crumb)'")); if (auth != null) con.setRequestProperty("Authorization", auth); String line = IOUtils.readFirstLine(con.getInputStream(), "UTF-8"); String[] components = line.split(":"); @@ -194,7 +193,7 @@ public static int remotePost(String[] args) throws Exception { } catch (HttpRetryException e) { if (e.getLocation() != null) { // retry with the new location - location = new URI(e.getLocation()).toURL(); + location = new URL(e.getLocation()); 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 9c7f29153bdc..649cc045b482 100644 --- a/core/src/main/java/hudson/PluginManager.java +++ b/core/src/main/java/hudson/PluginManager.java @@ -1846,7 +1846,7 @@ static class UrlPluginCopier implements PluginCopier { @Override public void copy(File target) throws Exception { - try (InputStream input = ProxyConfiguration.getInputStream(new URI(url).toURL())) { + try (InputStream input = ProxyConfiguration.getInputStream(new URL(url))) { Files.copy(input, target.toPath()); } } @@ -1948,7 +1948,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 URI(value).toURL(); + URL url = new URL(value); if (!url.getProtocol().startsWith("http")) { return FormValidation.error(Messages.PluginManager_invalidUrl()); } @@ -1956,7 +1956,7 @@ public HttpResponse doUploadPlugin(StaplerRequest req) throws IOException, Servl if (!url.getProtocol().equals("https")) { return FormValidation.warning(Messages.PluginManager_insecureUrl()); } - } catch (MalformedURLException | URISyntaxException e) { + } catch (MalformedURLException 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 e55ff99b2d35..3936071de2ef 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.URI; -import java.net.URISyntaxException; +import java.net.URL; import java.nio.channels.ServerSocketChannel; import java.nio.charset.StandardCharsets; import java.security.interfaces.RSAPublicKey; @@ -85,7 +85,8 @@ 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); @@ -119,7 +120,6 @@ public int getPort() { /** * Gets the TCP port number in which we are advertising. - * * @since 1.656 */ public int getAdvertisedPort() { @@ -128,23 +128,21 @@ 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 URI(Jenkins.get().getRootUrl()).getHost(); - } catch (URISyntaxException e) { + return new URL(Jenkins.get().getRootUrl()).getHost(); + } catch (MalformedURLException 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 */ @@ -167,7 +165,6 @@ public String getAgentProtocolNames() { /** * Gets Remoting minimum supported version to prevent unsupported agents from connecting - * * @since 2.171 */ public VersionNumber getRemotingMinimumVersion() { @@ -231,9 +228,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. @@ -410,8 +407,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 dfcd410522ee..cc9d3b59f91d 100644 --- a/core/src/main/java/hudson/cli/InstallPluginCommand.java +++ b/core/src/main/java/hudson/cli/InstallPluginCommand.java @@ -33,8 +33,6 @@ 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; @@ -106,7 +104,7 @@ protected int run() throws Exception { // is this an URL? try { - URL u = new URI(source).toURL(); + URL u = new URL(source); stdout.println(Messages.InstallPluginCommand_InstallingPluginFromUrl(u)); File f = getTmpFile(); FileUtils.copyURLToFile(u, f); // TODO JENKINS-58248 proxy @@ -115,7 +113,7 @@ protected int run() throws Exception { pm.dynamicLoad(f); } continue; - } catch (MalformedURLException | URISyntaxException e) { + } catch (MalformedURLException 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 ce0e34e44b3d..b9a5610c4e35 100644 --- a/core/src/main/java/hudson/model/DownloadService.java +++ b/core/src/main/java/hudson/model/DownloadService.java @@ -44,7 +44,6 @@ 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; @@ -80,7 +79,6 @@ 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. * @@ -110,7 +108,6 @@ 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 @@ -136,7 +133,6 @@ 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 @@ -219,13 +215,14 @@ 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) { @@ -289,6 +286,7 @@ 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 @@ -324,7 +322,8 @@ public List getUrls() { /** * How often do we retrieve the new image? * - * @return number of milliseconds between retrieval. + * @return + * number of milliseconds between retrieval. */ public long getInterval() { return interval; @@ -389,7 +388,7 @@ public FormValidation updateNow() throws IOException { } String jsonString; try { - jsonString = loadJSONHTML(new URI(site + ".html?id=" + URLEncoder.encode(getId(), StandardCharsets.UTF_8) + "&version=" + URLEncoder.encode(Jenkins.VERSION, StandardCharsets.UTF_8)).toURL()); + jsonString = loadJSONHTML(new URL(site + ".html?id=" + URLEncoder.encode(getId(), StandardCharsets.UTF_8) + "&version=" + URLEncoder.encode(Jenkins.VERSION, StandardCharsets.UTF_8))); toolInstallerMetadataExists = true; } catch (Exception e) { LOGGER.log(Level.FINE, "Could not load json from " + site, e); @@ -417,7 +416,6 @@ public FormValidation updateNow() throws IOException { /** * Function that takes multiple JSONObjects and returns a single one. - * * @param jsonList to be processed * @return a single JSONObject */ @@ -427,10 +425,9 @@ public JSONObject reduce(List jsonList) { /** * check if the list of update center entries has duplicates - * * @param genericList list of entries coming from multiple update centers - * @param comparator the unique ID of an entry - * @param the generic class + * @param comparator the unique ID of an entry + * @param the generic class * @return true if the list has duplicates, false otherwise */ public static boolean hasDuplicates(List genericList, String comparator) { @@ -473,6 +470,7 @@ public static ExtensionList all() { * {@link #idFor(Class)}). * * @param clazz The class to use to determine the downloadable's ID. + * * @since 2.244 */ @CheckForNull diff --git a/core/src/main/java/hudson/model/UpdateCenter.java b/core/src/main/java/hudson/model/UpdateCenter.java index 58d9982dbbd1..56fd6e77c988 100644 --- a/core/src/main/java/hudson/model/UpdateCenter.java +++ b/core/src/main/java/hudson/model/UpdateCenter.java @@ -66,8 +66,6 @@ import java.net.HttpRetryException; 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.net.UnknownHostException; @@ -1211,13 +1209,7 @@ public UpdateCenterConfiguration() { * @throws IOException if a connection can't be established */ public void checkConnection(ConnectionCheckJob job, String connectionCheckUrl) throws IOException { - try { - testConnection(new URI(connectionCheckUrl).toURL()); - } catch (URISyntaxException e) { - MalformedURLException mex = new MalformedURLException(e.getMessage()); - mex.initCause(e); - throw mex; - } + testConnection(new URL(connectionCheckUrl)); } /** @@ -1240,21 +1232,9 @@ public void checkUpdateCenter(ConnectionCheckJob job, String updateCenterUrl) th static URL toUpdateCenterCheckUrl(String updateCenterUrl) throws MalformedURLException { URL url; if (updateCenterUrl.startsWith("http://") || updateCenterUrl.startsWith("https://")) { - try { - url = new URI(updateCenterUrl + (updateCenterUrl.indexOf('?') == -1 ? "?uctest" : "&uctest")).toURL(); - } catch (URISyntaxException e) { - MalformedURLException mex = new MalformedURLException(e.getMessage()); - mex.initCause(e); - throw mex; - } + url = new URL(updateCenterUrl + (updateCenterUrl.indexOf('?') == -1 ? "?uctest" : "&uctest")); } else { - try { - url = new URI(updateCenterUrl).toURL(); - } catch (URISyntaxException e) { - MalformedURLException mex = new MalformedURLException(e.getMessage()); - mex.initCause(e); - throw mex; - } + url = new URL(updateCenterUrl); } return url; } @@ -2444,13 +2424,7 @@ public PluginDowngradeJob(Plugin plugin, UpdateSite site, Authentication auth) { @Override protected URL getURL() throws MalformedURLException { - try { - return new URI(plugin.url).toURL(); - } catch (URISyntaxException e) { - MalformedURLException mex = new MalformedURLException(e.getMessage()); - mex.initCause(e); - throw mex; - } + return new URL(plugin.url); } @Override @@ -2592,13 +2566,7 @@ protected URL getURL() throws MalformedURLException { if (site == null) { throw new MalformedURLException("no update site defined"); } - try { - return new URI(site.getData().core.url).toURL(); - } catch (URISyntaxException e) { - MalformedURLException mex = new MalformedURLException(e.getMessage()); - mex.initCause(e); - throw mex; - } + return new URL(site.getData().core.url); } @Override diff --git a/core/src/main/java/hudson/model/UpdateSite.java b/core/src/main/java/hudson/model/UpdateSite.java index 522afbc98962..1b5b734c7be6 100644 --- a/core/src/main/java/hudson/model/UpdateSite.java +++ b/core/src/main/java/hudson/model/UpdateSite.java @@ -46,9 +46,8 @@ import hudson.util.VersionNumber; import java.io.File; import java.io.IOException; -import java.net.MalformedURLException; import java.net.URI; -import java.net.URISyntaxException; +import java.net.URL; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.time.Instant; @@ -216,13 +215,7 @@ public long getDataTimestamp() { @Restricted(NoExternalUse.class) public @NonNull FormValidation updateDirectlyNow(boolean signatureCheck) throws IOException { - try { - return updateData(DownloadService.loadJSON(new URI(getUrl() + "?id=" + URLEncoder.encode(getId(), StandardCharsets.UTF_8) + "&version=" + URLEncoder.encode(Jenkins.VERSION, StandardCharsets.UTF_8)).toURL()), signatureCheck); - } catch (URISyntaxException e) { - MalformedURLException mex = new MalformedURLException(e.getMessage()); - mex.initCause(e); - throw mex; - } + return updateData(DownloadService.loadJSON(new URL(getUrl() + "?id=" + URLEncoder.encode(getId(), StandardCharsets.UTF_8) + "&version=" + URLEncoder.encode(Jenkins.VERSION, StandardCharsets.UTF_8))), signatureCheck); } private FormValidation updateData(String json, boolean signatureCheck) diff --git a/core/src/main/java/hudson/tools/ZipExtractionInstaller.java b/core/src/main/java/hudson/tools/ZipExtractionInstaller.java index 8fb77e5cfe59..1e1c171f6a95 100644 --- a/core/src/main/java/hudson/tools/ZipExtractionInstaller.java +++ b/core/src/main/java/hudson/tools/ZipExtractionInstaller.java @@ -37,9 +37,9 @@ import java.io.File; import java.io.IOException; import java.net.HttpURLConnection; -import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; +import java.net.URL; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; @@ -83,14 +83,8 @@ public String getSubdir() { @Override public FilePath performInstallation(ToolInstallation tool, Node node, TaskListener log) throws IOException, InterruptedException { FilePath dir = preferredLocation(tool, node); - try { - if (dir.installIfNecessaryFrom(new URI(url).toURL(), log, "Unpacking " + url + " to " + dir + " on " + node.getDisplayName())) { - dir.act(new ChmodRecAPlusX()); - } - } catch (URISyntaxException e) { - MalformedURLException mex = new MalformedURLException(e.getMessage()); - mex.initCause(e); - throw mex; + if (dir.installIfNecessaryFrom(new URL(url), log, "Unpacking " + url + " to " + dir + " on " + node.getDisplayName())) { + dir.act(new ChmodRecAPlusX()); } if (subdir == null) { return dir;