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

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

Introduction

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

Prototype

public Object getValue(String key) 

Source Link

Document

Get the value with the specified key, as an Object with types respecting the limitations of JSON.

Usage

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

License:Apache License

public void handle(Message<JsonObject> msg) {
    try {//from  w  w  w . j  a va2s .  c om
        JsonObject json = msg.body();
        String action = msg.headers().get("action");
        if (action == null) {
            throw new IllegalStateException("action not specified");
        }
        accessed();
        switch (action) {

        case "hello": {
            service.hello((java.lang.String) json.getValue("message"), createHandler(msg));
            break;
        }
        case "close": {
            service.close();
            close();
            break;
        }
        default: {
            throw new IllegalStateException("Invalid action: " + action);
        }
        }
    } catch (Throwable t) {
        msg.reply(new ServiceException(500, t.getMessage()));
        throw t;
    }
}

From source file:com.chibchasoft.vertx.verticle.deployment.DependentsDeployment.java

License:Open Source License

/**
 * Populates this object with the information from the supplied JsonObject
 * @param json The JSON Object/*from ww  w.j a  va  2  s .  com*/
 */
public void fromJson(JsonObject json) {
    Objects.requireNonNull(json, "json is required");
    if (json.getValue("configurations") instanceof JsonArray) {
        json.getJsonArray("configurations").forEach(item -> {
            if (item instanceof JsonObject) {
                DeploymentConfiguration cfg = new DeploymentConfiguration();
                cfg.fromJson((JsonObject) item);
                getConfigurations().add(cfg);
            }
        });
    }
}

From source file:com.chibchasoft.vertx.verticle.deployment.DeploymentConfiguration.java

License:Open Source License

/**
 * Populates this object with the information from the supplied JsonObject
 * @param json The JSON Object//from  ww w .  j a  v  a2  s .c  o m
 */
public void fromJson(JsonObject json) {
    Objects.requireNonNull(json, "json is required");
    if (json.getValue("name") instanceof String)
        setName((String) json.getValue("name"));
    if (json.getValue("deploymentOptions") instanceof JsonObject) {
        setDeploymentOptions(new DeploymentOptions());
        DeploymentOptionsConverter.fromJson((JsonObject) json.getValue("deploymentOptions"),
                this.getDeploymentOptions());
    }
    if (json.getValue("dependents") instanceof JsonArray) {
        json.getJsonArray("dependents").forEach(item -> {
            if (item instanceof JsonObject) {
                DependentsDeployment deps = new DependentsDeployment();
                deps.fromJson((JsonObject) item);
                getDependents().add(deps);
            }
        });
    }
}

From source file:com.cyngn.vertx.bosun.OpenTsDbMetric.java

License:Apache License

public OpenTsDbMetric(JsonObject obj) {
    if (obj == null) {
        throw new IllegalArgumentException("You must supply a non-null JsonObject");
    }//ww w  .  ja  v a2s  . c om

    this.metric = obj.getString(METRIC_FIELD);
    this.value = obj.getValue(VALUE_FIELD);
    this.tags = obj.getJsonObject(TAGS_FIELD);
    timestamp = System.currentTimeMillis();
    validateObj();
}

From source file:com.deblox.releaseboard.ReleaseBoardVerticle.java

License:Apache License

/**
 * Start the verticle//from   www .j a  v  a2  s  . c  om
 *
 * @param startFuture
 * @throws Exception
 */
