Search in sources :

Example 36 with FileSystem

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

the class CoreExamples method exampleFuture1.

public void exampleFuture1(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 37 with FileSystem

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

the class FileSystemExamples method example1.

public void example1(Vertx vertx) {
    FileSystem fs = vertx.fileSystem();
    // Copy file from foo.txt to bar.txt
    fs.copy("foo.txt", "bar.txt", res -> {
        if (res.succeeded()) {
        // Copied ok!
        } else {
        // Something went wrong
        }
    });
}
Also used : FileSystem(io.vertx.core.file.FileSystem)

Example 38 with FileSystem

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

the class FileSystemExamples method example2.

public void example2(Vertx vertx) {
    FileSystem fs = vertx.fileSystem();
    // Copy file from foo.txt to bar.txt synchronously
    fs.copyBlocking("foo.txt", "bar.txt");
}
Also used : FileSystem(io.vertx.core.file.FileSystem)

Example 39 with FileSystem

use of io.vertx.core.file.FileSystem 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));
    }
}
Also used : OpenOptions(io.vertx.core.file.OpenOptions) RandomAccessFile(java.io.RandomAccessFile) FileSystem(io.vertx.core.file.FileSystem) FileNotFoundException(java.io.FileNotFoundException) AsyncFile(io.vertx.core.file.AsyncFile) IOException(java.io.IOException) AsyncFile(io.vertx.core.file.AsyncFile) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 40 with FileSystem

use of io.vertx.core.file.FileSystem in project VX-API-Gateway by EliMirren.

the class VxApiClientJsonAuth method auth.

/**
 * 验证用户是否有权限
 *
 * @param user
 * @param vertx
 * @param handler
 */
public static void auth(JsonObject authInfo, Vertx vertx, Handler<AsyncResult<Boolean>> handler) {
    String username = authInfo.getString(VxApiRolesConstant.USER_NAME_KEY);
    if (username == null) {
        handler.handle(Future.succeededFuture(false));
    } else {
        String pwd = authInfo.getString(VxApiRolesConstant.USER_PWD_KEY);
        String role = authInfo.getString(VxApiRolesConstant.USER_ROLE_KEY);
        FileSystem file = vertx.fileSystem();
        String path = PathUtil.getPathString("user.json");
        file.readFile(path, res -> {
            if (res.succeeded()) {
                JsonObject users = res.result().toJsonObject();
                if (users.getValue(username) instanceof JsonObject) {
                    JsonObject user = users.getJsonObject(username);
                    if (pwd != null && pwd.equals(user.getString("pwd")) && user.getJsonArray("roles").contains(role)) {
                        handler.handle(Future.<Boolean>succeededFuture(true));
                    } else {
                        handler.handle(Future.<Boolean>succeededFuture(false));
                    }
                } else {
                    handler.handle(Future.<Boolean>succeededFuture(false));
                }
            } else {
                handler.handle(Future.failedFuture(res.cause()));
            }
        });
    }
}
Also used : FileSystem(io.vertx.core.file.FileSystem) JsonObject(io.vertx.core.json.JsonObject)

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