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

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

Introduction

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

Prototype

public JsonObject put(String key, Object value) 

Source Link

Document

Put an Object into the JSON object with the specified key.

Usage

From source file:io.flowly.engine.data.manager.FlowInstanceReadManager.java

License:Open Source License

public JsonObject getInbox(String subjectId, int pageNumber, int pageSize) {
    int low = (pageNumber - 1) * pageSize;
    int high = low + pageSize;
    List<JsonObject> tasks;

    try {//from  w  ww  . j  ava 2 s . c  o m
        tasks = graph.traversal().V().has(Schema.V_P_SUBJECT_ID, subjectId).outE(Schema.E_IS_ASSIGNED)
                .has(Schema.E_P_RECEIVED_STATUS, P.within(RECEIVED_STATUSES)).order()
                .by(Schema.E_P_ASSIGNED_ON, Order.decr).range(low, high + ADDITIONAL_RECORDS).as("e_asg").inV()
                .as("v_svc")
                .choose(__.values(Schema.P_STATUS).is(RECEIVED_STATUSES.get(0)), __.select("e_asg", "v_svc"),
                        __.repeat(__.outE().inV())
                                .until(it -> !it.get().value(Schema.P_STATUS).equals(STATUS_COMPLETED))
                                .as("v_view").select("e_asg", "v_svc", "v_view"))
                .map(m -> {
                    JsonObject task = new JsonObject();
                    Vertex v = (Vertex) m.get().get("v_svc");
                    Edge e = (Edge) m.get().get("e_asg");

                    task.put(JsonKeys.TASK_ID, v.id());
                    task.put(JsonKeys.INSTANCE_ID, v.property(Schema.V_P_INSTANCE_ID).value());
                    task.put(Schema.E_P_RECEIVED_STATUS, e.property(Schema.E_P_RECEIVED_STATUS).value());
                    task.put(Schema.P_SUB_FLOW_ID, v.property(Schema.P_SUB_FLOW_ID).value());

                    // TODO: Get due dates and subject.

                    if (m.get().containsKey("v_view")) {
                        Vertex view = (Vertex) m.get().get("v_view");

                        task.put(JsonKeys.VIEW_ID, view.id());
                        task.put(Schema.P_STATUS, view.value(Schema.P_STATUS).toString());
                        task.put(JsonKeys.VIEW_ROUTE, view.value(Schema.P_SUB_FLOW_ID).toString());
                    } else {
                        task.put(Schema.P_STATUS, v.value(Schema.P_STATUS).toString());
                    }

                    return task;
                }).toList();

        commit();
    } catch (Exception ex) {
        rollback();
        tasks = new ArrayList<>();
        logger.error("Unable to get the inbox for user: " + subjectId, ex);
    }

    return createInbox(tasks, low);
}

From source file:io.flowly.engine.data.manager.FlowInstanceReadManager.java

License:Open Source License

private JsonObject createInbox(List<JsonObject> tasks, int low) {
    JsonObject inbox = new JsonObject();

    inbox.put(JsonKeys.COUNT, low + tasks.size());
    inbox.put(JsonKeys.TASKS, tasks);//ww w  .java2  s .  c om

    return inbox;
}

From source file:io.flowly.engine.verticles.Engine.java

License:Open Source License

private void broadcastFlowLifecycleEvent(String eventType, FlowInstanceMetadata metadata, boolean enabled) {
    if (!enabled) {
        return;/*from  w w w  . j  av  a  2 s.com*/
    }

    JsonObject event = new JsonObject().put(JsonKeys.FLOW_EVENT_TYPE, eventType);
    Long instanceId = metadata.getInstanceId();
    Long parentFlowObjectInstanceId = metadata.getParentFlowObjectInstanceId();

    if (instanceId != null) {
        event.put(JsonKeys.INSTANCE_ID, instanceId);
    }

    if (parentFlowObjectInstanceId != null) {
        event.put(FlowInstanceMetadata._PARENT_FLOW_OBJECT_INSTANCE_ID, parentFlowObjectInstanceId);
    }

    if (metadata.getCurrentStep() != null) {
        event.put(FlowInstanceStep._FLOW_OBJECT_INSTANCE_ID,
                metadata.getCurrentStep().getFlowObjectInstanceId());
    }

    eventBus.publish(EngineAddresses.FLOW_LIFECYCLE_EVENT, event);
}

