Example usage for io.vertx.core.json JsonObject JsonObject

List of usage examples for io.vertx.core.json JsonObject JsonObject

Introduction

In this page you can find the example usage for io.vertx.core.json JsonObject JsonObject.

Prototype

public JsonObject() 

Source Link

Document

Create a new, empty instance

Usage

From source file:co.runrightfast.vertx.core.impl.VertxServiceImpl.java

License:Apache License

private Optional<JsonArray> toJsonObject(final List<Match> matches) {
    if (CollectionUtils.isEmpty(matches)) {
        return Optional.empty();
    }/*from   ww w.ja v a 2 s.c om*/

    final JsonArray jsonArray = new JsonArray();
    matches.stream().map(match -> new JsonObject().put("value", match.getValue()).put("type", match.getType()))
            .forEach(jsonArray::add);

    return Optional.of(jsonArray);
}

From source file:com.ab.controller.Controller.java

public void handlerHome(RoutingContext routingContext) {
    HttpServerResponse response = routingContext.response();
    response.putHeader("content-type", "application/json; charset=utf-8")
            .end(new JsonObject().put("status", "oke").encode());
}

From source file:com.ab.controller.Controller.java

public void handlerTest(RoutingContext routingContext) {
    HttpServerResponse response = routingContext.response();
    response.putHeader("content-type", "application/json; charset=utf-8")
            .end(new JsonObject().put("status", "Testing...").encode());
}

From source file:com.ab.main.MainApp.java

/**
 * @param args the command line arguments
 *//*from ww  w  . j a v  a 2 s .co m*/
public static void main(String[] args) {

    System.out.println("Vertx webserver started at port 8080");

    Config hazelcastConfig = new Config();

    ClusterManager mgr = new HazelcastClusterManager(hazelcastConfig);

    VertxOptions options = new VertxOptions().setClusterManager(mgr);
    int port = 8081;
    DeploymentOptions depOptions = new DeploymentOptions().setConfig(new JsonObject().put("http.port", port));
    Vertx.clusteredVertx(options, res -> {
        if (res.succeeded()) {
            Vertx vertx = res.result();
            vertx.deployVerticle(new MainVerticle(), depOptions);
            System.out.println("Server started at port " + port + "...");
        } else {
            // failed!
        }
    });
}

From source file:com.appdocker.iop.InternetOfPeopleServiceVertxEBProxy.java

License:Apache License

public void hello(String message, Handler<AsyncResult<String>> resultHandler) {
    if (closed) {
        resultHandler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return;/*from   w ww .  j  a  v  a  2 s .  c om*/
    }
    JsonObject _json = new JsonObject();
    _json.put("message", message);
    DeliveryOptions _deliveryOptions = (_options != null) ? new DeliveryOptions(_options)
            : new DeliveryOptions();
    _deliveryOptions.addHeader("action", "hello");
    _vertx.eventBus().<String>send(_address, _json, _deliveryOptions, res -> {
        if (res.failed()) {
            resultHandler.handle(Future.failedFuture(res.cause()));
        } else {
            resultHandler.handle(Future.succeededFuture(res.result().body()));
        }
    });
}

From source file:com.appdocker.iop.InternetOfPeopleServiceVertxEBProxy.java

License:Apache License

public void close() {
    if (closed) {
        throw new IllegalStateException("Proxy is closed");
    }/*from ww  w .j ava 2  s  . co m*/
    closed = true;
    JsonObject _json = new JsonObject();
    DeliveryOptions _deliveryOptions = (_options != null) ? new DeliveryOptions(_options)
            : new DeliveryOptions();
    _deliveryOptions.addHeader("action", "close");
    _vertx.eventBus().send(_address, _json, _deliveryOptions);
}

From source file:com.baldmountain.depot.DbSeed.java

License:Open Source License

