From 24b3fa7059051f03562e3fe23d4d2518659d3475 Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Thu, 4 Apr 2024 14:55:02 +0300 Subject: [PATCH] Ensure that index.html works in any directory in native mode --- .../deployment/StaticResourcesProcessor.java | 29 ++++++++++++- .../META-INF/resources/dummy/index.html | 7 ++++ .../META-INF/resources/dummy2/test.html | 0 .../META-INF/resources/l1/l2/index.html | 7 ++++ .../resources/META-INF/resources/test.txt | 1 + .../quarkus/it/vertx/StaticResourcesIT.java | 7 ++++ .../quarkus/it/vertx/StaticResourcesTest.java | 41 +++++++++++++++++++ 7 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 integration-tests/vertx-http/src/main/resources/META-INF/resources/dummy/index.html create mode 100644 integration-tests/vertx-http/src/main/resources/META-INF/resources/dummy2/test.html create mode 100644 integration-tests/vertx-http/src/main/resources/META-INF/resources/l1/l2/index.html create mode 100644 integration-tests/vertx-http/src/main/resources/META-INF/resources/test.txt create mode 100644 integration-tests/vertx-http/src/test/java/io/quarkus/it/vertx/StaticResourcesIT.java create mode 100644 integration-tests/vertx-http/src/test/java/io/quarkus/it/vertx/StaticResourcesTest.java diff --git a/extensions/vertx-http/deployment/src/main/java/io/quarkus/vertx/http/deployment/StaticResourcesProcessor.java b/extensions/vertx-http/deployment/src/main/java/io/quarkus/vertx/http/deployment/StaticResourcesProcessor.java index d80af2bca4b0e..11bcd0d08eaf8 100644 --- a/extensions/vertx-http/deployment/src/main/java/io/quarkus/vertx/http/deployment/StaticResourcesProcessor.java +++ b/extensions/vertx-http/deployment/src/main/java/io/quarkus/vertx/http/deployment/StaticResourcesProcessor.java @@ -66,7 +66,7 @@ public void runtimeInit(Optional staticResources, Stat @BuildStep(onlyIf = NativeOrNativeSourcesBuild.class) public void nativeImageResource(Optional staticResources, - BuildProducer producer) { + BuildProducer producer) throws IOException { if (staticResources.isPresent()) { Set entries = staticResources.get().getEntries(); List metaInfResources = new ArrayList<>(entries.size()); @@ -75,10 +75,34 @@ public void nativeImageResource(Optional staticResourc // TODO: do we perhaps want to register the whole directory? continue; } + String metaInfResourcesPath = StaticResourcesRecorder.META_INF_RESOURCES + entry.getPath(); metaInfResources.add(metaInfResourcesPath); } producer.produce(new NativeImageResourceBuildItem(metaInfResources)); + + // register all directories under META-INF/resources for reflection in order to enable + // the serving of index.html in arbitrarily nested directories + ClassPathUtils.consumeAsPaths(StaticResourcesRecorder.META_INF_RESOURCES, resource -> { + try { + Files.walkFileTree(resource, new SimpleFileVisitor<>() { + @Override + public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException { + if (e != null) { + throw e; + } + int index = dir.toString().indexOf(StaticResourcesRecorder.META_INF_RESOURCES); + if (index > 0) { + producer.produce(new NativeImageResourceBuildItem(dir.toString().substring(index))); + } + return FileVisitResult.CONTINUE; + } + }); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + }); + } } @@ -89,7 +113,8 @@ public void nativeImageResource(Optional staticResourc * @return the set of static resources * @throws Exception */ - private Set getClasspathResources(ApplicationArchivesBuildItem applicationArchivesBuildItem) + private Set getClasspathResources( + ApplicationArchivesBuildItem applicationArchivesBuildItem) throws Exception { Set knownPaths = new HashSet<>(); diff --git a/integration-tests/vertx-http/src/main/resources/META-INF/resources/dummy/index.html b/integration-tests/vertx-http/src/main/resources/META-INF/resources/dummy/index.html new file mode 100644 index 0000000000000..6cb29f572ede2 --- /dev/null +++ b/integration-tests/vertx-http/src/main/resources/META-INF/resources/dummy/index.html @@ -0,0 +1,7 @@ + + + Hello world + +Hello World + + diff --git a/integration-tests/vertx-http/src/main/resources/META-INF/resources/dummy2/test.html b/integration-tests/vertx-http/src/main/resources/META-INF/resources/dummy2/test.html new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/integration-tests/vertx-http/src/main/resources/META-INF/resources/l1/l2/index.html b/integration-tests/vertx-http/src/main/resources/META-INF/resources/l1/l2/index.html new file mode 100644 index 0000000000000..6cb29f572ede2 --- /dev/null +++ b/integration-tests/vertx-http/src/main/resources/META-INF/resources/l1/l2/index.html @@ -0,0 +1,7 @@ + + + Hello world + +Hello World + + diff --git a/integration-tests/vertx-http/src/main/resources/META-INF/resources/test.txt b/integration-tests/vertx-http/src/main/resources/META-INF/resources/test.txt new file mode 100644 index 0000000000000..9daeafb9864cf --- /dev/null +++ b/integration-tests/vertx-http/src/main/resources/META-INF/resources/test.txt @@ -0,0 +1 @@ +test diff --git a/integration-tests/vertx-http/src/test/java/io/quarkus/it/vertx/StaticResourcesIT.java b/integration-tests/vertx-http/src/test/java/io/quarkus/it/vertx/StaticResourcesIT.java new file mode 100644 index 0000000000000..d20e98d4a2c53 --- /dev/null +++ b/integration-tests/vertx-http/src/test/java/io/quarkus/it/vertx/StaticResourcesIT.java @@ -0,0 +1,7 @@ +package io.quarkus.it.vertx; + +import io.quarkus.test.junit.QuarkusIntegrationTest; + +@QuarkusIntegrationTest +public class StaticResourcesIT extends StaticResourcesTest { +} diff --git a/integration-tests/vertx-http/src/test/java/io/quarkus/it/vertx/StaticResourcesTest.java b/integration-tests/vertx-http/src/test/java/io/quarkus/it/vertx/StaticResourcesTest.java new file mode 100644 index 0000000000000..028044c2feda7 --- /dev/null +++ b/integration-tests/vertx-http/src/test/java/io/quarkus/it/vertx/StaticResourcesTest.java @@ -0,0 +1,41 @@ +package io.quarkus.it.vertx; + +import static io.restassured.RestAssured.when; + +import org.junit.jupiter.api.Test; + +import io.quarkus.test.junit.QuarkusTest; + +@QuarkusTest +public class StaticResourcesTest { + + @Test + public void testExisting() { + when().get("/test.txt").then().statusCode(200); + } + + @Test + public void testNonExisting() { + when().get("/test2.txt").then().statusCode(404); + } + + @Test + public void testIndexInDirectory() { + when().get("/dummy/").then().statusCode(200); + } + + @Test + public void testIndexInNestedDirectory() { + when().get("/l1/l2/").then().statusCode(200); + } + + @Test + public void testNonIndexInDirectory() { + when().get("/dummy2/").then().statusCode(404); + } + + @Test + public void testIndexInNonExistingDirectory() { + when().get("/dummy3/").then().statusCode(404); + } +}