public void start(Future<Void> startFuture) throws Exception {

    logger.info("starup with config: " + config().toString());

    // get expire_release in seconds
    int expire_timeout = config().getInteger("expire_timeout", 86000);

    // map of releases, should contain date events were fired in  / updated also
    releasesData = new HashMap<>();

    stateFile = config().getString("state_file", "/state.json");

    // connect to the eventbus
    eb = vertx.eventBus();

    // load the state file if exists
    vertx.fileSystem().exists(stateFile, h -> {
        if (h.succeeded()) {
            try {
                JsonArray history = Util.loadConfig(stateFile).getJsonArray("releases");
                for (Object release : history) {
                    JsonObject releaseJson = new JsonObject(release.toString());
                    logger.info("loading release: " + releaseJson.getString("id"));
                    releasesData.put(releaseJson.getString("id"), releaseJson.getJsonObject("data"));
                }

            } catch (IOException e) {
                logger.warn("unable to load state file, it will be created / overwritten");
                e.printStackTrace();
            }

        }
    });

    /*
     * listen for release events from other verticles / clients
     *
     * example release-event published direct to the eventbus ( see Server.java )
     *
            
      {
          "code": 205,
          "component": "maximus",
          "environment": "CI1",
          "status": "Deploy Succeeded",
          "version": "1.0.0.309"
      }
            
     *
     *
     */
    eb.consumer("release-event", event -> {
        logger.info(event.body().toString());

        JsonObject body = null;

        // create a json object from the message
        try {
            body = new JsonObject(event.body().toString());
        } catch (Exception e) {
            logger.warn("not a json object");
            event.reply(new JsonObject().put("result", "failure").put("reason", "that wasn't json"));
        }

        // create check if a id is specified, else combine component and version
        body.put("id", body.getString("id", body.getValue("component") + "-" + body.getValue("version")));

        // used for marking expired messages when time is not enough or too much
        body.put("expired", false);

        // add the date now
        body.put("date", LocalDateTime.now().format(formatter));

        // pop the old matching JIRA release
        releasesData.remove(body.getString("id"));

        // put the updated one
        releasesData.put(body.getString("id"), body);

        event.reply(new JsonObject().put("result", "success"));

    });

    // expire a release event and remove it from the map
    eb.consumer("expire-release-event", event -> {
        try {
            logger.info("delete event: " + event.body().toString());
            JsonObject request = new JsonObject(event.body().toString());
            releasesData.remove(request.getString("id"));

            // forulate the expire message
            JsonObject msg = new JsonObject().put("topic", "releases").put("action", "expire");
            JsonArray arr = new JsonArray().add(request.getString("id"));
            msg.put("data", arr);

            eb.publish("releases", msg);

            event.reply(new JsonObject().put("result", "success"));
        } catch (Exception e) {
            event.reply(new JsonObject().put("result", "error"));
        }
    });

    vertx.setPeriodic(10000, tid -> {

        JsonObject msg = new JsonObject();
        msg.put("topic", "releases");
        msg.put("action", "default");

        JsonArray rel = new JsonArray();

        Iterator<Map.Entry<String, JsonObject>> iter = releasesData.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry<String, JsonObject> entry = iter.next();
            rel.add(entry.getValue());
        }

        msg.put("data", rel);

        eb.publish("releases", msg);

    });

    // periodically expire old releases in the map
    vertx.setPeriodic(config().getInteger("check_expiry", 1000), res -> {
        // iterate over map, check dates for each, expire as needed

        Iterator<Map.Entry<String, JsonObject>> iter = releasesData.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry<String, JsonObject> entry = iter.next();

            logger.debug("checking expiry on " + entry.getKey() + " v " + entry.getValue());

            // now
            LocalDateTime now = LocalDateTime.now();

            // then
            LocalDateTime then = LocalDateTime.parse(entry.getValue().getString("date"), formatter);

            // delta
            Long delta = now.toEpochSecond(ZoneOffset.UTC) - then.toEpochSecond(ZoneOffset.UTC);

            if (delta >= expire_timeout) {
                logger.info("expiring stale release: " + entry.getValue() + " delta: " + delta.toString());
                iter.remove();
            }

        }

    });

    // save the current pile of releases into a JSON periodically
    vertx.setPeriodic(config().getInteger("save_interval", 60000), t -> {
        saveState();
    });

    startFuture.complete();

}

From source file:com.diabolicallabs.process.manager.service.KnowledgeServiceVertxProxyHandler.java

License:Apache License

