Search in sources :

Example 16 with StringWriteChannelListener

use of io.undertow.util.StringWriteChannelListener in project undertow by undertow-io.

the class H2CUpgradeResetTestCase method createClientCallback.

/**
 * Create the callback to receive the response and assign it to the list.
 * @param responses The list where the response will be added
 * @param latch The latch to count down when the response is received
 * @param message The message to send if it's a POST message (if null nothing is send)
 * @param boolean reset if true a RST is sent for the received the frame ID after completed
 * @return The created callback
 */
private static ClientCallback<ClientExchange> createClientCallback(final List<ClientResponse> responses, final CountDownLatch latch, String message, boolean reset) {
    return new ClientCallback<ClientExchange>() {

        @Override
        public void completed(ClientExchange result) {
            if (message != null) {
                new StringWriteChannelListener(message).setup(result.getRequestChannel());
            }
            result.setResponseListener(new ClientCallback<ClientExchange>() {

                @Override
                public void completed(final ClientExchange result) {
                    responses.add(result.getResponse());
                    new StringReadChannelListener(result.getConnection().getBufferPool()) {

                        @Override
                        protected void stringDone(String string) {
                            result.getResponse().putAttachment(RESPONSE_BODY, string);
                            if (reset) {
                                Http2StreamSourceChannel res = (Http2StreamSourceChannel) result.getResponseChannel();
                                res.getHttp2Channel().sendRstStream(res.getStreamId(), Http2Channel.ERROR_STREAM_CLOSED);
                            }
                            latch.countDown();
                        }

                        @Override
                        protected void error(IOException e) {
                            e.printStackTrace();
                            latch.countDown();
                        }
                    }.setup(result.getResponseChannel());
                }

                @Override
                public void failed(IOException e) {
                    e.printStackTrace();
                    latch.countDown();
                }
            });
        }

        @Override
        public void failed(IOException e) {
            e.printStackTrace();
            latch.countDown();
        }
    };
}
Also used : ClientExchange(io.undertow.client.ClientExchange) ClientCallback(io.undertow.client.ClientCallback) StringReadChannelListener(io.undertow.util.StringReadChannelListener) StringWriteChannelListener(io.undertow.util.StringWriteChannelListener) IOException(java.io.IOException) Http2StreamSourceChannel(io.undertow.protocols.http2.Http2StreamSourceChannel)

Example 17 with StringWriteChannelListener

use of io.undertow.util.StringWriteChannelListener in project undertow by undertow-io.

the class ChunkedResponseTrailersTestCase 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;
                }
                HeaderMap trailers = new HeaderMap();
                exchange.putAttachment(HttpAttachments.RESPONSE_TRAILERS, trailers);
                trailers.put(HttpString.tryFromString("foo"), "fooVal");
                trailers.put(HttpString.tryFromString("bar"), "barVal");
                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) HeaderMap(io.undertow.util.HeaderMap) OutputStream(java.io.OutputStream) StringWriteChannelListener(io.undertow.util.StringWriteChannelListener) IOException(java.io.IOException) BeforeClass(org.junit.BeforeClass)

Example 18 with StringWriteChannelListener

use of io.undertow.util.StringWriteChannelListener in project undertow by undertow-io.

the class ReadTimeoutTestCase method testReadTimeout.

