Search in sources :

Example 46 with Routing

use of io.helidon.webserver.Routing in project helidon by oracle.

the class CommentServiceTest method testRouting.

@Test
public void testRouting() throws Exception {
    Routing routing = Routing.builder().register(new CommentService()).build();
    TestResponse response = TestClient.create(routing).path("one").get();
    assertEquals(Http.Status.OK_200, response.status());
    // Add first comment
    response = TestClient.create(routing).path("one").post(MediaPublisher.create(MediaType.TEXT_PLAIN, "aaa"));
    assertEquals(Http.Status.OK_200, response.status());
    response = TestClient.create(routing).path("one").get();
    assertEquals(Http.Status.OK_200, response.status());
    byte[] data = response.asBytes().toCompletableFuture().get();
    assertEquals("anonymous: aaa\n", new String(data, StandardCharsets.UTF_8));
    // Add second comment
    response = TestClient.create(routing).path("one").post(MediaPublisher.create(MediaType.TEXT_PLAIN, "bbb"));
    assertEquals(Http.Status.OK_200, response.status());
    response = TestClient.create(routing).path("one").get();
    assertEquals(Http.Status.OK_200, response.status());
    data = response.asBytes().toCompletableFuture().get();
    assertEquals("anonymous: aaa\nanonymous: bbb\n", new String(data, StandardCharsets.UTF_8));
}
Also used : TestResponse(io.helidon.webserver.testsupport.TestResponse) Routing(io.helidon.webserver.Routing) Test(org.junit.jupiter.api.Test)

Example 47 with Routing

use of io.helidon.webserver.Routing in project helidon by oracle.

the class UserFilterTest method filter.

@Test
public void filter() throws Exception {
    AtomicReference<User> userReference = new AtomicReference<>();
    Routing routing = Routing.builder().any(new UserFilter()).any((req, res) -> {
        userReference.set(req.context().get(User.class).orElse(null));
        res.send();
    }).build();
    TestResponse response = TestClient.create(routing).path("/").get();
    assertEquals(User.ANONYMOUS, userReference.get());
    response = TestClient.create(routing).path("/").header("Cookie", "Unauthenticated-User-Alias=Foo").get();
    assertEquals("Foo", userReference.get().getAlias());
}
Also used : Test(org.junit.jupiter.api.Test) TestResponse(io.helidon.webserver.testsupport.TestResponse) TestClient(io.helidon.webserver.testsupport.TestClient) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Routing(io.helidon.webserver.Routing) AtomicReference(java.util.concurrent.atomic.AtomicReference) TestResponse(io.helidon.webserver.testsupport.TestResponse) Routing(io.helidon.webserver.Routing) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.jupiter.api.Test)

Example 48 with Routing

use of io.helidon.webserver.Routing in project helidon by oracle.

the class Main method firstRouting.

// ---------------- EXAMPLES
/**
 * True heart of WebServer API is {@link Routing}. It provides fluent way how to assign custom {@link Handler} to the routing
 * rule. The rule consists from two main factors - <i>HTTP method</i> and <i>path pattern</i>.
 * <p>
 * The (route) {@link Handler} is a functional interface which process HTTP {@link io.helidon.webserver.ServerRequest request} and
 * writes to the {@link io.helidon.webserver.ServerResponse response}.
 */
