You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2022/11/16 14:53:42 UTC

[tomcat] branch 10.1.x updated: More URL -> URI refactoring

This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch 10.1.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/10.1.x by this push:
     new 88640cc1c7 More URL -> URI refactoring
88640cc1c7 is described below

commit 88640cc1c77c4fc5fc99d4d6fb4e4e06abc82f45
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Wed Nov 16 14:53:17 2022 +0000

    More URL -> URI refactoring
---
 java/org/apache/catalina/connector/Response.java             |  7 +++++--
 java/org/apache/catalina/core/NamingContextListener.java     | 12 ++++++++----
 java/org/apache/catalina/startup/Bootstrap.java              | 10 ++++++----
 java/org/apache/catalina/startup/CatalinaProperties.java     |  4 ++--
 java/org/apache/catalina/startup/ClassLoaderFactory.java     | 10 ++++++----
 java/org/apache/catalina/startup/ContextConfig.java          | 11 +++++++----
 java/org/apache/catalina/startup/WebappServiceLoader.java    | 11 ++++++++++-
 .../tribes/membership/cloud/AbstractStreamProvider.java      | 11 +++++++++--
 .../catalina/webresources/AbstractArchiveResource.java       | 10 ++++++----
 java/org/apache/catalina/webresources/CachedResource.java    | 11 ++++++++++-
 java/org/apache/catalina/webresources/JarResourceRoot.java   | 10 ++++++----
 java/org/apache/catalina/webresources/StandardRoot.java      |  6 +++---
 12 files changed, 78 insertions(+), 35 deletions(-)

diff --git a/java/org/apache/catalina/connector/Response.java b/java/org/apache/catalina/connector/Response.java
index 4349c8867e..394614a9aa 100644
--- a/java/org/apache/catalina/connector/Response.java
+++ b/java/org/apache/catalina/connector/Response.java
@@ -20,6 +20,8 @@ import java.io.IOException;
 import java.io.PrintWriter;
 import java.io.UnsupportedEncodingException;
 import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.net.URL;
 import java.nio.charset.Charset;
 import java.security.AccessController;
