use of io.undertow.server.HttpHandler in project undertow by undertow-io.
the class FileHandlerTestCase method main.
/*
Starts simple file server, it is useful for testing directory browsing
*/
public static void main(String[] args) throws URISyntaxException {
Path rootPath = Paths.get(FileHandlerTestCase.class.getResource("page.html").toURI()).getParent().getParent();
HttpHandler root = new CanonicalPathHandler().setNext(new PathHandler().addPrefixPath("/path", new ResourceHandler(new PathResourceManager(rootPath, 1)).setDirectoryListingEnabled(true)));
Undertow undertow = Undertow.builder().addHttpListener(8888, "localhost").setHandler(root).build();
undertow.start();
}
use of io.undertow.server.HttpHandler in project undertow by undertow-io.
the class ResumeWritesTestCase method testResumeWritesFixedLength.
@Test
public void testResumeWritesFixedLength() throws IOException {
DefaultServer.setRootHandler(new HttpHandler() {
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
exchange.addResponseWrapper(new ReturnZeroWrapper());
exchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, HELLO_WORLD.length());
exchange.getResponseSender().send(HELLO_WORLD);
}
});
TestHttpClient client = new TestHttpClient();
try {
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Assert.assertEquals(HELLO_WORLD, HttpClientUtils.readResponse(result));
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Assert.assertEquals(HELLO_WORLD, HttpClientUtils.readResponse(result));
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Assert.assertEquals(HELLO_WORLD, HttpClientUtils.readResponse(result));
} finally {
client.getConnectionManager().shutdown();
}
}
use of io.undertow.server.HttpHandler in project undertow by undertow-io.
the class ResumeWritesTestCase method testResumeWritesChunked.
@Test
public void testResumeWritesChunked() throws IOException {
DefaultServer.setRootHandler(new HttpHandler() {
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
exchange.addResponseWrapper(new ReturnZeroWrapper());
exchange.getResponseSender().send(HELLO_WORLD);
}
});
TestHttpClient client = new TestHttpClient();
try {
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Assert.assertEquals(HELLO_WORLD, HttpClientUtils.readResponse(result));
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Assert.assertEquals(HELLO_WORLD, HttpClientUtils.readResponse(result));
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Assert.assertEquals(HELLO_WORLD, HttpClientUtils.readResponse(result));
} finally {
client.getConnectionManager().shutdown();
}
}
use of io.undertow.server.HttpHandler 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");
}
});
DefaultServer.setRootHandler(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.GET, "/foo/{bar}", new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
exchange.getResponseSender().send("foo-path" + exchange.getQueryParameters().get("bar"));
}
}).addAll(commonHandler).addAll(convienceHandler));
}
use of io.undertow.server.HttpHandler in project undertow by undertow-io.
the class CacheHandlerTestCase method testBasicPathBasedCaching.
@Test
public void testBasicPathBasedCaching() throws IOException {
final AtomicInteger responseCount = new AtomicInteger();
final HttpHandler messageHandler = new HttpHandler() {
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
final ResponseCache cache = exchange.getAttachment(ResponseCache.ATTACHMENT_KEY);
if (!cache.tryServeResponse()) {
final String data = "Response " + responseCount.incrementAndGet();
exchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, data.length() + "");
exchange.getResponseSender().send(data);
}
}
};
final CacheHandler cacheHandler = new CacheHandler(new DirectBufferCache(100, 10, 1000), messageHandler);
DefaultServer.setRootHandler(cacheHandler);
TestHttpClient client = new TestHttpClient();
try {
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
//it takes 5 hits to make an entry actually get cached
for (int i = 1; i <= 5; ++i) {
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Assert.assertEquals("Response " + i, HttpClientUtils.readResponse(result));
}
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Assert.assertEquals("Response 5", HttpClientUtils.readResponse(result));
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Assert.assertEquals("Response 5", HttpClientUtils.readResponse(result));
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Assert.assertEquals("Response 5", HttpClientUtils.readResponse(result));
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path2");
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Assert.assertEquals("Response 6", HttpClientUtils.readResponse(result));
} finally {
client.getConnectionManager().shutdown();
}
}
Aggregations