From 6dd9bb82ad7aa993f866bcf79c51c9fc36317471 Mon Sep 17 00:00:00 2001 From: Romain Manni-Bucau Date: Wed, 22 Mar 2023 17:24:00 +0100 Subject: [PATCH] sample to serve static resources using tomcat server --- .../fusion-http-server/resources.json | 3 +- .../http/server/StaticResourcesTest.java | 94 +++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 fusion-http-server/src/test/java/io/yupiik/fusion/http/server/StaticResourcesTest.java diff --git a/fusion-http-server/src/main/resources/META-INF/native-image/io.yupiik.fusion/fusion-http-server/resources.json b/fusion-http-server/src/main/resources/META-INF/native-image/io.yupiik.fusion/fusion-http-server/resources.json index 670a2546..b003f4a8 100644 --- a/fusion-http-server/src/main/resources/META-INF/native-image/io.yupiik.fusion/fusion-http-server/resources.json +++ b/fusion-http-server/src/main/resources/META-INF/native-image/io.yupiik.fusion/fusion-http-server/resources.json @@ -5,7 +5,8 @@ {"pattern": "org\\/apache\\/catalina\\/core\\/RestrictedServlets\\.properties"}, {"pattern": "org\\/apache\\/catalina\\/core\\/RestrictedFilters\\.properties"}, {"pattern": "org\\/apache\\/catalina\\/core\\/RestrictedListeners\\.properties"}, - {"pattern": "org\\/apache\\/catalina\\/util\\/ServerInfo\\.properties"} + {"pattern": "org\\/apache\\/catalina\\/util\\/ServerInfo\\.properties"}, + {"pattern": "org\\/apache\\/catalina\\/startup\\/MimeTypeMappings\\.properties"} ] } } \ No newline at end of file diff --git a/fusion-http-server/src/test/java/io/yupiik/fusion/http/server/StaticResourcesTest.java b/fusion-http-server/src/test/java/io/yupiik/fusion/http/server/StaticResourcesTest.java new file mode 100644 index 00000000..b8f93c0a --- /dev/null +++ b/fusion-http-server/src/test/java/io/yupiik/fusion/http/server/StaticResourcesTest.java @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2022-2023 - Yupiik SAS - https://www.yupiik.com + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package io.yupiik.fusion.http.server; + +import io.yupiik.fusion.framework.api.ConfiguringContainer; +import io.yupiik.fusion.framework.api.RuntimeContainer; +import io.yupiik.fusion.framework.api.container.FusionListener; +import io.yupiik.fusion.http.server.api.WebServer; +import io.yupiik.fusion.http.server.impl.tomcat.TomcatWebServerConfiguration; +import org.apache.catalina.servlets.DefaultServlet; +import org.apache.catalina.startup.Tomcat; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.io.IOException; +import java.lang.reflect.Type; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import java.util.Set; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class StaticResourcesTest { + @Test + void run(@TempDir final Path www) throws IOException, InterruptedException { + Files.createDirectories(www); + Files.writeString(www.resolve("index.html"), "

Hi Fusion!

"); + + try (final var container = ConfiguringContainer.of() + // can be replaced by + // public void onWebConfig(@OnEvent @Order(Integer.MAX_VALUE) WebServer.Configuration conf) {...} + .register(new FusionListener() { + @Override + public Type eventType() { + return WebServer.Configuration.class; + } + + @Override + public int priority() { + return Integer.MAX_VALUE; // last to not break custom configuration if any + } + + @Override + public void onEvent(final RuntimeContainer container, final WebServer.Configuration event) { + event.port(0); + + final var tomcat = event.unwrap(TomcatWebServerConfiguration.class); + tomcat.setContextCustomizers(concat(tomcat.getContextCustomizers(), ctx -> { + Tomcat.addDefaultMimeTypeMappings(ctx); + ctx.addWelcomeFile("index.html"); + ctx.setDocBase(www.toString()); + ctx.addServletContainerInitializer((ignored, sc) -> { + final var servlet = sc.addServlet("default", DefaultServlet.class); + servlet.setLoadOnStartup(1); + servlet.addMapping("/"); + sc.setSessionTimeout(1); + }, Set.of()); + })); + } + + private List concat(final List existing, final T newInstance) { + return existing == null ? List.of(newInstance) : Stream.concat(existing.stream(), Stream.of(newInstance)).toList(); + } + }) + .start(); + final var conf = container.lookup(WebServer.Configuration.class)) { + final var url = URI.create("http://localhost:" + conf.instance().port()); + final var home = HttpClient.newHttpClient().send( + HttpRequest.newBuilder().GET().uri(url).build(), + HttpResponse.BodyHandlers.ofString()); + assertEquals(200, home.statusCode()); + assertEquals("

Hi Fusion!

", home.body()); + } + } +}