@Override public int addBodyEndHandler(Handler<Void> handler) { return decoratedContext.addBodyEndHandler(handler); }
@Override public int addBodyEndHandler(Handler<Void> handler) { return inner.addBodyEndHandler(handler); }
@Override public void handle(RoutingContext ctx) { // We send a error response after timeout long tid = ctx.vertx().setTimer(timeout, t -> ctx.fail(errorCode)); ctx.addBodyEndHandler(v -> ctx.vertx().cancelTimer(tid)); ctx.next(); } }
@Test public void testBodyEndHandler() throws Exception { AtomicInteger cnt = new AtomicInteger(); router.route().handler(rc -> { rc.addBodyEndHandler(v -> cnt.incrementAndGet()); rc.next(); }); router.route().handler(rc -> { rc.addBodyEndHandler(v -> cnt.incrementAndGet()); rc.next(); }); router.route().handler(rc -> { rc.addBodyEndHandler(v -> cnt.incrementAndGet()); rc.response().end(); }); testRequest(HttpMethod.GET, "/", 200, "OK"); assertWaitUntil(() -> cnt.get() == 3); }
@Test public void testHeadersEndHandlerCalledBackwards2() throws Exception { final AtomicInteger cnt = new AtomicInteger(0); router.route().handler(rc -> { final int val = cnt.incrementAndGet(); rc.addBodyEndHandler(v -> assertEquals(val, cnt.getAndDecrement())); rc.next(); }); router.route().handler(rc -> { final int val = cnt.incrementAndGet(); rc.addBodyEndHandler(v -> assertEquals(val, cnt.getAndDecrement())); rc.next(); }); router.route().handler(rc -> { final int val = cnt.incrementAndGet(); rc.addBodyEndHandler(v -> assertEquals(val, cnt.getAndDecrement())); rc.response().end(); }); testRequest(HttpMethod.GET, "/", 200, "OK"); }
/** * Provides a handler that will be called after the last part of the body is written to the wire. * The handler is called asynchronously of when the response has been received by the client. * This provides a hook allowing you to do more operations once the request has been sent over the wire. * Do not use this for resource cleanup as this handler might never get called (e.g. if the connection is reset). * @param handler the handler * @return the id of the handler. This can be used if you later want to remove the handler. */ public int addBodyEndHandler(Handler<Void> handler) { int ret = delegate.addBodyEndHandler(handler); return ret; }
/** * Provides a handler that will be called after the last part of the body is written to the wire. * The handler is called asynchronously of when the response has been received by the client. * This provides a hook allowing you to do more operations once the request has been sent over the wire. * Do not use this for resource cleanup as this handler might never get called (e.g. if the connection is reset). * @param handler the handler * @return the id of the handler. This can be used if you later want to remove the handler. */ public int addBodyEndHandler(Handler<Void> handler) { int ret = delegate.addBodyEndHandler(handler); return ret; }
@Override public int addBodyEndHandler(Handler<Void> handler) { return decoratedContext.addBodyEndHandler(handler); }
@Override public int addBodyEndHandler(Handler<Void> handler) { return inner.addBodyEndHandler(handler); }
@Override public void handle(RoutingContext context) { // common logging data long timestamp = System.currentTimeMillis(); String remoteClient = getClientAddress(context.request().remoteAddress()); HttpMethod method = context.request().method(); String uri = context.request().uri(); HttpVersion version = context.request().version(); if (immediate) { log(context, timestamp, remoteClient, version, method, uri); } else { context.addBodyEndHandler(v -> log(context, timestamp, remoteClient, version, method, uri)); } context.next(); } }
@Override public int addBodyEndHandler(Handler<Void> handler) { return rc.addBodyEndHandler(handler); }
@Override public int addBodyEndHandler(Handler<Void> handler) { return delegate.addBodyEndHandler(handler); }
void doEnd() { if (failed) { deleteFileUploads(); return; } if (deleteUploadedFilesOnEnd) { context.addBodyEndHandler(x -> deleteFileUploads()); } HttpServerRequest req = context.request(); if (mergeFormAttributes && req.isExpectMultipart()) { req.params().addAll(req.formAttributes()); } context.setBody(body); context.next(); }
void doEnd() { if (failed) { deleteFileUploads(); return; } if (deleteUploadedFilesOnEnd) { context.addBodyEndHandler(x -> deleteFileUploads()); } HttpServerRequest req = context.request(); if (mergeFormAttributes && req.isExpectMultipart()) { req.params().addAll(req.formAttributes()); } context.setBody(body); context.next(); }
@Test public void testBodyEndHandlerRemoveHandler() throws Exception { AtomicInteger cnt = new AtomicInteger(); router.route().handler(rc -> { rc.addBodyEndHandler(v -> cnt.incrementAndGet()); rc.next(); }); router.route().handler(rc -> { Handler<Void> handler = v -> cnt.incrementAndGet(); int handlerID = rc.addBodyEndHandler(handler); vertx.setTimer(1, tid -> { assertTrue(rc.removeBodyEndHandler(handlerID)); assertFalse(rc.removeBodyEndHandler(handlerID + 1)); rc.response().end(); }); }); testRequest(HttpMethod.GET, "/", 200, "OK"); assertWaitUntil(() -> cnt.get() == 1); }
@Test public void testTimeoutWithCustomBodyEndHandler() throws Exception { long timeout = 500; AtomicBoolean ended = new AtomicBoolean(); router.route().handler(routingContext -> { routingContext.addBodyEndHandler(event -> ended.set(true)); routingContext.next(); }); router.route().handler(TimeoutHandler.create(timeout)); router.route().handler(rc -> { // Don't end it }); testRequest(HttpMethod.GET, "/", 503, "Service Unavailable"); waitUntil(ended::get); }
@Override public void handle(RoutingContext context) { long startTime = System.currentTimeMillis(); context.addBodyEndHandler(event -> log(context, startTime)); context.next(); }
private void timeout(RoutingContext ctx) { long tid = ctx.vertx().setTimer(5000, t -> { ctx.response().putHeader("location","/logout").setStatusCode(302).end(); }); ctx.addBodyEndHandler(v -> ctx.vertx().cancelTimer(tid)); }
@Override public void handle(RoutingContext ctx) { // We send a error response after timeout long tid = ctx.vertx().setTimer(timeout, t -> ctx.fail(errorCode)); ctx.addBodyEndHandler(v -> ctx.vertx().cancelTimer(tid)); ctx.next(); } }
@Test public void testTimeoutWithCustomBodyEndHandler() throws Exception { long timeout = 500; AtomicBoolean ended = new AtomicBoolean(); router.route().handler(routingContext -> { routingContext.addBodyEndHandler(event -> ended.set(true)); routingContext.next(); }); router.route().handler(TimeoutHandler.create(timeout)); router.route().handler(rc -> { // Don't end it }); testRequest(HttpMethod.GET, "/", 503, "Service Unavailable"); waitUntil(ended::get); }