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:org.entcore.feeder.utils.Validator.java

License:Open Source License

protected Validator(JsonObject schema, boolean notStoreLogins) {
    if (schema == null || schema.size() == 0) {
        throw new IllegalArgumentException("Missing schema.");
    }/*ww w  . j  ava2  s. c o m*/
    this.validate = schema.getJsonObject("validate");
    this.generate = schema.getJsonObject("generate");
    this.required = schema.getJsonArray("required");
    this.modifiable = schema.getJsonArray("modifiable");
    if (validate == null || generate == null || required == null || modifiable == null) {
        throw new IllegalArgumentException("Invalid schema.");
    }
    this.notStoreLogins = notStoreLogins;
}

From source file:org.entcore.portal.controllers.PortalController.java

License:Open Source License

private String getSkinFromConditions(HttpServerRequest request) {
    if (request == null) {
        return defaultSkin;
    }//  ww w.  j  av a 2  s.  co  m
    final String overrideTheme = CookieHelper.get("theme", request);
    if (isNotEmpty(overrideTheme)) {
        return overrideTheme;
    }
    if (request instanceof SecureHttpServerRequest
            && ((SecureHttpServerRequest) request).getSession() != null) {
        JsonObject cache = ((SecureHttpServerRequest) request).getSession().getJsonObject("cache");
        if (cache != null && cache.getJsonObject("preferences") != null) {
            final String theme = cache.getJsonObject("preferences").getString("theme");
            if (isNotEmpty(theme)) {
                return theme;
            }
        }
    }
    String skin = hostSkin.get(getHost(request));
    return (skin != null && !skin.trim().isEmpty()) ? skin : defaultSkin;
}

From source file:org.entcore.registry.services.impl.DefaultExternalApplicationService.java

License:Open Source License

@Override
public void listExternalApps(String structureId, final Handler<Either<String, JsonArray>> handler) {
    String filter = "";
    JsonObject params = null;//from w w w  .  j a  v a 2 s.c  om
    if (structureId != null && !structureId.trim().isEmpty()) {
        filter = ", (s:Structure)-[:HAS_ATTACHMENT*0..]->(p:Structure) "
                + "WHERE HAS(app.structureId) AND s.id = {structure} AND p.id = app.structureId AND r.structureId = app.structureId "
                + "AND (app.inherits = true OR p = s) ";
        params = new JsonObject().put("structure", structureId);
    }
    String query = "MATCH (app:Application:External)-[:PROVIDE]->(act:Action)<-[:AUTHORIZE]-(r:Role) " + filter
            + "WITH app, r, collect(distinct act) as roleActions " + "MATCH (app)-[:PROVIDE]->(action:Action) "
            + "RETURN distinct app as application, collect(action) as actions, collect(distinct {role: r, actions: roleActions}) as roles";
    neo.execute(query, params, validResultHandler(new Handler<Either<String, JsonArray>>() {
        public void handle(Either<String, JsonArray> event) {
            if (event.isLeft()) {
                handler.handle(event);
                return;
            }
            JsonArray rows = event.right().getValue();
            for (Object objRow : rows) {
                JsonObject row = (JsonObject) objRow;
                JsonObject application = row.getJsonObject("application");
                JsonArray actions = row.getJsonArray("actions");
                JsonArray roles = row.getJsonArray("roles");

                JsonObject appData = application.getJsonObject("data");
                JsonArray scope = appData.getJsonArray("scope");
                if (scope != null && scope.size() > 0) {
                    appData.put("scope", Joiner.on(" ").join(scope));
                } else {
                    appData.put("scope", "");
                }
                row.put("data", appData);
                row.remove("application");

                JsonArray actionsCopy = new fr.wseduc.webutils.collections.JsonArray();
                for (Object actionObj : actions) {
                    JsonObject action = (JsonObject) actionObj;
                    JsonObject data = action.getJsonObject("data");
                    actionsCopy.add(data);
                }
                row.put("actions", actionsCopy);

                for (Object roleObj : roles) {
                    JsonObject role = (JsonObject) roleObj;
                    JsonObject data = role.getJsonObject("role").getJsonObject("data");
                    role.put("role", data);
                    JsonArray acts = role.getJsonArray("actions");
                    JsonArray actsCopy = new fr.wseduc.webutils.collections.JsonArray();
                    for (Object actionObj : acts) {
                        JsonObject action = (JsonObject) actionObj;
                        actsCopy.add(action.getJsonObject("data"));
                    }
                    role.put("actions", actsCopy);

                }
            }

            handler.handle(event);
        }
    }));
}

From source file:org.entcore.session.AuthManager.java

