Search in sources :

Example 1 with Handler

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

the class Main method routingAsFilter.

/**
 * All routing rules (routes) are evaluated in a definition order. The {@link Handler} assigned with the first valid route
 * for given request is called. It is a responsibility of each handler to process in one of the following ways:
 * <ul>
 *     <li>Respond using one of {@link io.helidon.webserver.ServerResponse#send() ServerResponse.send(...)} method.</li>
 *     <li>Continue to next valid route using {@link io.helidon.webserver.ServerRequest#next() ServerRequest.next()} method.
 *     <i>It is possible to define filtering handlers.</i></li>
 * </ul>
 * <p>
 * If no valid {@link Handler} is found then routing respond by {@code HTTP 404} code.
 * <p>
 * If selected {@link Handler} doesn't process request than the request <b>stacks</b>!
 * <p>
 * <b>Blocking operations:</b><br>
 * For performance reason, {@link Handler} can be called directly by a selector thread. It is not good idea to block
 * such thread. If request must be processed by a blocking operation then such processing should be deferred to another
 * thread.
 */
public void routingAsFilter() {
    Routing routing = Routing.builder().any((req, res) -> {
        System.out.println(req.method() + " " + req.path());
        // Filters are just routing handlers which calls next()
        req.next();
    }).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 2 with Handler

use of io.helidon.webserver.Handler 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)

Aggregations

DataChunk (io.helidon.common.http.DataChunk)2 Http (io.helidon.common.http.Http)2 MediaType (io.helidon.common.http.MediaType)2 Parameters (io.helidon.common.http.Parameters)2 MediaContext (io.helidon.media.common.MediaContext)2 MessageBodyReader (io.helidon.media.common.MessageBodyReader)2 JsonpSupport (io.helidon.media.jsonp.JsonpSupport)2 Handler (io.helidon.webserver.Handler)2 HttpException (io.helidon.webserver.HttpException)2 RequestPredicate (io.helidon.webserver.RequestPredicate)2 Routing (io.helidon.webserver.Routing)2 WebServer (io.helidon.webserver.WebServer)2 JerseySupport (io.helidon.webserver.jersey.JerseySupport)2 StaticContentSupport (io.helidon.webserver.staticcontent.StaticContentSupport)2 Json (jakarta.json.Json)2 JsonBuilderFactory (jakarta.json.JsonBuilderFactory)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 Modifier (java.lang.reflect.Modifier)2 Collections (java.util.Collections)2