Merge remote-tracking branch 'origin/jetty-9.3.x' into jetty-9.4.x

This commit is contained in:
Greg Wilkins 2016-10-28 10:55:03 +11:00
commit eda0cff90a
3 changed files with 343 additions and 156 deletions

View File

@ -33,13 +33,10 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Stack; import java.util.Stack;
import java.util.jar.Attributes;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.resource.Resource; import org.eclipse.jetty.util.resource.Resource;
@ -50,10 +47,16 @@ import org.eclipse.jetty.util.resource.Resource;
* Replaces and expands: * Replaces and expands:
* <ul> * <ul>
* <li>${WAR}</li> * <li>${WAR}</li>
* <li>${WAR.path}</li>
* <li>${WAR.uri}</li>
* <li>${jetty.base}</li> * <li>${jetty.base}</li>
* <li>${jetty.base.uri}</li>
* <li>${jetty.home}</li> * <li>${jetty.home}</li>
* <li>${jetty.home.uri}</li>
* <li>${user.home}</li> * <li>${user.home}</li>
* <li>${user.home.uri}</li>
* <li>${user.dir}</li> * <li>${user.dir}</li>
* <li>${user.dir.uri}</li>
* </ul> * </ul>
*/ */
public class AttributeNormalizer public class AttributeNormalizer
@ -65,20 +68,40 @@ public class AttributeNormalizer
{ {
final String key; final String key;
final String value; final String value;
final int weight;
public Attribute(String key, String value) public Attribute(String key, String value, int weight)
{ {
this.key = key; this.key = key;
this.value = value; this.value = value;
this.weight = weight;
} }
} }
private static URI toCanonicalURI(URI uri)
{
uri = uri.normalize();
String ascii = uri.toASCIIString();
if (ascii.endsWith("/"))
{
try
{
uri = new URI(ascii.substring(0,ascii.length()-1));
}
catch(URISyntaxException e)
{
throw new IllegalArgumentException(e);
}
}
return uri;
}
private static Path toCanonicalPath(String path) private static Path toCanonicalPath(String path)
{ {
if (path == null) if (path == null)
{
return null; return null;
} if (path.length()>1 && path.endsWith("/"))
path = path.substring(0,path.length()-1);
return toCanonicalPath(FileSystems.getDefault().getPath(path)); return toCanonicalPath(FileSystems.getDefault().getPath(path));
} }
@ -106,9 +129,9 @@ public class AttributeNormalizer
{ {
public final Path path; public final Path path;
public PathAttribute(String key, Path path) public PathAttribute(String key, Path path, int weight)
{ {
super(key,path.toString()); super(key,path.toString(),weight);
this.path = path; this.path = path;
} }
@ -123,9 +146,9 @@ public class AttributeNormalizer
{ {
public final URI uri; public final URI uri;
public URIAttribute(String key, URI uri) public URIAttribute(String key, URI uri, int weight)
{ {
super(key,uri.toASCIIString()); super(key,uri.toASCIIString(),weight);
this.uri = uri; this.uri = uri;
} }
@ -136,11 +159,54 @@ public class AttributeNormalizer
} }
} }
private static void addPath(List<PathAttribute>paths,String key) private static Comparator<Attribute> attrComparator = new Comparator<Attribute>()
{
@Override
public int compare(Attribute o1, Attribute o2)
{
if( (o1.value == null) && (o2.value != null) )
{
return -1;
}
if( (o1.value != null) && (o2.value == null) )
{
return 1;
}
if( (o1.value == null) && (o2.value == null) )
{
return 0;
}
// Different lengths?
int diff = o2.value.length() - o1.value.length();
if(diff != 0)
{
return diff;
}
// Different names?
diff = o2.value.compareTo(o1.value);
if(diff != 0)
{
return diff;
}
// The paths are the same, base now on weight
return o2.weight - o1.weight;
}
};
private static void add(List<PathAttribute>paths,List<URIAttribute> uris,String key,int weight)
{ {
String value = System.getProperty(key); String value = System.getProperty(key);
if (value!=null) if (value!=null)
paths.add(new PathAttribute(key,toCanonicalPath(value))); {
Path path = toCanonicalPath(value);
paths.add(new PathAttribute(key,path,weight));
uris.add(new URIAttribute(key+".uri",toCanonicalURI(path.toUri()),weight));
}
} }
private URI warURI; private URI warURI;
@ -148,22 +214,27 @@ public class AttributeNormalizer
private List<PathAttribute> paths = new ArrayList<>(); private List<PathAttribute> paths = new ArrayList<>();
private List<URIAttribute> uris = new ArrayList<>(); private List<URIAttribute> uris = new ArrayList<>();
public AttributeNormalizer(Resource baseResource) public AttributeNormalizer(Resource baseResource)
{ {
if (baseResource==null) if (baseResource==null)
throw new IllegalArgumentException("No base resource!"); throw new IllegalArgumentException("No base resource!");
warURI = baseResource.getURI().normalize(); warURI = toCanonicalURI(baseResource.getURI());
if (!warURI.isAbsolute()) if (!warURI.isAbsolute())
throw new IllegalArgumentException("WAR URI is not absolute: " + warURI); throw new IllegalArgumentException("WAR URI is not absolute: " + warURI);
addPath(paths,"jetty.base"); add(paths,uris,"jetty.base",9);
addPath(paths,"jetty.home"); add(paths,uris,"jetty.home",8);
addPath(paths,"user.home"); add(paths,uris,"user.home",7);
addPath(paths,"user.dir"); add(paths,uris,"user.dir",6);
uris.add(new URIAttribute("WAR", warURI)); if (warURI.getScheme().equalsIgnoreCase("file"))
paths.add(new PathAttribute("WAR.path",toCanonicalPath(new File(warURI).toString()),10));
uris.add(new URIAttribute("WAR.uri", warURI,9)); // preferred encoding
uris.add(new URIAttribute("WAR", warURI,8)); // legacy encoding
Collections.sort(paths,attrComparator);
Collections.sort(uris,attrComparator);
Stream.concat(paths.stream(),uris.stream()).forEach(a->attributes.put(a.key,a)); Stream.concat(paths.stream(),uris.stream()).forEach(a->attributes.put(a.key,a));
@ -188,12 +259,17 @@ public class AttributeNormalizer
{ {
// Find a URI // Find a URI
URI uri = null; URI uri = null;
Path path = null;
if (o instanceof URI) if (o instanceof URI)
uri = ((URI)o).normalize(); uri = toCanonicalURI(((URI)o));
else if (o instanceof Resource)
uri = toCanonicalURI(((Resource)o).getURI());
else if (o instanceof URL) else if (o instanceof URL)
uri = ((URL)o).toURI().normalize(); uri = toCanonicalURI(((URL)o).toURI());
else if (o instanceof File) else if (o instanceof File)
uri = ((File)o).toURI().normalize(); path = ((File)o).getAbsoluteFile().getCanonicalFile().toPath();
else if (o instanceof Path)
path = (Path)o;
else else
{ {
String s = o.toString(); String s = o.toString();
@ -216,6 +292,8 @@ public class AttributeNormalizer
} }
} }
if (uri!=null)
{
if ("jar".equalsIgnoreCase(uri.getScheme())) if ("jar".equalsIgnoreCase(uri.getScheme()))
{ {
String raw = uri.getRawSchemeSpecificPart(); String raw = uri.getRawSchemeSpecificPart();
@ -232,6 +310,9 @@ public class AttributeNormalizer
} }
} }
} }
else if (path!=null)
return normalizePath(path);
}
catch (Exception e) catch (Exception e)
{ {
LOG.warn(e); LOG.warn(e);
@ -239,7 +320,7 @@ public class AttributeNormalizer
return String.valueOf(o); return String.valueOf(o);
} }
public String normalizeUri(URI uri) protected String normalizeUri(URI uri)
{ {
for (URIAttribute a : uris) for (URIAttribute a : uris)
{ {
@ -276,19 +357,22 @@ public class AttributeNormalizer
return uri.toASCIIString(); return uri.toASCIIString();
} }
public String normalizePath(Path path) protected String normalizePath(Path path)
{ {
for (PathAttribute a : paths) for (PathAttribute a : paths)
{ {
try try
{ {
if (path.startsWith(a.path) || path.equals(a.path) || Files.isSameFile(path,a.path)) if (path.equals(a.path) || Files.isSameFile(path,a.path))
return String.format("${%s}%s",a.key,a.path.relativize(path).toString()); return String.format("${%s}",a.key);
} }
catch (IOException ignore) catch (IOException ignore)
{ {
LOG.ignore(ignore); LOG.ignore(ignore);
} }
if (path.startsWith(a.path))
return String.format("${%s}/%s",a.key,a.path.relativize(path).toString());
} }
return path.toString(); return path.toString();

View File

@ -18,138 +18,241 @@
package org.eclipse.jetty.quickstart; package org.eclipse.jetty.quickstart;
import java.io.File;
import java.io.IOException;
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;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.jetty.toolchain.test.FS;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.toolchain.test.OS;
import org.eclipse.jetty.util.resource.Resource;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@RunWith(Parameterized.class)
public class AttributeNormalizerTest public class AttributeNormalizerTest
{ {
@Test @Parameterized.Parameters(name = "[{index}] {0} - {1}")
public void testNormalizeOrder() throws IOException public static List<Object[]> data()
{ {
String oldJettyHome = System.getProperty("jetty.home"); List<Object[]> data = new ArrayList<>();
String oldJettyBase = System.getProperty("jetty.base");
try String arch = String.format("%s/%s", System.getProperty("os.name"), System.getProperty("os.arch"));
{
String testJettyHome = EnvUtils.toSystemPath("/opt/jetty-distro");
String testJettyBase = EnvUtils.toSystemPath("/opt/jetty-distro/demo.base");
String testWar = EnvUtils.toSystemPath("/opt/jetty-distro/demo.base/webapps/FOO");
System.setProperty("jetty.home", testJettyHome); String title;
System.setProperty("jetty.base", testJettyBase); Map<String, String> env;
Resource webresource = Resource.newResource(testWar); // ------
AttributeNormalizer normalizer = new AttributeNormalizer(webresource); title = "Typical Setup";
String result = null;
// Normalize as String path env = new HashMap<>();
result = normalizer.normalize(testWar); env.put("jetty.home", asTargetPath(title,"jetty-distro"));
assertThat(result, is(testWar)); // only URL, File, URI are supported env.put("jetty.base", asTargetPath(title,"jetty-distro/demo.base"));
env.put("WAR", asTargetPath(title,"jetty-distro/demo.base/webapps/FOO"));
// Normalize as URI data.add(new Object[]{arch, title, env});
URI testWarURI = new File(testWar).toURI();
result = normalizer.normalize(testWarURI);
assertThat(result, is("${WAR}"));
// Normalize deep path as File // ------
File testWarDeep = new File(new File(testWar), "deep/ref").getAbsoluteFile(); // This puts the jetty.home inside of the jetty.base
result = normalizer.normalize(testWarDeep); title = "Overlap Setup";
assertThat(result, is("${WAR}/deep/ref")); env = new HashMap<>();
env.put("jetty.home", asTargetPath(title,"app/dist"));
env.put("jetty.base", asTargetPath(title,"app"));
env.put("WAR", asTargetPath(title,"app/webapps/FOO"));
// Normalize deep path as String data.add(new Object[]{arch, title, env});
result = normalizer.normalize(testWarDeep.toString());
assertThat(result, is(testWarDeep.toString()));
// Normalize deep path as URI // ------
result = normalizer.normalize(testWarDeep.toURI()); // This tests a path scenario often seen on various automatic deployments tooling
assertThat(result, is("${WAR}/deep/ref")); // such as Kubernetes, CircleCI, TravisCI, and Jenkins.
title = "Nasty Path Setup";
env = new HashMap<>();
env.put("jetty.home", asTargetPath(title,"app%2Fnasty/dist"));
env.put("jetty.base", asTargetPath(title,"app%2Fnasty/base"));
env.put("WAR", asTargetPath(title,"app%2Fnasty/base/webapps/FOO"));
} data.add(new Object[]{arch, title, env});
finally return data;
{
EnvUtils.restoreSystemProperty("jetty.home", oldJettyHome);
EnvUtils.restoreSystemProperty("jetty.base", oldJettyBase);
}
} }
private static final String asTargetPath(String title, String subpath)
{
Path rootPath = MavenTestingUtils.getTargetTestingPath(title);
FS.ensureDirExists(rootPath);
Path path = rootPath.resolve(OS.separators(subpath));
FS.ensureDirExists(path);
return path.toString();
}
private Map<String, String> oldValues = new HashMap<>();
private final String jettyHome;
private final String jettyBase;
private final String war;
private final String arch;
private final String title;
private final Map<String, String> env;
private final AttributeNormalizer normalizer;
public AttributeNormalizerTest(String arch, String title, Map<String, String> env) throws IOException
{
this.arch = arch;
this.title = title;
this.env = env;
// Remember old values
env.keySet().stream().forEach((key) ->
{
String old = System.getProperty(key);
oldValues.put(key, old);
});
// Grab specific values of interest in general
jettyHome = env.get("jetty.home");
jettyBase = env.get("jetty.base");
war = env.get("WAR");
// Set environment (skipping null and WAR)
env.entrySet().stream()
.filter((e) -> e.getValue() != null && !e.getKey().equalsIgnoreCase("WAR"))
.forEach((entry) -> System.setProperty(entry.getKey(), entry.getValue()));
// Setup normalizer
Resource webresource = Resource.newResource(war);
this.normalizer = new AttributeNormalizer(webresource);
}
@After
public void restoreEnv()
{
// Restore old values
oldValues.entrySet().stream().forEach((entry) ->
EnvUtils.restoreSystemProperty(entry.getKey(), entry.getValue())
);
}
private void assertNormalize(Object o, String expected)
{
String result = normalizer.normalize(o);
assertThat("normalize((" + o.getClass().getSimpleName() + ") " + o.toString() + ")",
result, is(expected));
}
private void assertExpandPath(String line, String expected)
{
String result = normalizer.expand(line);
// Treat output as strings
assertThat("expand('" + line + "')", result, is(expected));
}
private void assertExpandURI(String line, URI expected)
{
String result = normalizer.expand(line);
URI resultURI = URI.create(result);
assertThat("expand('" + line + "')", resultURI.getScheme(), is(expected.getScheme()));
assertThat("expand('" + line + "')", resultURI.getPath(), is(expected.getPath()));
}
@Test @Test
public void testNormalizeURIs() throws Exception public void testNormalizeWarAsString()
{ {
String testWar = EnvUtils.toSystemPath("/opt/jetty-distro/demo.base/webapps/FOO"); // Normalize WAR as String path
assertNormalize(war, war); // only URL, File, URI are supported
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
// Normalize as URI
URI testWarURI = new File(testWar).toURI();
result = normalizer.normalize(testWarURI);
assertThat(result, is("${WAR}"));
// Normalize as URI
URI subURI = new File(testWar,"WEB-INF/web.xml").toURI();
result = normalizer.normalize(subURI);
assertThat(result, is("${WAR}/WEB-INF/web.xml"));
// Normalize fake prefix
URI fakeURI = Resource.newResource(EnvUtils.toSystemPath("/opt/jetty-distro/demo.base/webapps/FOOBAR")).getURI();
result = normalizer.normalize(fakeURI);
assertThat(result, is(fakeURI.toASCIIString()));
// Normalize as no host
result = normalizer.normalize("file:"+testWarURI.getRawPath());
assertThat(result, is("${WAR}"));
// Normalize as null host
result = normalizer.normalize("file://"+testWarURI.getRawPath());
assertThat(result, is("${WAR}"));
// Normalize jar file
result = normalizer.normalize("jar:file:"+testWarURI.getRawPath()+"!/some/path");
assertThat(result, is("jar:${WAR}!/some/path"));
} }
@Test
public void testNormalizeJettyBaseAsFile()
{
// Normalize jetty.base as File path
assertNormalize(new File(jettyBase), "${jetty.base}");
}
@Test @Test
public void testNormalizeExpandWAR() throws IOException public void testNormalizeJettyHomeAsFile()
{ {
String webref = MavenTestingUtils.getTargetDir().getAbsolutePath() + "/bogus.war"; // Normalize jetty.home as File path
Resource webresource = Resource.newResource(webref); assertNormalize(new File(jettyHome), "${jetty.home}");
AttributeNormalizer normalizer = new AttributeNormalizer(webresource); }
String result = null;
File webrefFile = new File(webref); @Test
URI uri = webrefFile.toURI(); public void testNormalizeJettyBaseAsURI()
// As normal URI ref {
result = normalizer.normalize(uri); // Normalize jetty.base as URI path
assertThat("normalize(" + uri + ")", result, is("${WAR}")); assertNormalize(new File(jettyBase).toURI(), "${jetty.base.uri}");
}
// as jar internal resource reference @Test
uri = URI.create("jar:" + webrefFile.toURI().toASCIIString() + "!/deep/ref"); public void testNormalizeJettyHomeAsURI()
result = normalizer.normalize(uri); {
assertThat("normalize(" + uri + ")", result, is("jar:${WAR}!/deep/ref")); // Normalize jetty.home as URI path
assertNormalize(new File(jettyHome).toURI(), "${jetty.home.uri}");
}
// as jar internal resource reference @Test
String line = "jar:${WAR}!/other/file"; public void testExpandJettyBase()
result = normalizer.expand(line); {
uri = URI.create("jar:" + webrefFile.toPath().toUri().toASCIIString() + "!/other/file"); // Expand jetty.base
assertThat("expand(\"" + line + "\")", URI.create(result), is(uri)); assertExpandPath("${jetty.base}", jettyBase);
}
@Test
public void testExpandJettyHome()
{
// Expand jetty.home
assertExpandPath("${jetty.home}", jettyHome);
}
@Test
public void testNormalizeWarAsURI()
{
// Normalize WAR as URI
URI testWarURI = new File(war).toURI();
assertNormalize(testWarURI, "${WAR.uri}");
}
@Test
public void testNormalizeWarDeepAsFile()
{
// Normalize WAR deep path as File
File testWarDeep = new File(new File(war), OS.separators("deep/ref")).getAbsoluteFile();
assertNormalize(testWarDeep, "${WAR.path}/deep/ref");
}
@Test
public void testNormalizeWarDeepAsString()
{
// Normalize WAR deep path as String
File testWarDeep = new File(new File(war), OS.separators("deep/ref")).getAbsoluteFile();
assertNormalize(testWarDeep.toString(), testWarDeep.toString());
}
@Test
public void testNormalizeWarDeepAsURI()
{
// Normalize WAR deep path as URI
File testWarDeep = new File(new File(war), OS.separators("deep/ref")).getAbsoluteFile();
assertNormalize(testWarDeep.toURI(), "${WAR.uri}/deep/ref");
}
@Test
public void testExpandWarDeep()
{
// Expand WAR deep path
File testWarDeep = new File(new File(war), OS.separators("deep/ref"));
URI uri = URI.create("jar:" + testWarDeep.toURI().toASCIIString() + "!/other/file");
assertExpandURI("jar:${WAR.uri}/deep/ref!/other/file", uri);
} }
} }