diff --git a/examples/webserver/static-content/README.md b/examples/webserver/static-content/README.md index 6b4d1edfd8b..c6c4116cfc2 100644 --- a/examples/webserver/static-content/README.md +++ b/examples/webserver/static-content/README.md @@ -1,6 +1,6 @@ # Static Content Example -This application demonstrates use of the StaticContentSupport to serve static files +This application demonstrates use of the StaticContentService to serve static files together with a simple REST service. ## Build and run diff --git a/examples/webserver/static-content/pom.xml b/examples/webserver/static-content/pom.xml index be67e2962cd..0d245348695 100644 --- a/examples/webserver/static-content/pom.xml +++ b/examples/webserver/static-content/pom.xml @@ -37,21 +37,21 @@ - io.helidon.reactive.webserver.examples.staticcontent.Main + io.helidon.webserver.examples.staticcontent.Main - io.helidon.reactive.webserver - helidon-reactive-webserver + io.helidon.nima.webserver + helidon-nima-webserver - io.helidon.reactive.webserver - helidon-reactive-webserver-static-content + io.helidon.nima.webserver + helidon-nima-webserver-static-content - io.helidon.reactive.media - helidon-reactive-media-jsonp + io.helidon.nima.http.media + helidon-nima-http-media-jsonp org.junit.jupiter @@ -64,8 +64,8 @@ test - io.helidon.reactive.webserver - helidon-reactive-webserver-test-support + io.helidon.nima.testing.junit5 + helidon-nima-testing-junit5-webserver test diff --git a/examples/webserver/static-content/src/main/java/io/helidon/reactive/webserver/examples/staticcontent/Main.java b/examples/webserver/static-content/src/main/java/io/helidon/reactive/webserver/examples/staticcontent/Main.java deleted file mode 100644 index 3b7a3a1023f..00000000000 --- a/examples/webserver/static-content/src/main/java/io/helidon/reactive/webserver/examples/staticcontent/Main.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright (c) 2017, 2022 Oracle and/or its affiliates. - * - * 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.helidon.reactive.webserver.examples.staticcontent; - -import io.helidon.common.http.Http; -import io.helidon.reactive.media.jsonp.JsonpSupport; -import io.helidon.reactive.webserver.Routing; -import io.helidon.reactive.webserver.WebServer; -import io.helidon.reactive.webserver.staticcontent.StaticContentSupport; - -/** - * Application demonstrates combination of the static content with a simple REST API. It counts accesses and display it - * on the WEB page. - */ -public class Main { - private static final Http.HeaderValue UI_REDIRECT = Http.Header.createCached(Http.Header.LOCATION, "/ui"); - - private Main() { - } - - /** - * Creates new {@link Routing}. - * - * @return the new instance - */ - static Routing createRouting() { - return Routing.builder() - .any("/", (req, res) -> { - // showing the capability to run on any path, and redirecting from root - res.status(Http.Status.MOVED_PERMANENTLY_301); - res.headers().set(UI_REDIRECT); - res.send(); - }) - .register("/ui", new CounterService()) - .register("/ui", StaticContentSupport.builder("WEB") - .welcomeFileName("index.html") - .build()) - .build(); - } - - /** - * A java main class. - * - * @param args command line arguments. - */ - public static void main(String[] args) { - WebServer server = WebServer.builder(createRouting()) - .port(8080) - .addMediaSupport(JsonpSupport.create()) - .build(); - - // Start the server and print some info. - server.start().thenAccept(ws -> { - System.out.println("WEB server is up! http://localhost:" + ws.port()); - }); - - // Server threads are not demon. NO need to block. Just react. - server.whenShutdown() - .thenRun(() -> System.out.println("WEB server is DOWN. Good bye!")); - - } -} diff --git a/examples/webserver/static-content/src/main/java/io/helidon/reactive/webserver/examples/staticcontent/CounterService.java b/examples/webserver/static-content/src/main/java/io/helidon/webserver/examples/staticcontent/CounterService.java similarity index 74% rename from examples/webserver/static-content/src/main/java/io/helidon/reactive/webserver/examples/staticcontent/CounterService.java rename to examples/webserver/static-content/src/main/java/io/helidon/webserver/examples/staticcontent/CounterService.java index 67e4f4186a0..578bad4c7e2 100644 --- a/examples/webserver/static-content/src/main/java/io/helidon/reactive/webserver/examples/staticcontent/CounterService.java +++ b/examples/webserver/static-content/src/main/java/io/helidon/webserver/examples/staticcontent/CounterService.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, 2022 Oracle and/or its affiliates. + * Copyright (c) 2017, 2023 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,16 +14,16 @@ * limitations under the License. */ -package io.helidon.reactive.webserver.examples.staticcontent; +package io.helidon.webserver.examples.staticcontent; import java.util.Collections; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.LongAdder; -import io.helidon.reactive.webserver.Routing; -import io.helidon.reactive.webserver.ServerRequest; -import io.helidon.reactive.webserver.ServerResponse; -import io.helidon.reactive.webserver.Service; +import io.helidon.nima.webserver.http.HttpRules; +import io.helidon.nima.webserver.http.HttpService; +import io.helidon.nima.webserver.http.ServerRequest; +import io.helidon.nima.webserver.http.ServerResponse; import jakarta.json.Json; import jakarta.json.JsonBuilderFactory; @@ -32,21 +32,21 @@ /** * Counts access to the WEB service. */ -public class CounterService implements Service { +public class CounterService implements HttpService { private static final JsonBuilderFactory JSON = Json.createBuilderFactory(Collections.emptyMap()); private final LongAdder allAccessCounter = new LongAdder(); private final AtomicInteger apiAccessCounter = new AtomicInteger(); @Override - public void update(Routing.Rules routingRules) { - routingRules.any(this::handleAny) - .get("/api/counter", this::handleGet); + public void routing(HttpRules rules) { + rules.any(this::handleAny) + .get("/api/counter", this::handleGet); } private void handleAny(ServerRequest request, ServerResponse response) { allAccessCounter.increment(); - request.next(); + response.next(); } private void handleGet(ServerRequest request, ServerResponse response) { diff --git a/examples/webserver/static-content/src/main/java/io/helidon/webserver/examples/staticcontent/Main.java b/examples/webserver/static-content/src/main/java/io/helidon/webserver/examples/staticcontent/Main.java new file mode 100644 index 00000000000..346e93b657d --- /dev/null +++ b/examples/webserver/static-content/src/main/java/io/helidon/webserver/examples/staticcontent/Main.java @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2017, 2023 Oracle and/or its affiliates. + * + * 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.helidon.webserver.examples.staticcontent; + +import io.helidon.common.http.Http; +import io.helidon.logging.common.LogConfig; +import io.helidon.nima.webserver.WebServer; +import io.helidon.nima.webserver.http.HttpRouting; +import io.helidon.nima.webserver.staticcontent.StaticContentService; + +/** + * The application main class. + */ +public final class Main { + private static final Http.HeaderValue UI_REDIRECT = Http.Header.createCached(Http.Header.LOCATION, "/ui"); + + /** + * Cannot be instantiated. + */ + private Main() { + } + + /** + * Application main entry point. + * + * @param args command line arguments. + */ + public static void main(String[] args) { + // load logging configuration + LogConfig.configureRuntime(); + + WebServer server = WebServer.builder() + .port(8080) + .routing(Main::routing) + .start(); + + System.out.println("WEB server is up! http://localhost:" + server.port() + "/greet"); + } + + /** + * Updates HTTP Routing. + */ + static void routing(HttpRouting.Builder routing) { + routing.any("/", (req, res) -> { + // showing the capability to run on any path, and redirecting from root + res.status(Http.Status.MOVED_PERMANENTLY_301); + res.headers().set(UI_REDIRECT); + res.send(); + }) + .register("/ui", CounterService::new) + .register("/ui", StaticContentService.builder("WEB") + .welcomeFileName("index.html") + .build()); + } +} diff --git a/examples/webserver/static-content/src/main/java/io/helidon/reactive/webserver/examples/staticcontent/package-info.java b/examples/webserver/static-content/src/main/java/io/helidon/webserver/examples/staticcontent/package-info.java similarity index 72% rename from examples/webserver/static-content/src/main/java/io/helidon/reactive/webserver/examples/staticcontent/package-info.java rename to examples/webserver/static-content/src/main/java/io/helidon/webserver/examples/staticcontent/package-info.java index 415e225dbd3..815f68fe5eb 100644 --- a/examples/webserver/static-content/src/main/java/io/helidon/reactive/webserver/examples/staticcontent/package-info.java +++ b/examples/webserver/static-content/src/main/java/io/helidon/webserver/examples/staticcontent/package-info.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, 2022 Oracle and/or its affiliates. + * Copyright (c) 2017, 2023 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,8 +18,8 @@ * Application demonstrates combination of the static content with a simple REST API. It counts accesses and display it * on the WEB page. *

