Search in sources :

Example 16 with FileSystem

use of io.vertx.core.file.FileSystem in project vertx-openshift-it by cescoffier.

the class StreamingResultsTest method handle.

@Override
public void handle(RoutingContext rc) {
    jdbcClient.getConnection(ar -> {
        if (ar.failed()) {
            fail(rc, ar.cause());
            return;
        }
        SQLConnection connection = ar.result();
        rc.response().bodyEndHandler(v -> {
            connection.close();
        });
        FileSystem fileSystem = rc.vertx().fileSystem();
        fileSystem.open("db/migration/strings", new OpenOptions(), ores -> {
            if (ores.failed()) {
                fail(rc, ores.cause());
                return;
            }
            AsyncFile asyncFile = ores.result();
            List<String> values = new ArrayList<>();
            RecordParser parser = RecordParser.newDelimited("\n", out -> values.add(out.toString()));
            asyncFile.handler(parser);
            asyncFile.endHandler(v -> {
                asyncFile.close();
                List<String> statements = values.stream().map(value -> "insert into random_string (value) values ('" + value + "')").collect(toList());
                connection.batch(statements, ires -> {
                    if (ires.failed()) {
                        fail(rc, ires.cause());
                        return;
                    }
                    connection.queryStream("select value from random_string", sres -> {
                        if (sres.failed()) {
                            fail(rc, sres.cause());
                            return;
                        }
                        List<String> storedValues = new ArrayList<>(values.size());
                        SQLRowStream rowStream = sres.result();
                        rowStream.handler(row -> {
                            storedValues.add(row.getString(0));
                        }).endHandler(endRows -> {
                            if (!storedValues.equals(values)) {
                                fail(rc, storedValues.toString());
                            } else {
                                rc.response().setStatusCode(200).end();
                            }
                        });
                    });
                });
            });
        });
    });
}
Also used : OpenOptions(io.vertx.core.file.OpenOptions) AsyncFile(io.vertx.core.file.AsyncFile) OpenOptions(io.vertx.core.file.OpenOptions) SQLRowStream(io.vertx.ext.sql.SQLRowStream) RoutingContext(io.vertx.ext.web.RoutingContext) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) TestUtil(io.vertx.openshift.jdbc.TestUtil) List(java.util.List) JDBCClient(io.vertx.ext.jdbc.JDBCClient) FileSystem(io.vertx.core.file.FileSystem) SQLConnection(io.vertx.ext.sql.SQLConnection) RecordParser(io.vertx.core.parsetools.RecordParser) Handler(io.vertx.core.Handler) SQLConnection(io.vertx.ext.sql.SQLConnection) FileSystem(io.vertx.core.file.FileSystem) ArrayList(java.util.ArrayList) RecordParser(io.vertx.core.parsetools.RecordParser) AsyncFile(io.vertx.core.file.AsyncFile) SQLRowStream(io.vertx.ext.sql.SQLRowStream)

Example 17 with FileSystem

use of io.vertx.core.file.FileSystem in project vert.x by eclipse.

the class HttpServerFileUploadImpl method streamToFileSystem.

@Override
public Future<Void> streamToFileSystem(String filename) {
    synchronized (this) {
        if (pipe != null) {
            return context.failedFuture("Already streaming");
        }
        pipe = pipe().endOnComplete(true);
    }
    FileSystem fs = context.owner().fileSystem();
    Future<AsyncFile> fut = fs.open(filename, new OpenOptions());
    fut.onFailure(err -> {
        pipe.close();
    });
    return fut.compose(f -> {
        Future<Void> to = pipe.to(f);
        return to.compose(v -> {
            synchronized (HttpServerFileUploadImpl.this) {
                if (!cancelled) {
                    file = f;
                    return context.succeededFuture();
                }
                fs.delete(filename);
                return context.failedFuture("Streaming aborted");
            }
        }, err -> {
            fs.delete(filename);
            return context.failedFuture(err);
        });
    });
}
Also used : OpenOptions(io.vertx.core.file.OpenOptions) FileSystem(io.vertx.core.file.FileSystem) AsyncFile(io.vertx.core.file.AsyncFile)

Example 18 with FileSystem

use of io.vertx.core.file.FileSystem in project vert.x by eclipse.

the class CoreExamples method exampleFuture2.

