use of io.undertow.server.HttpHandler in project undertow by undertow-io.
the class ChunkedResponseTransferCodingTestCase method setup.
@BeforeClass
public static void setup() {
final BlockingHandler blockingHandler = new BlockingHandler();
DefaultServer.setRootHandler(blockingHandler);
blockingHandler.setRootHandler(new HttpHandler() {
@Override
public void handleRequest(final HttpServerExchange exchange) {
try {
if (connection == null) {
connection = exchange.getConnection();
} else if (!DefaultServer.isAjp() && !DefaultServer.isProxy() && connection != exchange.getConnection()) {
final OutputStream outputStream = exchange.getOutputStream();
outputStream.write("Connection not persistent".getBytes());
outputStream.close();
return;
}
new StringWriteChannelListener(message).setup(exchange.getResponseChannel());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
}
use of io.undertow.server.HttpHandler in project undertow by undertow-io.
the class ExceptionHandlerTestCase method testAttachException.
@Test
public void testAttachException() throws IOException {
HttpHandler pathHandler = Handlers.path().addExactPath("/", new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
throw new IllegalArgumentException();
}
});
final HttpHandler exceptionHandler = Handlers.exceptionHandler(pathHandler).addExceptionHandler(IllegalArgumentException.class, new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
exchange.getResponseSender().send("exception handled");
}
});
DefaultServer.setRootHandler(new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
Throwable throwable = exchange.getAttachment(ExceptionHandler.THROWABLE);
Assert.assertNull(throwable);
exceptionHandler.handleRequest(exchange);
throwable = exchange.getAttachment(ExceptionHandler.THROWABLE);
Assert.assertTrue(throwable instanceof IllegalArgumentException);
}
});
TestHttpClient client = new TestHttpClient();
try {
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/");
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Assert.assertEquals("exception handled", HttpClientUtils.readResponse(result));
} finally {
client.getConnectionManager().shutdown();
}
}
use of io.undertow.server.HttpHandler in project undertow by undertow-io.
the class ExceptionHandlerTestCase method testReThrowUnmatchedException.
@Test
public void testReThrowUnmatchedException() throws IOException {
HttpHandler pathHandler = Handlers.path().addExactPath("/", new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
throw new IllegalArgumentException();
}
});
// intentionally not adding any exception handlers
final HttpHandler exceptionHandler = Handlers.exceptionHandler(pathHandler);
DefaultServer.setRootHandler(exceptionHandler);
TestHttpClient client = new TestHttpClient();
try {
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/");
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.INTERNAL_SERVER_ERROR, result.getStatusLine().getStatusCode());
HttpClientUtils.readResponse(result);
} finally {
client.getConnectionManager().shutdown();
}
}
use of io.undertow.server.HttpHandler in project undertow by undertow-io.
the class FixedLengthRequestTestCase method setup.
@BeforeClass
public static void setup() {
final BlockingHandler blockingHandler = new BlockingHandler();
DefaultServer.setRootHandler(blockingHandler);
blockingHandler.setRootHandler(new HttpHandler() {
@Override
public void handleRequest(final HttpServerExchange exchange) {
try {
if (connection == null) {
connection = exchange.getConnection();
} else if (!DefaultServer.isAjp() && !DefaultServer.isProxy() && connection != exchange.getConnection()) {
exchange.setStatusCode(StatusCodes.INTERNAL_SERVER_ERROR);
final OutputStream outputStream = exchange.getOutputStream();
outputStream.write("Connection not persistent".getBytes());
outputStream.close();
return;
}
final OutputStream outputStream = exchange.getOutputStream();
final InputStream inputStream = exchange.getInputStream();
String m = HttpClientUtils.readResponse(inputStream);
Assert.assertEquals(message, m);
inputStream.close();
outputStream.close();
} catch (IOException e) {
exchange.getResponseHeaders().put(Headers.CONNECTION, "close");
exchange.setStatusCode(StatusCodes.INTERNAL_SERVER_ERROR);
throw new RuntimeException(e);
}
}
});
}
use of io.undertow.server.HttpHandler in project undertow by undertow-io.
the class GracefulShutdownTestCase method setup.
@BeforeClass
public static void setup() {
shutdown = Handlers.gracefulShutdown(new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
final CountDownLatch countDownLatch = latch2.get();
final CountDownLatch latch = latch1.get();
if (latch != null) {
latch.countDown();
}
if (countDownLatch != null) {
countDownLatch.await();
}
}
});
DefaultServer.setRootHandler(shutdown);
}
Aggregations