Example usage for io.vertx.core.file.impl WindowsFileSystem WindowsFileSystem

List of usage examples for io.vertx.core.file.impl WindowsFileSystem WindowsFileSystem

Introduction

In this page you can find the example usage for io.vertx.core.file.impl WindowsFileSystem WindowsFileSystem.

Prototype

public WindowsFileSystem(final VertxInternal vertx) 

Source Link

Usage

From source file:com.klwork.spring.vertx.render.MyStaticHandlerImpl.java

License:Open Source License

private synchronized void getFileProps(RoutingContext context, String file,
        Handler<AsyncResult<FileProps>> resultHandler) {
    // FileSystem fs = context.vertx().fileSystem();
    FileSystem fs = new WindowsFileSystem((VertxInternal) context.vertx());
    if (alwaysAsyncFS || useAsyncFS) {
        wrapInTCCLSwitch(() -> fs.props(file, resultHandler), resultHandler);
    } else {/*  w ww  .ja  va2s  . c o  m*/
        // Use synchronous access - it might well be faster!
        long start = 0;
        if (tuning) {
            start = System.nanoTime();
        }
        try {
            FileProps props = wrapInTCCLSwitch(() -> fs.propsBlocking(file), resultHandler);

            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 (FileSystemException e) {
            resultHandler.handle(Future.failedFuture(e.getCause()));
        }
    }
}

From source file:com.klwork.spring.vertx.render.MyStaticHandlerImpl.java

License:Open Source License

private void sendDirectoryListing(String dir, RoutingContext context) {
    FileSystem fileSystem = new WindowsFileSystem((VertxInternal) context.vertx());
    HttpServerRequest request = context.request();

    fileSystem.readDir(dir, asyncResult -> {
        if (asyncResult.failed()) {
            context.fail(asyncResult.cause());
        } else {/*from   www.  ja  v  a2 s  . c  o m*/

            String accept = request.headers().get("accept");
            if (accept == null) {
                accept = "text/plain";
            }

            if (accept.contains("html")) {
                String normalizedDir = context.normalisedPath();
                if (!normalizedDir.endsWith("/")) {
                    normalizedDir += "/";
                }

                String file;
                StringBuilder files = new StringBuilder("<ul id=\"files\">");

                List<String> list = asyncResult.result();
                Collections.sort(list);

                for (String s : list) {
                    file = s.substring(s.lastIndexOf(File.separatorChar) + 1);
                    // skip dot files
                    if (!includeHidden && file.charAt(0) == '.') {
                        continue;
                    }
                    files.append("<li><a href=\"");
                    files.append(normalizedDir);
                    files.append(file);
                    files.append("\" title=\"");
                    files.append(file);
                    files.append("\">");
                    files.append(file);
                    files.append("</a></li>");
                }

                files.append("</ul>");

                // link to parent dir
                int slashPos = 0;
                for (int i = normalizedDir.length() - 2; i > 0; i--) {
                    if (normalizedDir.charAt(i) == '/') {
                        slashPos = i;
                        break;
                    }
                }

                String parent = "<a href=\"" + normalizedDir.substring(0, slashPos + 1) + "\">..</a>";

                request.response().putHeader("content-type", "text/html");
                request.response().end(directoryTemplate(context.vertx()).replace("{directory}", normalizedDir)
                        .replace("{parent}", parent).replace("{files}", files.toString()));
            } else if (accept.contains("json")) {
                String file;
                JsonArray json = new JsonArray();

                for (String s : asyncResult.result()) {
                    file = s.substring(s.lastIndexOf(File.separatorChar) + 1);
                    // skip dot files
                    if (!includeHidden && file.charAt(0) == '.') {
                        continue;
                    }
                    json.add(file);
                }
                request.response().putHeader("content-type", "application/json");
                request.response().end(json.encode());
            } else {
                String file;
                StringBuilder buffer = new StringBuilder();

                for (String s : asyncResult.result()) {
                    file = s.substring(s.lastIndexOf(File.separatorChar) + 1);
                    // skip dot files
                    if (!includeHidden && file.charAt(0) == '.') {
                        continue;
                    }
                    buffer.append(file);
                    buffer.append('\n');
                }

                request.response().putHeader("content-type", "text/plain");
                request.response().end(buffer.toString());
            }
        }
    });
}