public void handle(Message<JsonObject> msg) {
    try {//  ww  w  .ja  va2 s  .  com
        JsonObject json = msg.body();
        String action = msg.headers().get("action");
        if (action == null) {
            throw new IllegalStateException("action not specified");
        }
        accessed();
        switch (action) {

        case "addClassPathResource": {
            service.addClassPathResource((java.lang.String) json.getValue("resourceName"), createHandler(msg));
            break;
        }
        case "addFileResource": {
            service.addFileResource((java.lang.String) json.getValue("fileName"), createHandler(msg));
            break;
        }
        case "processDefinitions": {
            service.processDefinitions(createHandler(msg));
            break;
        }
        case "getProcessService": {
            service.getProcessService(res -> {
                if (res.failed()) {
                    if (res.cause() instanceof ServiceException) {
                        msg.reply(res.cause());
                    } else {
                        msg.reply(new ServiceException(-1, res.cause().getMessage()));
                    }
                } else {
                    String proxyAddress = UUID.randomUUID().toString();
                    ProxyHelper.registerService(ProcessService.class, vertx, res.result(), proxyAddress, false,
                            timeoutSeconds);
                    msg.reply(null, new DeliveryOptions().addHeader("proxyaddr", proxyAddress));
                }
            });
            break;
        }
        case "getRuleService": {
            service.getRuleService(res -> {
                if (res.failed()) {
                    if (res.cause() instanceof ServiceException) {
                        msg.reply(res.cause());
                    } else {
                        msg.reply(new ServiceException(-1, res.cause().getMessage()));
                    }
                } else {
                    String proxyAddress = UUID.randomUUID().toString();
                    ProxyHelper.registerService(RuleService.class, vertx, res.result(), proxyAddress, false,
                            timeoutSeconds);
                    msg.reply(null, new DeliveryOptions().addHeader("proxyaddr", proxyAddress));
                }
            });
            break;
        }
        case "getTaskService": {
            service.getTaskService(res -> {
                if (res.failed()) {
                    if (res.cause() instanceof ServiceException) {
                        msg.reply(res.cause());
                    } else {
                        msg.reply(new ServiceException(-1, res.cause().getMessage()));
                    }
                } else {
                    String proxyAddress = UUID.randomUUID().toString();
                    ProxyHelper.registerService(TaskService.class, vertx, res.result(), proxyAddress, false,
                            timeoutSeconds);
                    msg.reply(null, new DeliveryOptions().addHeader("proxyaddr", proxyAddress));
                }
            });
            break;
        }
        case "getTaskServiceAddress": {
            service.getTaskServiceAddress(createHandler(msg));
            break;
        }
        case "close": {
            service.close();
            close();
            break;
        }
        default: {
            throw new IllegalStateException("Invalid action: " + action);
        }
        }
    } catch (Throwable t) {
        msg.reply(new ServiceException(500, t.getMessage()));
        throw t;
    }
}

From source file:com.diabolicallabs.process.manager.service.ProcessInstanceServiceVertxProxyHandler.java

License:Apache License

public void handle(Message<JsonObject> msg) {
    try {//from www.  j a v a 2 s .c om
        JsonObject json = msg.body();
        String action = msg.headers().get("action");
        if (action == null) {
            throw new IllegalStateException("action not specified");
        }
        accessed();
        switch (action) {

        case "abort": {
            service.abort(createHandler(msg));
            break;
        }
        case "start": {
            service.start(createHandler(msg));
            break;
        }
        case "getInstanceId": {
            service.getInstanceId(createHandler(msg));
            break;
        }
        case "getName": {
            service.getName(createHandler(msg));
            break;
        }
        case "getParentInstanceId": {
            service.getParentInstanceId(createHandler(msg));
            break;
        }
        case "getState": {
            service.getState(createHandler(msg));
            break;
        }
        case "signalEvent": {
            service.signalEvent((java.lang.String) json.getValue("eventName"),
                    (io.vertx.core.json.JsonObject) json.getValue("data"), createHandler(msg));
            break;
        }
        case "close": {
            service.close();
            close();
            break;
        }
        default: {
            throw new IllegalStateException("Invalid action: " + action);
        }
        }
    } catch (Throwable t) {
        msg.reply(new ServiceException(500, t.getMessage()));
        throw t;
    }
}

From source file:com.diabolicallabs.process.manager.service.ProcessServiceVertxProxyHandler.java

License:Apache License

