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);
}
});
}
Aggregations