List of usage examples for io.vertx.core.json JsonObject getString
public String getString(String key)
From source file:com.hubrick.vertx.s3.client.S3ClientOptions.java
License:Apache License
public S3ClientOptions(final JsonObject json) { super(json);/*from w w w. j a v a2 s . c o m*/ setSignPayload(json.getBoolean("signPayload")); setAwsAccessKey(json.getString("awsAccessKey")); setAwsSecretKey(json.getString("awsSecretKey")); setAwsRegion(json.getString("awsRegion")); setAwsServiceName(json.getString("awsServiceName")); setGlobalTimeoutMs(json.getLong("globalTimeoutMs")); setHostnameOverride(json.getString("hostnameOverride")); }
From source file:com.iot.i4.server.I4Server.java
@Override public void start() throws Exception { // Create a mongo client using all defaults (connect to localhost and default port) using the database name "demo". JsonObject config = new JsonObject().put("connection_string", "mongodb://localhost:27017").put("db_name", "i4"); mongo = MongoClient.createShared(Vertx.vertx(), config); // the load function just populates some data on the storage loadData(mongo);/*from w w w . j a v a 2 s . com*/ Router router = Router.router(Vertx.vertx()); router.route().handler(BodyHandler.create()); //1.findAll payloads router.get("/api/payloads").handler(ctx -> { mongo.find("payloads", new JsonObject(), lookup -> { // error handling if (lookup.failed()) { ctx.fail(500); return; } // now convert the list to a JsonArray because it will be easier to encode the final object as the response. final JsonArray json = new JsonArray(); for (JsonObject o : lookup.result()) { json.add(o); } ctx.response().putHeader(HttpHeaders.CONTENT_TYPE, "application/json"); ctx.response().end(json.encode()); }); }); //1. findAll router.get("/api/users").handler(ctx -> { mongo.find("users", new JsonObject(), lookup -> { // error handling if (lookup.failed()) { ctx.fail(500); return; } // now convert the list to a JsonArray because it will be easier to encode the final object as the response. final JsonArray json = new JsonArray(); for (JsonObject o : lookup.result()) { json.add(o); } ctx.response().putHeader(HttpHeaders.CONTENT_TYPE, "application/json"); ctx.response().end(json.encode()); }); }); //2.findOne router.get("/api/users/:id").handler(ctx -> { mongo.findOne("users", new JsonObject().put("_id", ctx.request().getParam("id")), null, lookup -> { // error handling if (lookup.failed()) { ctx.fail(500); return; } JsonObject user = lookup.result(); if (user == null) { ctx.fail(404); } else { ctx.response().putHeader(HttpHeaders.CONTENT_TYPE, "application/json"); ctx.response().end(user.encode()); } }); }); router.post("/api/users").handler(ctx -> { JsonObject newUser = ctx.getBodyAsJson(); mongo.findOne("users", new JsonObject().put("username", newUser.getString("username")), null, lookup -> { // error handling if (lookup.failed()) { ctx.fail(500); return; } JsonObject user = lookup.result(); if (user != null) { // already exists ctx.fail(500); } else { mongo.insert("users", newUser, insert -> { // error handling if (insert.failed()) { ctx.fail(500); return; } // add the generated id to the user object newUser.put("_id", insert.result()); ctx.response().putHeader(HttpHeaders.CONTENT_TYPE, "application/json"); ctx.response().end(newUser.encode()); }); } }); }); router.put("/api/users/:id").handler(ctx -> { mongo.findOne("users", new JsonObject().put("_id", ctx.request().getParam("id")), null, lookup -> { // error handling if (lookup.failed()) { ctx.fail(500); return; } JsonObject user = lookup.result(); if (user == null) { // does not exist ctx.fail(404); } else { // update the user properties JsonObject update = ctx.getBodyAsJson(); user.put("username", update.getString("username")); user.put("firstName", update.getString("firstName")); user.put("lastName", update.getString("lastName")); user.put("address", update.getString("address")); mongo.replace("users", new JsonObject().put("_id", ctx.request().getParam("id")), user, replace -> { // error handling if (replace.failed()) { ctx.fail(500); return; } ctx.response().putHeader(HttpHeaders.CONTENT_TYPE, "application/json"); ctx.response().end(user.encode()); }); } }); }); router.delete("/api/users/:id").handler(ctx -> { mongo.findOne("users", new JsonObject().put("_id", ctx.request().getParam("id")), null, lookup -> { // error handling if (lookup.failed()) { ctx.fail(500); return; } JsonObject user = lookup.result(); if (user == null) { // does not exist ctx.fail(404); } else { mongo.remove("users", new JsonObject().put("_id", ctx.request().getParam("id")), remove -> { // error handling if (remove.failed()) { ctx.fail(500); return; } ctx.response().setStatusCode(204); ctx.response().end(); }); } }); }); // Create a router endpoint for the static content. router.route().handler(StaticHandler.create()); Vertx.vertx().createHttpServer().requestHandler(router::accept).listen(8080); //Vertx.vertx().createHttpServer().requestHandler(req -> req.response().end("Hello World From I4Server!")).listen(8080); System.out.println("Server started & listening to [8080]"); }
From source file:com.nasa.ElasticSearchAdapter.java
public JsonObject getSymptoms(String word) throws IOException { JsonObject result = new JsonObject(); JsonArray matchesArr = new JsonArray(); String data = "{\n" + " \"size\": 0,\n" + " \"aggs\" : {\n" + " \"langs\" : {\n" + " \"terms\" : { \"field\" : \"symptoms.name\" }\n" + " }\n" + " }\n" + "}"; JsonObject response = doPost(ES_URL + "/healthy-travel/feedback/_search", data); JsonArray items = response.getJsonObject("aggregations").getJsonObject("langs").getJsonArray("buckets"); for (Object item : items) { JsonObject obj = (JsonObject) item; String key = obj.getString("key"); if (key != null && obj.getString("key").startsWith(word)) { matchesArr.add(key);/* w w w. j a va 2 s. c om*/ } } result.put("symptoms", matchesArr); return result; }
From source file:com.nasa.ElasticSearchAdapter.java
public JsonObject getConditions(String word) throws IOException { JsonObject result = new JsonObject(); JsonArray matchesArr = new JsonArray(); String data = "{\n" + " \"size\": 0,\n" + " \"aggs\" : {\n" + " \"langs\" : {\n" + " \"terms\" : { \"field\" : \"condition\" }\n" + " }\n" + " }\n" + "}"; JsonObject response = doPost(ES_URL + "/healthy-travel/feedback/_search", data); JsonArray items = response.getJsonObject("aggregations").getJsonObject("langs").getJsonArray("buckets"); for (Object item : items) { JsonObject obj = (JsonObject) item; String key = obj.getString("key"); if (key != null && obj.getString("key").startsWith(word)) { matchesArr.add(key);/* w ww .jav a 2 s. co m*/ } } result.put("conditions", matchesArr); return result; }
From source file:com.nasa.ElasticSearchAdapter.java
public JsonArray getCheck(Float lat, Float lon) throws IOException { JsonArray matchesArr = new JsonArray(); String data = "{\n" + " \"size\": 0,\n" + " \"query\": {\n" + " \"filtered\": {\n" + " \"query\": {\n" + " \"match_all\": {}\n" + " },\n" + " \"filter\": {\n" + " \"geo_distance\": {\n" + " \"distance\": \"100km\",\n" + " \"location\": {\n" + " \"lat\": " + lat + ",\n" + " \"lon\": " + lon + "\n" + " }\n" + " }\n" + " }\n" + " }\n" + " },\n" + " \"aggs\": {\n" + " \"langs\": {\n" + " \"terms\": {\n" + " \"field\": \"condition\"\n" + " }\n" + " }\n" + " }\n" + "}"; JsonObject response = doPost(ES_URL + "/healthy-travel/feedback/_search", data); JsonArray items = response.getJsonObject("aggregations").getJsonObject("langs").getJsonArray("buckets"); for (Object item : items) { JsonObject obj = (JsonObject) item; String key = obj.getString("key"); int value = obj.getInteger("doc_count"); if (key != null && value > 0) { JsonObject cond = new JsonObject(); cond.put("name", key); cond.put("reported_cases", value); matchesArr.add(cond);//from w w w. j a v a 2s. c o m } } return matchesArr; }
From source file:com.nasa.Transformer.java
public static JsonObject conditionToMap(JsonObject jsonObject) { // private String conditionsMap = "{\n" // + " \"Diseases\": [\n" // + " {\n" // + " \"lat\": 46.77029284,\n" // + " \"lng\": 23.57641889,\n" // + " \"type\": \"something good\",\n" // + " \"description\": \"good\",\n" // + " \"rating\": 1.23\n" // + " },\n" // + " {\n" // + " \"lat\": 46.78185201,\n" // + " \"lng\": 23.68522613,\n" // + " \"type\": \"something not bad\",\n" // + " \"description\": \"not bad\",\n" // + " \"rating\": 3.99\n" // + " },\n" // + " {\n" // + " \"lat\": 46.7558097,\n" // + " \"lng\": 23.5940353,\n" // + " \"type\": \"something bad\",\n" // + " \"description\": \"bad\",\n" // + " \"rating\": 4.75\n" // + " }\n" // + " ]\n" // + "}"; JsonObject result = new JsonObject(); JsonArray featuresArr = new JsonArray(); JsonArray items = jsonObject.getJsonObject("hits").getJsonArray("hits"); for (Object hit : items) { JsonObject source = ((JsonObject) hit).getJsonObject("_source"); JsonObject feature = new JsonObject(); Float lon = source.getJsonObject("location").getFloat("lon"); Float lat = source.getJsonObject("location").getFloat("lat"); feature.put("lat", lat); feature.put("lng", lon); feature.put("type", source.getString("condition")); String symptoms = ""; int ratingSum = 0; int ratingCount = 0; JsonArray symptomsJ = source.getJsonArray("symptoms"); for (Object sym : symptomsJ) { JsonObject symJ = (JsonObject) sym; symptoms += symJ.getString("name") + ", "; ratingSum += symJ.getInteger("rating"); ratingCount++;// w w w. ja v a 2 s .c o m } feature.put("description", symptoms); if (ratingCount > 0) { feature.put("rating", ratingSum / ratingCount); } else { feature.put("rating", 0); } featuresArr.add(feature); } result.put("Diseases", featuresArr); return result; }
From source file:com.tad.vertx.events.EventReceiver.java
License:Apache License
public void onSilly(@Observes JsonObject json) { System.out.println("Received JSON Payload" + json.getString("foo")); }
From source file:com.themonkee.vertx.web.impl.MongoSessionStoreImpl.java
License:Open Source License
public MongoSessionStoreImpl(Vertx vertx, MongoClient mongoClient, JsonObject options, Future<MongoSessionStore> resultHandler) { this.random = new PRNG(vertx); this.vertx = vertx; this.mongoClient = mongoClient; if (options != null) { if (options.containsKey("collection")) this.sessionCollection = options.getString("collection"); }/* w w w . j a va 2s . c om*/ Future<Void> futCreateColl = Future.future(); // try to create collection, if it is created or already exists its OK this.mongoClient.createCollection(this.sessionCollection, (AsyncResult<Void> res) -> { if (res.succeeded() || res.cause().getMessage().contains("collection already exists")) { futCreateColl.complete(); } else { futCreateColl.fail(res.cause()); } }); futCreateColl.compose(v -> { // create the session expiry index // SessionImpl sets _expire field to Date when session document must be deleted on save // so we set expireAfterSeconds to 0 so its deleted when that Date is hit // see https://docs.mongodb.com/manual/tutorial/expire-data/ this.mongoClient.createIndexWithOptions(this.sessionCollection, new JsonObject().put(SessionImpl.EXPIRE_FIELD, 1), new IndexOptions().expireAfter(0L, TimeUnit.SECONDS), res -> { if (res.succeeded()) { resultHandler.complete(this); } else { resultHandler.fail(res.cause()); } }); }, resultHandler); }
From source file:com.waves_rsp.ikb4stream.communication.web.VertxServer.java
License:Open Source License
/** * Convert a request from Json to Java object * * @param jsonRequest {@link JsonObject} json formatted request * @return {@link Request}/*from w ww.j a v a 2 s . c om*/ * @throws NullPointerException if jsonRequest is null */ private Request parseRequest(JsonObject jsonRequest) { Objects.requireNonNull(jsonRequest); Date start = new Date(jsonRequest.getLong("start")); Date end = new Date(jsonRequest.getLong("end")); String address = jsonRequest.getString("address"); Geocoder geocoder = Geocoder.geocode(address); if (geocoder.getLatLong() == null) { LOGGER.warn("Can't geocode this address {}", address); return null; } return new Request(start, end, new BoundingBox(geocoder.getBbox()), Date.from(Instant.now())); }
From source file:de.bischinger.anotherblog.RestVerticle.java
License:Open Source License
private void handleAddBlog(RoutingContext routingContext) { String blogKey = routingContext.request().getParam("blogKey"); HttpServerResponse response = routingContext.response(); if (blogKey == null) { sendError(400, response);/*from w ww . j av a 2 s . co m*/ } else { JsonObject blog = routingContext.getBodyAsJson(); if (blog == null) { sendError(400, response); } else { String id = blog.getString("title"); IndexRequest indexRequest = new IndexRequest(indexName, typeName, id).source(blog.toString()); vertx.executeBlocking(future -> { client.index(indexRequest).actionGet(); future.complete(); }, res -> response.end()); } } }