public void firstRouting() {
    Routing routing = Routing.builder().post("/post-endpoint", (req, res) -> res.status(Http.Status.CREATED_201).send()).get("/get-endpoint", (req, res) -> res.status(Http.Status.NO_CONTENT_204).send("Hello World!")).build();
    startServer(routing);
}
Also used : JerseySupport(io.helidon.webserver.jersey.JerseySupport) DataChunk(io.helidon.common.http.DataChunk) RequestPredicate(io.helidon.webserver.RequestPredicate) JsonBuilderFactory(jakarta.json.JsonBuilderFactory) MediaContext(io.helidon.media.common.MediaContext) MessageBodyReader(io.helidon.media.common.MessageBodyReader) InvocationTargetException(java.lang.reflect.InvocationTargetException) MediaType(io.helidon.common.http.MediaType) Json(jakarta.json.Json) JsonpSupport(io.helidon.media.jsonp.JsonpSupport) Handler(io.helidon.webserver.Handler) StaticContentSupport(io.helidon.webserver.staticcontent.StaticContentSupport) Modifier(java.lang.reflect.Modifier) Parameters(io.helidon.common.http.Parameters) HttpException(io.helidon.webserver.HttpException) WebServer(io.helidon.webserver.WebServer) Http(io.helidon.common.http.Http) Routing(io.helidon.webserver.Routing) Method(java.lang.reflect.Method) Collections(java.util.Collections) Routing(io.helidon.webserver.Routing)

Example 49 with Routing

use of io.helidon.webserver.Routing in project helidon by oracle.

the class Main method readContentEntity.

/**
 * Request payload (body/entity) is represented by {@link java.util.concurrent.Flow.Publisher Flow.Publisher}
 * of {@link DataChunk RequestChunks} to enable reactive processing of the content of any size.
 * But it is more convenient to process entity in some type specific form. WebServer supports few types which can be
 * used te read the whole entity:
 * <ul>
 *     <li>{@code byte[]}</li>
 *     <li>{@code String}</li>
 *     <li>{@code InputStream}</li>
 * </ul>
 * <p>
 * Similar approach is used for the response entity.
 */
public void readContentEntity() {
    Routing routing = Routing.builder().post("/foo", (req, res) -> {
        req.content().as(String.class).whenComplete((data, thr) -> {
            if (thr == null) {
                System.out.println("/foo DATA: " + data);
                res.send(data);
            } else {
                res.status(Http.Status.BAD_REQUEST_400);
            }
        });
    }).post("/bar", Handler.create(String.class, (req, res, data) -> {
        System.out.println("/foo DATA: " + data);
        res.send(data);
    })).build();
    startServer(routing);
}
Also used : Routing(io.helidon.webserver.Routing)

Example 50 with Routing

use of io.helidon.webserver.Routing in project helidon by oracle.

the class Main method organiseCode.

/**
 * Larger applications with many routing rules can cause complicated readability (maintainability) if all rules are
 * defined in a single fluent code. It is possible to register {@link io.helidon.webserver.Service Service} and organise
 * the code into services and resources. {@code Service} is an interface which can register more routing rules (routes).
 */
public void organiseCode() {
    Routing routing = Routing.builder().register("/catalog-context-path", new Catalog()).build();
    startServer(routing);
}
Also used : Routing(io.helidon.webserver.Routing)

Aggregations

Routing (io.helidon.webserver.Routing)86 WebServer (io.helidon.webserver.WebServer)38 Config (io.helidon.config.Config)33 Test (org.junit.jupiter.api.Test)32 Http (io.helidon.common.http.Http)23 TimeUnit (java.util.concurrent.TimeUnit)21 LogConfig (io.helidon.common.LogConfig)19 MediaType (io.helidon.common.http.MediaType)19 CoreMatchers.is (org.hamcrest.CoreMatchers.is)17 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)17 SecurityContext (io.helidon.security.SecurityContext)16 HttpException (io.helidon.webserver.HttpException)15 Optional (java.util.Optional)15 CountDownLatch (java.util.concurrent.CountDownLatch)15 WebSecurity (io.helidon.security.integration.webserver.WebSecurity)13 SERVICE_UNAVAILABLE_503 (io.helidon.common.http.Http.Status.SERVICE_UNAVAILABLE_503)11 JsonpSupport (io.helidon.media.jsonp.JsonpSupport)11 Security (io.helidon.security.Security)11 StaticContentSupport (io.helidon.webserver.staticcontent.StaticContentSupport)10 TestResponse (io.helidon.webserver.testsupport.TestResponse)10