public void handle(Message<JsonObject> msg) {
    try {//from w w w  .java2  s.  c o  m
        JsonObject json = msg.body();
        String action = msg.headers().get("action");
        if (action == null) {
            throw new IllegalStateException("action not specified");
        }
        accessed();
        switch (action) {

        case "abort": {
            service.abort(json.getValue("processInstanceId") == null ? null
                    : (json.getLong("processInstanceId").longValue()), createHandler(msg));
            break;
        }
        case "create": {
            service.create((java.lang.String) json.getValue("processId"), res -> {
                if (res.failed()) {
                    if (res.cause() instanceof ServiceException) {
                        msg.reply(res.cause());
                    } else {
                        msg.reply(new ServiceException(-1, res.cause().getMessage()));
                    }
                } else {
                    String proxyAddress = UUID.randomUUID().toString();
                    ProxyHelper.registerService(ProcessInstanceService.class, vertx, res.result(), proxyAddress,
                            false, timeoutSeconds);
                    msg.reply(null, new DeliveryOptions().addHeader("proxyaddr", proxyAddress));
                }
            });
            break;
        }
        case "createWithVariables": {
            service.createWithVariables((java.lang.String) json.getValue("processId"),
                    (io.vertx.core.json.JsonObject) json.getValue("variables"), res -> {
                        if (res.failed()) {
                            if (res.cause() instanceof ServiceException) {
                                msg.reply(res.cause());
                            } else {
                                msg.reply(new ServiceException(-1, res.cause().getMessage()));
                            }
                        } else {
                            String proxyAddress = UUID.randomUUID().toString();
                            ProxyHelper.registerService(ProcessInstanceService.class, vertx, res.result(),
                                    proxyAddress, false, timeoutSeconds);
                            msg.reply(null, new DeliveryOptions().addHeader("proxyaddr", proxyAddress));
                        }
                    });
            break;
        }
        case "signalEvent": {
            service.signalEvent((java.lang.String) json.getValue("eventName"),
                    (io.vertx.core.json.JsonObject) json.getValue("data"), createHandler(msg));
            break;
        }
        case "signalEventForProcess": {
            service.signalEventForProcess((java.lang.String) json.getValue("eventName"),
                    json.getValue("processInstanceId") == null ? null
                            : (json.getLong("processInstanceId").longValue()),
                    (io.vertx.core.json.JsonObject) json.getValue("data"), createHandler(msg));
            break;
        }
        case "startProcess": {
            service.startProcess((java.lang.String) json.getValue("processId"), res -> {
                if (res.failed()) {
                    if (res.cause() instanceof ServiceException) {
                        msg.reply(res.cause());
                    } else {
                        msg.reply(new ServiceException(-1, res.cause().getMessage()));
                    }
                } else {
                    String proxyAddress = UUID.randomUUID().toString();
                    ProxyHelper.registerService(ProcessInstanceService.class, vertx, res.result(), proxyAddress,
                            false, timeoutSeconds);
                    msg.reply(null, new DeliveryOptions().addHeader("proxyaddr", proxyAddress));
                }
            });
            break;
        }
        case "startProcessWithVariables": {
            service.startProcessWithVariables((java.lang.String) json.getValue("processId"),
                    (io.vertx.core.json.JsonObject) json.getValue("jsonObject"), res -> {
                        if (res.failed()) {
                            if (res.cause() instanceof ServiceException) {
                                msg.reply(res.cause());
                            } else {
                                msg.reply(new ServiceException(-1, res.cause().getMessage()));
                            }
                        } else {
                            String proxyAddress = UUID.randomUUID().toString();
                            ProxyHelper.registerService(ProcessInstanceService.class, vertx, res.result(),
                                    proxyAddress, false, timeoutSeconds);
                            msg.reply(null, new DeliveryOptions().addHeader("proxyaddr", proxyAddress));
                        }
                    });
            break;
        }
        default: {
            throw new IllegalStateException("Invalid action: " + action);
        }
        }
    } catch (Throwable t) {
        msg.reply(new ServiceException(500, t.getMessage()));
        throw t;
    }
}

From source file:com.diabolicallabs.process.manager.service.RuleServiceVertxProxyHandler.java

License:Apache License

public void handle(Message<JsonObject> msg) {
    try {//from  w w w .j av  a 2s .  c om
        JsonObject json = msg.body();
        String action = msg.headers().get("action");
        if (action == null) {
            throw new IllegalStateException("action not specified");
        }
        accessed();
        switch (action) {

        case "delete": {
            service.delete((java.lang.String) json.getValue("factHandle"), createHandler(msg));
            break;
        }
        case "fireAllRules": {
            service.fireAllRules(createHandler(msg));
            break;
        }
        case "getQueryResults": {
            service.getQueryResults((java.lang.String) json.getValue("queryName"),
                    (java.lang.String) json.getValue("resultName"), createHandler(msg));
            break;
        }
        case "insert": {
            service.insert((java.lang.String) json.getValue("packageName"),
                    (java.lang.String) json.getValue("typeName"),
                    (io.vertx.core.json.JsonObject) json.getValue("attributes"), createHandler(msg));
            break;
        }
        case "update": {
            service.update((java.lang.String) json.getValue("factHandle"),
                    (java.lang.String) json.getValue("factType"),
                    (io.vertx.core.json.JsonObject) json.getValue("attributes"), createHandler(msg));
            break;
        }
        default: {
            throw new IllegalStateException("Invalid action: " + action);
        }
        }
    } catch (Throwable t) {
        msg.reply(new ServiceException(500, t.getMessage()));
        throw t;
    }
}

