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

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

Introduction

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

Prototype

public JsonObject getJsonObject(String key) 

Source Link

Document

Get the JsonObject value with the specified key

Usage

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  ww. j a  va  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.reachauto.account.AccountServiceVertxProxyHandler.java

License:Apache License

public void handle(Message<JsonObject> msg) {
    try {//from w ww  . jav a 2  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 "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;
    }
}

From source file:com.reachauto.device.DeviceServiceVertxProxyHandler.java

License:Apache License

public void handle(Message<JsonObject> msg) {
    try {/* w w w.  j  av a  2  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 "addDevice": {
            service.addDevice(
                    json.getJsonObject("device") == null ? null
                            : new com.reachauto.device.Device(json.getJsonObject("device")),
                    createHandler(msg));
            break;
        }
        case "deleteDevice": {
            service.deleteDevice((java.lang.String) json.getValue("id"), createHandler(msg));
            break;
        }
        case "updateDevice": {
            service.updateDevice(json.getJsonObject("device") == null ? null
                    : new com.reachauto.device.Device(json.getJsonObject("device")), 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 "retrieveDevice": {
            service.retrieveDevice((java.lang.String) json.getValue("id"), 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;
    }
}

From source file:com.reachauto.product.ProductServiceVertxProxyHandler.java

License:Apache License

public void handle(Message<JsonObject> msg) {
    try {//from w  w  w. j  av 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 "addProduct": {
            service.addProduct(
                    json.getJsonObject("product") == null ? null
                            : new com.reachauto.product.Product(json.getJsonObject("product")),
                    createHandler(msg));
            break;
        }
        case "deleteProduct": {
            service.deleteProduct(json.getValue("id") == null ? null : (json.getLong("id").intValue()),
                    createHandler(msg));
            break;
        }
        case "updateProduct": {
            service.updateProduct(json.getJsonObject("product") == null ? null
                    : new com.reachauto.product.Product(json.getJsonObject("product")), 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 "retrieveProduct": {
            service.retrieveProduct(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;
    }
}

From source file:com.test.mailer.MailerServiceVertxProxyHandler.java

License:Apache License

public void handle(Message<JsonObject> msg) {
    try {//w ww. jav a  2  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 "sendAttachment": {
            service.sendAttachment((java.lang.String) json.getValue("To"),
                    json.getJsonObject("attachment") == null ? null
                            : new io.vertx.ext.mail.MailAttachment(json.getJsonObject("attachment")),
                    (java.lang.String) json.getValue("Title"), 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:enmasse.kafka.bridge.converter.JsonMessageConverter.java

License:Apache License

@Override
public Message toAmqpMessage(String amqpAddress, ConsumerRecord<String, byte[]> record) {

    Message message = Proton.message();/*from   w w w .j a  va 2s.co  m*/
    message.setAddress(amqpAddress);

    // get the root JSON
    JsonObject json = new JsonObject(new String(record.value()));

    // get AMQP properties from the JSON
    JsonObject jsonProperties = json.getJsonObject(JsonMessageConverter.PROPERTIES);
    if (jsonProperties != null) {

        for (Entry<String, Object> entry : jsonProperties) {

            if (entry.getValue() != null) {

                if (entry.getKey().equals(JsonMessageConverter.MESSAGE_ID)) {
                    message.setMessageId(entry.getValue());
                } else if (entry.getKey().equals(JsonMessageConverter.TO)) {
                    message.setAddress(entry.getValue().toString());
                } else if (entry.getKey().equals(JsonMessageConverter.SUBJECT)) {
                    message.setSubject(entry.getValue().toString());
                } else if (entry.getKey().equals(JsonMessageConverter.REPLY_TO)) {
                    message.setReplyTo(entry.getValue().toString());
                } else if (entry.getKey().equals(JsonMessageConverter.CORRELATION_ID)) {
                    message.setCorrelationId(entry.getValue());
                }
            }
        }
    }

    // get AMQP application properties from the JSON
    JsonObject jsonApplicationProperties = json.getJsonObject(JsonMessageConverter.APPLICATION_PROPERTIES);
    if (jsonApplicationProperties != null) {

        Map<Symbol, Object> applicationPropertiesMap = new HashMap<>();

        for (Entry<String, Object> entry : jsonApplicationProperties) {
            applicationPropertiesMap.put(Symbol.valueOf(entry.getKey()), entry.getValue());
        }

        ApplicationProperties applicationProperties = new ApplicationProperties(applicationPropertiesMap);
        message.setApplicationProperties(applicationProperties);
    }

    // put message annotations about partition, offset and key (if not null)
    Map<Symbol, Object> messageAnnotationsMap = new HashMap<>();
    messageAnnotationsMap.put(Symbol.valueOf(Bridge.AMQP_PARTITION_ANNOTATION), record.partition());
    messageAnnotationsMap.put(Symbol.valueOf(Bridge.AMQP_OFFSET_ANNOTATION), record.offset());
    if (record.key() != null)
        messageAnnotationsMap.put(Symbol.valueOf(Bridge.AMQP_KEY_ANNOTATION), record.key());
    messageAnnotationsMap.put(Symbol.valueOf(Bridge.AMQP_TOPIC_ANNOTATION), record.topic());

    // get AMQP message annotations from the JSON
    JsonObject jsonMessageAnnotations = json.getJsonObject(JsonMessageConverter.MESSAGE_ANNOTATIONS);
    if (jsonMessageAnnotations != null) {

        for (Entry<String, Object> entry : jsonMessageAnnotations) {
            messageAnnotationsMap.put(Symbol.valueOf(entry.getKey()), entry.getValue());
        }
    }

    MessageAnnotations messageAnnotations = new MessageAnnotations(messageAnnotationsMap);
    message.setMessageAnnotations(messageAnnotations);

    // get the AMQP message body from the JSON
    JsonObject jsonBody = json.getJsonObject(JsonMessageConverter.BODY);

    if (jsonBody != null) {

        // type attribtute for following sectin : AMQP value or raw data/binary
        String type = jsonBody.getString(JsonMessageConverter.SECTION_TYPE);

        if (type.equals(JsonMessageConverter.SECTION_AMQP_VALUE_TYPE)) {

            // section is an AMQP value
            Object jsonSection = jsonBody.getValue(JsonMessageConverter.SECTION);

            // encoded as String
            if (jsonSection instanceof String) {
                message.setBody(new AmqpValue(jsonSection));
                // encoded as an array/List
            } else if (jsonSection instanceof JsonArray) {
                JsonArray jsonArray = (JsonArray) jsonSection;
                message.setBody(new AmqpValue(jsonArray.getList()));
                // encoded as a Map
            } else if (jsonSection instanceof JsonObject) {
                JsonObject jsonObject = (JsonObject) jsonSection;
                message.setBody(new AmqpValue(jsonObject.getMap()));
            }

        } else if (type.equals(JsonMessageConverter.SECTION_DATA_TYPE)) {

            // section is a raw binary data

            // get the section from the JSON (it's base64 encoded)
            byte[] value = jsonBody.getBinary(JsonMessageConverter.SECTION);

            message.setBody(new Data(new Binary(Base64.getDecoder().decode(value))));
        }
    }

    return message;
}

From source file:eu.rethink.mn.component.AllocationManager.java

License:Apache License

@Override
public void handle(PipeContext ctx) {
    final PipeMessage msg = ctx.getMessage();
    final JsonObject body = msg.getBody();

    if (msg.getType().equals("create")) {
        //process JSON msg requesting a number of available addresses
        final JsonObject msgBodyValue = body.getJsonObject("value");
        final String scheme = body.getString("scheme");

        int number = msgBodyValue.getInteger("number", 5);
        final List<String> allocated = allocate(ctx, scheme, number);

        final PipeMessage reply = new PipeMessage();
        reply.setId(msg.getId());/*from w w w. ja v a  2 s  . co  m*/
        reply.setFrom(name);
        reply.setTo(msg.getFrom());
        reply.setReplyCode(ReplyCode.OK);

        final JsonObject value = new JsonObject();
        value.put("allocated", new JsonArray(allocated));

        reply.getBody().put("value", value);

        ctx.reply(reply);

    } else if (msg.getType().equals("delete")) {
        //process JSON msg releasing an address
        final String resource = body.getString("resource");
        final JsonArray childrenResourcesList = body.getJsonArray("childrenResources");
        if (resource != null) {
            deallocate(ctx, resource);
        } else {
            for (Object childrenResource : childrenResourcesList) {
                deallocate(ctx, childrenResource.toString());
            }
        }

        ctx.replyOK(name);
    }
}

From source file:eu.rethink.mn.component.ObjectAllocationManager.java

License:Apache License

@Override
public void handle(PipeContext ctx) {
    final PipeMessage msg = ctx.getMessage();
    final JsonObject body = msg.getBody();

    if (msg.getType().equals("create")) {
        //process JSON msg requesting a number of available addresses
        final String scheme = body.getString("scheme");

        //on value
        final JsonObject msgBodyValue = body.getJsonObject("value");
        final int number = msgBodyValue.getInteger("number", 5);

        final List<String> allocated = allocate(ctx, scheme, number);

        final PipeMessage reply = new PipeMessage();
        reply.setId(msg.getId());//from   w  ww  .j a  va2s  .  c o m
        reply.setFrom(name);
        reply.setTo(msg.getFrom());
        reply.setReplyCode(ReplyCode.OK);

        final JsonObject value = new JsonObject();
        value.put("allocated", new JsonArray(allocated));

        reply.getBody().put("value", value);

        ctx.reply(reply);
    } else if (msg.getType().equals("delete")) {
        //process JSON msg releasing an address
        final String resource = body.getString("resource");

        deallocate(ctx, resource);

        ctx.replyOK(name);
    }
}

From source file:examples.MetricsExamples.java

License:Open Source License

public void naming1(Vertx vertx, MetricsService metricsService) {
    JsonObject metrics = metricsService.getMetricsSnapshot(vertx);
    metrics.getJsonObject("vertx.eventbus.handlers");
}

From source file:examples.MetricsExamples.java

License:Open Source License

public void naming2(Vertx vertx, MetricsService metricsService) {
    EventBus eventBus = vertx.eventBus();
    JsonObject metrics = metricsService.getMetricsSnapshot(eventBus);
    metrics.getJsonObject("handlers");
}