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:org.entcore.common.events.impl.GenericEventStore.java

License:Open Source License

private JsonObject generateEvent(String eventType, UserInfos user, HttpServerRequest request,
        JsonObject customAttributes) {//w w w .j  a va 2 s .  c  om
    JsonObject event = new JsonObject();
    if (customAttributes != null && customAttributes.size() > 0) {
        event.mergeIn(customAttributes);
    }
    event.put("event-type", eventType).put("module", module).put("date", System.currentTimeMillis());
    if (user != null) {
        event.put("userId", user.getUserId()).put("profil", user.getType());
        if (user.getStructures() != null) {
            event.put("structures", new fr.wseduc.webutils.collections.JsonArray(user.getStructures()));
        }
        if (user.getClasses() != null) {
            event.put("classes", new fr.wseduc.webutils.collections.JsonArray(user.getClasses()));
        }
        if (user.getGroupsIds() != null) {
            event.put("groups", new fr.wseduc.webutils.collections.JsonArray(user.getGroupsIds()));
        }
    }
    if (request != null) {
        event.put("referer", request.headers().get("Referer"));
        event.put("sessionId", CookieHelper.getInstance().getSigned("oneSessionId", request));
    }
    return event;
}

From source file:org.entcore.common.http.request.ActionsUtils.java

License:Open Source License

public static void findWorkflowSecureActions(EventBus eb, final HttpServerRequest request,
        Controller controller) {/* ww  w  . j  a va 2  s.  c o  m*/
    final Map<String, Set<Binding>> bindings = controller.getSecuredUriBinding();
    UserUtils.getUserInfos(eb, request, new Handler<UserInfos>() {

        @Override
        public void handle(UserInfos user) {
            JsonObject actions = new JsonObject();
            if (user != null) {
                for (UserInfos.Action action : user.getAuthorizedActions()) {
                    if (bindings.containsKey(action.getName())) {
                        JsonArray b = new fr.wseduc.webutils.collections.JsonArray();
                        for (Binding binding : bindings.get(action.getName())) {
                            b.add(new JsonObject().put("verb", binding.getMethod().name())
                                    .put("path", binding.getUriPattern().pattern())
                                    .put("type", binding.getActionType().name()));
                        }
                        actions.put(action.getName(), b);
                    }
                }
            }
            Renders.renderJson(request, actions);
        }
    });
}

From source file:org.entcore.common.neo4j.Neo.java

License:Open Source License

@Deprecated
public void sendBatch(JsonArray queries, final Handler<Message<JsonObject>> handler) {
    neo4j.executeBatch(queries, new Handler<Message<JsonObject>>() {
        @Override/*from  ww  w.  ja va2s.c  o  m*/
        public void handle(Message<JsonObject> event) {
            if (handler != null) {
                JsonArray results = event.body().getJsonArray("results");
                if ("ok".equals(event.body().getString("status")) && results != null) {
                    for (Object o : results) {
                        if (!(o instanceof JsonObject))
                            continue;
                        JsonObject j = (JsonObject) o;
                        int i = 0;
                        JsonObject r = new JsonObject();
                        for (Object o2 : j.getJsonArray("result")) {
                            if (!(o2 instanceof JsonObject))
                                continue;
                            r.put(String.valueOf(i++), (JsonObject) o2);
                        }
                        j.put("result", r);
                    }
                }
                handler.handle(event);
            }
        }
    });
}

From source file:org.entcore.common.neo4j.Neo.java

License:Open Source License

@Deprecated
public void send(String query, Map<String, Object> params, final Handler<Message<JsonObject>> handler) {
    if (handler != null) {
        neo4j.execute(query, params, new Handler<Message<JsonObject>>() {
            @Override/* ww  w. ja  v  a2s .co m*/
            public void handle(Message<JsonObject> event) {
                JsonArray result = event.body().getJsonArray("result");
                if ("ok".equals(event.body().getString("status")) && result != null) {
                    int i = 0;
                    JsonObject r = new JsonObject();
                    for (Object o : result) {
                        if (!(o instanceof JsonObject))
                            continue;
                        r.put(String.valueOf(i++), (JsonObject) o);
                    }
                    event.body().put("result", r);
                }
                handler.handle(event);
            }
        });
    } else {
        neo4j.execute(query, params, (Handler<Message<JsonObject>>) null);
    }
}

