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.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.List;
import java.util.Stack; import java.util.Stack;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -147,6 +146,19 @@ public class AttributeNormalizer
} }
} }
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) public static String uriSeparators(String path)
{ {
StringBuilder ret = new StringBuilder(); StringBuilder ret = new StringBuilder();
@ -165,7 +177,7 @@ public class AttributeNormalizer
} }
private URI warURI; private URI warURI;
private List<PathAttribute> attributes = new ArrayList<>(); private PathAttributes attributes = new PathAttributes();
public AttributeNormalizer(Resource baseResource) 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("jetty.home", "jetty.home").weight(8));
attributes.add(new PathAttribute("user.home", "user.home").weight(7)); attributes.add(new PathAttribute("user.home", "user.home").weight(7));
attributes.add(new PathAttribute("user.dir", "user.dir").weight(6)); 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()); Collections.sort(attributes, new PathAttributeComparator());

View File

@ -19,20 +19,82 @@
package org.eclipse.jetty.quickstart; package org.eclipse.jetty.quickstart;
import java.io.IOException; 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.anyOf;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat; 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 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 @Test
public void testNormalizeWAR() throws MalformedURLException, IOException public void testNormalizeWAR() throws MalformedURLException, IOException
{ {