This is an automated email from the ASF dual-hosted git repository. markt pushed a commit to branch 9.0.x in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/9.0.x by this push: new 3de18e4cf6 More URL -> URI refactoring 3de18e4cf6 is described below commit 3de18e4cf6c730e572933120d88d83be802ed717 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 | 37 ++++++++++++ .../tomcat/util/descriptor/LocalResolver.java | 8 +-- test/org/apache/tomcat/util/buf/TestUriUtil.java | 67 ++++++++++++++++++++++ webapps/docs/changelog.xml | 9 +++ 4 files changed, 115 insertions(+), 6 deletions(-) diff --git a/java/org/apache/tomcat/util/buf/UriUtil.java b/java/org/apache/tomcat/util/buf/UriUtil.java index a1c56e51c7..22dfb2dc98 100644 --- a/java/org/apache/tomcat/util/buf/UriUtil.java +++ b/java/org/apache/tomcat/util/buf/UriUtil.java @@ -18,6 +18,8 @@ package org.apache.tomcat.util.buf; import java.io.File; import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; import java.net.URL; import java.util.regex.Pattern; @@ -228,4 +230,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 37a952bf39..ac641f7a18 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; @@ -118,12 +118,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 6ba5739a89..35eec47201 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -116,6 +116,15 @@ </fix> </changelog> </subsection> + <subsection name="Other"> + <changelog> + <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> <section name="Tomcat 9.0.69 (remm)" rtext="2022-11-14"> <subsection name="Catalina"> --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org