Package com.jetdrone.vertx.yoke

Examples of com.jetdrone.vertx.yoke.Middleware


    public void testIssue88() {
        Yoke yoke = new Yoke(this)
                .set("title", "Yoke 1.0.7: Issue #88")
                .engine("hbs", new HandlebarsEngine("issue88/"))
                .use(new ErrorHandler(true))
                .use("/$", new Middleware() {
                            @Override
                            public void handle(
                                    YokeRequest request,
                                    Handler<Object> next) {
                                try {
View Full Code Here


    public void testIsTooBusy() throws Exception {

        final Yoke yoke = new Yoke(this);
        final TooBusy tooBusy = new TooBusy();
        yoke.use(tooBusy);
        yoke.use(new Middleware() {
            double cnt = 0;
            final SecureRandom rand = new SecureRandom();

            @Override
            public void handle(@NotNull YokeRequest request, @NotNull Handler<Object> next) {
View Full Code Here

        new Yoke(this)
                // register the MVEL engine
                .engine("mvel", new MVELEngine("views"))

                // Test 1: JSON serialization
                .use("/json", new Middleware() {
                    @Override
                    public void handle(YokeRequest request, Handler<Object> next) {
                        // For each request, an object mapping the key message to Hello, World! must be instantiated.
                        request.response().end(new JsonObject().putString("message", "Hello, World!"));
                    }
                })
                // Test 2: db
                .use("/db", new Middleware() {
                    @Override
                    public void handle(final YokeRequest request, final Handler<Object> next) {

                        final Random random = ThreadLocalRandom.current();

                        eb.send(address, buildQuery("findone", "world", random.nextInt(10000) + 1), new Handler<Message<JsonObject>>() {
                            @Override
                            public void handle(Message<JsonObject> message) {
                                // get the body
                                final JsonObject body = message.body();

                                if ("ok".equals(body.getString("status"))) {
                                    // json -> string serialization
                                    request.response().end(body.getObject("result"));
                                    return;
                                }

                                next.handle(body.getString("message"));
                            }
                        });
                    }
                })
                // Test 3: queries
                .use("/queries", new Middleware() {
                    @Override
                    public void handle(final YokeRequest request, Handler<Object> next) {

                        final Random random = ThreadLocalRandom.current();

                        // Get the count of queries to run.
                        final int count = parseRequestParam(request, "queries");

                        final Handler<Message<JsonObject>> dbh = new Handler<Message<JsonObject>>() {
                            // how many messages have this handler received
                            int received = 0;
                            // keeps the received messages
                            JsonArray result = new JsonArray();

                            @Override
                            public void handle(Message<JsonObject> message) {
                                // increase the counter
                                received++;

                                // get the body
                                final JsonObject body = message.body();

                                if ("ok".equals(body.getString("status"))) {
                                    // json -> string serialization
                                    result.add(body.getObject("result"));
                                }

                                // end condition
                                if (received == count) {
                                    request.response().end(result);
                                }
                            }
                        };

                        for (int i = 0; i < count; i++) {
                            eb.send(address, buildQuery("findone", "world", random.nextInt(10000) + 1), dbh);
                        }
                    }
                })
                // Test 4: fortune
                .use("/fortunes", new Middleware() {
                    @Override
                    public void handle(final YokeRequest request, final Handler<Object> next) {

                        final List<JsonObject> results = new ArrayList<>();

                        eb.send(address, buildQuery("find", "fortune"), new Handler<Message<JsonObject>>() {
                            @Override
                            public void handle(Message<JsonObject> reply) {
                                String status = reply.body().getString("status");

                                if (status != null) {
                                    if ("ok".equalsIgnoreCase(status)) {
                                        JsonArray itResult = reply.body().getArray("results");
                                        for (Object o : itResult) {
                                            results.add((JsonObject) o);
                                        }
                                        // end condition
                                        results.add(new JsonObject().putNumber("id", 0).putString("message", "Additional fortune added at request time."));
                                        // sort ASC
                                        Collections.sort(results, new Comparator<JsonObject>() {
                                            @Override
                                            public int compare(JsonObject o1, JsonObject o2) {
                                                return o1.getNumber("id").intValue() - o2.getNumber("id").intValue();
                                            }
                                        });
                                        // render
                                        request.put("fortunes", results);
                                        request.response().render("fortunes.mvel", next);
                                        return;
                                    }
                                    if ("more-exist".equalsIgnoreCase(status)) {
                                        JsonArray itResult = reply.body().getArray("results");
                                        for (Object o : itResult) {
                                            results.add((JsonObject) o);
                                        }
                                        // reply asking for more
                                        reply.reply(this);
                                        return;
                                    }
                                }
                                next.handle("error");
                            }
                        });
                    }
                })
                // Test 5: updates
                .use("/updates", new Middleware() {
                    @Override
                    public void handle(final YokeRequest request, final Handler<Object> next) {

                        final Random random = ThreadLocalRandom.current();

                        // Get the count of queries to run.
                        final int count = parseRequestParam(request, "queries");

                        final Handler<Message<JsonObject>> dbh = new Handler<Message<JsonObject>>() {
                            // how many messages have this handler received
                            int received = 0;
                            // keeps the received messages
                            JsonArray worlds = new JsonArray();

                            @Override
                            public void handle(Message<JsonObject> message) {

                                // get the body
                                final JsonObject body = message.body();
                                JsonObject world;

                                if ("ok".equals(body.getString("status"))) {
                                    world = new JsonObject()
                                            .putNumber("id", received)
                                            .putNumber("randomNumber", body.getObject("result").getNumber("randomNumber"));
                                    worlds.add(world);

                                    world.putNumber("randomNumber", random.nextInt(10000) + 1);
                                    eb.send(address, buildUpdate("world", world.getInteger("id"), world.getInteger("randomNumber")));
                                }

                                // increase the counter
                                received++;

                                // end condition
                                if (received == count) {
                                    request.response().end(worlds);
                                }
                            }
                        };

                        for (int i = 0; i < count; i++) {
                            eb.send(address, buildQuery("findone", "world", random.nextInt(10000) + 1), dbh);
                        }
                    }
                })
                // Test 6: plain text
                .use("/plaintext", new Middleware() {
                    @Override
                    public void handle(YokeRequest request, Handler<Object> next) {
                        request.response().setContentType("text/plain");
                        // Write plaintext "Hello, World!" to the response.
                        request.response().end("Hello, World!");
View Full Code Here

        yoke.use(new Static("static"));
        yoke.use(new ErrorHandler(true));

        // routes
        yoke.use(new Router()
                .get("/", new Middleware() {
                    @Override
                    public void handle(final YokeRequest request, final Handler<Object> next) {
                        JsonObject sessionData = request.get("session");

                        if (sessionData == null) {
                            // no session
                            request.put("email", "null");
                        } else {
                            String email = sessionData.getString("email");

                            if (email == null) {
                                request.put("email", "null");
                            } else {
                                request.put("email", "'" + email + "'");
                            }
                        }

                        request.response().render("index.shtml", next);
                    }
                })
                .post("/auth/logout", new Middleware() {
                    @Override
                    public void handle(YokeRequest request, Handler<Object> next) {
                        // destroy session
                        request.destroySession();
                        // send OK
                        request.response().end(new JsonObject().putBoolean("success", true));
                    }
                })
                .post("/auth/login", new Middleware() {
                    @Override
                    public void handle(final YokeRequest request, final Handler<Object> next) {
                        String data;

                        try {
View Full Code Here

            this.prefix = prefix + "api-docs";
        } else {
            this.prefix = prefix + "/api-docs";
        }

        this.router.get(this.prefix, new Middleware() {
            @Override
            public void handle(@NotNull YokeRequest request, @NotNull Handler<Object> next) {
                JsonObject result = new JsonObject()
                        .putString("apiVersion", apiVersion)
                        .putString("swaggerVersion", "1.2");
View Full Code Here

        }

        final Resource resource = new Resource(path, description);
        resources.add(resource);

        router.get(prefix + path, new Middleware() {
            @Override
            public void handle(@NotNull YokeRequest request, @NotNull Handler<Object> next) {
                JsonObject result = new JsonObject()
                        .putString("apiVersion", apiVersion)
                        .putString("swaggerVersion", "1.2")
View Full Code Here

    @Test
    public void testRouterWithParams() {
        Yoke yoke = new Yoke(this);
        yoke.use(new com.jetdrone.vertx.yoke.middleware.Router() {{
            get("/api/:userId", new Middleware() {
                @Override
                public void handle(@NotNull YokeRequest request, @NotNull Handler<Object> next) {

                    assertNotNull(request.get("user"));
                    assertTrue(request.get("user") instanceof JsonObject);
                    request.response().end("OK");
                }
            });
            param("userId", new Middleware() {
                @Override
                public void handle(@NotNull YokeRequest request, @NotNull Handler<Object> next) {
                    assertEquals("1", request.params().get("userId"));
                    // pretend that we went on some DB and got a json object representing the user
                    request.put("user", new JsonObject("{\"id\":" + request.params().get("userId") + "}"));
View Full Code Here

    @Test
    public void testRouterWithRegExParamsFail() {
        Yoke yoke = new Yoke(this);
        yoke.use(new com.jetdrone.vertx.yoke.middleware.Router() {{
            get("/api/:userId", new Middleware() {
                @Override
                public void handle(@NotNull YokeRequest request, @NotNull Handler<Object> next) {
                    request.response().end("OK");
                }
            });
View Full Code Here

    @Test
    public void testRouterWithRegExParamsPass() {
        Yoke yoke = new Yoke(this);
        yoke.use(new com.jetdrone.vertx.yoke.middleware.Router() {{
            get("/api/:userId", new Middleware() {
                @Override
                public void handle(@NotNull YokeRequest request, @NotNull Handler<Object> next) {
                    request.response().end("OK");
                }
            });
View Full Code Here

    @Test
    public void testTrailingSlashes() {
        final Yoke yoke = new Yoke(this);
        yoke.use(new com.jetdrone.vertx.yoke.middleware.Router() {{
            get("/api", new Middleware() {
                @Override
                public void handle(@NotNull YokeRequest request, @NotNull Handler<Object> next) {
                    request.response().end("OK");
                }
            });
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.