License:Open Source License

private void doAddAttribute(Message<JsonObject> message) {
    JsonObject session = getSessionByUserId(message);
    if (session == null) {
        return;//from w w w  . j  a va  2 s. co  m
    }

    String key = message.body().getString("key");
    if (key == null || key.trim().isEmpty()) {
        sendError(message, "Invalid key.");
        return;
    }

    Object value = message.body().getValue("value");
    if (value == null) {
        sendError(message, "Invalid value.");
        return;
    }

    session.getJsonObject("cache").put(key, value);

    updateSessionByUserId(message, session);
    sendOK(message);
}

From source file:org.entcore.session.AuthManager.java

License:Open Source License

private void doRemoveAttribute(Message<JsonObject> message) {
    JsonObject session = getSessionByUserId(message);
    if (session == null) {
        return;/*from   ww  w .j  av  a 2s . c  om*/
    }

    String key = message.body().getString("key");
    if (key == null || key.trim().isEmpty()) {
        sendError(message, "Invalid key.");
        return;
    }

    session.getJsonObject("cache").remove(key);
    updateSessionByUserId(message, session);
    sendOK(message);
}

From source file:org.entcore.session.AuthManager.java

License:Open Source License

private void generateSessionInfos(final String userId, final Handler<JsonObject> handler) {
    final String query = "MATCH (n:User {id : {id}}) " + "WHERE HAS(n.login) "
            + "OPTIONAL MATCH n-[:IN]->(gp:Group) " + "OPTIONAL MATCH gp-[:DEPENDS]->(s:Structure) "
            + "OPTIONAL MATCH gp-[:DEPENDS]->(c:Class) "
            + "OPTIONAL MATCH n-[rf:HAS_FUNCTION]->fg-[:CONTAINS_FUNCTION*0..1]->(f:Function) "
            + "OPTIONAL MATCH n<-[:RELATED]-(child:User) " + "RETURN distinct "
            + "n.classes as classNames, n.level as level, n.login as login, COLLECT(distinct [c.id, c.name]) as classes, "
            + "n.lastName as lastName, n.firstName as firstName, n.externalId as externalId, n.federated as federated, "
            + "n.birthDate as birthDate, "
            + "n.displayName as username, HEAD(n.profiles) as type, COLLECT(distinct [child.id, child.lastName, child.firstName]) as childrenInfo, "
            + "COLLECT(distinct [s.id, s.name]) as structures, COLLECT(distinct [f.externalId, rf.scope]) as functions, "
            + "COLLECT(distinct s.UAI) as uai, "
            + "COLLECT(distinct gp.id) as groupsIds, n.federatedIDP as federatedIDP, n.functions as aafFunctions";
    final String query2 = "MATCH (n:User {id : {id}})-[:IN]->()-[:AUTHORIZED]->(:Role)-[:AUTHORIZE]->(a:Action)"
            + "<-[:PROVIDE]-(app:Application) " + "WHERE HAS(n.login) "
            + "RETURN DISTINCT COLLECT(distinct [a.name,a.displayName,a.type]) as authorizedActions, "
            + "COLLECT(distinct [app.name,app.address,app.icon,app.target,app.displayName,app.display,app.prefix]) as apps";
    final String query3 = "MATCH (u:User {id: {id}})-[:IN]->(g:Group)-[auth:AUTHORIZED]->(w:Widget) "
            + "WHERE HAS(u.login) "
            + "AND ( NOT(w<-[:HAS_WIDGET]-(:Application)-[:PROVIDE]->(:WorkflowAction)) "
            + "XOR w<-[:HAS_WIDGET]-(:Application)-[:PROVIDE]->(:WorkflowAction)<-[:AUTHORIZE]-(:Role)<-[:AUTHORIZED]-g )  "
            + "OPTIONAL MATCH (w)<-[:HAS_WIDGET]-(app:Application) "
            + "WITH w, app, collect(auth) as authorizations " + "RETURN DISTINCT COLLECT({"
            + "id: w.id, name: w.name, " + "path: coalesce(app.address, '') + w.path, "
            + "js: coalesce(app.address, '') + w.js, " + "i18n: coalesce(app.address, '') + w.i18n, "
            + "application: app.name, "
            + "mandatory: ANY(a IN authorizations WHERE HAS(a.mandatory) AND a.mandatory = true)"
            + "}) as widgets";
    final String query4 = "MATCH (s:Structure) return s.id as id, s.externalId as externalId";
    final String query5 = "MATCH (u:User {id: {id}})-[:PREFERS]->(uac:UserAppConf) RETURN uac AS preferences";
    JsonObject params = new JsonObject();
    params.put("id", userId);
    JsonArray statements = new fr.wseduc.webutils.collections.JsonArray()
            .add(new JsonObject().put("statement", query).put("parameters", params))
            .add(new JsonObject().put("statement", query2).put("parameters", params))
            .add(new JsonObject().put("statement", query3).put("parameters", params))
            .add(new JsonObject().put("statement", query4))
            .add(new JsonObject().put("statement", query5).put("parameters", params));
    neo4j.executeTransaction(statements, null, true, new Handler<Message<JsonObject>>() {

        @Override//from www  .ja va 2  s. com
        public void handle(Message<JsonObject> message) {
            JsonArray results = message.body().getJsonArray("results");
            if ("ok".equals(message.body().getString("status")) && results != null && results.size() == 5
                    && results.getJsonArray(0).size() > 0 && results.getJsonArray(1).size() > 0) {
                JsonObject j = results.getJsonArray(0).getJsonObject(0);
                JsonObject j2 = results.getJsonArray(1).getJsonObject(0);
                JsonObject j3 = results.getJsonArray(2).getJsonObject(0);
                JsonObject structureMapping = new JsonObject();
                for (Object o : results.getJsonArray(3)) {
                    if (!(o instanceof JsonObject))
                        continue;
                    JsonObject jsonObject = (JsonObject) o;
                    structureMapping.put(jsonObject.getString("externalId"), jsonObject.getString("id"));
                }
                j.put("userId", userId);
                JsonObject functions = new JsonObject();
                JsonArray actions = new fr.wseduc.webutils.collections.JsonArray();
                JsonArray apps = new fr.wseduc.webutils.collections.JsonArray();
                for (Object o : getOrElse(j2.getJsonArray("authorizedActions"),
                        new fr.wseduc.webutils.collections.JsonArray())) {
                    if (!(o instanceof JsonArray))
                        continue;
                    JsonArray a = (JsonArray) o;
                    actions.add(new JsonObject().put("name", a.getString(0)).put("displayName", a.getString(1))
                            .put("type", a.getString(2)));
                }
                for (Object o : getOrElse(j2.getJsonArray("apps"),
                        new fr.wseduc.webutils.collections.JsonArray())) {
                    if (!(o instanceof JsonArray))
                        continue;
                    JsonArray a = (JsonArray) o;
                    apps.add(new JsonObject().put("name", (String) a.getString(0))
                            .put("address", (String) a.getString(1)).put("icon", (String) a.getString(2))
                            .put("target", (String) a.getString(3)).put("displayName", (String) a.getString(4))
                            .put("display", ((a.getValue(5) == null) || a.getBoolean(5)))
                            .put("prefix", (String) a.getString(6)));
                }
                for (Object o : getOrElse(j.getJsonArray("aafFunctions"),
                        new fr.wseduc.webutils.collections.JsonArray())) {
                    if (o == null)
                        continue;
                    String[] sf = o.toString().split("\\$");
                    if (sf.length == 5) {
                        JsonObject jo = functions.getJsonObject(sf[1]);
                        if (jo == null) {
                            jo = new JsonObject().put("code", sf[1]).put("functionName", sf[2])
                                    .put("scope", new fr.wseduc.webutils.collections.JsonArray())
                                    .put("structureExternalIds", new fr.wseduc.webutils.collections.JsonArray())
                                    .put("subjects", new JsonObject());
                            functions.put(sf[1], jo);
                        }
                        JsonObject subject = jo.getJsonObject("subjects").getJsonObject(sf[3]);
                        if (subject == null) {
                            subject = new JsonObject().put("subjectCode", sf[3]).put("subjectName", sf[4])
                                    .put("scope", new fr.wseduc.webutils.collections.JsonArray())
                                    .put("structureExternalIds",
                                            new fr.wseduc.webutils.collections.JsonArray());
                            jo.getJsonObject("subjects").put(sf[3], subject);
                        }
                        jo.getJsonArray("structureExternalIds").add(sf[0]);
                        subject.getJsonArray("structureExternalIds").add(sf[0]);
                        String sid = structureMapping.getString(sf[0]);
                        if (sid != null) {
                            jo.getJsonArray("scope").add(sid);
                            subject.getJsonArray("scope").add(sid);
                        }
                    }
                }
                j.remove("aafFunctions");
                for (Object o : getOrElse(j.getJsonArray("functions"),
                        new fr.wseduc.webutils.collections.JsonArray())) {
                    if (!(o instanceof JsonArray))
                        continue;
                    JsonArray a = (JsonArray) o;
                    String code = a.getString(0);
                    if (code != null) {
                        functions.put(code, new JsonObject().put("code", code).put("scope", a.getJsonArray(1)));
                    }
                }
                final JsonObject children = new JsonObject();
                final List<String> childrenIds = new ArrayList<String>();
                for (Object o : getOrElse(j.getJsonArray("childrenInfo"),
                        new fr.wseduc.webutils.collections.JsonArray())) {
                    if (!(o instanceof JsonArray))
                        continue;
                    final JsonArray a = (JsonArray) o;
                    final String childId = a.getString(0);
                    if (childId != null) {
                        childrenIds.add(childId);
                        JsonObject jo = children.getJsonObject(childId);
                        if (jo == null) {
                            jo = new JsonObject().put("lastName", a.getString(1)).put("firstName",
                                    a.getString(2));
                            children.put(childId, jo);
                        }
                    }
                }
                j.remove("childrenInfo");
                final List<String> classesIds = new ArrayList<String>();
                final List<String> classesNames = new ArrayList<String>();
                for (Object o : getOrElse(j.getJsonArray("classes"),
                        new fr.wseduc.webutils.collections.JsonArray())) {
                    if (!(o instanceof JsonArray))
                        continue;
                    final JsonArray c = (JsonArray) o;
                    if (c.getString(0) != null) {
                        classesIds.add(c.getString(0));
                        classesNames.add(c.getString(1));
                    }
                }
                j.remove("classes");
                final List<String> structureIds = new ArrayList<String>();
                final List<String> structureNames = new ArrayList<String>();
                for (Object o : getOrElse(j.getJsonArray("structures"),
                        new fr.wseduc.webutils.collections.JsonArray())) {
                    if (!(o instanceof JsonArray))
                        continue;
                    final JsonArray s = (JsonArray) o;
                    if (s.getString(0) != null) {
                        structureIds.add(s.getString(0));
                        structureNames.add(StringUtils.trimToBlank(s.getString(1)));
                    }
                }
                j.remove("structures");
                j.put("structures", new fr.wseduc.webutils.collections.JsonArray(structureIds));
                j.put("structureNames", new fr.wseduc.webutils.collections.JsonArray(structureNames));
                j.put("classes", new fr.wseduc.webutils.collections.JsonArray(classesIds));
                j.put("realClassesNames", new fr.wseduc.webutils.collections.JsonArray(classesNames));
                j.put("functions", functions);
                j.put("authorizedActions", actions);
                j.put("apps", apps);
                j.put("childrenIds", new fr.wseduc.webutils.collections.JsonArray(childrenIds));
                j.put("children", children);
                final JsonObject cache = (results.getJsonArray(4) != null && results.getJsonArray(4).size() > 0
                        && results.getJsonArray(4).getJsonObject(0) != null)
                                ? results.getJsonArray(4).getJsonObject(0)
                                : new JsonObject();
                j.put("cache", cache);
                j.put("widgets",
                        getOrElse(j3.getJsonArray("widgets"), new fr.wseduc.webutils.collections.JsonArray()));
                handler.handle(j);
            } else {
                handler.handle(null);
            }
        }
    });
}

