@Override public Vertx vertx() { return decoratedContext.vertx(); }
@Override public Vertx vertx() { return inner.vertx(); }
@Override public void handle(RoutingContext context) { Route currentRoute = context.currentRoute(); context.vertx().executeBlocking(fut -> { decoratedHandler.handle(new RoutingContextDecorator(currentRoute, context)); fut.complete(); }, ordered, res -> { if (res.failed()) { // This means an exception was thrown from the blocking handler context.fail(res.cause()); } }); }
private synchronized void isFileExisting(RoutingContext context, String file, Handler<AsyncResult<Boolean>> resultHandler) { FileSystem fs = context.vertx().fileSystem(); wrapInTCCLSwitch(() -> fs.exists(file, resultHandler)); }
private void deleteFileUploads() { if (cleanup.compareAndSet(false, true)) { for (FileUpload fileUpload : context.fileUploads()) { FileSystem fileSystem = context.vertx().fileSystem(); String uploadedFileName = fileUpload.uploadedFileName(); fileSystem.exists(uploadedFileName, existResult -> { if (existResult.failed()) { LOGGER.warn("Could not detect if uploaded file exists, not deleting: " + uploadedFileName, existResult.cause()); } else if (existResult.result()) { fileSystem.delete(uploadedFileName, deleteResult -> { if (deleteResult.failed()) { LOGGER.warn("Delete of uploaded file failed: " + uploadedFileName, deleteResult.cause()); } }); } }); } } } }
private void deleteFileUploads() { if (cleanup.compareAndSet(false, true) && handleFileUploads) { for (FileUpload fileUpload : context.fileUploads()) { FileSystem fileSystem = context.vertx().fileSystem(); String uploadedFileName = fileUpload.uploadedFileName(); fileSystem.exists(uploadedFileName, existResult -> { if (existResult.failed()) { log.warn("Could not detect if uploaded file exists, not deleting: " + uploadedFileName, existResult.cause()); } else if (existResult.result()) { fileSystem.delete(uploadedFileName, deleteResult -> { if (deleteResult.failed()) { log.warn("Delete of uploaded file failed: " + uploadedFileName, deleteResult.cause()); } }); } }); } } } }
private synchronized void getFileProps(RoutingContext context, String file, Handler<AsyncResult<FileProps>> resultHandler) { FileSystem fs = context.vertx().fileSystem(); if (alwaysAsyncFS || useAsyncFS) { wrapInTCCLSwitch(() -> fs.props(file, resultHandler));
@Override public void handle(RoutingContext ctx) { if (icon == null) { init(ctx.vertx()); } if ("/favicon.ico".equals(ctx.request().path())) { ctx.response().headers().addAll(icon.headers); ctx.response().end(icon.body); } else { ctx.next(); } } }
@Override public void handle(Buffer buff) { if (failed) { return; } uploadSize += buff.length(); if (bodyLimit != -1 && uploadSize > bodyLimit) { failed = true; context.fail(413); // enqueue a delete for the error uploads context.vertx().runOnContext(v -> deleteFileUploads()); } else { // multipart requests will not end up in the request body // url encoded should also not, however jQuery by default // post in urlencoded even if the payload is something else if (!isMultipart /* && !isUrlEncoded */) { body.appendBuffer(buff); } } }
if (sessionID != null && sessionID.length() > minLength) { getSession(context.vertx(), sessionID, res -> { if (res.succeeded()) { Session session = res.result();
@Override public void handle(Buffer buff) { if (failed) { return; } uploadSize += buff.length(); if (bodyLimit != -1 && uploadSize > bodyLimit) { failed = true; // enqueue a delete for the error uploads context.fail(new InvocationException(Status.REQUEST_ENTITY_TOO_LARGE, Status.REQUEST_ENTITY_TOO_LARGE.getReasonPhrase())); context.vertx().runOnContext(v -> deleteFileUploads()); } else { // multipart requests will not end up in the request body // url encoded should also not, however jQuery by default // post in urlencoded even if the payload is something else if (!isMultipart /* && !isUrlEncoded */) { body.appendBuffer(buff); } } }
context.request().setExpectMultipart(true); if (handleFileUploads) { makeUploadDir(context.vertx().fileSystem());
@Test public void testBlockingHandlerFailure() throws Exception { List<Thread> threads = new ArrayList<>(); List<Context> contexts = new ArrayList<>(); router.route().handler(rc -> { threads.add(Thread.currentThread()); contexts.add(rc.vertx().getOrCreateContext()); rc.response().setChunked(true); rc.next(); }); router.route().blockingHandler(rc -> { assertTrue(!threads.get(0).equals(Thread.currentThread())); assertTrue(contexts.get(0).equals(rc.vertx().getOrCreateContext())); assertTrue(rc.currentRoute()!=null); rc.fail(501); }); router.route().failureHandler(rc -> { assertTrue(threads.get(0).equals(Thread.currentThread())); assertTrue(contexts.get(0).equals(rc.vertx().getOrCreateContext())); assertTrue(rc.currentRoute()!=null); rc.response().setStatusCode(rc.statusCode()).end(); }); testRequest(HttpMethod.GET, "/", 501, "Not Implemented"); }
@Test public void testBlockingHandlerFailureThrowException() throws Exception { List<Thread> threads = new ArrayList<>(); List<Context> contexts = new ArrayList<>(); router.route().handler(rc -> { threads.add(Thread.currentThread()); contexts.add(rc.vertx().getOrCreateContext()); rc.next(); }); router.route().blockingHandler(rc -> { assertTrue(!threads.get(0).equals(Thread.currentThread())); assertTrue(contexts.get(0).equals(rc.vertx().getOrCreateContext())); assertTrue(rc.currentRoute()!=null); throw new RuntimeException("foo"); }); router.route().failureHandler(rc -> { assertTrue(threads.get(0).equals(Thread.currentThread())); assertTrue(contexts.get(0).equals(rc.vertx().getOrCreateContext())); assertTrue(rc.currentRoute()!=null); Throwable t = rc.failure(); assertNotNull(t); assertTrue(t instanceof RuntimeException); assertEquals("foo", t.getMessage()); rc.response().setStatusCode(500).end(); }); testRequest(HttpMethod.GET, "/", 500, "Internal Server Error"); }
makeUploadDir(context.vertx().fileSystem()); context.request().setExpectMultipart(true); context.request().uploadHandler(upload -> {
router.get("/path").handler(routingContext -> { routingContext.put("response", "handler1"); routingContext.vertx().setTimer((int) (1 + Math.random() * 10), asyncResult -> routingContext.next()); }).handler(routingContext -> { routingContext.put("response", routingContext.get("response") + "handler2"); routingContext.vertx().setTimer((int) (1 + Math.random() * 10), asyncResult -> routingContext.next()); }).handler(routingContext -> { HttpServerResponse response = routingContext.response();
@Override public void setUp() throws Exception { // Use two servers so we test with HTTP request/response with load balanced SockJSSession access numServers = 2; preSockJSHandlerSetup = router -> { router.route().handler(BodyHandler.create()); // simulate an async handler router.route().handler(rtx -> rtx.vertx().executeBlocking(f -> f.complete(true), r -> rtx.next())); }; super.setUp(); }
router.route().handler(rc -> { threads.add(Thread.currentThread()); contexts.add(rc.vertx().getOrCreateContext()); assertTrue(rc.currentRoute() != null); rc.response().setChunked(true); router.route().blockingHandler(rc -> { assertTrue(!threads.get(0).equals(Thread.currentThread())); assertTrue(contexts.get(0).equals(rc.vertx().getOrCreateContext())); assertTrue(rc.currentRoute()!=null); rc.response().write("B"); router.route().blockingHandler(rc -> { assertTrue(!threads.get(0).equals(Thread.currentThread())); assertTrue(contexts.get(0).equals(rc.vertx().getOrCreateContext())); assertTrue(rc.currentRoute()!=null); rc.response().write("C"); router.route().handler(rc -> { assertTrue(threads.get(0).equals(Thread.currentThread())); assertTrue(contexts.get(0).equals(rc.vertx().getOrCreateContext())); assertTrue(rc.currentRoute()!=null); rc.response().write("D"); router.route().handler(rc -> { assertTrue(threads.get(0).equals(Thread.currentThread())); assertTrue(contexts.get(0).equals(rc.vertx().getOrCreateContext())); assertTrue(rc.currentRoute()!=null); rc.response().write("E");
} else { routingContext.put("response", "handler1"); routingContext.vertx().setTimer((int) (1 + Math.random() * 10), asyncResult -> routingContext.next()); routingContext.vertx().setTimer((int) (1 + Math.random() * 10), asyncResult -> routingContext.next()); }).handler(routingContext -> { routingContext.put("response", routingContext.get("response") + "handler2"); routingContext.vertx().setTimer((int) (1 + Math.random() * 10), asyncResult -> routingContext.next()); }).handler(routingContext -> { HttpServerResponse response = routingContext.response(); }).failureHandler(routingContext -> { routingContext.put("response", routingContext.get("response") + "fhandler2"); routingContext.vertx().setTimer((int) (1 + Math.random() * 10), asyncResult -> routingContext.next()); }).failureHandler(routingContext -> { HttpServerResponse response = routingContext.response();