use of io.vertx.core.file.AsyncFile in project raml-module-builder by folio-org.
the class ProcessUploads method parseFile.
/**
* Main work done by the FileDataHandler which reads in line by line and passes on that line
* to the correct Importer implementation for line processing
* @param fileSize
* @param conf
* @param replyHandler - the handler returns a job object with success and error counter parameters
* to be persisted by the job runner
*/
private void parseFile(long fileSize, Job conf, Handler<AsyncResult<Job>> replyHandler) {
String file = conf.getParameters().get(0).getValue();
vertx.fileSystem().open(file, new OpenOptions(), ar -> {
if (ar.succeeded()) {
AsyncFile rs = ar.result();
rs.handler(new FileDataHandler(vertx, conf, fileSize, importerCache.get(conf.getName()), reply -> {
if (reply.failed()) {
if (reply.cause().getMessage().contains(RTFConsts.STATUS_ERROR_THRESHOLD)) {
log.error("Stopping import... Error threshold exceeded for file " + file);
try {
// can throw an exception if the error threshold is met at
// the last bulk where the endHandler is called before the stop on error is called
rs.pause().close();
} catch (Exception e) {
log.error("Error threshold hit on last block of data ", e);
}
replyHandler.handle(io.vertx.core.Future.failedFuture(RTFConsts.STATUS_ERROR_THRESHOLD));
}
} else {
replyHandler.handle(io.vertx.core.Future.succeededFuture(reply.result()));
}
}));
rs.exceptionHandler(t -> {
log.error("Error reading from file " + file, t);
replyHandler.handle(io.vertx.core.Future.failedFuture(RTFConsts.STATUS_ERROR));
});
rs.endHandler(v -> {
rs.close(ar2 -> {
if (ar2.failed()) {
log.error("Error closing file " + file, ar2.cause());
}
});
});
} else {
log.error("Error opening file " + file, ar.cause());
replyHandler.handle(io.vertx.core.Future.failedFuture(RTFConsts.STATUS_ERROR));
}
});
}
use of io.vertx.core.file.AsyncFile in project vert.x by eclipse.
the class HttpServerFileUploadTest method testFormUploadFile.
private void testFormUploadFile(String filename, String extFilename, String contentStr, boolean includeLength, boolean streamToDisk, boolean abortClient, boolean cancelStream) {
String expectedFilename;
try {
if (extFilename != null) {
expectedFilename = URLDecoder.decode(extFilename, "UTF-8");
} else {
expectedFilename = filename;
}
} catch (UnsupportedEncodingException e) {
fail(e);
return;
}
waitFor(2);
Buffer content = Buffer.buffer(contentStr);
AtomicInteger attributeCount = new AtomicInteger();
AtomicReference<HttpConnection> clientConn = new AtomicReference<>();
AtomicReference<HttpConnection> serverConn = new AtomicReference<>();
Runnable checkClose = () -> {
if (clientConn.get() != null && serverConn.get() != null) {
clientConn.get().close();
}
};
server.requestHandler(req -> {
Context requestContext = vertx.getOrCreateContext();
if (req.method() == HttpMethod.POST) {
assertEquals(req.path(), "/form");
req.response().setChunked(true);
req.setExpectMultipart(true);
assertTrue(req.isExpectMultipart());
// Now try setting again, it shouldn't have an effect
req.setExpectMultipart(true);
assertTrue(req.isExpectMultipart());
req.uploadHandler(upload -> {
Context uploadContext = Vertx.currentContext();
assertNotNull(uploadContext);
assertSame(requestContext, uploadContext);
serverConn.set(req.connection());
checkClose.run();
Buffer tot = Buffer.buffer();
assertEquals("file", upload.name());
assertEquals(expectedFilename, upload.filename());
assertEquals("image/gif", upload.contentType());
String uploadedFileName;
if (!streamToDisk) {
upload.handler(tot::appendBuffer);
upload.exceptionHandler(err -> {
assertTrue(abortClient);
complete();
});
upload.endHandler(v -> {
assertFalse(abortClient);
assertEquals(content, tot);
assertTrue(upload.isSizeAvailable());
assertEquals(content.length(), upload.size());
assertNull(upload.file());
complete();
});
} else {
uploadedFileName = new File(testDir, UUID.randomUUID().toString()).getPath();
upload.streamToFileSystem(uploadedFileName, ar -> {
if (ar.succeeded()) {
Buffer uploaded = vertx.fileSystem().readFileBlocking(uploadedFileName);
assertEquals(content.length(), uploaded.length());
assertEquals(content, uploaded);
AsyncFile file = upload.file();
assertNotNull(file);
try {
file.flush();
fail("Was expecting uploaded file to be closed");
} catch (IllegalStateException ignore) {
// File has been closed
}
} else {
assertTrue(ar.failed());
}
complete();
});
if (cancelStream) {
BooleanSupplier test = () -> {
File f = new File(uploadedFileName);
if (f.length() == contentStr.length() / 2) {
assertTrue(upload.cancelStreamToFileSystem());
long now = System.currentTimeMillis();
vertx.setPeriodic(10, id -> {
assertTrue(System.currentTimeMillis() - now < 20_000);
if (!new File(uploadedFileName).exists()) {
vertx.cancelTimer(id);
req.response().end();
}
});
return true;
} else {
return false;
}
};
if (!test.getAsBoolean()) {
long now = System.currentTimeMillis();
vertx.setPeriodic(10, id -> {
assertTrue(System.currentTimeMillis() - now < 20_000);
if (test.getAsBoolean()) {
vertx.cancelTimer(id);
}
});
}
}
}
});
req.endHandler(v -> {
MultiMap attrs = req.formAttributes();
attributeCount.set(attrs.size());
req.response().end();
});
}
});
server.listen(testAddress, onSuccess(s -> {
client.request(new RequestOptions(requestOptions).setMethod(HttpMethod.POST).setURI("/form")).onComplete(onSuccess(req -> {
String boundary = "dLV9Wyq26L_-JQxk6ferf-RT153LhOO";
String epi = "\r\n" + "--" + boundary + "--\r\n";
String pro = "--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"file\"" + (filename == null ? "" : "; filename=\"" + filename + "\"") + (extFilename == null ? "" : "; filename*=\"UTF-8''" + extFilename) + "\"\r\n" + "Content-Type: image/gif\r\n" + (includeLength ? "Content-Length: " + contentStr.length() + "\r\n" : "") + "\r\n";
req.headers().set("content-length", "" + (pro + contentStr + epi).length());
req.headers().set("content-type", "multipart/form-data; boundary=" + boundary);
if (abortClient || cancelStream) {
Future<Void> fut = req.write(pro + contentStr.substring(0, contentStr.length() / 2));
if (abortClient) {
fut.onComplete(onSuccess(v -> {
clientConn.set(req.connection());
checkClose.run();
}));
}
} else {
req.end(pro + contentStr + epi);
}
if (abortClient) {
req.response(onFailure(err -> complete()));
} else {
req.response(onSuccess(resp -> {
assertEquals(200, resp.statusCode());
resp.bodyHandler(body -> {
assertEquals(0, body.length());
});
assertEquals(0, attributeCount.get());
complete();
}));
}
}));
}));
await();
}
use of io.vertx.core.file.AsyncFile in project vert.x by eclipse.
the class FileSystemExamples method asyncFileRead.
public void asyncFileRead(Vertx vertx) {
vertx.fileSystem().open("target/classes/les_miserables.txt", new OpenOptions(), result -> {
if (result.succeeded()) {
AsyncFile file = result.result();
Buffer buff = Buffer.buffer(1000);
for (int i = 0; i < 10; i++) {
file.read(buff, i * 100, i * 100, 100, ar -> {
if (ar.succeeded()) {
System.out.println("Read ok!");
} else {
System.err.println("Failed to write: " + ar.cause());
}
});
}
} else {
System.err.println("Cannot open file " + result.cause());
}
});
}
use of io.vertx.core.file.AsyncFile in project vert.x by eclipse.
the class FileSystemExamples method asyncFileWrite.
public void asyncFileWrite(Vertx vertx) {
vertx.fileSystem().open("target/classes/hello.txt", new OpenOptions(), result -> {
if (result.succeeded()) {
AsyncFile file = result.result();
Buffer buff = Buffer.buffer("foo");
for (int i = 0; i < 5; i++) {
file.write(buff, buff.length() * i, ar -> {
if (ar.succeeded()) {
System.out.println("Written ok!");
// etc
} else {
System.err.println("Failed to write: " + ar.cause());
}
});
}
} else {
System.err.println("Cannot open file " + result.cause());
}
});
}
use of io.vertx.core.file.AsyncFile in project vert.x by eclipse.
the class HttpNetSocket method sendFile.
@Override
public NetSocket sendFile(String filename, long offset, long length, Handler<AsyncResult<Void>> resultHandler) {
VertxInternal vertx = conn.getContext().owner();
Handler<AsyncResult<Void>> h;
if (resultHandler != null) {
Context resultCtx = vertx.getOrCreateContext();
h = ar -> {
resultCtx.runOnContext((v) -> {
resultHandler.handle(ar);
});
};
} else {
h = ar -> {
};
}
HttpUtils.resolveFile(vertx, filename, offset, length, ar -> {
if (ar.succeeded()) {
AsyncFile file = ar.result();
file.pipe().endOnComplete(false).to(this, ar1 -> file.close(ar2 -> {
Throwable failure = ar1.failed() ? ar1.cause() : ar2.failed() ? ar2.cause() : null;
if (failure == null)
h.handle(ar1);
else
h.handle(Future.failedFuture(failure));
}));
} else {
h.handle(ar.mapEmpty());
}
});
return this;
}
Aggregations