@Test public void testHandleFormWithNoUploadedFile(TestContext context) { FrontendServer server = new FrontendServer(new MicroserviceClientFactory(vertx, ServiceDiscovery.create(vertx))); HttpServerResponse response = mock(HttpServerResponse.class); RoutingContext ctx = mock(RoutingContext.class); doReturn(response).when(ctx).response(); doReturn(response).when(response).putHeader(anyString(), anyString()); doReturn(Collections.emptySet()).when(ctx).fileUploads(); server.handleForm(ctx); verify(response).end(eq("No file uploaded")); }
void handleForm(RoutingContext ctx) { HttpServerResponse response = ctx.response() .putHeader(HttpHeaders.CONTENT_TYPE.toString(), "text/plain"); Set<FileUpload> uploadedFiles = ctx.fileUploads(); if (uploadedFiles.isEmpty()) { response.end("No file uploaded"); return; } Observable.from(uploadedFiles).flatMap(fileUpload -> { File file = new File(fileUpload.uploadedFileName()); MimeType mimeType = MimeType.valueOf(fileUpload.contentType()); Future<Void> closed = Future.future(); return createHttpClientForFileStorage(closed) .flatMap(client -> { HttpClientRequest req = client.post("/file/") .setChunked(true) .putHeader("Content-Length", Long.toString(fileUpload.size())) .putHeader(HttpHeaders.CONTENT_TYPE.toString(), mimeType.toString()) .putHeader("File-Name", fileUpload.fileName()); Single<HttpClientResponse> result = req.toObservable().toSingle(); vertx.fileSystem().openObservable(file.getAbsolutePath(), new OpenOptions().setRead(true).setCreateNew(false)) .subscribe(asyncFile -> { asyncFile.endHandler(v -> { req.end(); }); Pump.pump(asyncFile, req).start(); }); return result; }) .map(res -> { if (res.statusCode() != 200) { throw new RuntimeException("Failed to store uploaded video to file storage. Status code is: " + res.statusCode() + ", status message is: " + res.statusMessage()); } UUID fileId = UUID.fromString(res.getHeader("File-Id")); VideoUploadedEvent event = new VideoUploadedEvent(fileId, fileUpload.fileName(), Collections.singleton("vga")); vertx.eventBus().send("file-uploaded", event); return event; }) .toObservable() .doOnCompleted(closed::complete); }).subscribe(v -> {}, error -> { log.warn("Failed to store task to registry", error); ctx.fail(error); }, () -> { response.end("registered"); }); }
FrontendServer extends AbstractVerticle { void handleForm(RoutingContext ctx) { HttpServerResponse response = ctx.response() .putHeader(HttpHeaders.CONTENT_TYPE.toString(), "text/plain"); Set<FileUpload> uploadedFiles = ctx.fileUploads(); if (uploadedFiles.isEmpty()) { response.end("No file uploaded"); return; } Observable.from(uploadedFiles).flatMap(fileUpload -> { File file = new File(fileUpload.uploadedFileName()); MimeType mimeType = MimeType.valueOf(fileUpload.contentType()); Future<Void> closed = Future.future(); return createHttpClientForFileStorage(closed) .flatMap(client -> { HttpClientRequest req = client.post("/file/") .setChunked(true) .putHeader("Content-Length", Long.toString(fileUpload.size())) .putHeader(HttpHeaders.CONTENT_TYPE.toString(), mimeType.toString()) .putHeader("File-Name", fileUpload.fileName()); Single<HttpClientResponse> result = req.toObservable().toSingle(); vertx.fileSystem().openObservable(file.getAbsolutePath(), new OpenOptions().setRead(true).setCreateNew(false)) .subscribe(asyncFile -> { asyncFile.endHandler(v -> { req.end(); }); Pump.pump(asyncFile, req).start(); }); return result; }) .map(res -> { if (res.statusCode() != 200) { throw new RuntimeException("Failed to store uploaded video to file storage. Status code is: " + res.statusCode() + ", status message is: " + res.statusMessage()); } UUID fileId = UUID.fromString(res.getHeader("File-Id")); VideoUploadedEvent event = new VideoUploadedEvent(fileId, fileUpload.fileName(), Collections.singleton("vga")); vertx.eventBus().send("file-uploaded", event); return event; }) .toObservable() .doOnCompleted(closed::complete); }).subscribe(v -> {}, error -> { log.warn("Failed to store task to registry", error); ctx.fail(error); }, () -> { response.end("registered"); }); } }
FrontendServer extends AbstractVerticle { void handleForm(RoutingContext ctx) { HttpServerResponse response = ctx.response() .putHeader(HttpHeaders.CONTENT_TYPE.toString(), "text/plain"); Set<FileUpload> uploadedFiles = ctx.fileUploads(); if (uploadedFiles.isEmpty()) { response.end("No file uploaded"); return; } Observable.from(uploadedFiles).flatMap(fileUpload -> { File file = new File(fileUpload.uploadedFileName()); MimeType mimeType = MimeType.valueOf(fileUpload.contentType()); Future<Void> closed = Future.future(); return createHttpClientForFileStorage(closed) .flatMap(client -> { HttpClientRequest req = client.post("/file/") .setChunked(true) .putHeader("Content-Length", Long.toString(fileUpload.size())) .putHeader(HttpHeaders.CONTENT_TYPE.toString(), mimeType.toString()) .putHeader("File-Name", fileUpload.fileName()); Single<HttpClientResponse> result = req.toObservable().toSingle(); vertx.fileSystem().openObservable(file.getAbsolutePath(), new OpenOptions().setRead(true).setCreateNew(false)) .subscribe(asyncFile -> { asyncFile.endHandler(v -> { req.end(); }); Pump.pump(asyncFile, req).start(); }); return result; }) .map(res -> { if (res.statusCode() != 200) { throw new RuntimeException("Failed to store uploaded video to file storage. Status code is: " + res.statusCode() + ", status message is: " + res.statusMessage()); } UUID fileId = UUID.fromString(res.getHeader("File-Id")); VideoUploadedEvent event = new VideoUploadedEvent(fileId, fileUpload.fileName(), Collections.singleton("vga")); vertx.eventBus().send("file-uploaded", event); return event; }) .toObservable() .doOnCompleted(closed::complete); }).subscribe(v -> {}, error -> { log.warn("Failed to store task to registry", error); ctx.fail(error); }, () -> { response.end("registered"); }); }  }
FrontendServer extends AbstractVerticle { void handleForm(RoutingContext ctx) { HttpServerResponse response = ctx.response() .putHeader(HttpHeaders.CONTENT_TYPE.toString(), "text/plain"); Set<FileUpload> uploadedFiles = ctx.fileUploads(); if (uploadedFiles.isEmpty()) { response.end("No file uploaded"); return; } Observable.from(uploadedFiles).flatMap(fileUpload -> { File file = new File(fileUpload.uploadedFileName()); MimeType mimeType = MimeType.valueOf(fileUpload.contentType()); Future<Void> closed = Future.future(); return createHttpClientForFileStorage(closed) .flatMap(client -> { HttpClientRequest req = client.post("/file/") .setChunked(true) .putHeader("Content-Length", Long.toString(fileUpload.size())) .putHeader(HttpHeaders.CONTENT_TYPE.toString(), mimeType.toString()) .putHeader("File-Name", fileUpload.fileName()); Single<HttpClientResponse> result = req.toObservable().toSingle(); vertx.fileSystem().openObservable(file.getAbsolutePath(), new OpenOptions().setRead(true).setCreateNew(false)) .subscribe(asyncFile -> { asyncFile.endHandler(v -> { req.end(); }); Pump.pump(asyncFile, req).start(); }); return result; }) .map(res -> { if (res.statusCode() != 200) { throw new RuntimeException("Failed to store uploaded video to file storage. Status code is: " + res.statusCode() + ", status message is: " + res.statusMessage()); } UUID fileId = UUID.fromString(res.getHeader("File-Id")); VideoUploadedEvent event = new VideoUploadedEvent(fileId, fileUpload.fileName(), Collections.singleton("vga")); vertx.eventBus().send("file-uploaded", event); return event; }) .toObservable() .doOnCompleted(closed::complete); }).subscribe(v -> {}, error -> { log.warn("Failed to store task to registry", error); ctx.fail(error); }, () -> { response.end("registered"); }); }  @Override void start(); }
FrontendServer extends AbstractVerticle { void handleForm(RoutingContext ctx) { HttpServerResponse response = ctx.response() .putHeader(HttpHeaders.CONTENT_TYPE.toString(), "text/plain"); Set<FileUpload> uploadedFiles = ctx.fileUploads(); if (uploadedFiles.isEmpty()) { response.end("No file uploaded"); return; } Observable.from(uploadedFiles).flatMap(fileUpload -> { File file = new File(fileUpload.uploadedFileName()); MimeType mimeType = MimeType.valueOf(fileUpload.contentType()); Future<Void> closed = Future.future(); return createHttpClientForFileStorage(closed) .flatMap(client -> { HttpClientRequest req = client.post("/file/") .setChunked(true) .putHeader("Content-Length", Long.toString(fileUpload.size())) .putHeader(HttpHeaders.CONTENT_TYPE.toString(), mimeType.toString()) .putHeader("File-Name", fileUpload.fileName()); Single<HttpClientResponse> result = req.toObservable().toSingle(); vertx.fileSystem().openObservable(file.getAbsolutePath(), new OpenOptions().setRead(true).setCreateNew(false)) .subscribe(asyncFile -> { asyncFile.endHandler(v -> { req.end(); }); Pump.pump(asyncFile, req).start(); }); return result; }) .map(res -> { if (res.statusCode() != 200) { throw new RuntimeException("Failed to store uploaded video to file storage. Status code is: " + res.statusCode() + ", status message is: " + res.statusMessage()); } UUID fileId = UUID.fromString(res.getHeader("File-Id")); VideoUploadedEvent event = new VideoUploadedEvent(fileId, fileUpload.fileName(), Collections.singleton("vga")); vertx.eventBus().send("file-uploaded", event); return event; }) .toObservable() .doOnCompleted(closed::complete); }).subscribe(v -> {}, error -> { log.warn("Failed to store task to registry", error); ctx.fail(error); }, () -> { response.end("registered"); }); }  @Override void start();  }