|
| 1 | +/* |
| 2 | + * JBoss, Home of Professional Open Source. |
| 3 | + * Copyright 2025 Red Hat, Inc., and individual contributors |
| 4 | + * as indicated by the @author tags. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package io.undertow.examples.routingserver; |
| 19 | + |
| 20 | +import io.undertow.Handlers; |
| 21 | +import io.undertow.Undertow; |
| 22 | +import io.undertow.examples.UndertowExample; |
| 23 | +import io.undertow.server.HttpServerExchange; |
| 24 | +import io.undertow.server.RoutingHandler; |
| 25 | +import io.undertow.util.Headers; |
| 26 | + |
| 27 | +import java.nio.charset.StandardCharsets; |
| 28 | + |
| 29 | +/** |
| 30 | + * An example of how to use the RoutingHandler to dispatch |
| 31 | + * to different handlers based on the request method (GET, POST, etc.). |
| 32 | + * |
| 33 | + * Demonstrates: |
| 34 | + * - GET /greet |
| 35 | + * - POST /greet |
| 36 | + * - Fallback 404 handler |
| 37 | + * |
| 38 | + * Author: anamitraupadhyay |
| 39 | + */ |
| 40 | +@UndertowExample("Routing Handler") |
| 41 | +public class RoutingServer { |
| 42 | + |
| 43 | + public static void main(final String[] args) { |
| 44 | + RoutingHandler handler = Handlers.routing() |
| 45 | + .get("/greet", RoutingServer::handleGetRequest) |
| 46 | + .post("/greet", RoutingServer::handlePostRequest) |
| 47 | + .get("/*", exchange -> { |
| 48 | + exchange.setStatusCode(404); |
| 49 | + exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain"); |
| 50 | + exchange.getResponseSender().send("Page Not Found"); |
| 51 | + }); |
| 52 | + |
| 53 | + Undertow server = Undertow.builder() |
| 54 | + .addHttpListener(8080, "localhost") |
| 55 | + .setHandler(handler) |
| 56 | + .build(); |
| 57 | + server.start(); |
| 58 | + } |
| 59 | + |
| 60 | + private static void handleGetRequest(HttpServerExchange exchange) { |
| 61 | + String name = "World"; |
| 62 | + exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain"); |
| 63 | + exchange.getResponseSender().send("Hello " + name); |
| 64 | + } |
| 65 | + |
| 66 | + private static void handlePostRequest(HttpServerExchange exchange) { |
| 67 | + exchange.getRequestReceiver().receiveFullString( |
| 68 | + (HttpServerExchange exc, String body) -> { |
| 69 | + exc.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain"); |
| 70 | + exc.getResponseSender().send("Hello " + body); |
| 71 | + }, |
| 72 | + StandardCharsets.UTF_8 |
| 73 | + ); |
| 74 | + } |
| 75 | +} |
0 commit comments