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.groupon.vertx.redis.RedisCommand.java

License:Apache License

/**
 * If the command represented by the JsonObject doesn't come in the form:
 * <br>//from   w w w .j av  a2  s . com
 * <code>
 * {
 *   'command': 'GET',
 *   'arguments': [ 'somekey' ]
 * }
 * </code>
 * <br>
 * Then an exception will be thrown.
 *
 * @param commandJson - The command to be created.
 */
public RedisCommand(JsonObject commandJson) {
    if (commandJson == null || commandJson.size() != 2) {
        log.warn("initRedisCommand", "failure", new String[] { "reason" }, "Invalid command format");
        throw new IllegalArgumentException("Invalid command format");
    }

    try {
        type = RedisCommandType.valueOf(commandJson.getString("command"));
    } catch (Exception ex) {
        log.warn("initRedisCommand", "failure", new String[] { "reason" }, "Invalid command");
        throw new IllegalArgumentException("Invalid or unsupported command provided");
    }

    Object objectArguments = commandJson.getValue("arguments");
    if (objectArguments != null) {
        if (objectArguments instanceof JsonArray) {
            for (Object arg : (JsonArray) objectArguments) {
                if (arg instanceof String) {
                    arguments.add((String) arg);
                } else {
                    arguments.add(arg.toString());
                }
            }
        } else {
            arguments.add(objectArguments.toString());
        }
    }
}

From source file:com.groupon.vertx.redis.RedisInputStream.java

License:Apache License

/**
 * This method is fired when enough data is in the Buffer to complete a command.  If the
 * command does not match the signature of the buffered data then an exception is thrown
 * and the socket should be closed as the command/response queues are no longer in sync.
 *
 * @param command - The command to process from the response buffer.
 *//*from   www. jav a 2s  .  c  o  m*/
private void processCommand(RedisCommand command) {
    if (command == null) {
        // No command to process so return.  Should add log message here.
        log.warn("processCommand", "noCommandFound");
        return;
    }

    JsonObject response = new JsonObject();

    byte[] line = completedLines.poll();

    if (line == null) {
        log.warn("processCommand", "noCompletedLinesFound", new String[] { "command" }, command.getCommand());
        response.put("status", "error");
        response.put("message", "Unable to find completed line for command: " + command.getCommand());
    } else if (line[0] == RedisResponseType.ERROR.marker) {
        log.warn("processCommand", "redisReturnedError", new String[] { "command" }, command.getCommand());
        response.put("status", "fail");
        response.put("data", processLine(line));
    } else if (line[0] == RedisResponseType.BULK_REPLY.marker && line[1] == '-') {
        log.debug("processCommand", "redisReturnedNil", new String[] { "command" }, command.getCommand());
        response.put("status", "success");
        response.put("data", processBulkLine(line));
    } else if (line[0] != command.getResponseType().marker) {
        log.warn("processCommand", "mismatchedResponse",
                new String[] { "command", "expectedDelim", "foundDelim" }, command.getCommand(),
                (char) command.getResponseType().marker, (char) line[0]);
        throw new RedisCommandException("Invalid response found");
    } else {
        response.put("status", "success");

        if (command.getResponseType() == RedisResponseType.MULTI_BULK_REPLY) {
            response.put("data", processMultiLine(line));
        } else if (command.getResponseType() == RedisResponseType.BULK_REPLY) {
            response.put("data", processBulkLine(line));
        } else if (command.getResponseType() == RedisResponseType.INTEGER_REPLY) {
            response.put("data", processIntegerLine(line));
        } else {
            response.put("data", processLine(line));
        }

        log.trace("processCommand", "redisCommandSuccess", new String[] { "command", "data" },
                command.getCommand(), response.getValue("data"));
    }

    command.setResponse(response);
}

From source file:com.groupon.vertx.utils.config.VerticleConfig.java

License:Apache License

