Package com.jetdrone.vertx.yoke

Examples of com.jetdrone.vertx.yoke.Middleware


            out.close();
            final String location = temp.getAbsolutePath();

            Yoke yoke = new Yoke(this);
            yoke.engine("mvel", new MVELEngine(""));
            yoke.use(new Middleware() {
                @Override
                public void handle(YokeRequest request, Handler<Object> next) {
                    request.put("name", "Paulo");
                    request.response().render(location, next);
                }
View Full Code Here


            out.close();
            final String location = temp.getAbsolutePath();

            Yoke yoke = new Yoke(this);
            yoke.engine("shtml", new com.jetdrone.vertx.yoke.engine.StringPlaceholderEngine(""));
            yoke.use(new Middleware() {
                @Override
                public void handle(@NotNull YokeRequest request, @NotNull Handler<Object> next) {
                    request.put("name", "Paulo");
                    request.response().render(location, next);
                }
View Full Code Here

                public String exec(Map<String, Object> context, Object... args) {
                    return "Paulo " + args[0];
                }
            });
            yoke.engine("shtml", new com.jetdrone.vertx.yoke.engine.StringPlaceholderEngine(""));
            yoke.use(new Middleware() {
                @Override
                public void handle(@NotNull YokeRequest request, @NotNull Handler<Object> next) {
                    request.put("name", "Paulo");
                    request.response().render(location, next);
                }
View Full Code Here

            out.close();
            final String location = temp.getAbsolutePath();

            Yoke yoke = new Yoke(this);
            yoke.engine("mvel", new MVELEngine(""));
            yoke.use(new Middleware() {
                @Override
                public void handle(YokeRequest request, Handler<Object> next) {
                    List<Map<String, String>> list = new ArrayList<>();
                    Map<String, String> item = new HashMap<>();
                    item.put("uri", "a");
View Full Code Here

        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
                            JsonArray json = new JsonArray(read.result().toString());
                            // iterate
                            new AsyncIterator<Object>(json) {
                                @Override
                                public void handle(Object o) {
                                    if (hasNext()) {
                                        final JsonObject json = (JsonObject) o;
                                        db.set(domain.namespace + "&" + json.getString("key"), json.getString("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 {
                                                    next();
                                                }
                                            }
                                        });
                                    } else {
                                        request.response().redirect("/admin");
                                    }
                                }
                            };
                        }
                    });
                }
            }));

        // if the request fall through it is a view to render from the db
        yoke.use(new Middleware() {
            @Override
            public void handle(final YokeRequest request, final Handler<Object> next) {
                final Config.Domain domain = request.get("domain");
                final String file = request.path().toLowerCase();

View Full Code Here

public class YokeBench extends Verticle {

    @Override
    public void start() {

        final Middleware foo = new Middleware() {
            @Override
            public void handle(YokeRequest request, Handler<Object> next) {
                next.handle(null);
            }
        };
View Full Code Here

    @Test
    public void testEngine() {
        Yoke yoke = new Yoke(this);
        yoke.engine("jade", new Jade4JEngine("views"));
        yoke.use(new Middleware() {
            @Override
            public void handle(YokeRequest request, Handler<Object> next) {
                request.response().render("template.jade");
            }
        });
View Full Code Here

    @Test
    public void testEngine2() {
        Yoke yoke = new Yoke(this);
        yoke.engine("jade", new Jade4JEngine("views"));
        yoke.use(new Middleware() {
            @Override
            public void handle(YokeRequest request, Handler<Object> next) {
                request.put("pageName", "Vert.X Test");
                request.response().render("template2.jade");
            }
View Full Code Here

    @Test
    public void testBasicAuth() {
        final Yoke yoke = new Yoke(this);
        yoke.use(new com.jetdrone.vertx.yoke.middleware.BasicAuth("Aladdin", "open sesame"));
        yoke.use(new Middleware() {
            @Override
            public void handle(@NotNull YokeRequest request, @NotNull Handler<Object> next) {
                request.response().end();
            }
        });
View Full Code Here

                    result.handle(null);
                }
            }
        }));

        yoke.use(new Middleware() {
            @Override
            public void handle(@NotNull YokeRequest request, @NotNull Handler<Object> next) {
                request.response().end();
            }
        });
View Full Code Here

TOP

Related Classes of com.jetdrone.vertx.yoke.Middleware

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.