- * Start with {@link io.helidon.reactive.webserver.examples.staticcontent.Main} class. + * Start with {@link io.helidon.webserver.examples.staticcontent.Main} class. * - * @see io.helidon.reactive.webserver.examples.staticcontent.Main + * @see io.helidon.webserver.examples.staticcontent.Main */ -package io.helidon.reactive.webserver.examples.staticcontent; +package io.helidon.webserver.examples.staticcontent; diff --git a/examples/webserver/static-content/src/test/java/io/helidon/webserver/examples/staticcontent/MainTest.java b/examples/webserver/static-content/src/test/java/io/helidon/webserver/examples/staticcontent/MainTest.java new file mode 100644 index 00000000000..21411a72893 --- /dev/null +++ b/examples/webserver/static-content/src/test/java/io/helidon/webserver/examples/staticcontent/MainTest.java @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2023 Oracle and/or its affiliates. + * + * 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.helidon.webserver.examples.staticcontent; + +import io.helidon.common.http.Http; +import io.helidon.nima.testing.junit5.webserver.ServerTest; +import io.helidon.nima.testing.junit5.webserver.SetUpRoute; +import io.helidon.nima.webclient.http1.Http1Client; +import io.helidon.nima.webclient.http1.Http1ClientResponse; +import io.helidon.nima.webserver.http.HttpRouting; +import jakarta.json.JsonNumber; +import jakarta.json.JsonObject; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +@ServerTest +class MainTest { + + private final Http1Client client; + + protected MainTest(Http1Client client) { + this.client = client; + } + + @SetUpRoute + static void routing(HttpRouting.Builder builder) { + Main.routing(builder); + } + + @Test + void testUi() { + assertThat(allCounter(), is(1)); + try (Http1ClientResponse response = client.get("/ui/index.html").request()) { + assertThat(response.status(), is(Http.Status.OK_200)); + assertThat(response.headers().contentType().orElseThrow().text(), is("text/html")); + } + try (Http1ClientResponse response = client.get("/ui/css/app.css").request()) { + assertThat(response.status(), is(Http.Status.OK_200)); + assertThat(response.headers().contentType().orElseThrow().text(), is("text/css")); + } + try (Http1ClientResponse response = client.get("/ui/js/app.js").request()) { + assertThat(response.status(), is(Http.Status.OK_200)); + assertThat(response.headers().contentType().orElseThrow().text(), is("text/javascript")); + } + assertThat(allCounter(), is(5)); // includes /ui/api/counter calls + } + + private int allCounter() { + try (Http1ClientResponse response = client.get("/ui/api/counter").request()) { + assertThat(response.status(), is(Http.Status.OK_200)); + JsonNumber number = (JsonNumber) response.as(JsonObject.class).get("all"); + return number.intValue(); + } + } +}