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));
}
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()));
}
}
}
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()));
}
});
}
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 -> {
});
});
}
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));
}
});
});
});
}
Aggregations