public VerticleConfig(String name, JsonObject deployConfig) {

    if (name == null) {
        throw new IllegalStateException("Field `name` not specified for verticle");
    }//from ww w  . j  ava  2 s. co  m

    if (deployConfig == null) {
        throw new IllegalStateException(String.format("Verticle %s config cannot be null", name));
    }

    this.name = name;
    final Object instancesAsObject = deployConfig.getValue("instances");
    if (instancesAsObject instanceof Integer) {
        instances = (Integer) instancesAsObject;
    } else if (instancesAsObject instanceof String) {
        instances = parseInstances((String) instancesAsObject);
    } else {
        throw new ClassCastException("Unsupported class type for 'instances'");
    }
    className = deployConfig.getString("class");
    config = deployConfig.getValue("config");
    isWorker = deployConfig.getBoolean("worker", false);
    isMultiThreaded = deployConfig.getBoolean("multiThreaded", false);

    JsonArray dependencyJson = deployConfig.getJsonArray("dependencies");
    if (dependencyJson != null) {
        dependencies = new HashSet<>(dependencyJson.size());
        for (Object dep : dependencyJson) {
            if (dep instanceof String) {
                dependencies.add((String) dep);
            }
        }
    } else {
        dependencies = Collections.emptySet();
    }

    if (instances < 1) {
        throw new IllegalStateException(
                String.format("Field `instances` not specified or less than 1 for verticle %s", name));
    }

    if (className == null) {
        throw new IllegalStateException(
                String.format("Field `className` not specified for for verticle %s", name));
    }
}

From source file:com.jedlab.vertee.DatabaseServiceVertxProxyHandler.java

License:Apache License

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

        case "persist": {
            service.persist((io.vertx.core.json.JsonObject) json.getValue("document"), 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.julienviet.aeron.client.AeronClientOptionsConverter.java

License:Apache License

public static void fromJson(JsonObject json, AeronClientOptions obj) {
    if (json.getValue("directory") instanceof String) {
        obj.setDirectory((String) json.getValue("directory"));
    }//  w  w w  .j a  v  a2  s.  c o m
}

From source file:com.panjiesw.std.service.user.UserServiceVertxProxyHandler.java

License:Apache License

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

    case "save": {
        service.save((io.vertx.core.json.JsonObject) json.getValue("payload"), createHandler(msg));
        break;
    }
    case "query": {
        service.query((java.lang.String) json.getValue("sql"), createListHandler(msg));
        break;
    }
    case "queryOne": {
        service.queryOne((java.lang.String) json.getValue("sql"), createHandler(msg));
        break;
    }
    case "one": {
        service.one((java.lang.Long) json.getValue("id"), createHandler(msg));
        break;
    }
    case "findByUsername": {
        service.findByUsername((java.lang.String) json.getValue("username"), createHandler(msg));
        break;
    }
    case "findByEmail": {
        service.findByEmail((java.lang.String) json.getValue("email"), createHandler(msg));
        break;
    }
    case "start": {
        service.start(createHandler(msg));
        break;
    }
    case "stop": {
        service.stop(createHandler(msg));
        close();
        break;
    }
    default: {
        throw new IllegalStateException("Invalid action: " + action);
    }
    }
}

From source file:com.pluralsight.dockerproductionaws.portfolio.PortfolioConverter.java

License:Apache License

public static void fromJson(JsonObject json, Portfolio obj) {
    if (json.getValue("cash") instanceof Number) {
        obj.setCash(((Number) json.getValue("cash")).doubleValue());
    }/*w  w  w.j a  v a  2 s . c o m*/
    if (json.getValue("shares") instanceof JsonObject) {
        java.util.Map<String, java.lang.Integer> map = new java.util.LinkedHashMap<>();
        json.getJsonObject("shares").forEach(entry -> {
            if (entry.getValue() instanceof Number)
                map.put(entry.getKey(), ((Number) entry.getValue()).intValue());
        });
        obj.setShares(map);
    }
}

From source file:com.pluralsight.dockerproductionaws.portfolio.PortfolioServiceVertxProxyHandler.java

License:Apache License

