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));
}
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) {
}
}
}
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);
});
}
Aggregations