use of io.vertx.core.file.AsyncFile in project vert.x by eclipse.
the class HttpUtils method resolveFile.
static void resolveFile(VertxInternal vertx, String filename, long offset, long length, Handler<AsyncResult<AsyncFile>> resultHandler) {
File file_ = vertx.resolveFile(filename);
if (!file_.exists()) {
resultHandler.handle(Future.failedFuture(new FileNotFoundException()));
return;
}
// i.e is not a directory
try (RandomAccessFile raf = new RandomAccessFile(file_, "r")) {
FileSystem fs = vertx.fileSystem();
fs.open(filename, new OpenOptions().setCreate(false).setWrite(false), ar -> {
if (ar.succeeded()) {
AsyncFile file = ar.result();
long contentLength = Math.min(length, file_.length() - offset);
file.setReadPos(offset);
file.setReadLength(contentLength);
}
resultHandler.handle(ar);
});
} catch (IOException e) {
resultHandler.handle(Future.failedFuture(e));
}
}
use of io.vertx.core.file.AsyncFile in project java-chassis by ServiceComb.
the class ReadStreamPart method onFileOpened.
protected void onFileOpened(File file, AsyncResult<AsyncFile> ar, CompletableFuture<File> future) {
if (ar.failed()) {
future.completeExceptionally(ar.cause());
return;
}
AsyncFile asyncFile = ar.result();
CompletableFuture<Void> saveFuture = saveToWriteStream(asyncFile);
saveFuture.whenComplete((v, saveException) -> {
asyncFile.close(closeAr -> {
if (closeAr.failed()) {
LOGGER.error("Failed to close file {}.", file);
}
// result just only related to write
if (saveException == null) {
future.complete(file);
return;
}
future.completeExceptionally(saveException);
});
});
}
use of io.vertx.core.file.AsyncFile in project incubator-servicecomb-java-chassis by apache.
the class ReadStreamPart method onFileOpened.
protected void onFileOpened(File file, AsyncResult<AsyncFile> ar, CompletableFuture<File> future) {
if (ar.failed()) {
future.completeExceptionally(ar.cause());
return;
}
AsyncFile asyncFile = ar.result();
CompletableFuture<Void> saveFuture = saveToWriteStream(asyncFile);
saveFuture.whenComplete((v, saveException) -> {
asyncFile.close(closeAr -> {
if (closeAr.failed()) {
LOGGER.error("Failed to close file {}.", file);
}
// result just only related to write
if (saveException == null) {
future.complete(file);
return;
}
future.completeExceptionally(saveException);
});
});
}
use of io.vertx.core.file.AsyncFile in project vertx-web by vert-x3.
the class WebClientTest method testRequestWithBody.
private void testRequestWithBody(HttpMethod method, boolean chunked) throws Exception {
String expected = TestUtils.randomAlphaString(1024 * 1024);
File f = File.createTempFile("vertx", ".data");
f.deleteOnExit();
Files.write(f.toPath(), expected.getBytes(StandardCharsets.UTF_8));
waitFor(2);
server.requestHandler(req -> req.bodyHandler(buff -> {
assertEquals(method, req.method());
assertEquals(Buffer.buffer(expected), buff);
complete();
req.response().end();
}));
startServer();
vertx.runOnContext(v -> {
AsyncFile asyncFile = vertx.fileSystem().openBlocking(f.getAbsolutePath(), new OpenOptions());
HttpRequest<Buffer> builder = null;
switch(method.name()) {
case "POST":
builder = webClient.post(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath");
break;
case "PUT":
builder = webClient.put(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath");
break;
case "PATCH":
builder = webClient.patch(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath");
break;
default:
fail("Invalid HTTP method");
}
if (!chunked) {
builder = builder.putHeader("Content-Length", "" + expected.length());
}
builder.sendStream(asyncFile, onSuccess(resp -> {
assertEquals(200, resp.statusCode());
complete();
}));
});
await();
}
use of io.vertx.core.file.AsyncFile in project vertx-web by vert-x3.
the class SessionAwareWebClientTest method testSendRequest.
@Test
public void testSendRequest(TestContext context) throws IOException {
AtomicInteger count = new AtomicInteger(0);
client = buildClient(plainWebClient, new CookieStoreImpl() {
@Override
public CookieStore put(Cookie cookie) {
count.incrementAndGet();
return super.put(cookie);
}
});
String encodedCookie = ServerCookieEncoder.STRICT.encode(new DefaultCookie("a", "1"));
prepareServer(context, req -> {
req.response().headers().add("set-cookie", encodedCookie);
});
int expected = 7;
Async async = context.async(expected);
Handler<AsyncResult<HttpResponse<Buffer>>> handler = ar -> {
async.countDown();
};
HttpRequest<Buffer> req = client.post("/");
req.send(handler);
req.sendBuffer(Buffer.buffer(), handler);
req.sendForm(HttpHeaders.set("a", "b"), handler);
req.sendJson("", handler);
req.sendJsonObject(new JsonObject(), handler);
req.sendMultipartForm(MultipartForm.create().attribute("a", "b"), handler);
File f = File.createTempFile("vertx", ".tmp");
f.deleteOnExit();
AsyncFile asyncFile = vertx.fileSystem().openBlocking(f.getAbsolutePath(), new OpenOptions());
req.sendStream(asyncFile, handler);
async.await();
asyncFile.close();
context.assertEquals(expected, count.get());
}
Aggregations