Skip to content

Commit

Permalink
Issue #10926 - add support for CombinedResource in AttributeNormalizer (
Browse files Browse the repository at this point in the history
#10927)

* #10926 - skip existence check if only one Normalizer
* fix for AttributeNormalizer

---------

Signed-off-by: Lachlan Roberts <[email protected]>
Co-authored-by: gregw <[email protected]>
  • Loading branch information
lachlan-roberts and gregw authored Nov 30, 2023
1 parent 02c0782 commit 876796d
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.stream.Stream;

import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.URIUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -195,34 +196,39 @@ public String toString()
return o2.weight - o1.weight;
};

private final Resource baseResource;
private final List<PathAttribute> paths = new ArrayList<>();
private final List<URIAttribute> uris = new ArrayList<>();

public AttributeNormalizer(Resource baseResource)
{
if (baseResource == null)
throw new IllegalArgumentException("No base resource!");
// Combined resources currently are not supported (see #8921) but such support
// could be added. That would require some form of (potentially expensive)
// existence check in expand() to figure out which URIAttribute actually
// contains the resource.
if (Resources.isCombined(baseResource))
throw new IllegalArgumentException("Base resource cannot be combined!");

this.baseResource = baseResource;

addSystemProperty("jetty.base", 9);
addSystemProperty("jetty.home", 8);
addSystemProperty("user.home", 7);
addSystemProperty("user.dir", 6);

URI warURI = toCanonicalURI(baseResource.getURI());
if (!warURI.isAbsolute())
throw new IllegalArgumentException("WAR URI is not absolute: " + warURI);

Path path = baseResource.getPath();
if (path != null)
paths.add(new PathAttribute("WAR.path", toCanonicalPath(path), 10));
uris.add(new URIAttribute("WAR.uri", warURI, 9)); // preferred encoding
uris.add(new URIAttribute("WAR", warURI, 8)); // legacy encoding
int weight = 9;
for (Resource base : baseResource)
{
URI uri = base.getURI();
if (uri == null)
continue;
URI warURI = toCanonicalURI(uri);
if (!warURI.isAbsolute())
throw new IllegalArgumentException("WAR URI is not absolute: " + warURI);

Path path = base.getPath();
if (path != null)
paths.add(new PathAttribute("WAR.path", toCanonicalPath(path), weight));
uris.add(new URIAttribute("WAR.uri", warURI, weight - 1)); // preferred encoding
uris.add(new URIAttribute("WAR", warURI, weight - 2)); // legacy encoding
weight += 3;
}

paths.sort(attrComparator);
uris.sort(attrComparator);
Expand Down Expand Up @@ -401,6 +407,24 @@ private String expand(String prefix, String property, String suffix)
if (property == null)
return null;

switch (property)
{
case "WAR", "WAR.path" ->
{
Resource r = baseResource.resolve(suffix);
if (r == null)
return prefix + URIUtil.addPaths(baseResource.iterator().next().getPath().toString(), suffix);
return prefix + r.getPath();
}
case "WAR.uri" ->
{
Resource r = baseResource.resolve(suffix);
if (r == null)
return prefix + URIUtil.addPaths(baseResource.iterator().next().getURI().toString(), suffix);
return prefix + r.getURI();
}
}

// Check for URI matches
for (URIAttribute attr : uris)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -101,6 +103,11 @@ private static Path asTargetPath(String title, String subpath)
return path;
}

private static Resource asTargetResource(String title, String subpath)
{
return resourceFactory.newResource(asTargetPath(title, subpath));
}

private static final Map<String, String> originalEnv = new HashMap<>();
private static ResourceFactory.Closeable resourceFactory;

Expand Down Expand Up @@ -308,6 +315,35 @@ public void testNormalizeWarDeepAsURI(final Scenario scenario)
assertNormalize(scenario, testWarDeep.toUri(), "${WAR.uri}/deep/ref");
}

@Test
public void testCombinedResource() throws Exception
{
String title = "CombinedResource Setup";
Resource r1 = asTargetResource(title, "dir1");
Resource r2 = asTargetResource(title, "dir2");
Resource r3 = asTargetResource(title, "dir3");

// Create files in each of these directories.
Files.createFile(r1.getPath().resolve("file1")).toFile().deleteOnExit();
Files.createFile(r2.getPath().resolve("file2")).toFile().deleteOnExit();
Files.createFile(r3.getPath().resolve("file3")).toFile().deleteOnExit();

Resource combined = CombinedResource.combine(List.of(r1, r2, r3));
AttributeNormalizer normalizer = new AttributeNormalizer(combined);

// Uses the appropriate resource if the target exists.
assertThat(normalizer.expand("${WAR.uri}/file1"), containsString("/dir1/file1"));
assertThat(normalizer.expand("${WAR.uri}/file2"), containsString("/dir2/file2"));
assertThat(normalizer.expand("${WAR.uri}/file3"), containsString("/dir3/file3"));
assertThat(normalizer.expand("${WAR.path}/file1"), containsString("/dir1/file1"));
assertThat(normalizer.expand("${WAR.path}/file2"), containsString("/dir2/file2"));
assertThat(normalizer.expand("${WAR.path}/file3"), containsString("/dir3/file3"));

// If file cannot be found it just uses the first resource.
assertThat(normalizer.expand("${WAR.uri}/file4"), containsString("/dir1/file4"));
assertThat(normalizer.expand("${WAR.path}/file4"), containsString("/dir1/file4"));
}

public static class Scenario
{
private final Path jettyHome;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public void visitContextParam(WebAppContext context, Descriptor descriptor, XmlP
if (o instanceof Collection<?>)
libs.addAll((Collection<?>)o);
libs.addAll(values);
if (libs.size() > 0)
if (!libs.isEmpty())
context.setAttribute(ServletContext.ORDERED_LIBS, libs);
}
case AnnotationConfiguration.CONTAINER_INITIALIZERS ->
Expand Down

0 comments on commit 876796d

Please sign in to comment.