From source file:org.entcore.common.neo4j.Neo4j.java

License:Open Source License

private Handler<JsonObject> resultHandler(final Handler<Message<JsonObject>> m) {
    return new Handler<JsonObject>() {

        @Override//from  w  ww.  j ava2 s.co  m
        public void handle(JsonObject res) {
            if (res.getString("message") != null) {
                log.error(res.getString("exception") + " : " + res.getString("message"));
                res.put("status", "error");
            }
            if (m != null) {
                m.handle(new ResultMessage(res));
            }
        }
    };
}

From source file:org.entcore.common.neo4j.Neo4jRest.java

License:Open Source License

private void createIndex(final JsonObject j) {
    try {//from  www  . j  a  va  2s. c o m
        final HttpClientRequest req = nodeManager.getClient().post("/db/data/index/" + j.getString("for"),
                new Handler<HttpClientResponse>() {
                    @Override
                    public void handle(HttpClientResponse event) {
                        if (event.statusCode() != 201) {
                            event.bodyHandler(new Handler<Buffer>() {
                                @Override
                                public void handle(Buffer event) {
                                    logger.error(
                                            "Error creating index : " + j.encode() + " -> " + event.toString());
                                }
                            });
                        }
                    }
                });
        JsonObject body = new JsonObject().put("name", j.getString("name"));
        body.put("config",
                new JsonObject().put("type", j.getString("type", "exact")).put("provider", "lucene"));
        req.exceptionHandler(e -> logger.error("Error creating index : " + j.encode(), e));
        req.end(body.encode());
    } catch (Neo4jConnectionException e) {
        logger.error(e.getMessage(), e);
    }
}

From source file:org.entcore.common.neo4j.Neo4jRest.java

License:Open Source License

public void executeTransaction(final JsonArray statements, final Integer transactionId, final boolean commit,
        final boolean allowRetry, final Handler<JsonObject> handler) {
    String uri = "/transaction";
    if (transactionId != null) {
        uri += "/" + transactionId;
    }//from   w w  w  . j av a 2  s.c  o  m
    if (commit) {
        uri += "/commit";
    }
    try {
        sendRequest(uri, new JsonObject().put("statements", statements), new Handler<HttpClientResponse>() {
            @Override
            public void handle(final HttpClientResponse resp) {
                resp.bodyHandler(new Handler<Buffer>() {

                    @Override
                    public void handle(Buffer b) {
                        logger.debug(b.toString());
                        if (resp.statusCode() != 404 && resp.statusCode() != 500) {
                            JsonObject json = new JsonObject(b.toString("UTF-8"));
                            JsonArray results = json.getJsonArray("results");
                            if (json.getJsonArray("errors", new fr.wseduc.webutils.collections.JsonArray())
                                    .size() == 0 && results != null) {
                                JsonArray out = new fr.wseduc.webutils.collections.JsonArray();
                                for (Object o : results) {
                                    if (!(o instanceof JsonObject))
                                        continue;
                                    out.add(transformJson((JsonObject) o));
                                }
                                json.put("results", out);
                                String commit = json.getString("commit");
                                if (commit != null) {
                                    String[] c = commit.split("/");
                                    if (c.length > 2) {
                                        json.put("transactionId", Integer.parseInt(c[c.length - 2]));
                                    }
                                }
                                json.remove("errors");
                                handler.handle(json);
                            } else {
                                if (transactionId == null && commit && allowRetry
                                        && json.getJsonArray("errors") != null
                                        && json.getJsonArray("errors").size() > 0) {
                                    JsonArray errors = json.getJsonArray("errors");
                                    for (Object o : errors) {
                                        if (!(o instanceof JsonObject))
                                            continue;
                                        switch (((JsonObject) o).getString("code", "")) {
                                        case "Neo.TransientError.Transaction.ConstraintsChanged":
                                        case "Neo.TransientError.Transaction.DeadlockDetected":
                                        case "Neo.TransientError.Transaction.InstanceStateChanged":
                                        case "Neo.TransientError.Schema.SchemaModifiedConcurrently":
                                            executeTransaction(statements, transactionId, commit, false,
                                                    handler);
                                            if (logger.isDebugEnabled()) {
                                                logger.debug("Retry transaction : " + statements.encode());
                                            }
                                            return;
                                        }
                                    }
                                }
                                handler.handle(
                                        new JsonObject().put("message",
                                                json.getJsonArray("errors",
                                                        new fr.wseduc.webutils.collections.JsonArray())
                                                        .encode()));
                            }
                        } else {
                            handler.handle(new JsonObject().put("message",
                                    resp.statusMessage() + " : " + b.toString()));
                        }
                    }
                });
            }
        });
    } catch (Neo4jConnectionException e) {
        ExceptionUtils.exceptionToJson(e);
    }
}