From source file:org.entcore.timeline.controllers.helper.NotificationHelper.java

License:Open Source License

public void sendImmediateNotifications(final HttpServerRequest request, final JsonObject json) {
    //Get notification properties (mixin : admin console configuration which overrides default properties)
    final String notificationName = json.getString("notificationName");
    final JsonObject notification = json.getJsonObject("notification");
    configService.getNotificationProperties(notificationName, new Handler<Either<String, JsonObject>>() {
        public void handle(final Either<String, JsonObject> properties) {
            if (properties.isLeft() || properties.right().getValue() == null) {
                log.error("[NotificationHelper] Issue while retrieving notification (" + notificationName
                        + ") properties.");
                return;
            }//from www  .  ja  va2s .c o m
            final JsonObject notificationProperties = properties.right().getValue();
            //Get users preferences (overrides notification properties)
            NotificationUtils.getUsersPreferences(eb, json.getJsonArray("recipientsIds"),
                    "language: uac.language, tokens: uac.fcmTokens ", new Handler<JsonArray>() {
                        public void handle(final JsonArray userList) {
                            if (userList == null) {
                                log.error("[NotificationHelper] Issue while retrieving users preferences.");
                                return;
                            }
                            mailerService.sendImmediateMails(request, notificationName, notification,
                                    json.getJsonObject("params"), userList, notificationProperties);

                            if (pushNotifService != null && json.containsKey("pushNotif")
                                    && notificationProperties.getBoolean("push-notif")
                                    && !TimelineNotificationsLoader.Restrictions.INTERNAL.name()
                                            .equals(notificationProperties.getString("restriction"))
                                    && !TimelineNotificationsLoader.Restrictions.HIDDEN.name()
                                            .equals(notificationProperties.getString("restriction")))

                                pushNotifService.sendImmediateNotifs(notificationName, json, userList,
                                        notificationProperties);
                        }
                    });
        }
    });
}