public static void main(String[] args) {
    Vertx vertx = Vertx.vertx();//from  w ww  . j a  v a2 s  . c  o  m
    JsonObject config = new JsonObject().put("db_name", "depot_development");

    mongoService = MongoService.create(vertx, config);
    mongoService.start();

    mongoService.dropCollection("line_items", res1 -> {
        if (res1.succeeded()) {
            mongoService.dropCollection("carts", res2 -> {
                if (res2.succeeded()) {
                    mongoService.dropCollection("products", event1 -> {
                        if (event1.succeeded()) {
                            LinkedList<Product> products = new LinkedList<>();
                            products.add(new Product("CoffeeScript", "<p>\n"
                                    + "        CoffeeScript is JavaScript done right. It provides all of JavaScript's "
                                    + "functionality wrapped in a cleaner, more succinct syntax. In the first "
                                    + "book on this exciting new language, CoffeeScript guru Trevor Burnham "
                                    + "shows you how to hold onto all the power and flexibility of JavaScript "
                                    + "while writing clearer, cleaner, and safer code.</p>", "/images/cs.jpg",
                                    new BigDecimal(36.00).setScale(2, RoundingMode.CEILING)));
                            products.add(new Product("Programming Ruby 1.9 & 2.0",
                                    "<p>Ruby is the fastest growing and most exciting dynamic language "
                                            + "out there. If you need to get working programs delivered fast, "
                                            + "you should add Ruby to your toolbox. </p>",
                                    "/images/ruby.jpg",
                                    new BigDecimal(49.95).setScale(2, RoundingMode.CEILING)));
                            products.add(new Product("Rails Test Prescriptions",
                                    "<p><em>Rails Test Prescriptions</em> is a comprehensive guide to testing "
                                            + "Rails applications, covering Test-Driven Development from both a theoretical perspective (why to test) and from a practical perspective "
                                            + "(how to test effectively). It covers the core Rails testing tools and procedures for Rails 2 and Rails 3, and introduces popular add-ons, "
                                            + "including Cucumber, Shoulda, Machinist, Mocha, and Rcov.</p>",
                                    "/images/rtp.jpg",
                                    new BigDecimal(34.95).setScale(2, RoundingMode.CEILING)));
                            saveAProduct(products);
                        } else {
                            setDone("Trouble dropping products");
                        }
                    });
                } else {
                    setDone("Trouble dropping carts");
                }
            });
        } else {
            setDone("Trouble dropping line_items");
        }
    });

    try {
        synchronized (lock) {
            while (!done)
                lock.wait();
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    vertx.close();
}

From source file:com.baldmountain.depot.DepotVerticle.java

License:Open Source License

@CodeTranslate
@Override//from  www. j ava  2s  . c o m
public void start() throws Exception {

    JsonObject config = new JsonObject().put("db_name", "depot_development");
    mongoService = MongoService.create(vertx, config);
    mongoService.start();

    // Now do stuff with it:

    //        mongoService.count("products", new JsonObject(), res -> {
    //
    //            // ...
    //            if (res.succeeded()) {
    //                log.error("win: "+res.result());
    //            } else {
    //                log.error("fail: "+res.cause().getMessage());
    //            }
    //        });
    final Router router = Router.router(vertx);

    // We need cookies, sessions and request bodies
    router.route().handler(CookieHandler.create());
    router.route().handler(SessionHandler.create(LocalSessionStore.create(vertx)));
    router.route().handler(BodyHandler.create());

    // Simple auth service which uses a properties file for user/role info
    //        final AuthService authService = ShiroAuthService.create(vertx,
    //                ShiroAuthRealmType.PROPERTIES, new JsonObject());

    // Any requests to URI starting '/private/' require login
    //        router.route("/private/*").handler(
    //                RedirectAuthHandler.create(authService, "/loginpage.html"));

    // Serve the static private pages from directory 'private'
    //        router.route("/private/*").handler(
    //                StaticHandler.create().setCachingEnabled(false)
    //                        .setWebRoot("private"));

    // Handles the actual login
    //        router.route("/loginhandler").handler(
    //                FormLoginHandler.create(authService));

    // Implement logout
    //        router.route("/logout").handler(context -> {
    //            context.session().logout();
    //            // Redirect back to the index page
    //            context.response().putHeader("location", "/")
    //                    .setStatusCode(302).end();
    //        });

    controllers.add(new StoreController(router, mongoService).setupRoutes());
    controllers.add(new ProductsController(router, mongoService).setupRoutes());
    controllers.add(new LineItemsController(router, mongoService).setupRoutes());
    controllers.add(new CartsController(router, mongoService).setupRoutes());

    router.route("/").handler(context -> {
        HttpServerResponse response = context.response();
        response.putHeader("location", "/store");
        response.setStatusCode(302);
        response.end();
    });

    //        router.route("/").handler(context -> {
    //            Product.all(mongoService, result -> {
    //                if (result.succeeded()) {
    //                    log.info("product count: " + result.result().size());
    //                } else {
    //                    context.response().end("Hello world");
    //                }
    //            });
    //        });

    // Serve the non private static pages
    router.route().handler(StaticHandler.create());

    vertx.createHttpServer().requestHandler(router::accept).listen(8080);
}

From source file:com.baldmountain.depot.models.BaseModel.java

License:Open Source License

public BaseModel delete(MongoService service, Handler<AsyncResult<Void>> resultHandler) {
    if ("0".equals(id)) {
        resultHandler.handle(new ConcreteAsyncResult<>((Void) null));
    } else {//from   w w  w  .  j a va 2  s  . co m
        service.remove(collection, new JsonObject().put("_id", id), res -> {
            if (res.succeeded()) {
                id = null;
                resultHandler.handle(new ConcreteAsyncResult<>((Void) null));
            } else {
                resultHandler.handle(new ConcreteAsyncResult<>(res.cause()));
            }
        });
    }
    return this;
}

From source file:com.baldmountain.depot.models.Cart.java

License:Open Source License

public static void find(MongoService mongoService, String id, Handler<AsyncResult<Cart>> resultHandler) {
    assert !"0".equals(id);
    Cart cart = cache.get(id);/*w  w  w .j a  v a 2  s.c om*/
    if (cart != null) {
        resultHandler.handle(new ConcreteAsyncResult<>(cart));
    } else {
        JsonObject query = new JsonObject().put("_id", id);
        mongoService.findOne("carts", query, null, res -> {
            if (res.succeeded()) {
                Cart newCart;
                if (res.result() == null) {
                    // it's null?
                    newCart = new Cart(new JsonObject().put("_id", id));
                } else {
                    newCart = new Cart(res.result());
                }
                assert !"0".equals(id);
                cache.put(id, newCart);
                resultHandler.handle(new ConcreteAsyncResult<>(newCart));
            } else {
                resultHandler.handle(new ConcreteAsyncResult<>(res.cause()));
            }
        });
    }
}