@Test
public void testReadTimeout() throws InterruptedException, IOException {
    final CountDownLatch errorLatch = new CountDownLatch(1);
    DefaultServer.setRootHandler((final HttpServerExchange exchange) -> {
        final StreamSinkChannel response = exchange.getResponseChannel();
        final StreamSourceChannel request = exchange.getRequestChannel();
        request.getReadSetter().set(ChannelListeners.drainListener(Long.MAX_VALUE, (final Channel channel) -> {
            new StringWriteChannelListener("COMPLETED") {

                @Override
                protected void writeDone(final StreamSinkChannel channel) {
                    exchange.endExchange();
                }
            }.setup(response);
        }, (final StreamSourceChannel channel, final IOException e) -> {
            e.printStackTrace();
            exchange.endExchange();
            exception = e;
            errorLatch.countDown();
        }));
        request.wakeupReads();
    });
    final TestHttpClient client = new TestHttpClient();
    try {
        HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL());
        post.setEntity(new AbstractHttpEntity() {

            @Override
            public InputStream getContent() throws IllegalStateException {
                return null;
            }

            @Override
            public void writeTo(final OutputStream outstream) throws IOException {
                for (int i = 0; i < 5; ++i) {
                    outstream.write('*');
                    outstream.flush();
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }

            @Override
            public boolean isStreaming() {
                return true;
            }

            @Override
            public boolean isRepeatable() {
                return false;
            }

            @Override
            public long getContentLength() {
                return 5;
            }
        });
        post.addHeader(Headers.CONNECTION_STRING, "close");
        boolean socketFailure = false;
        try {
            client.execute(post);
        } catch (SocketException e) {
            Assert.assertTrue(e.getMessage(), e.getMessage().contains("Broken pipe") || e.getMessage().contains("connection abort"));
            socketFailure = true;
        }
        Assert.assertTrue("Test sent request without any exception", socketFailure);
        if (errorLatch.await(5, TimeUnit.SECONDS)) {
            Assert.assertTrue(getExceptionDescription(exception), exception instanceof ReadTimeoutException || (DefaultServer.isProxy() && exception instanceof IOException));
            if (exception.getSuppressed() != null && exception.getSuppressed().length > 0) {
                for (Throwable supressed : exception.getSuppressed()) {
                    Assert.assertEquals(getExceptionDescription(supressed), ReadTimeoutException.class, exception.getClass());
                }
            }
        } else if (!DefaultServer.isProxy()) {
            // ignore if proxy, because when we're on proxy, we might not be able to see the exception
            Assert.fail("Did not get ReadTimeoutException");
        }
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : StreamSourceChannel(org.xnio.channels.StreamSourceChannel) HttpPost(org.apache.http.client.methods.HttpPost) SocketException(java.net.SocketException) InputStream(java.io.InputStream) ReadTimeoutException(org.xnio.channels.ReadTimeoutException) StreamSourceChannel(org.xnio.channels.StreamSourceChannel) StreamSinkChannel(org.xnio.channels.StreamSinkChannel) Channel(java.nio.channels.Channel) OutputStream(java.io.OutputStream) StreamSinkChannel(org.xnio.channels.StreamSinkChannel) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) TestHttpClient(io.undertow.testutils.TestHttpClient) StringWriteChannelListener(io.undertow.util.StringWriteChannelListener) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) Test(org.junit.Test)

Example 19 with StringWriteChannelListener

use of io.undertow.util.StringWriteChannelListener 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 20 with StringWriteChannelListener

use of io.undertow.util.StringWriteChannelListener in project undertow by undertow-io.

the class PreChunkedResponseTransferCodingTestCase 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;
                }
                exchange.getResponseHeaders().put(Headers.TRANSFER_ENCODING, Headers.CHUNKED.toString());
                exchange.putAttachment(HttpAttachments.PRE_CHUNKED_RESPONSE, true);
                new StringWriteChannelListener(chunkedMessage).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)

Aggregations

StringWriteChannelListener (io.undertow.util.StringWriteChannelListener)20 IOException (java.io.IOException)19 CountDownLatch (java.util.concurrent.CountDownLatch)13 Test (org.junit.Test)11 URI (java.net.URI)9 AtomicReference (java.util.concurrent.atomic.AtomicReference)9 AbstractReceiveListener (io.undertow.websockets.core.AbstractReceiveListener)8 BufferedTextMessage (io.undertow.websockets.core.BufferedTextMessage)8 StreamSinkFrameChannel (io.undertow.websockets.core.StreamSinkFrameChannel)8 WebSocketChannel (io.undertow.websockets.core.WebSocketChannel)8 StringReadChannelListener (io.undertow.util.StringReadChannelListener)6 ClientCallback (io.undertow.client.ClientCallback)5 ClientExchange (io.undertow.client.ClientExchange)5 UndertowXnioSsl (io.undertow.protocols.ssl.UndertowXnioSsl)4 OutputStream (java.io.OutputStream)4 ClientConnection (io.undertow.client.ClientConnection)3 ClientRequest (io.undertow.client.ClientRequest)3 UndertowClient (io.undertow.client.UndertowClient)3 HttpHandler (io.undertow.server.HttpHandler)3 HttpServerExchange (io.undertow.server.HttpServerExchange)3