Issue #1038 - treating ${WAR} as a URI always, not a Path

+ Still needs testing on MS Windows
This commit is contained in:
Joakim Erdfelt 2016-10-26 15:30:34 -07:00
parent ebc8983721
commit ca5e075ba0
3 changed files with 69 additions and 28 deletions

View File

@ -53,20 +53,21 @@ public class AttributeNormalizer
{
private static final Logger LOG = Log.getLogger(AttributeNormalizer.class);
private static final Pattern __propertyPattern = Pattern.compile("(?<=[^$]|^)\\$\\{([^}]*)\\}");
private static class PathAttribute
{
public final Path path;
public final String key;
private boolean isUriBased = false;
private int weight = -1;
public PathAttribute(String key, Path path) throws IOException
{
this.key = key;
this.path = toCanonicalPath(path);
// TODO: Don't allow non-directory paths? (but what if the path doesn't exist?)
}
public PathAttribute(String key, String systemPropertyKey) throws IOException
{
this(key, toCanonicalPath(System.getProperty(systemPropertyKey)));
@ -93,7 +94,41 @@ public class AttributeNormalizer
}
return path.toAbsolutePath();
}
public String toUri()
{
if (isUriBased)
{
// Return "{KEY}" -> "<uri>" (including scheme)
return path.toUri().toASCIIString();
}
else
{
// Return "{KEY}" -> "<path>" (excluding scheme)
return path.toUri().getSchemeSpecificPart();
}
}
public String getNormalizedScheme()
{
if (isUriBased)
{
// If we are treating the {KEY} -> "<uri>" (scheme is expanded)
return "";
}
else
{
// If we are treating the {KEY} -> "<path>" (scheme is not part of KEY)
return "file:";
}
}
public PathAttribute treatAsUri()
{
this.isUriBased = true;
return this;
}
public PathAttribute weight(int newweight)
{
this.weight = newweight;
@ -195,7 +230,7 @@ public class AttributeNormalizer
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));
attributes.add(new PathAttribute("WAR", new File(warURI).toPath().toAbsolutePath()).treatAsUri().weight(10));
}
Collections.sort(attributes, new PathAttributeComparator());
@ -245,7 +280,7 @@ public class AttributeNormalizer
}
else if ("file".equalsIgnoreCase(uri.getScheme()))
{
return "file:" + normalizePath(new File(uri.getRawSchemeSpecificPart()).toPath());
return normalizePath(new File(uri.getRawSchemeSpecificPart()).toPath());
}
else
{
@ -284,7 +319,7 @@ public class AttributeNormalizer
{
if (path.startsWith(attr.path) || path.equals(attr.path) || Files.isSameFile(path,attr.path))
{
return uriSeparators(URIUtil.addPaths("${" + attr.key + "}",attr.path.relativize(path).toString()));
return attr.getNormalizedScheme() + uriSeparators(URIUtil.addPaths("${" + attr.key + "}", attr.path.relativize(path).toString()));
}
}
catch (IOException ignore)
@ -385,15 +420,7 @@ public class AttributeNormalizer
{
if (attr.key.equalsIgnoreCase(property))
{
String path = uriSeparators(attr.path.toString());
if (path.endsWith("/"))
{
return path.substring(0, path.length() - 1);
}
else
{
return path;
}
return attr.toUri();
}
}

View File

@ -21,6 +21,7 @@ package org.eclipse.jetty.quickstart;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
@ -46,7 +47,6 @@ public class AttributeNormalizerPathTest
public static List<String[]> data()
{
String[][] tests = {
{ "WAR", testRoot.toString() },
{ "jetty.home", EnvUtils.toSystemPath("/opt/jetty-distro") },
{ "jetty.base", EnvUtils.toSystemPath("/opt/jetty-distro/demo.base") },
{ "user.home", EnvUtils.toSystemPath("/home/user") },
@ -117,7 +117,8 @@ public class AttributeNormalizerPathTest
@Test
public void testExpandEqual()
{
assertExpand("file:${" + key + "}", "file:" + path);
String expectedPath = new File(path).toPath().toUri().getRawSchemeSpecificPart();
assertExpand("file:${" + key + "}", "file:" + expectedPath);
}
@Test
@ -189,7 +190,8 @@ public class AttributeNormalizerPathTest
@Test
public void testExpandJarFileEquals_FileBangFile()
{
assertExpand("jar:file:${" + key + "}/file!/file", "jar:file:" + path + "/file!/file");
String expectedPath = new File(path).toPath().toUri().getRawSchemeSpecificPart();
assertExpand("jar:file:${" + key + "}/file!/file", "jar:file:" + expectedPath + "/file!/file");
}
@Test

View File

@ -28,6 +28,7 @@ import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.util.resource.Resource;
import org.junit.Test;
@ -60,12 +61,12 @@ public class AttributeNormalizerTest
// Normalize as URI
result = normalizer.normalize(testWarURI);
assertThat(result, is("file:${WAR}"));
assertThat(result, is("${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"));
assertThat(result, is("${WAR}/deep/ref"));
// Normalize deep path as String
result = normalizer.normalize(testWarDeep.toString());
@ -73,7 +74,7 @@ public class AttributeNormalizerTest
// Normalize deep path as URI
result = normalizer.normalize(testWarDeep.toURI());
assertThat(result, is("file:${WAR}/deep/ref"));
assertThat(result, is("${WAR}/deep/ref"));
}
finally
{
@ -83,18 +84,29 @@ public class AttributeNormalizerTest
}
@Test
public void testNormalizeWAR() throws MalformedURLException
public void testNormalizeExpandWAR() throws MalformedURLException
{
String webref = "http://localhost/resource/webapps/root";
String webref = MavenTestingUtils.getTargetDir().getAbsolutePath() + "/bogus.war";
Resource webresource = Resource.newResource(webref);
AttributeNormalizer normalizer = new AttributeNormalizer(webresource);
String result = null;
result = normalizer.normalize(URI.create(webref));
assertThat(result, is("${WAR}"));
File webrefFile = new File(webref);
URI uri = webrefFile.toURI();
// As normal URI ref
result = normalizer.normalize(uri);
assertThat("normalize(" + uri + ")", result, is("${WAR}"));
result = normalizer.normalize(URI.create(webref + "/deep/ref"));
assertThat(result, is("${WAR}/deep/ref"));
// as jar internal resource reference
uri = URI.create("jar:" + webrefFile.toURI().toASCIIString() + "!/deep/ref");
result = normalizer.normalize(uri);
assertThat("normalize(" + uri + ")", result, is("jar:${WAR}!/deep/ref"));
// as jar internal resource reference
String line = "jar:${WAR}!/other/file";
result = normalizer.expand(line);
uri = URI.create("jar:" + webrefFile.toPath().toUri().toASCIIString() + "!/other/file");
assertThat("expand(\"" + line + "\")", URI.create(result), is(uri));
}
@Test