Skip to content

Commit

Permalink
Ensure that index.html works in any directory in native mode
Browse files Browse the repository at this point in the history
(cherry picked from commit 24b3fa7)
  • Loading branch information
geoand authored and gsmet committed Apr 9, 2024
1 parent 6e6fabd commit 7ffc564
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void runtimeInit(Optional<StaticResourcesBuildItem> staticResources, Stat

@BuildStep(onlyIf = NativeOrNativeSourcesBuild.class)
public void nativeImageResource(Optional<StaticResourcesBuildItem> staticResources,
BuildProducer<NativeImageResourceBuildItem> producer) {
BuildProducer<NativeImageResourceBuildItem> producer) throws IOException {
if (staticResources.isPresent()) {
Set<StaticResourcesBuildItem.Entry> entries = staticResources.get().getEntries();
List<String> metaInfResources = new ArrayList<>(entries.size());
Expand All @@ -75,10 +75,34 @@ public void nativeImageResource(Optional<StaticResourcesBuildItem> 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);
}
});

}
}

Expand All @@ -89,7 +113,8 @@ public void nativeImageResource(Optional<StaticResourcesBuildItem> staticResourc
* @return the set of static resources
* @throws Exception
*/
private Set<StaticResourcesBuildItem.Entry> getClasspathResources(ApplicationArchivesBuildItem applicationArchivesBuildItem)
private Set<StaticResourcesBuildItem.Entry> getClasspathResources(
ApplicationArchivesBuildItem applicationArchivesBuildItem)
throws Exception {
Set<StaticResourcesBuildItem.Entry> knownPaths = new HashSet<>();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<head>
<title>Hello world</title></head>
<body>
Hello World
</body>
</html>
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<head>
<title>Hello world</title></head>
<body>
Hello World
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.quarkus.it.vertx;

import io.quarkus.test.junit.QuarkusIntegrationTest;

@QuarkusIntegrationTest
public class StaticResourcesIT extends StaticResourcesTest {
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 7ffc564

Please sign in to comment.