Skip to content

Commit

Permalink
Fix servlet file listing
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas committed Feb 15, 2023
1 parent 3216065 commit 37c0216
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.quarkus.undertow.test;

import static org.hamcrest.Matchers.is;

import java.io.IOException;
import java.util.TreeSet;
import java.util.stream.Collectors;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;

public class ResourceManagerTestCase {

private static final String CONTEXT_PATH = "/foo";
public static final String META_INF_RESOURCES = "META-INF/resources/";

@RegisterExtension
static QuarkusUnitTest runner = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(ContextPathServlet.class)
.addAsResource(new StringAsset("index.html"), "META-INF/resources/index.html")
.addAsResource(new StringAsset("foo/foo.html"), "META-INF/resources/foo/foo.html")
.addAsResource(new StringAsset("foo/bar/bar.html"), "META-INF/resources/foo/bar/bar.html"));

@Test
public void testServlet() {
RestAssured.when().get("/").then()
.statusCode(200)
.body(is("[foo, index.html]"));
RestAssured.when().get("/foo").then()
.statusCode(200)
.body(is("[foo/bar, foo/foo.html]"));
RestAssured.when().get("/foo/bar").then()
.statusCode(200)
.body(is("[foo/bar/bar.html]"));
}

@WebServlet(urlPatterns = "/*")
public static class ContextPathServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
var paths = req.getServletContext().getResourcePaths(req.getPathInfo() == null ? "/" : req.getPathInfo());
resp.getWriter().write(String.valueOf(new TreeSet<>(
paths.stream().map(s -> s.substring(s.lastIndexOf(META_INF_RESOURCES) + META_INF_RESOURCES.length()))
.collect(Collectors.toSet()))));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
import java.net.URL;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.NavigableSet;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import io.undertow.httpcore.OutputChannel;
Expand All @@ -24,7 +24,7 @@
public class KnownPathResourceManager implements ResourceManager {

private final NavigableSet<String> files;
private final Set<String> directories;
private final NavigableSet<String> directories;
private final ResourceManager underlying;

public KnownPathResourceManager(Set<String> files, Set<String> directories, ResourceManager underlying) {
Expand All @@ -51,7 +51,7 @@ public KnownPathResourceManager(Set<String> files, Set<String> directories, Reso
tmp.add(i);
}
tmp.add("");
this.directories = Collections.unmodifiableSet(tmp);
this.directories = new TreeSet<>(tmp);
}

@Override
Expand Down Expand Up @@ -118,18 +118,36 @@ public boolean isDirectory() {
@Override
public List<Resource> list() {
List<Resource> ret = new ArrayList<>();
String slashPath = path + "/";
for (String i : files.headSet(path)) {
if (i.startsWith(slashPath)) {
try {
ret.add(underlying.getResource(i));
} catch (IOException e) {
throw new RuntimeException(e);
String slashPath = path.isEmpty() ? path : path + "/";
SortedSet<String> fileSet;
SortedSet<String> dirSet;

fileSet = files.tailSet(slashPath);
dirSet = directories.tailSet(slashPath);
for (var s : List.of(fileSet, dirSet)) {
for (String i : s) {
if (i.equals(slashPath)) {
continue;
}
if (i.startsWith(slashPath)) {
if (!i.substring(slashPath.length()).contains("/")) {
try {
Resource resource = underlying.getResource(i);
if (resource == null) {
throw new RuntimeException("Unable to get listed resource " + i + " from directory " + path
+ " for path " + slashPath + " from underlying manager " + underlying);
}
ret.add(resource);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
} else {
break;
}
} else {
break;
}
}

return ret;
}

Expand Down

0 comments on commit 37c0216

Please sign in to comment.