Search in sources :

Example 6 with ConduitStreamSourceChannel

use of org.xnio.conduits.ConduitStreamSourceChannel in project undertow by undertow-io.

the class ConnectionUtils method doDrain.

private static void doDrain(final StreamConnection connection, final Closeable... additional) {
    if (!connection.getSourceChannel().isOpen()) {
        IoUtils.safeClose(connection);
        IoUtils.safeClose(additional);
        return;
    }
    final ByteBuffer b = ByteBuffer.allocate(1);
    try {
        int res = connection.getSourceChannel().read(b);
        b.clear();
        if (res == 0) {
            final XnioExecutor.Key key = WorkerUtils.executeAfter(connection.getIoThread(), new Runnable() {

                @Override
                public void run() {
                    IoUtils.safeClose(connection);
                    IoUtils.safeClose(additional);
                }
            }, MAX_DRAIN_TIME, TimeUnit.MILLISECONDS);
            connection.getSourceChannel().setReadListener(new ChannelListener<ConduitStreamSourceChannel>() {

                @Override
                public void handleEvent(ConduitStreamSourceChannel channel) {
                    try {
                        int res = channel.read(b);
                        if (res != 0) {
                            IoUtils.safeClose(connection);
                            IoUtils.safeClose(additional);
                            key.remove();
                        }
                    } catch (Exception e) {
                        if (e instanceof IOException) {
                            UndertowLogger.REQUEST_IO_LOGGER.ioException((IOException) e);
                        } else {
                            UndertowLogger.REQUEST_IO_LOGGER.ioException(new IOException(e));
                        }
                        IoUtils.safeClose(connection);
                        IoUtils.safeClose(additional);
                        key.remove();
                    }
                }
            });
            connection.getSourceChannel().resumeReads();
        } else {
            IoUtils.safeClose(connection);
            IoUtils.safeClose(additional);
        }
    } catch (Exception e) {
        if (e instanceof IOException) {
            UndertowLogger.REQUEST_IO_LOGGER.ioException((IOException) e);
        } else {
            UndertowLogger.REQUEST_IO_LOGGER.ioException(new IOException(e));
        }
        IoUtils.safeClose(connection);
        IoUtils.safeClose(additional);
    }
}
Also used : IOException(java.io.IOException) ConduitStreamSourceChannel(org.xnio.conduits.ConduitStreamSourceChannel) ByteBuffer(java.nio.ByteBuffer) XnioExecutor(org.xnio.XnioExecutor) IOException(java.io.IOException)

Example 7 with ConduitStreamSourceChannel

use of org.xnio.conduits.ConduitStreamSourceChannel in project indy by Commonjava.

the class ProxyAcceptHandler method handleEvent.

@Override
public void handleEvent(AcceptingChannel<StreamConnection> channel) {
    final Logger logger = LoggerFactory.getLogger(getClass());
    StreamConnection accepted;
    try {
        accepted = channel.accept();
    } catch (IOException e) {
        logger.error("Failed to addMetadata httprox connection: " + e.getMessage(), e);
        return;
    }
    if (accepted == null) {
        return;
    }
    logger.debug("accepted {}", accepted.getPeerAddress());
    final ConduitStreamSourceChannel source = accepted.getSourceChannel();
    final ConduitStreamSinkChannel sink = accepted.getSinkChannel();
    final ProxyRepositoryCreator creator = createRepoCreator();
    final ProxyResponseWriter writer = new ProxyResponseWriter(config, storeManager, contentController, proxyAuthenticator, cacheProvider, creator);
    logger.debug("Setting writer: {}", writer);
    sink.getWriteSetter().set(writer);
    final ProxyRequestReader reader = new ProxyRequestReader(writer, sink);
    logger.debug("Setting reader: {}", reader);
    source.getReadSetter().set(reader);
    source.resumeReads();
}
Also used : IOException(java.io.IOException) Logger(org.slf4j.Logger) StreamConnection(org.xnio.StreamConnection) ConduitStreamSourceChannel(org.xnio.conduits.ConduitStreamSourceChannel) ConduitStreamSinkChannel(org.xnio.conduits.ConduitStreamSinkChannel)

Example 8 with ConduitStreamSourceChannel

use of org.xnio.conduits.ConduitStreamSourceChannel in project undertow by undertow-io.

the class AjpReadListener method exchangeComplete.

public void exchangeComplete(final HttpServerExchange exchange) {
    if (!exchange.isUpgrade() && exchange.isPersistent()) {
        startRequest();
        ConduitStreamSourceChannel channel = ((AjpServerConnection) exchange.getConnection()).getChannel().getSourceChannel();
        channel.getReadSetter().set(this);
        channel.wakeupReads();
    } else if (!exchange.isPersistent()) {
        safeClose(exchange.getConnection());
    }
}
Also used : ConduitStreamSourceChannel(org.xnio.conduits.ConduitStreamSourceChannel)

Aggregations

ConduitStreamSourceChannel (org.xnio.conduits.ConduitStreamSourceChannel)8 IOException (java.io.IOException)4 HttpString (io.undertow.util.HttpString)3 ByteBuffer (java.nio.ByteBuffer)2 ConduitStreamSinkChannel (org.xnio.conduits.ConduitStreamSinkChannel)2 ClientRequest (io.undertow.client.ClientRequest)1 BytesSentStreamSinkConduit (io.undertow.conduits.BytesSentStreamSinkConduit)1 ChunkedStreamSinkConduit (io.undertow.conduits.ChunkedStreamSinkConduit)1 ChunkedStreamSourceConduit (io.undertow.conduits.ChunkedStreamSourceConduit)1 EmptyStreamSourceConduit (io.undertow.conduits.EmptyStreamSourceConduit)1 PooledByteBuffer (io.undertow.connector.PooledByteBuffer)1 HeaderMap (io.undertow.util.HeaderMap)1 Logger (org.slf4j.Logger)1 ChannelExceptionHandler (org.xnio.ChannelExceptionHandler)1 StreamConnection (org.xnio.StreamConnection)1 XnioExecutor (org.xnio.XnioExecutor)1 StreamSourceChannel (org.xnio.channels.StreamSourceChannel)1 StreamSinkConduit (org.xnio.conduits.StreamSinkConduit)1 StreamSourceConduit (org.xnio.conduits.StreamSourceConduit)1