public void exampleFuture2(Vertx vertx, Handler<HttpServerRequest> requestHandler) {
    FileSystem fs = vertx.fileSystem();
    Future<FileProps> future = fs.props("/my_file.txt");
    future.onComplete((AsyncResult<FileProps> ar) -> {
        if (ar.succeeded()) {
            FileProps props = ar.result();
            System.out.println("File size = " + props.size());
        } else {
            System.out.println("Failure: " + ar.cause().getMessage());
        }
    });
}
Also used : FileSystem(io.vertx.core.file.FileSystem) FileProps(io.vertx.core.file.FileProps)

Example 19 with FileSystem

use of io.vertx.core.file.FileSystem in project azure-iot-sdk-java by Azure.

the class MainApiVerticle method start.

@SuppressWarnings("deprecation")
@Override
public void start(Future<Void> startFuture) throws Exception {
    Json.mapper.registerModule(new JavaTimeModule());
    FileSystem vertxFileSystem = vertx.fileSystem();
    vertxFileSystem.readFile("swagger.json", readFile -> {
        if (readFile.succeeded()) {
            Swagger swagger = new SwaggerParser().parse(readFile.result().toString(StandardCharsets.UTF_8));
            Router swaggerRouter = SwaggerRouter.swaggerRouter(router, swagger, vertx.eventBus(), new OperationIdServiceIdResolver(), t -> new DeliveryOptions().setSendTimeout(90000));
            deployVerticles(startFuture);
            vertx.createHttpServer().requestHandler(swaggerRouter::accept).listen(8080);
            startFuture.complete();
        } else {
            startFuture.fail(readFile.cause());
        }
    });
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) FileSystem(io.vertx.core.file.FileSystem) Swagger(io.swagger.models.Swagger) SwaggerRouter(com.github.phiz71.vertx.swagger.router.SwaggerRouter) Router(io.vertx.ext.web.Router) DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions) OperationIdServiceIdResolver(com.github.phiz71.vertx.swagger.router.OperationIdServiceIdResolver)

Example 20 with FileSystem

use of io.vertx.core.file.FileSystem in project vertx-examples by vert-x3.

the class Client method start.

@Override
public void start() throws Exception {
    HttpClientRequest req = vertx.createHttpClient(new HttpClientOptions()).put(8080, "localhost", "/someurl", resp -> {
        System.out.println("Response " + resp.statusCode());
    });
    String filename = "upload.txt";
    FileSystem fs = vertx.fileSystem();
    fs.props(filename, ares -> {
        FileProps props = ares.result();
        System.out.println("props is " + props);
        long size = props.size();
        req.headers().set("content-length", "" + size);
        fs.open(filename, new OpenOptions(), ares2 -> {
            AsyncFile file = ares2.result();
            Pump pump = Pump.pump(file, req);
            file.endHandler(v -> {
                req.end();
            });
            pump.start();
        });
    });
}
Also used : OpenOptions(io.vertx.core.file.OpenOptions) HttpClientRequest(io.vertx.core.http.HttpClientRequest) FileSystem(io.vertx.core.file.FileSystem) AsyncFile(io.vertx.core.file.AsyncFile) FileProps(io.vertx.core.file.FileProps) HttpClientOptions(io.vertx.core.http.HttpClientOptions) Pump(io.vertx.core.streams.Pump)

Aggregations

FileSystem (io.vertx.core.file.FileSystem)44 Buffer (io.vertx.core.buffer.Buffer)22 Vertx (io.vertx.core.Vertx)21 JsonObject (io.vertx.core.json.JsonObject)21 Future (io.vertx.core.Future)19 Handler (io.vertx.core.Handler)19 Context (io.vertx.core.Context)18 EventBus (io.vertx.core.eventbus.EventBus)18 TestContext (io.vertx.ext.unit.TestContext)18 Before (org.junit.Before)18 Test (org.junit.Test)17 Async (io.vertx.ext.unit.Async)16 StandardCharsets (java.nio.charset.StandardCharsets)16 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)15 HttpURLConnection (java.net.HttpURLConnection)15 Constants (org.eclipse.hono.util.Constants)15 CoreMatchers.is (org.hamcrest.CoreMatchers.is)15 Assert.assertThat (org.junit.Assert.assertThat)15 RunWith (org.junit.runner.RunWith)15 Mockito (org.mockito.Mockito)15