Search in sources :

Example 11 with FileSystem

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

the class FileBasedTenantServiceTest method testDoStartCreatesFile.

/**
 * Verifies that the tenant service creates a file for persisting tenants
 * data if it does not exist yet during startup.
 *
 * @param ctx The vert.x context.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testDoStartCreatesFile(final TestContext ctx) {
    // GIVEN a tenant service configured to persist data to a not yet existing file
    props.setSaveToFile(true);
    props.setFilename(FILE_NAME);
    when(fileSystem.existsBlocking(FILE_NAME)).thenReturn(Boolean.FALSE);
    doAnswer(invocation -> {
        final Handler handler = invocation.getArgument(1);
        handler.handle(Future.succeededFuture());
        return null;
    }).when(fileSystem).createFile(eq(props.getFilename()), any(Handler.class));
    doAnswer(invocation -> {
        final Handler handler = invocation.getArgument(1);
        handler.handle(Future.failedFuture("malformed file"));
        return null;
    }).when(fileSystem).readFile(eq(props.getFilename()), any(Handler.class));
    // WHEN starting the service
    final Async startup = ctx.async();
    final Future<Void> startupTracker = Future.future();
    startupTracker.setHandler(ctx.asyncAssertSuccess(started -> {
        startup.complete();
    }));
    svc.doStart(startupTracker);
    // THEN the file gets created
    startup.await();
    verify(fileSystem).createFile(eq(FILE_NAME), any(Handler.class));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) HttpURLConnection(java.net.HttpURLConnection) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) TenantConstants(org.eclipse.hono.util.TenantConstants) RunWith(org.junit.runner.RunWith) Constants(org.eclipse.hono.util.Constants) Context(io.vertx.core.Context) Assert.assertThat(org.junit.Assert.assertThat) ArgumentCaptor(org.mockito.ArgumentCaptor) EventBus(io.vertx.core.eventbus.EventBus) Matchers.eq(org.mockito.Matchers.eq) TenantService(org.eclipse.hono.service.tenant.TenantService) Timeout(org.junit.rules.Timeout) JsonObject(io.vertx.core.json.JsonObject) Before(org.junit.Before) Vertx(io.vertx.core.Vertx) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Future(io.vertx.core.Future) StandardCharsets(java.nio.charset.StandardCharsets) Matchers.any(org.mockito.Matchers.any) Mockito(org.mockito.Mockito) JsonArray(io.vertx.core.json.JsonArray) Rule(org.junit.Rule) Buffer(io.vertx.core.buffer.Buffer) FileSystem(io.vertx.core.file.FileSystem) Handler(io.vertx.core.Handler) Async(io.vertx.ext.unit.Async) Handler(io.vertx.core.Handler) Test(org.junit.Test)

Example 12 with FileSystem

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

the class StaticHandlerImpl method getFileProps.

private synchronized void getFileProps(RoutingContext context, String file, Handler<AsyncResult<FileProps>> resultHandler) {
    FileSystem fs = context.vertx().fileSystem();
    if (alwaysAsyncFS || useAsyncFS) {
        wrapInTCCLSwitch(() -> fs.props(file, resultHandler));
    } else {
        // Use synchronous access - it might well be faster!
        long start = 0;
        if (tuning) {
            start = System.nanoTime();
        }
        try {
            FileProps props = wrapInTCCLSwitch(() -> fs.propsBlocking(file));
            if (tuning) {
                long end = System.nanoTime();
                long dur = end - start;
                totalTime += dur;
                numServesBlocking++;
                if (numServesBlocking == Long.MAX_VALUE) {
                    // Unlikely.. but...
                    resetTuning();
                } else if (numServesBlocking == nextAvgCheck) {
                    double avg = (double) totalTime / numServesBlocking;
                    if (avg > maxAvgServeTimeNanoSeconds) {
                        useAsyncFS = true;
                        log.info("Switching to async file system access in static file server as fs access is slow! (Average access time of " + avg + " ns)");
                        tuning = false;
                    }
                    nextAvgCheck += NUM_SERVES_TUNING_FS_ACCESS;
                }
            }
            resultHandler.handle(Future.succeededFuture(props));
        } catch (RuntimeException e) {
            resultHandler.handle(Future.failedFuture(e.getCause()));
        }
    }
}
Also used : FileSystem(io.vertx.core.file.FileSystem) FileProps(io.vertx.core.file.FileProps)

Example 13 with FileSystem

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

the class VxApiUserAuthUtil method auth.

/**
 * 验证用户是否正确,如果正确返回json格式的用户如果不存在返回null
 *
 * @param suser
 *            用户名字
 * @param spass
 *            用户密码
 * @param vertx
 *            vertx
 * @param handler
 *            结果
 */
