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.Map;
import java.util.Stack;
import java.util.jar.Attributes;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.resource.Resource;
@ -50,10 +47,16 @@ import org.eclipse.jetty.util.resource.Resource;
* Replaces and expands:
* <ul>
* <li>${WAR}</li>
* <li>${WAR.path}</li>
* <li>${WAR.uri}</li>
* <li>${jetty.base}</li>
* <li>${jetty.base.uri}</li>
* <li>${jetty.home}</li>
* <li>${jetty.home.uri}</li>
* <li>${user.home}</li>
* <li>${user.home.uri}</li>
* <li>${user.dir}</li>
* <li>${user.dir.uri}</li>
* </ul>
*/
public class AttributeNormalizer
@ -65,20 +68,40 @@ public class AttributeNormalizer
{
final String key;
final String value;
final int weight;
public Attribute(String key, String value)
public Attribute(String key, String value, int weight)
{
this.key = key;
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)
{
if (path == null)
{
return null;
}
if (path.length()>1 && path.endsWith("/"))
path = path.substring(0,path.length()-1);
return toCanonicalPath(FileSystems.getDefault().getPath(path));
}
@ -106,9 +129,9 @@ public class AttributeNormalizer
{
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;
}
@ -123,9 +146,9 @@ public class AttributeNormalizer
{
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;
}
@ -136,37 +159,85 @@ 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);
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 Map<String,Attribute> attributes = new HashMap<>();
private List<PathAttribute> paths = new ArrayList<>();
private List<URIAttribute> uris = new ArrayList<>();
public AttributeNormalizer(Resource baseResource)
{
if (baseResource==null)
throw new IllegalArgumentException("No base resource!");
warURI = baseResource.getURI().normalize();
warURI = toCanonicalURI(baseResource.getURI());
if (!warURI.isAbsolute())
throw new IllegalArgumentException("WAR URI is not absolute: " + warURI);
addPath(paths,"jetty.base");
addPath(paths,"jetty.home");
addPath(paths,"user.home");
addPath(paths,"user.dir");
add(paths,uris,"jetty.base",9);
add(paths,uris,"jetty.home",8);
add(paths,uris,"user.home",7);
add(paths,uris,"user.dir",6);
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
uris.add(new URIAttribute("WAR", warURI));
Collections.sort(paths,attrComparator);
Collections.sort(uris,attrComparator);
Stream.concat(paths.stream(),uris.stream()).forEach(a->attributes.put(a.key,a));
if (LOG.isDebugEnabled())
{
for (Attribute attr : attributes.values())
@ -188,12 +259,17 @@ public class AttributeNormalizer
{
// Find a URI
URI uri = null;
Path path = null;
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)
uri = ((URL)o).toURI().normalize();
uri = toCanonicalURI(((URL)o).toURI());
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
{
String s = o.toString();
@ -216,21 +292,26 @@ public class AttributeNormalizer
}
}
if ("jar".equalsIgnoreCase(uri.getScheme()))
if (uri!=null)
{
String raw = uri.getRawSchemeSpecificPart();
int bang = raw.indexOf("!/");
String normal = normalize(raw.substring(0,bang));
String suffix = raw.substring(bang);
return "jar:" + normal + suffix;
}
else
{
if(uri.isAbsolute())
if ("jar".equalsIgnoreCase(uri.getScheme()))
{
return normalizeUri(uri);
String raw = uri.getRawSchemeSpecificPart();
int bang = raw.indexOf("!/");
String normal = normalize(raw.substring(0,bang));
String suffix = raw.substring(bang);
return "jar:" + normal + suffix;
}
else
{
if(uri.isAbsolute())
{
return normalizeUri(uri);
}
}
}
else if (path!=null)
return normalizePath(path);
}
catch (Exception e)
{
@ -239,7 +320,7 @@ public class AttributeNormalizer
return String.valueOf(o);
}
public String normalizeUri(URI uri)
protected String normalizeUri(URI uri)
{
for (URIAttribute a : uris)
{
@ -276,19 +357,22 @@ public class AttributeNormalizer
return uri.toASCIIString();
}
public String normalizePath(Path path)
protected String normalizePath(Path path)
{
for (PathAttribute a : paths)
{
try
{
if (path.startsWith(a.path) || path.equals(a.path) || Files.isSameFile(path,a.path))
return String.format("${%s}%s",a.key,a.path.relativize(path).toString());
if (path.equals(a.path) || Files.isSameFile(path,a.path))
return String.format("${%s}",a.key);
}
catch (IOException ignore)
{
LOG.ignore(ignore);
}
if (path.startsWith(a.path))
return String.format("${%s}/%s",a.key,a.path.relativize(path).toString());
}
return path.toString();

View File

@ -555,7 +555,7 @@ public class QuickStartDescriptorGenerator
Object o = _webApp.getAttribute(attribute);
if (o == null)
return;
Collection<?> c = (o instanceof Collection)? (Collection<?>)o:Collections.singletonList(o);
StringBuilder v=new StringBuilder();
for (Object i:c)

View File

@ -18,138 +18,241 @@
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.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
{
@Test
public void testNormalizeOrder() throws IOException
@Parameterized.Parameters(name = "[{index}] {0} - {1}")
public static List<Object[]> data()
{
String oldJettyHome = System.getProperty("jetty.home");
String oldJettyBase = System.getProperty("jetty.base");
List<Object[]> data = new ArrayList<>();
String arch = String.format("%s/%s", System.getProperty("os.name"), System.getProperty("os.arch"));
try
{
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);
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
// Normalize as URI
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();
result = normalizer.normalize(testWarDeep);
assertThat(result, is("${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("${WAR}/deep/ref"));
}
finally
{
EnvUtils.restoreSystemProperty("jetty.home", oldJettyHome);
EnvUtils.restoreSystemProperty("jetty.base", oldJettyBase);
}
String title;
Map<String, String> env;
// ------
title = "Typical Setup";
env = new HashMap<>();
env.put("jetty.home", asTargetPath(title,"jetty-distro"));
env.put("jetty.base", asTargetPath(title,"jetty-distro/demo.base"));
env.put("WAR", asTargetPath(title,"jetty-distro/demo.base/webapps/FOO"));
data.add(new Object[]{arch, title, env});
// ------
// This puts the jetty.home inside of the jetty.base
title = "Overlap Setup";
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"));
data.add(new Object[]{arch, title, env});
// ------
// This tests a path scenario often seen on various automatic deployments tooling
// 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});
return data;
}
@Test
public void testNormalizeURIs() throws Exception
private static final String asTargetPath(String title, String subpath)
{
String testWar = EnvUtils.toSystemPath("/opt/jetty-distro/demo.base/webapps/FOO");
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"));
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
public void testNormalizeExpandWAR() throws IOException
public void testNormalizeWarAsString()
{
String webref = MavenTestingUtils.getTargetDir().getAbsolutePath() + "/bogus.war";
Resource webresource = Resource.newResource(webref);
AttributeNormalizer normalizer = new AttributeNormalizer(webresource);
String result = null;
File webrefFile = new File(webref);
URI uri = webrefFile.toURI();
// As normal URI ref
result = normalizer.normalize(uri);
assertThat("normalize(" + uri + ")", result, is("${WAR}"));
// 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));
// Normalize WAR as String path
assertNormalize(war, war); // only URL, File, URI are supported
}
@Test
public void testNormalizeJettyBaseAsFile()
{
// Normalize jetty.base as File path
assertNormalize(new File(jettyBase), "${jetty.base}");
}
@Test
public void testNormalizeJettyHomeAsFile()
{
// Normalize jetty.home as File path
assertNormalize(new File(jettyHome), "${jetty.home}");
}
@Test
public void testNormalizeJettyBaseAsURI()
{
// Normalize jetty.base as URI path
assertNormalize(new File(jettyBase).toURI(), "${jetty.base.uri}");
}
@Test
public void testNormalizeJettyHomeAsURI()
{
// Normalize jetty.home as URI path
assertNormalize(new File(jettyHome).toURI(), "${jetty.home.uri}");
}
@Test
public void testExpandJettyBase()
{
// Expand jetty.base
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);
}
}