Merge branch 'jetty-9.3.x' into jetty-9.4.x

This commit is contained in:
Joakim Erdfelt 2016-10-26 11:42:49 -07:00
commit a7ac269027
2 changed files with 91 additions and 13 deletions

View File

@ -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<AttributeNormalizer.PathAttribute>
{
@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<PathAttribute> 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());

View File

@ -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("${"))));
}