Do not rely on exceptions to determine if a file exists
@@ -183,10 +183,23 @@ public class StaticHandlerImpl implements StaticHandler {
file = getFile(path, context);
}
- String sfile = file;
+ final String sfile = file;
+
+ // verify if the file exists
+ isFileExisting(context, sfile, exists -> {
+ if (exists.failed()) {
+ context.fail(exists.cause());
+ return;
+ }
+
+ // file does not exist, continue...
+ if (!exists.result()) {
+ context.next();
+ return;
+ }
// Need to read the props from the filesystem
- getFileProps(context, file, res -> {
+ getFileProps(context, sfile, res -> {
if (res.succeeded()) {
FileProps fprops = res.result();
if (fprops == null) {
@@ -198,14 +211,10 @@ public class StaticHandlerImpl implements StaticHandler {
propsCache().put(path, new CacheEntry(fprops, System.currentTimeMillis()));
sendFile(context, sfile, fprops);
}
- } else {
- if (res.cause() instanceof NoSuchFileException || (res.cause().getCause() != null && res.cause().getCause() instanceof NoSuchFileException)) {
- // this is a special case, we can't handle it as an error
- context.next();
} else {
context.fail(res.cause());
}
- }
+ });
});
}
@@ -249,6 +258,11 @@ public class StaticHandlerImpl implements StaticHandler {
}
}
+ private synchronized void isFileExisting(RoutingContext context, String file, Handler<AsyncResult<Boolean>> resultHandler) {
+ FileSystem fs = context.vertx().fileSystem();
+ wrapInTCCLSwitch(() -> fs.exists(file, resultHandler));
+ }
+
private synchronized void getFileProps(RoutingContext context, String file, Handler<AsyncResult<FileProps>> resultHandler) {
FileSystem fs = context.vertx().fileSystem();
if (alwaysAsyncFS || useAsyncFS) {
