Package org.vertx.java.core.file

Examples of org.vertx.java.core.file.FileSystem


     * @param request
     * @param dir
     * @param next
     */
    private void sendDirectory(final YokeRequest request, final String dir, final Handler<Object> next) {
        final FileSystem fileSystem = vertx().fileSystem();

        fileSystem.readDir(dir, new AsyncResultHandler<String[]>() {
            @Override
            public void handle(AsyncResult<String[]> asyncResult) {
                if (asyncResult.failed()) {
                    next.handle(asyncResult.cause());
                } else {
View Full Code Here


                    next.handle(null);
                    return;
                }
            }

            final FileSystem fileSystem = vertx().fileSystem();

            fileSystem.exists(file, new AsyncResultHandler<Boolean>() {
                @Override
                public void handle(AsyncResult<Boolean> asyncResult) {
                    if (asyncResult.failed()) {
                        next.handle(asyncResult.cause());
                    } else {
                        if (!asyncResult.result()) {
                            // no static file found, let the next middleware handle it
                            next.handle(null);
                        } else {
                            fileSystem.props(file, new AsyncResultHandler<FileProps>() {
                                @Override
                                public void handle(AsyncResult<FileProps> props) {
                                    if (props.failed()) {
                                        next.handle(props.cause());
                                    } else {
View Full Code Here

    @Override
    public void start() {
        final Config config = new Config(container.config());
        final EventBus eb = vertx.eventBus();
        final FileSystem fileSystem = vertx.fileSystem();

        final RedisClient db = new RedisClient(eb, Config.REDIS_ADDRESS);

        db.deployModule(container, config.dbServer, config.dbPort);

        final Yoke yoke = new Yoke(this);
        // register jMustache render engine
        yoke.engine("shtml", new StringPlaceholderEngine("com/jetdrone/vertx/kitcms/views"));
        // log the requests
        yoke.use(new Logger());
        // install the pretty error handler middleware
        yoke.use(new ErrorHandler(true));
        // install the favicon middleware
        yoke.use(new Favicon());
        // install custom middleware to identify the domain
        yoke.use(new DomainMiddleware(config));
        // install the static file server
        // note that since we are mounting under /static the root for the static middleware
        // will always be prefixed with /static
        yoke.use("/static", new Static("."));
        // install the BasicAuth middleware
        yoke.use("/admin", new BasicAuth(config.adminUsername, config.adminPassword));
        // install body parser for /admin requests
        yoke.use("/admin", new BodyParser());
        // install router for admin requests
        yoke.use(new Router()
            .get("/admin", new Middleware() {
                @Override
                public void handle(final YokeRequest request, final Handler<Object> next) {
                    final Config.Domain domain = request.get("domain");

                    db.keys(domain.namespace + "&*", new Handler<Message<JsonObject>>() {
                        @Override
                        public void handle(Message<JsonObject> message) {
                            if (!"ok".equals(message.body().getString("status"))) {
                                next.handle(message.body().getString("message"));
                            } else {
                                StringBuilder keys = new StringBuilder();
                                JsonArray json = message.body().getArray("value");

                                for (int i = 0; i < json.size(); i++) {
                                    keys.append("<li data-value=\"");
                                    keys.append(json.get(i));
                                    keys.append("\">");
                                    keys.append(json.get(i));
                                    keys.append("</li>");
                                }

                                request.put("keys", keys);
                                request.response().render("admin.shtml", next);
                            }
                        }
                    });
                }
            })
            .get("/admin/keys", new Middleware() {
                @Override
                public void handle(final YokeRequest request, final Handler<Object> next) {
                    final Config.Domain domain = request.get("domain");

                    db.keys(domain.namespace + "&*", new Handler<Message<JsonObject>>() {
                        @Override
                        public void handle(Message<JsonObject> message) {
                            if (!"ok".equals(message.body().getString("status"))) {
                                next.handle(message.body().getString("message"));
                            } else {
                                request.response().end(message.body().getString("value"));
                            }
                        }
                    });
                }
            })
            .get("/admin/get", new Middleware() {
                @Override
                public void handle(final YokeRequest request, final Handler<Object> next) {
                    final Config.Domain domain = request.get("domain");
                    String key = request.params().get("key");

                    if (key == null) {
                        request.response().end("Missing key");
                        return;
                    }

                    db.get(domain.namespace + "&" + key, new Handler<Message<JsonObject>>() {
                        @Override
                        public void handle(Message<JsonObject> message) {
                            if (!"ok".equals(message.body().getString("status"))) {
                                next.handle(message.body().getString("message"));
                            } else {
                                request.response().setContentType("application/json");
                                // TODO: escape
                                request.response().end("\"" + message.body().getString("value") + "\"");
                            }
                        }
                    });
                }
            })
            .post("/admin/set", new Middleware() {
                @Override
                public void handle(final YokeRequest request, final Handler<Object> next) {
                    final Config.Domain domain = request.get("domain");

                    MultiMap body = request.formAttributes();

                    String key = body.get("key");
                    String value = body.get("value");

                    if (key == null) {
                        request.response().end("Missing key");
                        return;
                    }

                    if (value == null) {
                        request.response().end("Missing value");
                        return;
                    }

                    db.set(domain.namespace + "&" + key, value, new Handler<Message<JsonObject>>() {
                        @Override
                        public void handle(Message<JsonObject> message) {
                            if (!"ok".equals(message.body().getString("status"))) {
                                next.handle(message.body().getString("message"));
                            } else {
                                request.response().setContentType("application/json");
                                request.response().end("\"OK\"");
                            }
                        }
                    });
                }
            })
            .post("/admin/unset", new Middleware() {
                @Override
                public void handle(final YokeRequest request, final Handler<Object> next) {
                    final Config.Domain domain = request.get("domain");

                    MultiMap body = request.formAttributes();

                    String key = body.get("key");

                    if (key == null) {
                        request.response().end("Missing key");
                        return;
                    }

                    db.del(domain.namespace + "&" + key, new Handler<Message<JsonObject>>() {
                        @Override
                        public void handle(Message<JsonObject> message) {
                            if (!"ok".equals(message.body().getString("status"))) {
                                next.handle(message.body().getString("message"));
                            } else {
                                request.response().setContentType("application/json");
                                request.response().end("\"OK\"");
                            }
                        }
                    });
                }
            })
            .get("/admin/export", new Middleware() {
                @Override
                public void handle(final YokeRequest request, final Handler<Object> next) {
                    final Config.Domain domain = request.get("domain");

                    db.keys(domain.namespace + "&*", new Handler<Message<JsonObject>>() {
                        @Override
                        public void handle(Message<JsonObject> message) {
                            if (!"ok".equals(message.body().getString("status"))) {
                                next.handle(message.body().getString("message"));
                            } else {
                                // need to iterate all json array elements and get from redis
                                new AsyncIterator<Object>(message.body().getArray("value")) {

                                    final JsonArray buffer = new JsonArray();

                                    @Override
                                    public void handle(Object o) {
                                        if (hasNext()) {
                                            final String key = (String) o;
                                            db.get(domain.namespace + "&" + key, new Handler<Message<JsonObject>>() {
                                                @Override
                                                public void handle(Message<JsonObject> message) {
                                                    if (!"ok".equals(message.body().getString("status"))) {
                                                        next.handle(message.body().getString("message"));
                                                    } else {
                                                        JsonObject json = new JsonObject();
                                                        json.putString("key", key);
                                                        json.putString("value", message.body().getString("value"));
                                                        buffer.addObject(json);

                                                        next();
                                                    }
                                                }
                                            });
                                        } else {
                                            YokeResponse response = request.response();

                                            String filename = System.currentTimeMillis() + "_export.kit";

                                            response.putHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
                                            response.end(buffer);
                                        }
                                    }
                                };
                            }
                        }
                    });
                }
            })
            .post("/admin/import", new Middleware() {
                @Override
                public void handle(final YokeRequest request, final Handler<Object> next) {
                    final Config.Domain domain = request.get("domain");

                    final YokeFileUpload file = request.getFile("file");

                    fileSystem.readFile(file.path(), new AsyncResultHandler<Buffer>() {
                        @Override
                        public void handle(AsyncResult<Buffer> read) {
                            // delete the temp file
                            file.delete();
                            // parse
View Full Code Here

     *
     * @param filename File to look for
     * @param next next asynchronous handler
     */
    public void isFresh(final String filename, final Handler<Boolean> next) {
        final FileSystem fileSystem = vertx.fileSystem();

        fileSystem.props(filename, new AsyncResultHandler<FileProps>() {
            @Override
            public void handle(AsyncResult<FileProps> asyncResult) {
                if (asyncResult.failed()) {
                    next.handle(false);
                } else {
View Full Code Here

            }
        });
    }

    private void loadToCache(final String filename, final Handler<Throwable> next) {
        final FileSystem fileSystem = vertx.fileSystem();

        fileSystem.props(filename, new AsyncResultHandler<FileProps>() {
            @Override
            public void handle(AsyncResult<FileProps> asyncResult) {
                if (asyncResult.failed()) {
                    next.handle(asyncResult.cause());
                } else {
                    final Date lastModified = asyncResult.result().lastModifiedTime();
                    // load from the file system
                    fileSystem.readFile(filename, new AsyncResultHandler<Buffer>() {
                        @Override
                        public void handle(AsyncResult<Buffer> asyncResult) {
                            if (asyncResult.failed()) {
                                next.handle(asyncResult.cause());
                            } else {
View Full Code Here

     * Verifies if a file in the filesystem is still fresh against the cache. Errors are treated as not fresh.
     *
     * @param filename File to look for
     */
    public boolean isFresh(final String filename) {
        final FileSystem fileSystem = vertx.fileSystem();

        try {
            FileProps fileProps = fileSystem.propsSync(filename);
            LRUCache.CacheEntry<String, T> cacheEntry = cache.get(filename);
            final Date lastModified = fileProps.lastModifiedTime();

            if (cacheEntry == null) {
                return false;
View Full Code Here

        }
        return cacheEntry.lastModified;
    }

    private void loadToCache(final String filename) {
        final FileSystem fileSystem = vertx.fileSystem();

        if (fileSystem.existsSync(filename)) {
            FileProps fileProps = fileSystem.propsSync(filename);
            final Date lastModified = fileProps.lastModifiedTime();
            // load from the file system
            Buffer content = fileSystem.readFileSync(filename);
            // cache the result
            cache.put(filename, new LRUCache.CacheEntry<String, T>(lastModified, content.toString(contentEncoding())));
        }
    }
View Full Code Here

        templateResolver.setPrefix(prefix);
    }

    @Override
    public void setVertx(Vertx vertx) {
        final FileSystem fs = vertx.fileSystem();

        templateResolver.setResourceResolver(new IResourceResolver() {

            @Override
            public String getName() {
                return "Yoke/Thymeleaf";
            }

            @Override
            public InputStream getResourceAsStream(TemplateProcessingParameters templateProcessingParameters, String resourceName) {

                if (!fs.existsSync(resourceName)) {
                    return null;
                }

                final Buffer buffer = fs.readFileSync(resourceName);

                return new InputStream() {
                    int pos = 0;

                    @Override
View Full Code Here

TOP

Related Classes of org.vertx.java.core.file.FileSystem

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.