Search in sources :

Example 11 with StringWriteChannelListener

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

the class WebSocketExtensionBasicTestCase method testLongMessageWithoutExtensions.

@Test
@Ignore
public void testLongMessageWithoutExtensions() throws Exception {
    XnioWorker client;
    Xnio xnio = Xnio.getInstance(WebSocketExtensionBasicTestCase.class.getClassLoader());
    client = xnio.createWorker(OptionMap.builder().set(Options.WORKER_IO_THREADS, 2).set(Options.CONNECTION_HIGH_WATER, 1000000).set(Options.CONNECTION_LOW_WATER, 1000000).set(Options.WORKER_TASK_CORE_THREADS, 30).set(Options.WORKER_TASK_MAX_THREADS, 30).set(Options.TCP_NODELAY, true).set(Options.CORK, true).getMap());
    WebSocketProtocolHandshakeHandler handler = webSocketDebugHandler().addExtension(new PerMessageDeflateHandshake());
    DebugExtensionsHeaderHandler debug = new DebugExtensionsHeaderHandler(handler);
    DefaultServer.setRootHandler(path().addPrefixPath("/", debug));
    final WebSocketClientNegotiation negotiation = null;
    final WebSocketChannel clientChannel = WebSocketClient.connect(client, DefaultServer.getBufferPool(), OptionMap.EMPTY, new URI("http://localhost:8080"), WebSocketVersion.V13, negotiation).get();
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<String> result = new AtomicReference<>();
    clientChannel.getReceiveSetter().set(new AbstractReceiveListener() {

        @Override
        protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) throws IOException {
            String data = message.getData();
            WebSocketLogger.ROOT_LOGGER.info("onFullTextMessage() - Client - Received: " + data.getBytes().length + " bytes");
            result.set(data);
            latch.countDown();
        }

        @Override
        protected void onFullCloseMessage(WebSocketChannel channel, BufferedBinaryMessage message) throws IOException {
            message.getData().close();
            WebSocketLogger.ROOT_LOGGER.info("onFullCloseMessage");
        }

        @Override
        protected void onError(WebSocketChannel channel, Throwable error) {
            WebSocketLogger.ROOT_LOGGER.info("onError");
            super.onError(channel, error);
            error.printStackTrace();
            latch.countDown();
        }
    });
    clientChannel.resumeReceives();
    int LONG_MSG = 75 * 1024;
    StringBuilder longMsg = new StringBuilder(LONG_MSG);
    for (int i = 0; i < LONG_MSG; i++) {
        longMsg.append(new Integer(i).toString().charAt(0));
    }
    StreamSinkFrameChannel sendChannel = clientChannel.send(WebSocketFrameType.TEXT);
    new StringWriteChannelListener(longMsg.toString()).setup(sendChannel);
    latch.await(10, TimeUnit.SECONDS);
    Assert.assertEquals(longMsg.toString(), result.get());
    clientChannel.sendClose();
    stopWorker(client);
}
Also used : XnioWorker(org.xnio.XnioWorker) WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) StreamSinkFrameChannel(io.undertow.websockets.core.StreamSinkFrameChannel) URI(java.net.URI) BufferedTextMessage(io.undertow.websockets.core.BufferedTextMessage) WebSocketClientNegotiation(io.undertow.websockets.client.WebSocketClientNegotiation) Xnio(org.xnio.Xnio) AbstractReceiveListener(io.undertow.websockets.core.AbstractReceiveListener) WebSocketProtocolHandshakeHandler(io.undertow.websockets.WebSocketProtocolHandshakeHandler) BufferedBinaryMessage(io.undertow.websockets.core.BufferedBinaryMessage) StringWriteChannelListener(io.undertow.util.StringWriteChannelListener) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 12 with StringWriteChannelListener

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

the class HttpReadListener method sendBadRequestAndClose.

private void sendBadRequestAndClose(final StreamConnection connection, final Throwable exception) {
    UndertowLogger.REQUEST_IO_LOGGER.failedToParseRequest(exception);
    connection.getSourceChannel().suspendReads();
    new StringWriteChannelListener(BAD_REQUEST) {

        @Override
        protected void writeDone(final StreamSinkChannel c) {
            super.writeDone(c);
            c.suspendWrites();
            IoUtils.safeClose(connection);
        }

        @Override
        protected void handleError(StreamSinkChannel channel, IOException e) {
            IoUtils.safeClose(connection);
        }
    }.setup(connection.getSinkChannel());
}
Also used : StreamSinkChannel(org.xnio.channels.StreamSinkChannel) ConduitStreamSinkChannel(org.xnio.conduits.ConduitStreamSinkChannel) StringWriteChannelListener(io.undertow.util.StringWriteChannelListener) IOException(java.io.IOException)

Example 13 with StringWriteChannelListener

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

the class H2CUpgradeContinuationTestCase 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)
 * @return The created callback
 */
