This is an automated email from the ASF dual-hosted git repository. markt pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/main by this push: new e9d1404b2d More URL -> URI refactoring e9d1404b2d is described below commit e9d1404b2d8c7713847592023771d58e032423c5 Author: Mark Thomas <ma...@apache.org> AuthorDate: Wed Nov 16 19:04:55 2022 +0000 More URL -> URI refactoring This one is a little more interesting as we were deliberately using one of the URL constructors. --- java/org/apache/tomcat/util/buf/UriUtil.java | 77 ++++++++++++++++++---- .../tomcat/util/descriptor/LocalResolver.java | 8 +-- test/org/apache/tomcat/util/buf/TestUriUtil.java | 67 +++++++++++++++++++ webapps/docs/changelog.xml | 5 ++ 4 files changed, 140 insertions(+), 17 deletions(-) diff --git a/java/org/apache/tomcat/util/buf/UriUtil.java b/java/org/apache/tomcat/util/buf/UriUtil.java index a1c56e51c7..12a1280ace 100644 --- a/java/org/apache/tomcat/util/buf/UriUtil.java +++ b/java/org/apache/tomcat/util/buf/UriUtil.java @@ -17,7 +17,10 @@ package org.apache.tomcat.util.buf; 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.util.regex.Pattern; @@ -102,22 +105,22 @@ public final class UriUtil { } - public static URL buildJarUrl(File jarFile) throws MalformedURLException { + public static URL buildJarUrl(File jarFile) throws IOException { return buildJarUrl(jarFile, null); } - public static URL buildJarUrl(File jarFile, String entryPath) throws MalformedURLException { + public static URL buildJarUrl(File jarFile, String entryPath) throws IOException { return buildJarUrl(jarFile.toURI().toString(), entryPath); } - public static URL buildJarUrl(String fileUrlString) throws MalformedURLException { + public static URL buildJarUrl(String fileUrlString) throws IOException { return buildJarUrl(fileUrlString, null); } - public static URL buildJarUrl(String fileUrlString, String entryPath) throws MalformedURLException { + public static URL buildJarUrl(String fileUrlString, String entryPath) throws IOException { String safeString = makeSafeForJarUrl(fileUrlString); StringBuilder sb = new StringBuilder(); sb.append(safeString); @@ -125,13 +128,25 @@ public final class UriUtil { if (entryPath != null) { sb.append(makeSafeForJarUrl(entryPath)); } - return new URL("jar", null, -1, sb.toString()); + URI uri; + try { + uri = new URI("jar", sb.toString(), null); + } catch (URISyntaxException e) { + throw new IOException(e); + } + return uri.toURL(); } - public static URL buildJarSafeUrl(File file) throws MalformedURLException { + public static URL buildJarSafeUrl(File file) throws IOException { String safe = makeSafeForJarUrl(file.toURI().toString()); - return new URL(safe); + URI uri; + try { + uri = new URI(safe); + } catch (URISyntaxException e) { + throw new IOException(e); + } + return uri.toURL(); } @@ -173,9 +188,9 @@ public final class UriUtil { * * @return The equivalent JAR URL * - * @throws MalformedURLException If the conversion fails + * @throws IOException If the conversion fails */ - public static URL warToJar(URL warUrl) throws MalformedURLException { + public static URL warToJar(URL warUrl) throws IOException { // Assumes that the spec is absolute and starts war:file:/... String file = warUrl.getFile(); if (file.contains("*/")) { @@ -185,8 +200,13 @@ public final class UriUtil { } else if (PATTERN_CUSTOM != null) { file = file.replaceFirst(PATTERN_CUSTOM.pattern(), "!/"); } - - return new URL("jar", warUrl.getHost(), warUrl.getPort(), file); + URI uri; + try { + uri = new URI("jar", file, null); + } catch (URISyntaxException e) { + throw new IOException(e); + } + return uri.toURL(); } @@ -228,4 +248,39 @@ public final class UriUtil { } return false; } + + + /** + * Replicates the behaviour of {@link URI#resolve(String)} and adds support + * for URIs of the form {@code jar:file:/... }. + * + * @param base The base URI to resolve against + * @param target The path to resolve + * + * @return The resulting URI as per {@link URI#resolve(String)} + * + * @throws MalformedURLException + * If the base URI cannot be converted to a URL + * @throws URISyntaxException + * If the resulting URL cannot be converted to a URI + */ + public static URI resolve(URI base, String target) throws MalformedURLException, URISyntaxException { + if (base.getScheme().equals("jar")) { + /* + * Previously used: + * new URL(base.toURL(), target).toURI() + * This delegated the work to the jar stream handler which correctly + * resolved the target against the base. + * + * Deprecation of all the URL constructors mean a different approach + * is required. + */ + URI fileUri = new URI(base.getSchemeSpecificPart()); + URI fileUriResolved = fileUri.resolve(target); + + return new URI("jar:" + fileUriResolved.toString()); + } else { + return base.resolve(target); + } + } } diff --git a/java/org/apache/tomcat/util/descriptor/LocalResolver.java b/java/org/apache/tomcat/util/descriptor/LocalResolver.java index 68516e57c3..dbfd593a8b 100644 --- a/java/org/apache/tomcat/util/descriptor/LocalResolver.java +++ b/java/org/apache/tomcat/util/descriptor/LocalResolver.java @@ -21,9 +21,9 @@ import java.io.IOException; import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; -import java.net.URL; import java.util.Map; +import org.apache.tomcat.util.buf.UriUtil; import org.apache.tomcat.util.res.StringManager; import org.xml.sax.InputSource; import org.xml.sax.SAXException; @@ -119,12 +119,8 @@ public class LocalResolver implements EntityResolver2 { if (base == null) { systemUri = new URI(systemId); } else { - // Can't use URI.resolve() because "jar:..." URLs are not valid - // hierarchical URIs so resolve() does not work. new URL() - // delegates to the jar: stream handler and it manages to figure - // it out. URI baseUri = new URI(base); - systemUri = new URL(baseUri.toURL(), systemId).toURI(); + systemUri = UriUtil.resolve(baseUri, systemId); } systemUri = systemUri.normalize(); } catch (URISyntaxException e) { diff --git a/test/org/apache/tomcat/util/buf/TestUriUtil.java b/test/org/apache/tomcat/util/buf/TestUriUtil.java new file mode 100644 index 0000000000..fc7531c13e --- /dev/null +++ b/test/org/apache/tomcat/util/buf/TestUriUtil.java @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tomcat.util.buf; + +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; + +import org.junit.Assert; +import org.junit.Test; + +public class TestUriUtil { + + @Test + public void testResolve01() throws URISyntaxException, MalformedURLException { + URI base = new URI("file:/aaa/bbb/base.xml"); + String target = "target.xml"; + URI result = UriUtil.resolve(base, target); + Assert.assertEquals(new URI("file:/aaa/bbb/target.xml"), result); + } + + @Test + public void testResolve02() throws URISyntaxException, MalformedURLException { + URI base = new URI("file:/aaa/bbb/base.xml"); + String target = "../target.xml"; + URI result = UriUtil.resolve(base, target); + Assert.assertEquals(new URI("file:/aaa/target.xml"), result); + } + + @Test + public void testResolve03() throws URISyntaxException, MalformedURLException { + URI base = new URI("jar:file:/aaa/bbb!/ccc/ddd/base.xml"); + String target = "target.xml"; + URI result = UriUtil.resolve(base, target); + Assert.assertEquals(new URI("jar:file:/aaa/bbb!/ccc/ddd/target.xml"), result); + } + + @Test + public void testResolve04() throws URISyntaxException, MalformedURLException { + URI base = new URI("jar:file:/aaa/bbb!/ccc/ddd/base.xml"); + String target = "../target.xml"; + URI result = UriUtil.resolve(base, target); + Assert.assertEquals(new URI("jar:file:/aaa/bbb!/ccc/target.xml"), result); + } + + @Test + public void testResolve05() throws URISyntaxException, MalformedURLException { + URI base = new URI("jar:file:/aaa/bbb!/ccc/ddd/base.xml"); + String target = "../../target.xml"; + URI result = UriUtil.resolve(base, target); + Assert.assertEquals(new URI("jar:file:/aaa/bbb!/target.xml"), result); + } +} diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index ddf984a862..753b22eea8 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -248,6 +248,11 @@ Update the packaged version of the Apache Tomcat Migration Tool for Jakarta EE to 1.0.5. (markt) </update> + <scode> + Refactor code base to replace use of URL constructors. While they are + deprecated in Java 20 onwards, the reasons for deprecation are valid for + all versions so move away from them now. (markt) + </scode> </changelog> </subsection> </section> --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org