public static void auth(String suser, String spass, Vertx vertx, Handler<AsyncResult<JsonObject>> handler) {
    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(suser) instanceof JsonObject) {
                JsonObject user = users.getJsonObject(suser);
                if (spass.equals(user.getString("pwd"))) {
                    handler.handle(Future.<JsonObject>succeededFuture(user));
                } else {
                    handler.handle(Future.<JsonObject>succeededFuture(null));
                }
            } else {
                handler.handle(Future.<JsonObject>succeededFuture(null));
            }
        } else {
            handler.handle(Future.failedFuture(res.cause()));
        }
    });
}
Also used : FileSystem(io.vertx.core.file.FileSystem) JsonObject(io.vertx.core.json.JsonObject)

Example 14 with FileSystem

use of io.vertx.core.file.FileSystem in project georocket by georocket.

the class StoreEndpoint method onPost.

/**
 * Handles the HTTP POST request
 * @param context the routing context
 */
private void onPost(RoutingContext context) {
    HttpServerRequest request = context.request();
    request.pause();
    String layer = getEndpointPath(context);
    String tagsStr = request.getParam("tags");
    String propertiesStr = request.getParam("props");
    String fallbackCRSString = request.getParam("fallbackCRS");
    List<String> tags = StringUtils.isNotEmpty(tagsStr) ? Splitter.on(',').trimResults().splitToList(tagsStr) : null;
    Map<String, Object> properties = new HashMap<>();
    if (StringUtils.isNotEmpty(propertiesStr)) {
        String regex = "(?<!" + Pattern.quote("\\") + ")" + Pattern.quote(":");
        String[] parts = propertiesStr.split(",");
        for (String part : parts) {
            part = part.trim();
            String[] property = part.split(regex);
            if (property.length != 2) {
                request.response().setStatusCode(400).end("Invalid property syntax: " + part);
                return;
            }
            String key = StringEscapeUtils.unescapeJava(property[0].trim());
            String value = StringEscapeUtils.unescapeJava(property[1].trim());
            properties.put(key, value);
        }
    }
    // get temporary filename
    String incoming = storagePath + "/incoming";
    String filename = new ObjectId().toString();
    String filepath = incoming + "/" + filename;
    String correlationId = UUID.randomUUID().toString();
    long startTime = System.currentTimeMillis();
    this.onReceivingFileStarted(correlationId, layer, startTime);
    // create directory for incoming files
    FileSystem fs = vertx.fileSystem();
    ObservableFuture<Void> observable = RxHelper.observableFuture();
    fs.mkdirs(incoming, observable.toHandler());
    observable.flatMap(v -> {
        // create temporary file
        ObservableFuture<AsyncFile> openObservable = RxHelper.observableFuture();
        fs.open(filepath, new OpenOptions(), openObservable.toHandler());
        return openObservable;
    }).flatMap(f -> {
        // write request body into temporary file
        ObservableFuture<Void> pumpObservable = RxHelper.observableFuture();
        Handler<AsyncResult<Void>> pumpHandler = pumpObservable.toHandler();
        Pump.pump(request, f).start();
        Handler<Throwable> errHandler = (Throwable t) -> {
            request.endHandler(null);
            f.close();
            pumpHandler.handle(Future.failedFuture(t));
        };
        f.exceptionHandler(errHandler);
        request.exceptionHandler(errHandler);
        request.endHandler(v -> {
            f.close();
            pumpHandler.handle(Future.succeededFuture());
        });
        request.resume();
        return pumpObservable;
    }).flatMap(v -> {
        String contentTypeHeader = request.getHeader("Content-Type");
        String mimeType = null;
        try {
            ContentType contentType = ContentType.parse(contentTypeHeader);
            mimeType = contentType.getMimeType();
        } catch (ParseException | IllegalArgumentException ex) {
        // mimeType already null
        }
        // detect content type of file to import
        if (mimeType == null || mimeType.trim().isEmpty() || mimeType.equals("application/octet-stream") || mimeType.equals("application/x-www-form-urlencoded")) {
            // fallback: if the client has not sent a Content-Type or if it's
            // a generic one, then try to guess it
            log.debug("Mime type '" + mimeType + "' is invalid or generic. " + "Trying to guess the right type.");
            return detectContentType(filepath).doOnNext(guessedType -> {
                log.debug("Guessed mime type '" + guessedType + "'.");
            });
        }
        return Observable.just(mimeType);
    }).subscribe(detectedContentType -> {
        long duration = System.currentTimeMillis() - startTime;
        this.onReceivingFileFinished(correlationId, duration, layer, null);
        // run importer
        JsonObject msg = new JsonObject().put("filename", filename).put("layer", layer).put("contentType", detectedContentType).put("correlationId", correlationId);
        if (tags != null) {
            msg.put("tags", new JsonArray(tags));
        }
        if (!properties.isEmpty()) {
            msg.put("properties", new JsonObject(properties));
        }
        if (fallbackCRSString != null) {
            msg.put("fallbackCRSString", fallbackCRSString);
        }
        request.response().setStatusCode(// Accepted
        202).putHeader("X-Correlation-Id", correlationId).setStatusMessage("Accepted file - importing in progress").end();
        // run importer
        vertx.eventBus().send(AddressConstants.IMPORTER_IMPORT, msg);
    }, err -> {
        long duration = System.currentTimeMillis() - startTime;
        this.onReceivingFileFinished(correlationId, duration, layer, err);
        fail(request.response(), err);
        err.printStackTrace();
        fs.delete(filepath, ar -> {
        });
    });
}
Also used : Arrays(java.util.Arrays) Router(io.vertx.ext.web.Router) RxStoreCursor(io.georocket.storage.RxStoreCursor) RoutingContext(io.vertx.ext.web.RoutingContext) StringUtils(org.apache.commons.lang3.StringUtils) ChunkMeta(io.georocket.storage.ChunkMeta) RxStore(io.georocket.storage.RxStore) StoreCursor(io.georocket.storage.StoreCursor) Single(rx.Single) Pair(org.apache.commons.lang3.tuple.Pair) Map(java.util.Map) Pump(io.vertx.core.streams.Pump) JsonObject(io.vertx.core.json.JsonObject) Logger(io.vertx.core.logging.Logger) Splitter(com.google.common.base.Splitter) OpenOptions(io.vertx.core.file.OpenOptions) ContentType(org.apache.http.entity.ContentType) UUID(java.util.UUID) Future(io.vertx.core.Future) FileNotFoundException(java.io.FileNotFoundException) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) HttpServerResponse(io.vertx.core.http.HttpServerResponse) FileSystem(io.vertx.core.file.FileSystem) RxHelper(io.vertx.rx.java.RxHelper) MultiMerger(io.georocket.output.MultiMerger) Pattern(java.util.regex.Pattern) AddressConstants(io.georocket.constants.AddressConstants) AsyncFile(io.vertx.core.file.AsyncFile) HttpServerRequest(io.vertx.core.http.HttpServerRequest) MimeTypeUtils(io.georocket.util.MimeTypeUtils) HashMap(java.util.HashMap) LoggerFactory(io.vertx.core.logging.LoggerFactory) Observable(rx.Observable) ServerAPIException(io.georocket.ServerAPIException) WriteStream(io.vertx.core.streams.WriteStream) StoreFactory(io.georocket.storage.StoreFactory) AsyncResult(io.vertx.core.AsyncResult) HttpException(io.georocket.util.HttpException) ParseException(org.apache.http.ParseException) ObservableFuture(io.vertx.rx.java.ObservableFuture) Vertx(io.vertx.core.Vertx) IOException(java.io.IOException) StringEscapeUtils(org.apache.commons.text.StringEscapeUtils) RxAsyncCursor(io.georocket.storage.RxAsyncCursor) File(java.io.File) JsonArray(io.vertx.core.json.JsonArray) ObjectId(org.bson.types.ObjectId) Merger(io.georocket.output.Merger) Handler(io.vertx.core.Handler) ConfigConstants(io.georocket.constants.ConfigConstants) OpenOptions(io.vertx.core.file.OpenOptions) ContentType(org.apache.http.entity.ContentType) HashMap(java.util.HashMap) ObjectId(org.bson.types.ObjectId) HttpServerRequest(io.vertx.core.http.HttpServerRequest) JsonObject(io.vertx.core.json.JsonObject) JsonArray(io.vertx.core.json.JsonArray) ObservableFuture(io.vertx.rx.java.ObservableFuture) FileSystem(io.vertx.core.file.FileSystem) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult)