From source file:io.flowly.engine.verticles.services.Email.java

License:Open Source License

private void registerEmailHandler() {
    vertx.eventBus().consumer(EngineAddresses.SEND_EMAIL, request -> {
        JsonObject mail = (JsonObject) request.body();
        MailMessage email = new MailMessage();

        try {/* w w w. java2 s .  co m*/
            // TODO: Should "from" variable be configured per app or per call?
            email.setFrom("test@flowly.io");

            // TODO: Test multiple send to issue.
            // If emailTo is a string, create an array.
            Object emailTo = mail.getValue(JsonKeys.EMAIL_TO);

            if (emailTo instanceof String) {
                email.setTo((String) emailTo);
            } else {
                email.setTo(mail.getJsonArray(JsonKeys.EMAIL_TO).getList());
            }

            email.setSubject(mail.getString(JsonKeys.EMAIL_SUBJECT));
            email.setHtml(mail.getString(JsonKeys.EMAIL_BODY));

            mailClient.sendMail(email, result -> {
                if (result.succeeded()) {
                    if (logger.isInfoEnabled()) {
                        logger.info("Email: " + email.getSubject() + ", sent to: " + email.getTo());
                    }

                    JsonObject message = new JsonObject();
                    message.put(JsonKeys.RESULT, true);
                    request.reply(message);
                } else {
                    Failure failure = new Failure(6000, "Failed to send email.", result.cause());
                    logger.error(failure.getError(), failure.getCause());
                    request.fail(failure.getCode(), failure.getMessage());
                }
            });
        } catch (Exception ex) {
            Failure failure = new Failure(6001, "Unable to parse email message.", ex);
            logger.error(failure.getError(), failure.getCause());
            request.fail(failure.getCode(), failure.getMessage());
        }
    });
}

From source file:io.flowly.webapp.FlowApiRouter.java

License:Open Source License

private void prepareGetInboxRoute(Router api, Vertx vertx) {
    Route getInboxRoute = api.route(HttpMethod.GET, "/inbox/:subjectId").produces(JSON_CONTENT_TYPE);
    getInboxRoute.handler(routingContext -> {
        String subjectId = routingContext.request().getParam(ObjectKeys.SUBJECT_ID);
        logger.info("Get inbox request received: " + subjectId);

        JsonObject args = new JsonObject();
        args.put(ObjectKeys.SUBJECT_ID, subjectId);

        String pageNumber = routingContext.request().getParam(ObjectKeys.PAGE_NUMBER);
        if (pageNumber != null) {
            args.put(ObjectKeys.PAGE_NUMBER, Integer.parseInt(pageNumber));
        }/*  w  ww  .  j  av a 2 s .com*/

        String pageSize = routingContext.request().getParam(ObjectKeys.PAGE_SIZE);
        if (pageSize != null) {
            args.put(ObjectKeys.PAGE_SIZE, Integer.parseInt(pageSize));
        }

        vertx.eventBus().send(ClusterAddresses.GET_USER_INBOX, args, reply -> {
            JsonObject inbox = (JsonObject) reply.result().body();
            writeResponse(routingContext, inbox.encode());
        });
    });
}

From source file:io.github.cdelmas.spike.vertx.infrastructure.auth.BearerAuthHandler.java

License:Apache License