From source file:org.entcore.common.neo4j.Neo4jRest.java

License:Open Source License

private JsonArray transformJson(JsonObject json) {
    final JsonArray columns = json.getJsonArray("columns");
    final JsonArray data = json.getJsonArray("data");
    final JsonArray out = new fr.wseduc.webutils.collections.JsonArray();

    if (data != null && columns != null) {
        for (Object r : data) {
            JsonArray row;/*from  w  w w .  ja  v  a 2  s .co  m*/
            if (r instanceof JsonArray) {
                row = (JsonArray) r;
            } else if (r instanceof JsonObject) {
                row = ((JsonObject) r).getJsonArray("row");
            } else {
                continue;
            }
            JsonObject outRow = new fr.wseduc.webutils.collections.JsonObject();
            out.add(outRow);
            for (int j = 0; j < row.size(); j++) {
                Object value = row.getValue(j);
                if (value == null) {
                    outRow.put(columns.getString(j), (String) null);
                } else if (value instanceof String) {
                    outRow.put(columns.getString(j), (String) value);
                } else if (value instanceof JsonArray) {
                    outRow.put(columns.getString(j), (JsonArray) value);
                } else if (value instanceof JsonObject) {
                    outRow.put(columns.getString(j), (JsonObject) value);
                } else if (value instanceof Boolean) {
                    outRow.put(columns.getString(j), (Boolean) value);
                } else if (value instanceof Number) {
                    outRow.put(columns.getString(j), (Number) value);
                } else {
                    outRow.put(columns.getString(j), value.toString());
                }
            }
        }
    }
    return out;
}

From source file:org.entcore.common.neo4j.Neo4jResult.java

License:Open Source License

public static Either<String, JsonObject> fullNodeMerge(String nodeAttr, Message<JsonObject> res,
        String... otherNodes) {/*from   ww w. ja  va  2 s  . c o m*/
    Either<String, JsonObject> r = validUniqueResult(res);
    if (r.isRight() && r.right().getValue().size() > 0) {
        JsonObject j = r.right().getValue();
        JsonObject data = j.getJsonObject(nodeAttr, new JsonObject()).getJsonObject("data");
        if (otherNodes != null && otherNodes.length > 0) {
            for (String attr : otherNodes) {
                Object e = j.getValue(attr);
                if (e == null)
                    continue;
                if (e instanceof JsonObject) {
                    data.put(attr, ((JsonObject) e).getJsonObject("data"));
                } else if (e instanceof JsonArray) {
                    JsonArray a = new fr.wseduc.webutils.collections.JsonArray();
                    for (Object o : (JsonArray) e) {
                        if (!(o instanceof JsonObject))
                            continue;
                        JsonObject jo = (JsonObject) o;
                        a.add(jo.getJsonObject("data"));
                    }
                    data.put(attr, a);
                }
                j.remove(attr);
            }
        }
        if (data != null) {
            j.remove(nodeAttr);
            return new Either.Right<>(data.mergeIn(j));
        }
    }
    return r;
}

From source file:org.entcore.common.neo4j.StatementsBuilder.java

License:Open Source License

public StatementsBuilder add(String query, JsonObject params) {
    if (query != null && !query.trim().isEmpty()) {
        JsonObject statement = new JsonObject().put("statement", query);
        if (params != null) {
            statement.put("parameters", params);
        }//from w  w w.  j  av  a  2  s. c o m
        statements.add(statement);
    }
    return this;
}