From source file:org.entcore.timeline.controllers.TimelineController.java

License:Open Source License

@BusAddress("wse.timeline")
public void busApi(final Message<JsonObject> message) {
    if (message == null) {
        return;//  w  w  w.  j  av  a2s. c om
    }
    final JsonObject json = message.body();
    if (json == null) {
        message.reply(new JsonObject().put("status", "error").put("message", "Invalid body."));
        return;
    }

    final Handler<JsonObject> handler = new Handler<JsonObject>() {
        public void handle(JsonObject event) {
            message.reply(event);
        }
    };

    String action = json.getString("action");
    if (action == null) {
        log.warn("Invalid action.");
        message.reply(new JsonObject().put("status", "error").put("message", "Invalid action."));
        return;
    }

    switch (action) {
    case "add":
        final String sender = json.getString("sender");
        if (sender == null || sender.startsWith("no-reply") || json.getBoolean("disableAntiFlood", false)
                || antiFlood.add(sender)) {
            store.add(json, new Handler<JsonObject>() {
                public void handle(JsonObject result) {
                    notificationHelper.sendImmediateNotifications(
                            new JsonHttpServerRequest(json.getJsonObject("request")), json);
                    handler.handle(result);
                }
            });
            if (refreshTypesCache && eventTypes != null && !eventTypes.contains(json.getString("type"))) {
                eventTypes = null;
            }
        } else {
            message.reply(new JsonObject().put("status", "error").put("message", "flood"));
        }
        break;
    case "get":
        UserInfos u = new UserInfos();
        u.setUserId(json.getString("recipient"));
        u.setExternalId(json.getString("externalId"));
        store.get(u, null, json.getInteger("offset", 0), json.getInteger("limit", 25), null, false, handler);
        break;
    case "delete":
        store.delete(json.getString("resource"), handler);
        break;
    case "deleteSubResource":
        store.deleteSubResource(json.getString("sub-resource"), handler);
    case "list-types":
        store.listTypes(new Handler<JsonArray>() {
            @Override
            public void handle(JsonArray types) {
                message.reply(new JsonObject().put("status", "ok").put("types", types));
            }
        });
        break;
    default:
        message.reply(new JsonObject().put("status", "error").put("message", "Invalid action."));
    }
}

