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

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


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

commit 2e4330ddc418c4a4e7cf4abb2e177ddb96dbc13f
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 ++++++++++-
 .../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 +++---
 11 files changed, 69 insertions(+), 33 deletions(-)

diff --git a/java/org/apache/catalina/connector/Response.java 
b/java/org/apache/catalina/connector/Response.java
index 67dc666fdc..c9e8d479e3 100644
--- a/java/org/apache/catalina/connector/Response.java
+++ b/java/org/apache/catalina/connector/Response.java
@@ -19,6 +19,8 @@ package org.apache.catalina.connector;
 import java.io.IOException;
 import java.io.PrintWriter;
 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;
@@ -1576,8 +1578,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 0eb9b99827..90cde74adc 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;
@@ -852,8 +854,9 @@ public class NamingContextListener
                 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) {
@@ -885,8 +888,9 @@ public class NamingContextListener
                 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 0a55f69c8d..307c8b4cb6 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 211406fca4..7ed368a5da 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;
 
@@ -63,7 +63,7 @@ public class CatalinaProperties {
         try {
             String configUrl = System.getProperty("catalina.config");
             if (configUrl != null) {
-                is = (new URL(configUrl)).openStream();
+                is = (new URI(configUrl)).toURL().openStream();
             }
         } catch (Throwable t) {
             handleThrowable(t);
diff --git a/java/org/apache/catalina/startup/ClassLoaderFactory.java 
b/java/org/apache/catalina/startup/ClassLoaderFactory.java
index 558d079ed9..08387e47d0 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;
@@ -294,21 +296,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 022f758a61..78f66c1113 100644
--- a/java/org/apache/catalina/startup/ContextConfig.java
+++ b/java/org/apache/catalina/startup/ContextConfig.java
@@ -22,6 +22,7 @@ import java.io.FileNotFoundException;
 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;
@@ -1428,10 +1429,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) {
@@ -1448,10 +1450,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 73394ace2b..880d26e54e 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;
@@ -179,7 +181,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/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 6d3a462bdd..a3b0d07812 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;
@@ -825,8 +825,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-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to