private static ClientCallback<ClientExchange> createClientCallback(final List<ClientResponse> responses, final CountDownLatch latch, String message) {
    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);
                            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();
                }
            });
            try {
                result.getRequestChannel().shutdownWrites();
                if (!result.getRequestChannel().flush()) {
                    result.getRequestChannel().getWriteSetter().set(ChannelListeners.<StreamSinkChannel>flushingChannelListener(null, null));
                    result.getRequestChannel().resumeWrites();
                }
            } catch (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) HttpString(io.undertow.util.HttpString) IOException(java.io.IOException)

Example 14 with StringWriteChannelListener

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

the class Http2ClientTestCase method testPostRequest.

@Test
public void testPostRequest() throws Exception {
    // 
    final UndertowClient client = createClient();
    final String postMessage = "This is a post request";
    final List<String> responses = new CopyOnWriteArrayList<>();
    final CountDownLatch latch = new CountDownLatch(10);
    final ClientConnection connection = client.connect(ADDRESS, worker, new UndertowXnioSsl(worker.getXnio(), OptionMap.EMPTY, DefaultServer.getClientSSLContext()), DefaultServer.getBufferPool(), OptionMap.create(UndertowOptions.ENABLE_HTTP2, true)).get();
    try {
        connection.getIoThread().execute(new Runnable() {

            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    final ClientRequest request = new ClientRequest().setMethod(Methods.POST).setPath(POST);
                    request.getRequestHeaders().put(Headers.HOST, DefaultServer.getHostAddress());
                    request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
                    connection.sendRequest(request, new ClientCallback<ClientExchange>() {

                        @Override
                        public void completed(ClientExchange result) {
                            new StringWriteChannelListener(postMessage).setup(result.getRequestChannel());
                            result.setResponseListener(new ClientCallback<ClientExchange>() {

                                @Override
                                public void completed(ClientExchange result) {
                                    new StringReadChannelListener(DefaultServer.getBufferPool()) {

                                        @Override
                                        protected void stringDone(String string) {
                                            responses.add(string);
                                            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();
                        }
                    });
                }
            }
        });
        latch.await(10, TimeUnit.SECONDS);
        Assert.assertEquals(10, responses.size());
        for (final String response : responses) {
            Assert.assertEquals(postMessage, response);
        }
    } finally {
        IoUtils.safeClose(connection);
    }
}
Also used : ClientExchange(io.undertow.client.ClientExchange) ClientCallback(io.undertow.client.ClientCallback) StringReadChannelListener(io.undertow.util.StringReadChannelListener) UndertowClient(io.undertow.client.UndertowClient) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ClientConnection(io.undertow.client.ClientConnection) StringWriteChannelListener(io.undertow.util.StringWriteChannelListener) UndertowXnioSsl(io.undertow.protocols.ssl.UndertowXnioSsl) ClientRequest(io.undertow.client.ClientRequest) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.Test)

Example 15 with StringWriteChannelListener

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

the class AjpClientTestCase method testPostRequest.

@Test
public void testPostRequest() throws Exception {
    // 
    final UndertowClient client = createClient();
    final String postMessage = "This is a post request";
    final List<String> responses = new CopyOnWriteArrayList<>();
    final CountDownLatch latch = new CountDownLatch(10);
    final ClientConnection connection = client.connect(ADDRESS, worker, DefaultServer.getBufferPool(), OptionMap.EMPTY).get();
    try {
        connection.getIoThread().execute(() -> {
            for (int i = 0; i < 10; i++) {
                final ClientRequest request = new ClientRequest().setMethod(Methods.POST).setPath(POST);
                request.getRequestHeaders().put(Headers.HOST, DefaultServer.getHostAddress());
                request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
                connection.sendRequest(request, new ClientCallback<ClientExchange>() {

                    @Override
                    public void completed(ClientExchange result) {
                        new StringWriteChannelListener(postMessage).setup(result.getRequestChannel());
                        result.setResponseListener(new ClientCallback<ClientExchange>() {

                            @Override
                            public void completed(ClientExchange result) {
                                new StringReadChannelListener(DefaultServer.getBufferPool()) {

                                    @Override
                                    protected void stringDone(String string) {
                                        responses.add(string);
                                        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();
                    }
                });
            }
        });
        assertTrue(latch.await(10, TimeUnit.SECONDS));
        Assert.assertEquals(10, responses.size());
        for (final String response : responses) {
            Assert.assertEquals(postMessage, response);
        }
    } finally {
        IoUtils.safeClose(connection);
    }
}
Also used : ClientExchange(io.undertow.client.ClientExchange) ClientCallback(io.undertow.client.ClientCallback) StringReadChannelListener(io.undertow.util.StringReadChannelListener) UndertowClient(io.undertow.client.UndertowClient) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ClientConnection(io.undertow.client.ClientConnection) StringWriteChannelListener(io.undertow.util.StringWriteChannelListener) ClientRequest(io.undertow.client.ClientRequest) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.Test)

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