Search in sources :

Example 16 with ChannelListener

use of org.xnio.ChannelListener in project undertow by undertow-io.

the class ServerSentEventHandler method handleRequest.

@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
    exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/event-stream; charset=UTF-8");
    exchange.setPersistent(false);
    final StreamSinkChannel sink = exchange.getResponseChannel();
    if (!sink.flush()) {
        sink.getWriteSetter().set(ChannelListeners.flushingChannelListener(new ChannelListener<StreamSinkChannel>() {

            @Override
            public void handleEvent(StreamSinkChannel channel) {
                handleConnect(channel, exchange);
            }
        }, new ChannelExceptionHandler<StreamSinkChannel>() {

            @Override
            public void handleException(StreamSinkChannel channel, IOException exception) {
                IoUtils.safeClose(exchange.getConnection());
            }
        }));
        sink.resumeWrites();
    } else {
        exchange.dispatch(exchange.getIoThread(), new Runnable() {

            @Override
            public void run() {
                handleConnect(sink, exchange);
            }
        });
    }
}
Also used : ChannelListener(org.xnio.ChannelListener) StreamSinkChannel(org.xnio.channels.StreamSinkChannel) ChannelExceptionHandler(org.xnio.ChannelExceptionHandler) IOException(java.io.IOException)

Example 17 with ChannelListener

use of org.xnio.ChannelListener in project undertow by undertow-io.

the class HttpContinue method internalSendContinueResponse.

private static void internalSendContinueResponse(final HttpServerExchange exchange, final IoCallback callback) {
    if (exchange.getAttachment(ALREADY_SENT) != null) {
        callback.onComplete(exchange, null);
        return;
    }
    HttpServerExchange newExchange = exchange.getConnection().sendOutOfBandResponse(exchange);
    exchange.putAttachment(ALREADY_SENT, true);
    newExchange.setStatusCode(StatusCodes.CONTINUE);
    newExchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, 0);
    final StreamSinkChannel responseChannel = newExchange.getResponseChannel();
    try {
        responseChannel.shutdownWrites();
        if (!responseChannel.flush()) {
            responseChannel.getWriteSetter().set(ChannelListeners.flushingChannelListener(new ChannelListener<StreamSinkChannel>() {

                @Override
                public void handleEvent(StreamSinkChannel channel) {
                    channel.suspendWrites();
                    exchange.dispatch(new HttpHandler() {

                        @Override
                        public void handleRequest(HttpServerExchange exchange) throws Exception {
                            callback.onComplete(exchange, null);
                        }
                    });
                }
            }, new ChannelExceptionHandler<Channel>() {

                @Override
                public void handleException(Channel channel, final IOException e) {
                    exchange.dispatch(new HttpHandler() {

                        @Override
                        public void handleRequest(HttpServerExchange exchange) throws Exception {
                            callback.onException(exchange, null, e);
                        }
                    });
                }
            }));
            responseChannel.resumeWrites();
            exchange.dispatch();
        } else {
            callback.onComplete(exchange, null);
        }
    } catch (IOException e) {
        callback.onException(exchange, null, e);
    }
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) ChannelListener(org.xnio.ChannelListener) StreamSinkChannel(org.xnio.channels.StreamSinkChannel) Channel(java.nio.channels.Channel) StreamSinkChannel(org.xnio.channels.StreamSinkChannel) ChannelExceptionHandler(org.xnio.ChannelExceptionHandler) IOException(java.io.IOException) IOException(java.io.IOException)

Example 18 with ChannelListener

use of org.xnio.ChannelListener in project undertow by undertow-io.

the class BufferedTextMessage method read.

