Search in sources :

Example 1 with Receiver

use of io.undertow.io.Receiver 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)1 Receiver (io.undertow.io.Receiver)1 Sender (io.undertow.io.Sender)1 HttpHandler (io.undertow.server.HttpHandler)1 HttpServerExchange (io.undertow.server.HttpServerExchange)1 IOException (java.io.IOException)1 Deque (java.util.Deque)1 LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)1 BeforeClass (org.junit.BeforeClass)1