@@ -1482,8 +1484,9 @@ public class Response implements HttpServletResponse {
         // Is this a valid absolute URL?
         URL url = null;
         try {
-            url = new URL(location);
-        } catch (MalformedURLException e) {
+            URI uri = new URI(location);
+            url = uri.toURL();
+        } catch (MalformedURLException | URISyntaxException e) {
             return false;
         }
 
diff --git a/java/org/apache/catalina/core/NamingContextListener.java b/java/org/apache/catalina/core/NamingContextListener.java
index 964ee8648d..b5a691a8bd 100644
--- a/java/org/apache/catalina/core/NamingContextListener.java
+++ b/java/org/apache/catalina/core/NamingContextListener.java
@@ -21,6 +21,8 @@ import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
 import java.lang.reflect.Constructor;
 import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.net.URL;
 import java.util.Collection;
 import java.util.HashMap;
@@ -831,8 +833,9 @@ public class NamingContextListener implements LifecycleListener, PropertyChangeL
                 URL wsdlURL = null;
 
                 try {
-                    wsdlURL = new URL(service.getWsdlfile());
-                } catch (MalformedURLException e) {
+                    URI wsdlURI = new URI(service.getWsdlfile());
+                    wsdlURL = wsdlURI.toURL();
+                } catch (MalformedURLException | URISyntaxException e) {
                     // Ignore and carry on
                 }
                 if (wsdlURL == null) {
@@ -864,8 +867,9 @@ public class NamingContextListener implements LifecycleListener, PropertyChangeL
                 URL jaxrpcURL = null;
 
                 try {
-                    jaxrpcURL = new URL(service.getJaxrpcmappingfile());
-                } catch (MalformedURLException e) {
+                    URI jaxrpcURI = new URI(service.getJaxrpcmappingfile());
+                    jaxrpcURL = jaxrpcURI.toURL();
+                } catch (MalformedURLException | URISyntaxException e) {
                     // Ignore and carry on
                 }
                 if (jaxrpcURL == null) {
diff --git a/java/org/apache/catalina/startup/Bootstrap.java b/java/org/apache/catalina/startup/Bootstrap.java
index 6a16c8b330..2f8999a829 100644
--- a/java/org/apache/catalina/startup/Bootstrap.java
+++ b/java/org/apache/catalina/startup/Bootstrap.java
@@ -21,6 +21,8 @@ import java.io.IOException;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.List;
@@ -156,8 +158,7 @@ public final class Bootstrap {
     }
 
 
-    private ClassLoader createClassLoader(String name, ClassLoader parent)
-        throws Exception {
+    private ClassLoader createClassLoader(String name, ClassLoader parent) throws Exception {
 
         String value = CatalinaProperties.getProperty(name + ".loader");
         if ((value == null) || (value.equals(""))) {
@@ -173,11 +174,12 @@ public final class Bootstrap {
         for (String repository : repositoryPaths) {
             // Check for a JAR URL repository
             try {
+                URI uri = new URI(repository);
                 @SuppressWarnings("unused")
-                URL url = new URL(repository);
+                URL url = uri.toURL();
                 repositories.add(new Repository(repository, RepositoryType.URL));
                 continue;
-            } catch (MalformedURLException e) {
+            } catch (MalformedURLException | URISyntaxException e) {
                 // Ignore
             }
 
diff --git a/java/org/apache/catalina/startup/CatalinaProperties.java b/java/org/apache/catalina/startup/CatalinaProperties.java
index be2f940ac3..9bdad9d300 100644
--- a/java/org/apache/catalina/startup/CatalinaProperties.java
+++ b/java/org/apache/catalina/startup/CatalinaProperties.java
@@ -20,7 +20,7 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.net.URL;
+import java.net.URI;
 import java.util.Enumeration;
 import java.util.Properties;
 
@@ -69,7 +69,7 @@ public class CatalinaProperties {
                     // No '/'. Must be a file name rather than a URL
                     fileName = configUrl;
                 } else {
-                    is = (new URL(configUrl)).openStream();
+                    is = (new URI(configUrl)).toURL().openStream();
                 }
             }
         } catch (Throwable t) {
diff --git a/java/org/apache/catalina/startup/ClassLoaderFactory.java b/java/org/apache/catalina/startup/ClassLoaderFactory.java
index ac9c35338d..a104f57b66 100644
--- a/java/org/apache/catalina/startup/ClassLoaderFactory.java
+++ b/java/org/apache/catalina/startup/ClassLoaderFactory.java
@@ -19,6 +19,8 @@ package org.apache.catalina.startup;
 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.URLClassLoader;
 import java.security.AccessController;
@@ -288,21 +290,21 @@ public final class ClassLoaderFactory {
      * org.apache.tomcat.util.buf.UriUtil but that class is not visible until
      * after the class loaders have been constructed.
      */
-    private static URL buildClassLoaderUrl(String urlString) throws MalformedURLException {
+    private static URL buildClassLoaderUrl(String urlString) throws MalformedURLException, URISyntaxException {
         // URLs passed to class loaders may point to directories that contain
         // JARs. If these URLs are used to construct URLs for resources in a JAR
         // the URL will be used as is. It is therefore necessary to ensure that
         // the sequence "!/" is not present in a class loader URL.
         String result = urlString.replaceAll("!/", "%21/");
-        return new URL(result);
+        return (new URI(result)).toURL();
     }
 
 
-    private static URL buildClassLoaderUrl(File file) throws MalformedURLException {
+    private static URL buildClassLoaderUrl(File file) throws MalformedURLException, URISyntaxException {
         // Could be a directory or a file
         String fileUrlString = file.toURI().toString();
         fileUrlString = fileUrlString.replaceAll("!/", "%21/");
-        return new URL(fileUrlString);
+        return (new URI(fileUrlString)).toURL();
     }
 
 
diff --git a/java/org/apache/catalina/startup/ContextConfig.java b/java/org/apache/catalina/startup/ContextConfig.java
index b06c7e4294..ea164cfb6b 100644
--- a/java/org/apache/catalina/startup/ContextConfig.java
+++ b/java/org/apache/catalina/startup/ContextConfig.java
@@ -23,6 +23,7 @@ import java.io.FileWriter;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.MalformedURLException;
+import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
 import java.net.URLConnection;
@@ -1672,10 +1673,11 @@ public class ContextConfig implements LifecycleListener {
         if (globalWebXml != null) {
             URLConnection uc = null;
             try {
-                URL url = new URL(globalWebXml.getSystemId());
+                URI uri = new URI(globalWebXml.getSystemId());
+                URL url = uri.toURL();
                 uc = url.openConnection();
                 globalTimeStamp = uc.getLastModified();
-            } catch (IOException e) {
+            } catch (IOException | URISyntaxException e) {
                 globalTimeStamp = -1;
             } finally {
                 if (uc != null) {
@@ -1692,10 +1694,11 @@ public class ContextConfig implements LifecycleListener {
         if (hostWebXml != null) {
             URLConnection uc = null;
             try {
-                URL url = new URL(hostWebXml.getSystemId());
+                URI uri = new URI(hostWebXml.getSystemId());
+                URL url = uri.toURL();
                 uc = url.openConnection();
                 hostTimeStamp = uc.getLastModified();
-            } catch (IOException e) {
+            } catch (IOException | URISyntaxException e) {
                 hostTimeStamp = -1;
             } finally {
                 if (uc != null) {
diff --git a/java/org/apache/catalina/startup/WebappServiceLoader.java b/java/org/apache/catalina/startup/WebappServiceLoader.java
index 3c0415af20..02c36147bc 100644
--- a/java/org/apache/catalina/startup/WebappServiceLoader.java
+++ b/java/org/apache/catalina/startup/WebappServiceLoader.java
@@ -21,6 +21,8 @@ import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.net.URL;
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
@@ -173,7 +175,14 @@ public class WebappServiceLoader<T> {
                 String base = jarUrl.toExternalForm();
                 URL url;
                 if (base.endsWith("/")) {
-                    url = new URL(base + configFile);
+                    URI uri;
+                    try {
+                        uri = new URI(base + configFile);
+                    } catch (URISyntaxException e) {
+                        // Not ideal but consistent with public API
+                        throw new IOException(e);
+                    }
+                    url = uri.toURL();
                 } else {
                     url = JarFactory.getJarEntryURL(jarUrl, configFile);
                 }
diff --git a/java/org/apache/catalina/tribes/membership/cloud/AbstractStreamProvider.java b/java/org/apache/catalina/tribes/membership/cloud/AbstractStreamProvider.java
index f782d0e1e0..2361169710 100644
--- a/java/org/apache/catalina/tribes/membership/cloud/AbstractStreamProvider.java
+++ b/java/org/apache/catalina/tribes/membership/cloud/AbstractStreamProvider.java
@@ -21,7 +21,8 @@ import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
-import java.net.URL;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.net.URLConnection;
 import java.security.KeyStore;
 import java.security.cert.Certificate;
@@ -77,7 +78,13 @@ public abstract class AbstractStreamProvider implements StreamProvider {
             log.debug(String.format("%s opening connection: url [%s], headers [%s], connectTimeout [%s], readTimeout [%s]",
                     getClass().getSimpleName(), url, headers, Integer.toString(connectTimeout), Integer.toString(readTimeout)));
         }
-        URLConnection connection = new URL(url).openConnection();
+        URLConnection connection;
+        try {
+            connection = new URI(url).toURL().openConnection();
+        } catch (URISyntaxException e) {
+            // Not ideal but consistent with API
+            throw new IOException(e);
+        }
         if (headers != null) {
             for (Map.Entry<String, String> entry : headers.entrySet()) {
                 connection.addRequestProperty(entry.getKey(), entry.getValue());
diff --git a/java/org/apache/catalina/webresources/AbstractArchiveResource.java b/java/org/apache/catalina/webresources/AbstractArchiveResource.java
index e5ececf6db..534a3db429 100644
--- a/java/org/apache/catalina/webresources/AbstractArchiveResource.java
+++ b/java/org/apache/catalina/webresources/AbstractArchiveResource.java
@@ -19,6 +19,8 @@ package org.apache.catalina.webresources;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.net.URL;
 import java.security.cert.Certificate;
 import java.util.concurrent.atomic.AtomicBoolean;
@@ -139,8 +141,8 @@ public abstract class AbstractArchiveResource extends AbstractResource {
     public URL getURL() {
         String url = baseUrl + resource.getName();
         try {
-            return new URL(url);
-        } catch (MalformedURLException e) {
+            return new URI(url).toURL();
+        } catch (MalformedURLException | URISyntaxException e) {
             if (getLog().isDebugEnabled()) {
                 getLog().debug(sm.getString("fileResource.getUrlFail", url), e);
             }
@@ -151,8 +153,8 @@ public abstract class AbstractArchiveResource extends AbstractResource {
     @Override
     public URL getCodeBase() {
         try {
-            return new URL(codeBaseUrl);
-        } catch (MalformedURLException e) {
+            return new URI(codeBaseUrl).toURL();
+        } catch (MalformedURLException | URISyntaxException e) {
             if (getLog().isDebugEnabled()) {
                 getLog().debug(sm.getString("fileResource.getUrlFail", codeBaseUrl), e);
             }
diff --git a/java/org/apache/catalina/webresources/CachedResource.java b/java/org/apache/catalina/webresources/CachedResource.java
index 960571c7c5..516effdeff 100644
--- a/java/org/apache/catalina/webresources/CachedResource.java
+++ b/java/org/apache/catalina/webresources/CachedResource.java
@@ -21,6 +21,8 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.net.JarURLConnection;
 import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.net.URL;
 import java.net.URLConnection;
 import java.net.URLStreamHandler;
@@ -459,7 +461,14 @@ public class CachedResource implements WebResource {
             } else {
                 // The stream handler has been inherited by a URL that was
                 // constructed from a cache URL. We need to break that link.
-                URL constructedURL = new URL(u.toExternalForm());
+                URI constructedURI;
+                try {
+                    constructedURI = new URI(u.toExternalForm());
+                } catch (URISyntaxException e) {
+                    // Not ideal but consistent with API
+                    throw new IOException(e);
+                }
+                URL constructedURL = constructedURI.toURL();
                 return constructedURL.openConnection();
             }
         }
diff --git a/java/org/apache/catalina/webresources/JarResourceRoot.java b/java/org/apache/catalina/webresources/JarResourceRoot.java
index 68b32d684c..48d1d366e0 100644
--- a/java/org/apache/catalina/webresources/JarResourceRoot.java
+++ b/java/org/apache/catalina/webresources/JarResourceRoot.java
@@ -19,6 +19,8 @@ package org.apache.catalina.webresources;
 import java.io.File;
 import java.io.InputStream;
 import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.net.URL;
 import java.security.cert.Certificate;
 import java.util.jar.Manifest;
@@ -124,8 +126,8 @@ public class JarResourceRoot extends AbstractResource {
     public URL getURL() {
         String url = baseUrl + "!/";
         try {
-            return new URL(url);
-        } catch (MalformedURLException e) {
+            return (new URI(url)).toURL();
+        } catch (MalformedURLException | URISyntaxException e) {
             if (log.isDebugEnabled()) {
                 log.debug(sm.getString("fileResource.getUrlFail", url), e);
             }
@@ -136,8 +138,8 @@ public class JarResourceRoot extends AbstractResource {
     @Override
     public URL getCodeBase() {
         try {
-            return new URL(baseUrl);
-        } catch (MalformedURLException e) {
+            return (new URI(baseUrl)).toURL();
+        } catch (MalformedURLException | URISyntaxException e) {
             if (getLog().isDebugEnabled()) {
                 getLog().debug(sm.getString("fileResource.getUrlFail", baseUrl), e);
             }
diff --git a/java/org/apache/catalina/webresources/StandardRoot.java b/java/org/apache/catalina/webresources/StandardRoot.java
index 5515348aed..69efa79b73 100644
--- a/java/org/apache/catalina/webresources/StandardRoot.java
+++ b/java/org/apache/catalina/webresources/StandardRoot.java
@@ -19,7 +19,7 @@ package org.apache.catalina.webresources;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
-import java.net.MalformedURLException;
+import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
 import java.util.ArrayList;
@@ -854,8 +854,8 @@ public class StandardRoot extends LifecycleMBeanBase implements WebResourceRoot
                 }
                 String fileUrl = jarUrl.substring(4, endOfFileUrl);
                 try {
-                    f = new File(new URL(fileUrl).toURI());
-                } catch (MalformedURLException | URISyntaxException e) {
+                    f = new File(new URI(fileUrl));
+                } catch (URISyntaxException e) {
                     throw new IllegalArgumentException(e);
                 }
                 int startOfArchivePath = endOfFileUrl + 2;


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org