From source file:com.diabolicallabs.process.manager.service.TaskServiceVertxProxyHandler.java

License:Apache License

public void handle(Message<JsonObject> msg) {
    try {/*  w w w .  j  av a2  s .  c o m*/
        JsonObject json = msg.body();
        String action = msg.headers().get("action");
        if (action == null) {
            throw new IllegalStateException("action not specified");
        }
        accessed();
        switch (action) {

        case "addComment": {
            service.addComment(json.getValue("taskId") == null ? null : (json.getLong("taskId").longValue()),
                    (java.lang.String) json.getValue("userId"), (java.lang.String) json.getValue("comment"),
                    createHandler(msg));
            break;
        }
        case "claim": {
            service.claim(json.getValue("taskId") == null ? null : (json.getLong("taskId").longValue()),
                    (java.lang.String) json.getValue("userId"), createHandler(msg));
            break;
        }
        case "complete": {
            service.complete(json.getValue("taskId") == null ? null : (json.getLong("taskId").longValue()),
                    (java.lang.String) json.getValue("userId"),
                    (io.vertx.core.json.JsonObject) json.getValue("data"), createHandler(msg));
            break;
        }
        case "delegate": {
            service.delegate(json.getValue("taskId") == null ? null : (json.getLong("taskId").longValue()),
                    (java.lang.String) json.getValue("userId"), (java.lang.String) json.getValue("newUserId"),
                    createHandler(msg));
            break;
        }
        case "exit": {
            service.exit(json.getValue("taskId") == null ? null : (json.getLong("taskId").longValue()),
                    (java.lang.String) json.getValue("userId"), createHandler(msg));
            break;
        }
        case "fail": {
            service.fail(json.getValue("taskId") == null ? null : (json.getLong("taskId").longValue()),
                    (java.lang.String) json.getValue("userId"),
                    (io.vertx.core.json.JsonObject) json.getValue("data"), createHandler(msg));
            break;
        }
        case "forward": {
            service.forward(json.getValue("taskId") == null ? null : (json.getLong("taskId").longValue()),
                    (java.lang.String) json.getValue("userId"), (java.lang.String) json.getValue("newUserId"),
                    createHandler(msg));
            break;
        }
        case "getContent": {
            service.getContent(json.getValue("taskId") == null ? null : (json.getLong("taskId").longValue()),
                    createHandler(msg));
            break;
        }
        case "release": {
            service.release(json.getValue("taskId") == null ? null : (json.getLong("taskId").longValue()),
                    (java.lang.String) json.getValue("userId"), createHandler(msg));
            break;
        }
        case "resume": {
            service.resume(json.getValue("taskId") == null ? null : (json.getLong("taskId").longValue()),
                    (java.lang.String) json.getValue("userId"), createHandler(msg));
            break;
        }
        case "skip": {
            service.skip(json.getValue("taskId") == null ? null : (json.getLong("taskId").longValue()),
                    (java.lang.String) json.getValue("userId"), createHandler(msg));
            break;
        }
        case "start": {
            service.start(json.getValue("taskId") == null ? null : (json.getLong("taskId").longValue()),
                    (java.lang.String) json.getValue("userId"), createHandler(msg));
            break;
        }
        case "stop": {
            service.stop(json.getValue("taskId") == null ? null : (json.getLong("taskId").longValue()),
                    (java.lang.String) json.getValue("userId"), createHandler(msg));
            break;
        }
        case "suspend": {
            service.suspend(json.getValue("taskId") == null ? null : (json.getLong("taskId").longValue()),
                    (java.lang.String) json.getValue("userId"), createHandler(msg));
            break;
        }
        default: {
            throw new IllegalStateException("Invalid action: " + action);
        }
        }
    } catch (Throwable t) {
        msg.reply(new ServiceException(500, t.getMessage()));
        throw t;
    }
}