Skip to content

Commit

Permalink
Port of static content example to Nima webserver (#6778)
Browse files Browse the repository at this point in the history
* Port of static content example to Nima webserver. Added new @servertest based test.

* Fixed checkstyle.

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>

* Replaced StaticContentSupport by StaticContentService.

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>

---------

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>
  • Loading branch information
spericas authored May 9, 2023
1 parent f736829 commit 8c0d1fe
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 101 deletions.
2 changes: 1 addition & 1 deletion examples/webserver/static-content/README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
18 changes: 9 additions & 9 deletions examples/webserver/static-content/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,21 @@
</description>

<properties>
<mainClass>io.helidon.reactive.webserver.examples.staticcontent.Main</mainClass>
<mainClass>io.helidon.webserver.examples.staticcontent.Main</mainClass>
</properties>

<dependencies>
<dependency>
<groupId>io.helidon.reactive.webserver</groupId>
<artifactId>helidon-reactive-webserver</artifactId>
<groupId>io.helidon.nima.webserver</groupId>
<artifactId>helidon-nima-webserver</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.reactive.webserver</groupId>
<artifactId>helidon-reactive-webserver-static-content</artifactId>
<groupId>io.helidon.nima.webserver</groupId>
<artifactId>helidon-nima-webserver-static-content</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.reactive.media</groupId>
<artifactId>helidon-reactive-media-jsonp</artifactId>
<groupId>io.helidon.nima.http.media</groupId>
<artifactId>helidon-nima-http-media-jsonp</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand All @@ -64,8 +64,8 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.reactive.webserver</groupId>
<artifactId>helidon-reactive-webserver-test-support</artifactId>
<groupId>io.helidon.nima.testing.junit5</groupId>
<artifactId>helidon-nima-testing-junit5-webserver</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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.
* <p>
* 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;
Original file line number Diff line number Diff line change
@@ -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();
}
}
}

0 comments on commit 8c0d1fe

Please sign in to comment.