public void read(final StreamSourceFrameChannel channel, final WebSocketCallback<BufferedTextMessage> callback) {
    PooledByteBuffer pooled = channel.getWebSocketChannel().getBufferPool().allocate();
    final ByteBuffer buffer = pooled.getBuffer();
    try {
        try {
            for (; ; ) {
                int res = channel.read(buffer);
                if (res == -1) {
                    this.complete = true;
                    buffer.flip();
                    data.write(buffer);
                    callback.complete(channel.getWebSocketChannel(), this);
                    return;
                } else if (res == 0) {
                    buffer.flip();
                    if (buffer.hasRemaining()) {
                        data.write(buffer);
                        if (!bufferFullMessage) {
                            callback.complete(channel.getWebSocketChannel(), this);
                        }
                    }
                    channel.getReadSetter().set(new ChannelListener<StreamSourceFrameChannel>() {

                        @Override
                        public void handleEvent(StreamSourceFrameChannel channel) {
                            if (complete) {
                                return;
                            }
                            PooledByteBuffer pooled = channel.getWebSocketChannel().getBufferPool().allocate();
                            final ByteBuffer buffer = pooled.getBuffer();
                            try {
                                try {
                                    for (; ; ) {
                                        int res = channel.read(buffer);
                                        if (res == -1) {
                                            checkMaxSize(channel, res);
                                            buffer.flip();
                                            data.write(buffer);
                                            complete = true;
                                            callback.complete(channel.getWebSocketChannel(), BufferedTextMessage.this);
                                            return;
                                        } else if (res == 0) {
                                            buffer.flip();
                                            if (buffer.hasRemaining()) {
                                                data.write(buffer);
                                                if (!bufferFullMessage) {
                                                    callback.complete(channel.getWebSocketChannel(), BufferedTextMessage.this);
                                                }
                                            }
                                            return;
                                        }
                                        if (!buffer.hasRemaining()) {
                                            buffer.flip();
                                            data.write(buffer);
                                            buffer.clear();
                                            if (!bufferFullMessage) {
                                                callback.complete(channel.getWebSocketChannel(), BufferedTextMessage.this);
                                            }
                                        }
                                    }
                                } catch (IOException e) {
                                    callback.onError(channel.getWebSocketChannel(), BufferedTextMessage.this, e);
                                }
                            } finally {
                                pooled.close();
                            }
                        }
                    });
                    channel.resumeReads();
                    return;
                }
                checkMaxSize(channel, res);
                if (!buffer.hasRemaining()) {
                    buffer.flip();
                    data.write(buffer);
                    buffer.clear();
                    if (!bufferFullMessage) {
                        callback.complete(channel.getWebSocketChannel(), this);
                    }
                }
            }
        } catch (IOException e) {
            callback.onError(channel.getWebSocketChannel(), this, e);
        }
    } finally {
        pooled.close();
    }
}
Also used : ChannelListener(org.xnio.ChannelListener) PooledByteBuffer(io.undertow.connector.PooledByteBuffer) IOException(java.io.IOException) PooledByteBuffer(io.undertow.connector.PooledByteBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 19 with ChannelListener

use of org.xnio.ChannelListener in project undertow by undertow-io.

the class WriteTimeoutTestCase method testWriteTimeout.

@Test
public void testWriteTimeout() throws IOException, InterruptedException {
    DefaultServer.setRootHandler(new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) throws Exception {
            final StreamSinkChannel response = exchange.getResponseChannel();
            try {
                response.setOption(Options.WRITE_TIMEOUT, 10);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            //1mb
            final int capacity = 1 * 1024 * 1024;
            final ByteBuffer originalBuffer = ByteBuffer.allocateDirect(capacity);
            for (int i = 0; i < capacity; ++i) {
                originalBuffer.put((byte) '*');
            }
            originalBuffer.flip();
            response.getWriteSetter().set(new ChannelListener<Channel>() {

                private ByteBuffer buffer = originalBuffer.duplicate();

                int count = 0;

                @Override
                public void handleEvent(final Channel channel) {
                    do {
                        try {
                            int res = response.write(buffer);
                            if (res == 0) {
                                return;
                            }
                        } catch (IOException e) {
                            exception = e;
                            errorLatch.countDown();
                        }
                        if (!buffer.hasRemaining()) {
                            count++;
                            buffer = originalBuffer.duplicate();
                        }
                    } while (count < 1000);
                    exchange.endExchange();
                }
            });
            response.wakeupWrites();
        }
    });
    final TestHttpClient client = new TestHttpClient();
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL());
        try {
            HttpResponse result = client.execute(get);
            InputStream content = result.getEntity().getContent();
            byte[] buffer = new byte[512];
            int r = 0;
            while ((r = content.read(buffer)) > 0) {
                Thread.sleep(200);
                if (exception != null) {
                    Assert.assertEquals(WriteTimeoutException.class, exception.getClass());
                    return;
                }
            }
            Assert.fail("Write did not time out");
        } catch (IOException e) {
            if (errorLatch.await(5, TimeUnit.SECONDS)) {
                Assert.assertEquals(WriteTimeoutException.class, exception.getClass());
            } else {
                Assert.fail("Write did not time out");
            }
        }
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : ChannelListener(org.xnio.ChannelListener) InputStream(java.io.InputStream) StreamSinkChannel(org.xnio.channels.StreamSinkChannel) Channel(java.nio.channels.Channel) HttpGet(org.apache.http.client.methods.HttpGet) StreamSinkChannel(org.xnio.channels.StreamSinkChannel) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) WriteTimeoutException(org.xnio.channels.WriteTimeoutException) IOException(java.io.IOException) TestHttpClient(io.undertow.testutils.TestHttpClient) WriteTimeoutException(org.xnio.channels.WriteTimeoutException) Test(org.junit.Test)

