Search in sources :

Example 6 with IoCallback

use of io.undertow.io.IoCallback in project undertow by undertow-io.

the class BlockingWriterSenderImpl method invokeOnComplete.

private void invokeOnComplete(final IoCallback callback) {
    inCall = true;
    try {
        callback.onComplete(exchange, this);
    } finally {
        inCall = false;
    }
    while (next != null) {
        String next = this.next;
        IoCallback queuedCallback = this.queuedCallback;
        this.next = null;
        this.queuedCallback = null;
        writer.write(next);
        if (writer.checkError()) {
            queuedCallback.onException(exchange, this, new IOException());
        } else {
            inCall = true;
            try {
                queuedCallback.onComplete(exchange, this);
            } finally {
                inCall = false;
            }
        }
    }
}
Also used : IoCallback(io.undertow.io.IoCallback) IOException(java.io.IOException)

Example 7 with IoCallback

use of io.undertow.io.IoCallback in project undertow by undertow-io.

the class ReceiverTestCase method setup.

@BeforeClass
public static void setup() {
    HttpHandler testFullString = new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) throws Exception {
            exchange.getRequestReceiver().receiveFullString(new Receiver.FullStringCallback() {

                @Override
                public void handle(HttpServerExchange exchange, String message) {
                    exchange.getResponseSender().send(message);
                }
            }, ERROR_CALLBACK);
        }
    };
    HttpHandler testPartialString = new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) throws Exception {
            final StringBuilder sb = new StringBuilder();
            exchange.getRequestReceiver().receivePartialString(new Receiver.PartialStringCallback() {

                @Override
                public void handle(HttpServerExchange exchange, String message, boolean last) {
                    sb.append(message);
                    if (last) {
                        exchange.getResponseSender().send(sb.toString());
                    }
                }
            }, ERROR_CALLBACK);
        }
    };
    HttpHandler testFullBytes = new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) throws Exception {
            exchange.getRequestReceiver().receiveFullBytes(new Receiver.FullBytesCallback() {

                @Override
                public void handle(HttpServerExchange exchange, byte[] message) {
                    exchange.getResponseSender().send(ByteBuffer.wrap(message));
                }
            }, ERROR_CALLBACK);
        }
    };
    HttpHandler testPartialBytes = new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) throws Exception {
            class CB implements Receiver.PartialBytesCallback, IoCallback {

                final Receiver receiver;

                final Sender sender;

                CB(Receiver receiver, Sender sender) {
                    this.receiver = receiver;
                    this.sender = sender;
                }

                @Override
                public void onComplete(HttpServerExchange exchange, Sender sender) {
                    receiver.resume();
                }

                @Override
                public void onException(HttpServerExchange exchange, Sender sender, IOException exception) {
                    exception.printStackTrace();
                    exchange.setStatusCode(StatusCodes.INTERNAL_SERVER_ERROR);
                    exchange.endExchange();
                }

                @Override
                public void handle(HttpServerExchange exchange, byte[] message, boolean last) {
                    receiver.pause();
                    sender.send(ByteBuffer.wrap(message), last ? IoCallback.END_EXCHANGE : this);
                }
            }
            CB callback = new CB(exchange.getRequestReceiver(), exchange.getResponseSender());
            exchange.getRequestReceiver().receivePartialBytes(callback);
        }
    };
    final PathHandler handler = new PathHandler().addPrefixPath("/fullstring", testFullString).addPrefixPath("/partialstring", testPartialString).addPrefixPath("/fullbytes", testFullBytes).addPrefixPath("/partialbytes", testPartialBytes);
    DefaultServer.setRootHandler(new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            Deque<String> block = exchange.getQueryParameters().get("blocking");
            if (block != null) {
                exchange.startBlocking();
                exchange.dispatch(handler);
                return;
            }
            handler.handleRequest(exchange);
        }
    });
}
Also used : HttpHandler(io.undertow.server.HttpHandler) Receiver(io.undertow.io.Receiver) IOException(java.io.IOException) Deque(java.util.Deque) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) IOException(java.io.IOException) HttpServerExchange(io.undertow.server.HttpServerExchange) Sender(io.undertow.io.Sender) IoCallback(io.undertow.io.IoCallback) BeforeClass(org.junit.BeforeClass)

Aggregations

IoCallback (io.undertow.io.IoCallback)7 IOException (java.io.IOException)7 Sender (io.undertow.io.Sender)6 HttpServerExchange (io.undertow.server.HttpServerExchange)6 HttpHandler (io.undertow.server.HttpHandler)3 FileChannel (java.nio.channels.FileChannel)3 BeforeClass (org.junit.BeforeClass)3 ByteBuffer (java.nio.ByteBuffer)2 PooledByteBuffer (io.undertow.connector.PooledByteBuffer)1 Receiver (io.undertow.io.Receiver)1 BlockingHandler (io.undertow.server.handlers.BlockingHandler)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 URI (java.net.URI)1 NoSuchFileException (java.nio.file.NoSuchFileException)1 Path (java.nio.file.Path)1 Deque (java.util.Deque)1 LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)1