Search in sources :

Example 21 with AsyncFile

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));
        }
    });
}
Also used : OpenOptions(io.vertx.core.file.OpenOptions) Importer(org.folio.rest.resource.interfaces.Importer) AsyncFile(io.vertx.core.file.AsyncFile) OpenOptions(io.vertx.core.file.OpenOptions) FileDataHandler(org.folio.rest.resource.handlers.FileDataHandler) Vertx(io.vertx.core.Vertx) Message(io.vertx.core.eventbus.Message) HashMap(java.util.HashMap) Job(org.folio.rest.jaxrs.model.Job) LoggerFactory(io.vertx.core.logging.LoggerFactory) ArrayList(java.util.ArrayList) Messages(org.folio.rest.tools.messages.Messages) InterfaceToImpl(org.folio.rest.tools.utils.InterfaceToImpl) Map(java.util.Map) JobAPI(org.folio.rest.resource.interfaces.JobAPI) JsonObject(io.vertx.core.json.JsonObject) RTFConsts(org.folio.rest.tools.RTFConsts) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) Logger(io.vertx.core.logging.Logger) JobConf(org.folio.rest.jaxrs.model.JobConf) AsyncFile(io.vertx.core.file.AsyncFile) FileDataHandler(org.folio.rest.resource.handlers.FileDataHandler)

Example 22 with AsyncFile

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();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) Context(io.vertx.core.Context) AsyncFile(io.vertx.core.file.AsyncFile) DecoderException(io.netty.handler.codec.DecoderException) Arrays(java.util.Arrays) URLDecoder(java.net.URLDecoder) MultiMap(io.vertx.core.MultiMap) Vertx(io.vertx.core.Vertx) Test(org.junit.Test) UUID(java.util.UUID) Context(io.vertx.core.Context) Future(io.vertx.core.Future) AtomicReference(java.util.concurrent.atomic.AtomicReference) File(java.io.File) BooleanSupplier(java.util.function.BooleanSupplier) URLEncoder(java.net.URLEncoder) Rule(org.junit.Rule) Buffer(io.vertx.core.buffer.Buffer) TestUtils(io.vertx.test.core.TestUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Collections(java.util.Collections) TemporaryFolder(org.junit.rules.TemporaryFolder) SocketAddress(io.vertx.core.net.SocketAddress) UnsupportedEncodingException(java.io.UnsupportedEncodingException) AtomicReference(java.util.concurrent.atomic.AtomicReference) MultiMap(io.vertx.core.MultiMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AsyncFile(io.vertx.core.file.AsyncFile) AsyncFile(io.vertx.core.file.AsyncFile) File(java.io.File) BooleanSupplier(java.util.function.BooleanSupplier)

Example 23 with AsyncFile

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());
        }
    });
}
Also used : OpenOptions(io.vertx.core.file.OpenOptions) Buffer(io.vertx.core.buffer.Buffer) AsyncFile(io.vertx.core.file.AsyncFile)

Example 24 with AsyncFile

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());
        }
    });
}
Also used : OpenOptions(io.vertx.core.file.OpenOptions) Buffer(io.vertx.core.buffer.Buffer) AsyncFile(io.vertx.core.file.AsyncFile)

Example 25 with AsyncFile

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;
}
Also used : Context(io.vertx.core.Context) VertxException(io.vertx.core.VertxException) AsyncFile(io.vertx.core.file.AsyncFile) VertxInternal(io.vertx.core.impl.VertxInternal) ClosedChannelException(java.nio.channels.ClosedChannelException) Promise(io.vertx.core.Promise) ContextInternal(io.vertx.core.impl.ContextInternal) ConnectionBase(io.vertx.core.net.impl.ConnectionBase) X509Certificate(javax.security.cert.X509Certificate) Context(io.vertx.core.Context) Future(io.vertx.core.Future) Nullable(io.vertx.codegen.annotations.Nullable) List(java.util.List) Pipe(io.vertx.core.streams.Pipe) SSLSession(javax.net.ssl.SSLSession) Certificate(java.security.cert.Certificate) Buffer(io.vertx.core.buffer.Buffer) WriteStream(io.vertx.core.streams.WriteStream) ReadStream(io.vertx.core.streams.ReadStream) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) StreamResetException(io.vertx.core.http.StreamResetException) NetSocket(io.vertx.core.net.NetSocket) SocketAddress(io.vertx.core.net.SocketAddress) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) VertxInternal(io.vertx.core.impl.VertxInternal) AsyncFile(io.vertx.core.file.AsyncFile) AsyncResult(io.vertx.core.AsyncResult)

Aggregations

AsyncFile (io.vertx.core.file.AsyncFile)31 OpenOptions (io.vertx.core.file.OpenOptions)26 Buffer (io.vertx.core.buffer.Buffer)19 Handler (io.vertx.core.Handler)10 Test (org.junit.Test)10 Vertx (io.vertx.core.Vertx)9 File (java.io.File)9 AsyncResult (io.vertx.core.AsyncResult)8 FileSystem (io.vertx.core.file.FileSystem)7 List (java.util.List)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 Future (io.vertx.core.Future)6 JsonObject (io.vertx.core.json.JsonObject)6 WriteStream (io.vertx.core.streams.WriteStream)6 IOException (java.io.IOException)6 ReadStream (io.vertx.core.streams.ReadStream)5 FileNotFoundException (java.io.FileNotFoundException)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 JsonArray (io.vertx.core.json.JsonArray)4 TestUtils (io.vertx.test.core.TestUtils)4