public void handle(Message<JsonObject> msg) {
    try {/*from ww w .j a v a2s .  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 "getPortfolio": {
            service.getPortfolio(res -> {
                if (res.failed()) {
                    if (res.cause() instanceof ServiceException) {
                        msg.reply(res.cause());
                    } else {
                        msg.reply(new ServiceException(-1, res.cause().getMessage()));
                    }
                } else {
                    msg.reply(res.result() == null ? null : res.result().toJson());
                }
            });
            break;
        }
        case "buy": {
            service.buy(json.getValue("amount") == null ? null : (json.getLong("amount").intValue()),
                    (io.vertx.core.json.JsonObject) json.getValue("quote"), res -> {
                        if (res.failed()) {
                            if (res.cause() instanceof ServiceException) {
                                msg.reply(res.cause());
                            } else {
                                msg.reply(new ServiceException(-1, res.cause().getMessage()));
                            }
                        } else {
                            msg.reply(res.result() == null ? null : res.result().toJson());
                        }
                    });
            break;
        }
        case "sell": {
            service.sell(json.getValue("amount") == null ? null : (json.getLong("amount").intValue()),
                    (io.vertx.core.json.JsonObject) json.getValue("quote"), res -> {
                        if (res.failed()) {
                            if (res.cause() instanceof ServiceException) {
                                msg.reply(res.cause());
                            } else {
                                msg.reply(new ServiceException(-1, res.cause().getMessage()));
                            }
                        } else {
                            msg.reply(res.result() == null ? null : res.result().toJson());
                        }
                    });
            break;
        }
        case "evaluate": {
            service.evaluate(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.reachauto.account.AccountConverter.java

License:Apache License

public static void fromJson(JsonObject json, Account obj) {
    if (json.getValue("delFlag") instanceof String) {
        obj.setDelFlag((String) json.getValue("delFlag"));
    }/*from   w  ww . ja  v a  2s.  com*/
    if (json.getValue("id") instanceof Number) {
        obj.setId(((Number) json.getValue("id")).intValue());
    }
    if (json.getValue("password") instanceof String) {
        obj.setPassword((String) json.getValue("password"));
    }
    if (json.getValue("username") instanceof String) {
        obj.setUsername((String) json.getValue("username"));
    }
}

From source file:com.reachauto.account.AccountServiceVertxProxyHandler.java

License:Apache License

public void handle(Message<JsonObject> msg) {
    try {/*ww w  . j  a v  a2 s  .  co  m*/
        JsonObject json = msg.body();
        String action = msg.headers().get("action");
        if (action == null) {
            throw new IllegalStateException("action not specified");
        }
        accessed();
        switch (action) {
        case "addAccount": {
            service.addAccount(
                    json.getJsonObject("account") == null ? null
                            : new com.reachauto.account.Account(json.getJsonObject("account")),
                    createHandler(msg));
            break;
        }
        case "deleteAccount": {
            service.deleteAccount(json.getValue("id") == null ? null : (json.getLong("id").intValue()),
                    createHandler(msg));
            break;
        }
        case "updateAccount": {
            service.updateAccount(json.getJsonObject("account") == null ? null
                    : new com.reachauto.account.Account(json.getJsonObject("account")), res -> {
                        if (res.failed()) {
                            if (res.cause() instanceof ServiceException) {
                                msg.reply(res.cause());
                            } else {
                                msg.reply(new ServiceException(-1, res.cause().getMessage()));
                            }
                        } else {
                            msg.reply(res.result() == null ? null : res.result().toJson());
                        }
                    });
            break;
        }
        case "retrieveAccount": {
            service.retrieveAccount(json.getValue("id") == null ? null : (json.getLong("id").intValue()),
                    res -> {
                        if (res.failed()) {
                            if (res.cause() instanceof ServiceException) {
                                msg.reply(res.cause());
                            } else {
                                msg.reply(new ServiceException(-1, res.cause().getMessage()));
                            }
                        } else {
                            msg.reply(res.result() == null ? null : res.result().toJson());
                        }
                    });
            break;
        }
        default: {
            throw new IllegalStateException("Invalid action: " + action);
        }
        }
    } catch (Throwable t) {
        msg.reply(new ServiceException(500, t.getMessage()));
        throw t;
    }
}