Search in sources :

Example 71 with HttpHandler

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);
            }
        }
    });
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) OutputStream(java.io.OutputStream) StringWriteChannelListener(io.undertow.util.StringWriteChannelListener) IOException(java.io.IOException) BeforeClass(org.junit.BeforeClass)

Example 72 with HttpHandler

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();
    }
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 73 with HttpHandler

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();
    }
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 74 with HttpHandler

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);
            }
        }
    });
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) BeforeClass(org.junit.BeforeClass)

Example 75 with HttpHandler

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);
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) CountDownLatch(java.util.concurrent.CountDownLatch) BeforeClass(org.junit.BeforeClass)

Aggregations

HttpHandler (io.undertow.server.HttpHandler)126 HttpServerExchange (io.undertow.server.HttpServerExchange)72 IOException (java.io.IOException)54 BeforeClass (org.junit.BeforeClass)35 Test (org.junit.Test)25 TestHttpClient (io.undertow.testutils.TestHttpClient)20 HttpResponse (org.apache.http.HttpResponse)20 HttpGet (org.apache.http.client.methods.HttpGet)19 PathHandler (io.undertow.server.handlers.PathHandler)17 HttpString (io.undertow.util.HttpString)15 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)13 Undertow (io.undertow.Undertow)12 ArrayList (java.util.ArrayList)11 HandlerWrapper (io.undertow.server.HandlerWrapper)9 InMemorySessionManager (io.undertow.server.session.InMemorySessionManager)9 SessionAttachmentHandler (io.undertow.server.session.SessionAttachmentHandler)9 Header (org.apache.http.Header)9 SessionCookieConfig (io.undertow.server.session.SessionCookieConfig)8 AuthenticationMechanism (io.undertow.security.api.AuthenticationMechanism)7 AuthenticationCallHandler (io.undertow.security.handlers.AuthenticationCallHandler)7