Search in sources :

Example 1 with HttpServerConnection

use of io.undertow.server.protocol.http.HttpServerConnection in project undertow by undertow-io.

the class ChunkedRequestTrailersTestCase method setup.

@BeforeClass
public static void setup() {
    final BlockingHandler blockingHandler = new BlockingHandler();
    existing = DefaultServer.getUndertowOptions();
    DefaultServer.setUndertowOptions(OptionMap.create(UndertowOptions.ALWAYS_SET_DATE, false));
    DefaultServer.setRootHandler(blockingHandler);
    blockingHandler.setRootHandler(new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) {
            try {
                if (connection == null) {
                    connection = (HttpServerConnection) exchange.getConnection();
                } else if (!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("abcdefghi", m);
                HeaderMap headers = exchange.getAttachment(HttpAttachments.REQUEST_TRAILERS);
                for (HeaderValues header : headers) {
                    for (String val : header) {
                        outputStream.write(header.getHeaderName().toString().getBytes());
                        outputStream.write(": ".getBytes());
                        outputStream.write(val.getBytes());
                        outputStream.write("\r\n".getBytes());
                    }
                }
                inputStream.close();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
    });
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) HttpServerConnection(io.undertow.server.protocol.http.HttpServerConnection) HeaderMap(io.undertow.util.HeaderMap) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) HeaderValues(io.undertow.util.HeaderValues) IOException(java.io.IOException) BeforeClass(org.junit.BeforeClass)

Aggregations

HttpHandler (io.undertow.server.HttpHandler)1 HttpServerExchange (io.undertow.server.HttpServerExchange)1 HttpServerConnection (io.undertow.server.protocol.http.HttpServerConnection)1 HeaderMap (io.undertow.util.HeaderMap)1 HeaderValues (io.undertow.util.HeaderValues)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 BeforeClass (org.junit.BeforeClass)1