diff --git a/jetty-quickstart/src/main/java/org/eclipse/jetty/quickstart/AttributeNormalizer.java b/jetty-quickstart/src/main/java/org/eclipse/jetty/quickstart/AttributeNormalizer.java index cc897b7ae10..1cddeb5711c 100644 --- a/jetty-quickstart/src/main/java/org/eclipse/jetty/quickstart/AttributeNormalizer.java +++ b/jetty-quickstart/src/main/java/org/eclipse/jetty/quickstart/AttributeNormalizer.java @@ -28,7 +28,6 @@ import java.nio.file.Path; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; -import java.util.List; import java.util.Stack; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -146,7 +145,20 @@ public class AttributeNormalizer return o2.weight - o1.weight; } } - + + private static class PathAttributes extends ArrayList + { + @Override + public boolean add(AttributeNormalizer.PathAttribute pathAttribute) + { + if (pathAttribute.path == null) + { + return false; + } + return super.add(pathAttribute); + } + } + public static String uriSeparators(String path) { StringBuilder ret = new StringBuilder(); @@ -165,7 +177,7 @@ public class AttributeNormalizer } private URI warURI; - private List attributes = new ArrayList<>(); + private PathAttributes attributes = new PathAttributes(); public AttributeNormalizer(Resource baseResource) { @@ -181,6 +193,10 @@ public class AttributeNormalizer attributes.add(new PathAttribute("jetty.home", "jetty.home").weight(8)); attributes.add(new PathAttribute("user.home", "user.home").weight(7)); attributes.add(new PathAttribute("user.dir", "user.dir").weight(6)); + if(warURI != null && warURI.getScheme().equals("file")) + { + attributes.add(new PathAttribute("WAR", new File(warURI).toPath().toAbsolutePath()).weight(10)); + } Collections.sort(attributes, new PathAttributeComparator()); diff --git a/tests/test-quickstart/src/test/java/org/eclipse/jetty/quickstart/AttributeNormalizerTest.java b/tests/test-quickstart/src/test/java/org/eclipse/jetty/quickstart/AttributeNormalizerTest.java index 575d972e011..8e9b1a70068 100644 --- a/tests/test-quickstart/src/test/java/org/eclipse/jetty/quickstart/AttributeNormalizerTest.java +++ b/tests/test-quickstart/src/test/java/org/eclipse/jetty/quickstart/AttributeNormalizerTest.java @@ -19,20 +19,82 @@ package org.eclipse.jetty.quickstart; import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URI; - -import org.eclipse.jetty.util.resource.Resource; -import org.junit.Test; - import static org.hamcrest.Matchers.anyOf; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; import static org.junit.Assert.assertThat; +import java.io.File; +import java.net.MalformedURLException; +import java.net.URI; + +import org.eclipse.jetty.util.resource.Resource; +import org.junit.Test; + public class AttributeNormalizerTest { + @Test + public void testNormalizeOrder() throws MalformedURLException + { + String oldJettyHome = System.getProperty("jetty.home"); + String oldJettyBase = System.getProperty("jetty.base"); + + try + { + String testJettyHome = AttributeNormalizerPathTest.toSystemPath("/opt/jetty-distro"); + String testJettyBase = AttributeNormalizerPathTest.toSystemPath("/opt/jetty-distro/demo.base"); + String testWar = AttributeNormalizerPathTest.toSystemPath("/opt/jetty-distro/demo.base/webapps/FOO"); + + System.setProperty("jetty.home", testJettyHome); + System.setProperty("jetty.base", testJettyBase); + + Resource webresource = Resource.newResource(testWar); + AttributeNormalizer normalizer = new AttributeNormalizer(webresource); + String result = null; + + // Normalize as String path + result = normalizer.normalize(testWar); + assertThat(result, is(testWar)); // only URL, File, URI are supported + + URI testWarURI = new File(testWar).toURI(); + + // Normalize as URI + result = normalizer.normalize(testWarURI); + assertThat(result, is("file:${WAR}")); + + // Normalize deep path as File + File testWarDeep = new File(new File(testWar), "deep/ref").getAbsoluteFile(); + result = normalizer.normalize(testWarDeep); + assertThat(result, is("file:${WAR}/deep/ref")); + + // Normalize deep path as String + result = normalizer.normalize(testWarDeep.toString()); + assertThat(result, is(testWarDeep.toString())); + + // Normalize deep path as URI + result = normalizer.normalize(testWarDeep.toURI()); + assertThat(result, is("file:${WAR}/deep/ref")); + } + finally + { + restoreSystemProperty("jetty.home", oldJettyHome); + restoreSystemProperty("jetty.base", oldJettyBase); + } + } + + private void restoreSystemProperty(String key, String value) + { + if (value == null) + { + System.clearProperty(key); + } + else + { + System.setProperty(key, value); + } + } + @Test public void testNormalizeWAR() throws MalformedURLException, IOException { @@ -47,7 +109,7 @@ public class AttributeNormalizerTest result = normalizer.normalize(URI.create(webref + "/deep/ref")); assertThat(result, is("${WAR}/deep/ref")); } - + @Test public void testWindowsTLD() throws MalformedURLException, IOException { @@ -55,16 +117,16 @@ public class AttributeNormalizerTest String webref = "http://localhost/resource/webapps/root"; Resource webresource = Resource.newResource(webref); AttributeNormalizer normalizer = new AttributeNormalizer(webresource); - + // Setup example from windows String javaUserHome = System.getProperty("user.home"); String realUserHome = AttributeNormalizerPathTest.toSystemPath(javaUserHome); String userHome = AttributeNormalizer.uriSeparators(realUserHome); String path = "jar:file:" + userHome + "/.m2/repository/something/somejar.jar!/META-INF/some.tld"; - + String result = normalizer.normalize(path); assertThat(result, is("jar:file:${user.home}/.m2/repository/something/somejar.jar!/META-INF/some.tld")); - + String expanded = normalizer.expand(result); assertThat(expanded, not(anyOf(containsString("\\"), containsString("${")))); }