@Override
public void handle(RoutingContext routingContext) {
    HttpServerRequest request = routingContext.request();
    request.pause();//from  w ww  .  j a  v a2s .  c o m
    String authorization = request.headers().get(HttpHeaders.AUTHORIZATION);
    if (authorization == null) {
        routingContext.fail(401);
    } else {
        String[] parts = authorization.split(" ");
        if (parts.length != 2) {
            routingContext.fail(401);
        } else {
            String scheme = parts[0];
            if (!"bearer".equalsIgnoreCase(scheme)) {
                routingContext.fail(401);
            } else {
                String token = parts[1];
                JsonObject credentials = new JsonObject();
                credentials.put("token", token);

                authProvider.authenticate(credentials, res -> {
                    if (res.succeeded()) {
                        routingContext.setUser(res.result());
                        request.resume();
                        routingContext.next();
                    } else {
                        routingContext.fail(401);
                    }
                });
            }
        }
    }
}

From source file:io.github.jdocker.DockerHost.java

License:Apache License

public JsonObject toJSON() {
    JsonObject ob = new JsonObject().put("name", getName()).put("uri", getUri());
    JsonArray propsArray = new JsonArray();
    for (Map.Entry<String, String> en : properties.entrySet()) {
        propsArray.add(new JsonObject().put(en.getKey(), en.getValue()));
    }// ww  w . j av  a2s  . c  o m
    ob.put("properties", propsArray);
    ob.put("cpus", getCPUs()).put("memory", getMemory()).put("disksize", getDiskSize()).put("status",
            getStatus().toString());
    return ob;
}

From source file:io.github.jdocker.Machine.java

License:Apache License

public JsonObject toJSON() {
    JsonObject ob = new JsonObject().put("name", getName()).put("uri", getUri());
    JsonArray propsArray = new JsonArray();
    for (Map.Entry<String, String> en : properties.entrySet()) {
        propsArray.add(new JsonObject().put(en.getKey(), en.getValue()));
    }/*ww w  .j  a  v a2 s  .  c o  m*/
    ob.put("properties", propsArray);
    return ob;
}

From source file:io.github.jdocker.MachineConfig.java

License:Apache License

public JsonObject toJSON() {
    JsonObject o = new JsonObject().put("type", MachineConfig.class.getName()).put("name", getName())
            .put("driver", getDriver()).put("uri", installURL);
    if (unmanagedMachine != null) {
        o.put("unmanagedMachine", unmanagedMachine.toJSON());
    }/*from  www  .  j  a v  a2 s  .com*/
    if (swarmConfig != null) {
        o.put("swarmConfig", swarmConfig.toJson());
    }
    // labelsl
    JsonArray array = new JsonArray();
    for (Map.Entry<String, String> en : labels.entrySet()) {
        if (en.getKey().equals(en.getValue())) {
            array.add(en.getValue());
        } else {
            array.add(en.getKey() + '=' + en.getValue());
        }
    }
    o.put("labels", array);
    array = new JsonArray();
    for (String env : machineEnvironment) {
        array.add(env);
    }
    o.put("machineEnvironment", array);
    array = new JsonArray();
    for (String env : insecureRegistries) {
        array.add(env);
    }
    o.put("insecureRegistries", array);
    array = new JsonArray();
    for (String env : engineOptions) {
        array.add(env);
    }
    o.put("engineOptions", array);
    return o;
}

From source file:io.github.jdocker.SwarmConfig.java

License:Apache License

public JsonObject toJson() {
    JsonObject o = new JsonObject();
    o.put("dockerHost", dockerHost);
    o.put("swarmImage", swarmImage);
    o.put("swarmMaster", swarmMaster);
    o.put("swarmDiscoveryToken", swarmDiscoveryToken);
    o.put("swarmStrategy", swarmStrategy);
    o.put("swarmHostUri", swarmHostURI);
    o.put("swarmAdvertizeUri", swarmAdvertizeURI);
    JsonArray array = new JsonArray();
    for (String env : swarmEnvironment) {
        array.add(env);//  w w w  . j ava  2s.  c o m
    }
    o.put("swarmEnvironment", array);
    return o;
}