Search in sources :

Example 1 with RoutingHandler

use of io.undertow.server.RoutingHandler in project undertow by undertow-io.

the class RoutingHandlerTestCase method setup.

@BeforeClass
public static void setup() {
    RoutingHandler commonHandler = Handlers.routing().add(Methods.GET, "/baz", new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseSender().send("baz");
        }
    }).add(Methods.GET, "/baz/{foo}", new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseSender().send("baz-path" + exchange.getQueryParameters().get("foo"));
        }
    });
    RoutingHandler convienceHandler = Handlers.routing().get("/bar", new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseSender().send("GET bar");
        }
    }).put("/bar", new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseSender().send("PUT bar");
        }
    }).post("/bar", new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseSender().send("POST bar");
        }
    }).delete("/bar", new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseSender().send("DELETE bar");
        }
    });
    HttpHandler handler = Handlers.routing().add(Methods.GET, "/wild/{test}/*", new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseSender().send("wild:" + exchange.getQueryParameters().get("test") + ":" + exchange.getQueryParameters().get("*"));
        }
    }).add(Methods.GET, "/wilder/*", new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseSender().send("wilder:" + exchange.getQueryParameters().get("*"));
        }
    }).add(Methods.GET, "/wildest*", new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseSender().send("wildest:" + exchange.getQueryParameters().get("*"));
        }
    }).add(Methods.GET, "/foo", new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseSender().send("foo");
        }
    }).add(Methods.GET, "/foo", Predicates.parse("contains[value=%{i,SomeHeader},search='special'] "), new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseSender().send("special foo");
        }
    }).add(Methods.POST, "/foo", new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseSender().send("posted foo");
        }
    }).add(Methods.POST, "/foo/{baz}", new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseSender().send("foo-path" + exchange.getQueryParameters().get("bar"));
        }
    }).add(Methods.GET, "/foo/{bar}", new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseSender().send("foo-path" + exchange.getQueryParameters().get("bar"));
        }
    }).get("/", new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseSender().send("GET /");
        }
    }).add(Methods.GET, "scoop/{scoop}", new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseSender().send("SCOOP GET");
        }
    }).add(Methods.POST, "scoop/{scoop}", new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseSender().send("SCOOP POST");
        }
    }).addAll(commonHandler).addAll(convienceHandler);
    DefaultServer.setRootHandler(Handlers.path(handler).addPrefixPath("/prefix", handler));
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) RoutingHandler(io.undertow.server.RoutingHandler) IOException(java.io.IOException) BeforeClass(org.junit.BeforeClass)

Example 2 with RoutingHandler

use of io.undertow.server.RoutingHandler in project undertow by undertow-io.

the class URLDecodingHandlerTestCase method testDecodeCharactersInMatchedPaths.

@Test
public void testDecodeCharactersInMatchedPaths() throws Exception {
    // When UndertowOptions.DECODE_URL is disabled, the URLDecodingHandler should decode values.
    Undertow undertow = Undertow.builder().setServerOption(UndertowOptions.DECODE_URL, false).addHttpListener(PORT, "0.0.0.0").setHandler(new RoutingHandler().get("/api/{pathParam}/tail", new URLDecodingHandler(new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            String matched = exchange.getAttachment(PathTemplateMatch.ATTACHMENT_KEY).getParameters().get("pathParam");
            exchange.getResponseSender().send(matched);
        }
    }, "UTF-8"))).build();
    undertow.start();
    try {
        TestHttpClient client = new TestHttpClient();
        // '%253E' decodes to '%3E', which would decode to '>' if decoded twice
        try (CloseableHttpResponse response = client.execute(new HttpGet("http://localhost:" + PORT + "/api/test%2Ftest+test%2Btest%20test/tail"))) {
            Assert.assertEquals("test/test+test+test test", getResponseString(response));
        }
    } finally {
        undertow.stop();
        // sleep 1 s to prevent BindException (Address already in use) when restarting the server
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ignore) {
        }
    }
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) RoutingHandler(io.undertow.server.RoutingHandler) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Undertow(io.undertow.Undertow) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 3 with RoutingHandler

use of io.undertow.server.RoutingHandler in project light-4j by networknt.

the class CorrelationHandlerTest method getTestHandler.

static RoutingHandler getTestHandler() {
    return Handlers.routing().add(Methods.GET, "/with", exchange -> {
        String cid = exchange.getRequestHeaders().getFirst(Constants.CORRELATION_ID);
        exchange.getResponseSender().send(cid);
    }).add(Methods.GET, "/without", exchange -> {
        String cid = exchange.getRequestHeaders().getFirst(Constants.CORRELATION_ID);
        exchange.getResponseSender().send(cid);
    });
}
Also used : ClientException(com.networknt.exception.ClientException) RoutingHandler(io.undertow.server.RoutingHandler) AfterClass(org.junit.AfterClass) Handlers(io.undertow.Handlers) Logger(org.slf4j.Logger) BeforeClass(org.junit.BeforeClass) LoggerFactory(org.slf4j.LoggerFactory) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) Undertow(io.undertow.Undertow) HttpHandler(io.undertow.server.HttpHandler) HttpString(io.undertow.util.HttpString) OptionMap(org.xnio.OptionMap) CountDownLatch(java.util.concurrent.CountDownLatch) ClientConnection(io.undertow.client.ClientConnection) Constants(com.networknt.utility.Constants) ClientRequest(io.undertow.client.ClientRequest) Methods(io.undertow.util.Methods) ClientResponse(io.undertow.client.ClientResponse) URI(java.net.URI) Http2Client(com.networknt.client.Http2Client) Assert(org.junit.Assert) IoUtils(org.xnio.IoUtils) HttpString(io.undertow.util.HttpString)

Aggregations

HttpHandler (io.undertow.server.HttpHandler)3 RoutingHandler (io.undertow.server.RoutingHandler)3 Undertow (io.undertow.Undertow)2 HttpServerExchange (io.undertow.server.HttpServerExchange)2 BeforeClass (org.junit.BeforeClass)2 Test (org.junit.Test)2 Http2Client (com.networknt.client.Http2Client)1 ClientException (com.networknt.exception.ClientException)1 Constants (com.networknt.utility.Constants)1 Handlers (io.undertow.Handlers)1 ClientConnection (io.undertow.client.ClientConnection)1 ClientRequest (io.undertow.client.ClientRequest)1 ClientResponse (io.undertow.client.ClientResponse)1 TestHttpClient (io.undertow.testutils.TestHttpClient)1 HttpString (io.undertow.util.HttpString)1 Methods (io.undertow.util.Methods)1 IOException (java.io.IOException)1 URI (java.net.URI)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1