Example 15 with FileSystem

use of io.vertx.core.file.FileSystem in project georocket by georocket.

the class FileStore method doAddChunk.

@Override
protected void doAddChunk(String chunk, String path, Handler<AsyncResult<String>> handler) {
    if (path == null || path.isEmpty()) {
        path = "/";
    }
    String dir = Paths.get(root, path).toString();
    String finalPath = path;
    // create storage folder
    vertx.fileSystem().mkdirs(dir, ar -> {
        if (ar.failed()) {
            handler.handle(Future.failedFuture(ar.cause()));
            return;
        }
        // generate new file name
        String filename = generateChunkId();
        // open new file
        FileSystem fs = vertx.fileSystem();
        fs.open(Paths.get(dir, filename).toString(), new OpenOptions(), openar -> {
            if (openar.failed()) {
                handler.handle(Future.failedFuture(openar.cause()));
                return;
            }
            // write contents to file
            AsyncFile f = openar.result();
            Buffer buf = Buffer.buffer(chunk);
            f.write(buf, 0, writear -> {
                f.close();
                if (writear.failed()) {
                    handler.handle(Future.failedFuture(writear.cause()));
                } else {
                    String result = PathUtils.join(finalPath, filename);
                    handler.handle(Future.succeededFuture(result));
                }
            });
        });
    });
}
Also used : OpenOptions(io.vertx.core.file.OpenOptions) Buffer(io.vertx.core.buffer.Buffer) FileSystem(io.vertx.core.file.FileSystem) AsyncFile(io.vertx.core.file.AsyncFile)

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