Example 20 with ChannelListener

use of org.xnio.ChannelListener in project undertow by undertow-io.

the class DefaultServer method wrapOpenListener.

private static ChannelListener<StreamConnection> wrapOpenListener(final ChannelListener<StreamConnection> listener) {
    if (!single) {
        return listener;
    }
    return new ChannelListener<StreamConnection>() {

        @Override
        public void handleEvent(StreamConnection channel) {
            channel.getSinkChannel().setConduit(new SingleByteStreamSinkConduit(channel.getSinkChannel().getConduit(), 10000));
            channel.getSourceChannel().setConduit(new SingleByteStreamSourceConduit(channel.getSourceChannel().getConduit(), 10000));
            listener.handleEvent(channel);
        }
    };
}
Also used : SingleByteStreamSinkConduit(io.undertow.util.SingleByteStreamSinkConduit) ChannelListener(org.xnio.ChannelListener) StreamConnection(org.xnio.StreamConnection) SingleByteStreamSourceConduit(io.undertow.util.SingleByteStreamSourceConduit)

Aggregations

ChannelListener (org.xnio.ChannelListener)23 IOException (java.io.IOException)16 InetSocketAddress (java.net.InetSocketAddress)8 OptionMap (org.xnio.OptionMap)7 DefaultByteBufferPool (io.undertow.server.DefaultByteBufferPool)6 HttpOpenListener (io.undertow.server.protocol.http.HttpOpenListener)6 ByteBuffer (java.nio.ByteBuffer)6 StreamConnection (org.xnio.StreamConnection)6 StreamSinkChannel (org.xnio.channels.StreamSinkChannel)6 PooledByteBuffer (io.undertow.connector.PooledByteBuffer)5 HttpHandler (io.undertow.server.HttpHandler)4 HttpServerExchange (io.undertow.server.HttpServerExchange)4 ChannelExceptionHandler (org.xnio.ChannelExceptionHandler)4 StreamSourceChannel (org.xnio.channels.StreamSourceChannel)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Channel (java.nio.channels.Channel)3 UndertowXnioSsl (io.undertow.protocols.ssl.UndertowXnioSsl)2 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)2 DeploymentManager (io.undertow.servlet.api.DeploymentManager)2 FilterInfo (io.undertow.servlet.api.FilterInfo)2