Search in sources :

Example 36 with HttpServerExchange

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

the class FixedLengthResponseTestCase method setup.

@BeforeClass
public static void setup() {
    DefaultServer.setRootHandler(new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) throws Exception {
            if (connection == null) {
                connection = exchange.getConnection();
            } else if (!DefaultServer.isAjp() && !DefaultServer.isProxy() && connection != exchange.getConnection()) {
                Sender sender = exchange.getResponseSender();
                sender.send("Connection not persistent");
                return;
            }
            exchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, message.length() + "");
            final Sender sender = exchange.getResponseSender();
            sender.send(message);
        }
    });
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) Sender(io.undertow.io.Sender) HttpHandler(io.undertow.server.HttpHandler) IOException(java.io.IOException) BeforeClass(org.junit.BeforeClass)

Example 37 with HttpServerExchange

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

the class HttpContinueConduitWrappingHandlerBufferLeakTestCase method setup.

@BeforeClass
public static void setup() {
    final BlockingHandler blockingHandler = new BlockingHandler();
    final HttpContinueReadHandler handler = new HttpContinueReadHandler(blockingHandler);
    DefaultServer.setRootHandler(handler);
    blockingHandler.setRootHandler(new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) {
            try {
                if (exchange.getQueryParameters().containsKey("reject")) {
                    exchange.getRequestChannel();
                    exchange.setStatusCode(StatusCodes.EXPECTATION_FAILED);
                    exchange.getOutputStream().close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    });
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) IOException(java.io.IOException) BeforeClass(org.junit.BeforeClass)

Example 38 with HttpServerExchange

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

the class HttpContinueConduitWrappingHandlerTestCase method setup.

@BeforeClass
public static void setup() {
    final BlockingHandler blockingHandler = new BlockingHandler();
    final HttpContinueReadHandler handler = new HttpContinueReadHandler(blockingHandler);
    DefaultServer.setRootHandler(handler);
    blockingHandler.setRootHandler(new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) {
            try {
                if (!accept) {
                    HttpContinue.rejectExchange(exchange);
                    return;
                }
                byte[] buffer = new byte[1024];
                final ByteArrayOutputStream b = new ByteArrayOutputStream();
                int r = 0;
                final OutputStream outputStream = exchange.getOutputStream();
                final InputStream inputStream = exchange.getInputStream();
                while ((r = inputStream.read(buffer)) > 0) {
                    b.write(buffer, 0, r);
                }
                outputStream.write(b.toByteArray());
                outputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    });
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BeforeClass(org.junit.BeforeClass)

Example 39 with HttpServerExchange

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

the class LotsOfHeadersRequestTestCase method setup.

@BeforeClass
public static void setup() {
    Assume.assumeFalse(DefaultServer.isH2upgrade());
    final BlockingHandler blockingHandler = new BlockingHandler();
    DefaultServer.setRootHandler(blockingHandler);
    blockingHandler.setRootHandler(new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) {
            HeaderMap headers = exchange.getRequestHeaders();
            for (HeaderValues header : headers) {
                for (String val : header) {
                    exchange.getResponseHeaders().put(HttpString.tryFromString(header.getHeaderName().toString()), val);
                }
            }
        }
    });
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) HeaderMap(io.undertow.util.HeaderMap) HeaderValues(io.undertow.util.HeaderValues) HttpString(io.undertow.util.HttpString) BeforeClass(org.junit.BeforeClass)

Example 40 with HttpServerExchange

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

the class AsyncWebSocketHttpServerExchange method sendData.

@Override
public IoFuture<Void> sendData(final ByteBuffer data) {
    if (sender == null) {
        this.sender = exchange.getResponseSender();
    }
    final FutureResult<Void> future = new FutureResult<>();
    sender.send(data, new IoCallback() {

        @Override
        public void onComplete(final HttpServerExchange exchange, final Sender sender) {
            future.setResult(null);
        }

        @Override
        public void onException(final HttpServerExchange exchange, final Sender sender, final IOException exception) {
            UndertowLogger.REQUEST_IO_LOGGER.ioException(exception);
            future.setException(exception);
        }
    });
    return future.getIoFuture();
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) Sender(io.undertow.io.Sender) FutureResult(org.xnio.FutureResult) IoCallback(io.undertow.io.IoCallback) IOException(java.io.IOException)

Aggregations

HttpServerExchange (io.undertow.server.HttpServerExchange)270 HttpHandler (io.undertow.server.HttpHandler)126 Test (org.junit.Test)109 IOException (java.io.IOException)87 UnitTest (io.undertow.testutils.category.UnitTest)45 BeforeClass (org.junit.BeforeClass)43 TestHttpClient (io.undertow.testutils.TestHttpClient)42 HttpGet (org.apache.http.client.methods.HttpGet)40 HttpResponse (org.apache.http.HttpResponse)37 HttpString (io.undertow.util.HttpString)36 Header (org.apache.http.Header)24 Undertow (io.undertow.Undertow)19 ByteBuffer (java.nio.ByteBuffer)19 Map (java.util.Map)16 SessionConfig (io.undertow.server.session.SessionConfig)15 Sender (io.undertow.io.Sender)14 ExchangeCompletionListener (io.undertow.server.ExchangeCompletionListener)14 HeaderMap (io.undertow.util.HeaderMap)13 HeaderValues (io.undertow.util.HeaderValues)12 URI (java.net.URI)12