From source file:org.entcore.timeline.events.DefaultTimelineEventStore.java

License:Open Source License

@Override
public void add(JsonObject event, final Handler<JsonObject> result) {
    JsonObject doc = validAndGet(event);
    if (doc != null) {
        if (!doc.containsKey("date")) {
            doc.put("date", MongoDb.now());
        }/*from  w  w w . java  2s . com*/
        doc.put("created", doc.getJsonObject("date"));
        mongo.save(TIMELINE_COLLECTION, doc, resultHandler(result));
    } else {
        result.handle(invalidArguments());
    }
}

From source file:org.entcore.timeline.services.impl.DefaultPushNotifService.java

License:Open Source License

public void processMessage(final JsonObject notification, String language, final boolean typeNotification,
        final boolean typeData, final Handler<JsonObject> handler) {
    final JsonObject message = new JsonObject();

    translateMessage(language, new Handler<JsonObject>() {
        @Override/*from  w  ww .jav  a  2 s.  c  o  m*/
        public void handle(JsonObject keys) {
            final JsonObject notif = new JsonObject();
            final JsonObject data = new JsonObject();
            final JsonObject pushNotif = notification.getJsonObject("pushNotif");
            String body = pushNotif.getString("body", "");
            body = body.length() < MAX_BODY_LENGTH ? body : body.substring(0, MAX_BODY_LENGTH) + "...";

            notif.put("title", keys.getString(pushNotif.getString("title"), pushNotif.getString("title", "")));
            notif.put("body", body);
            if (typeData) {
                if (notification.containsKey("params"))
                    data.put("params", notification.getJsonObject("params").toString());
                if (notification.containsKey("resource"))
                    data.put("resource", notification.getString("resource"));
                if (notification.containsKey("sub-resource"))
                    data.put("sub-resource", notification.getString("sub-resource"));
                if (!typeNotification)
                    data.put("notification", notif);
                message.put("data", data);
            }
            if (typeNotification)
                message.put("notification", notif);

            handler.handle(message);
        }
    });
}