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

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

Introduction

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

Prototype

public String getString(String key) 

Source Link

Document

Get the string value with the specified key, special cases are addressed for extended JSON types Instant , byte[] and Enum which can be converted to String.

Usage

From source file:org.entcore.common.storage.StorageFactory.java

License:Open Source License

public StorageFactory(Vertx vertx, JsonObject config, AbstractApplicationStorage applicationStorage) {
    this.vertx = vertx;
    LocalMap<Object, Object> server = vertx.sharedData().getLocalMap("server");
    String s = (String) server.get("swift");
    if (s != null) {
        this.swift = new JsonObject(s);
    }/*  w w w.  j av  a 2s.c  o  m*/
    s = (String) server.get("file-system");
    if (s != null) {
        this.fs = new JsonObject(s);
    }
    this.gridfsAddress = (String) server.get("gridfsAddress");
    if (config != null && config.getJsonObject("swift") != null) {
        this.swift = config.getJsonObject("swift");
    } else if (config != null && config.getJsonObject("file-system") != null) {
        this.fs = config.getJsonObject("file-system");
    } else if (config != null && config.getString("gridfs-address") != null) {
        this.gridfsAddress = config.getString("gridfs-address");
    }

    if (applicationStorage != null) {
        applicationStorage.setVertx(vertx);
        vertx.eventBus().localConsumer("storage", applicationStorage);
    }
}

From source file:org.entcore.common.storage.StorageFactory.java

License:Open Source License

public Storage getStorage() {
    Storage storage = null;//from  www. j a  v a 2s . c  om
    if (swift != null) {
        String uri = swift.getString("uri");
        String container = swift.getString("container");
        String username = swift.getString("user");
        String password = swift.getString("key");
        try {
            storage = new SwiftStorage(vertx, new URI(uri), container, username, password);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    } else if (fs != null) {
        storage = new FileStorage(vertx, fs.getString("path"), fs.getBoolean("flat", false));
        JsonObject antivirus = fs.getJsonObject("antivirus");
        if (antivirus != null) {
            final String h = antivirus.getString("host");
            final String c = antivirus.getString("credential");
            if (isNotEmpty(h) && isNotEmpty(c)) {
                AntivirusClient av = new HttpAntivirusClient(vertx, h, c);
                ((FileStorage) storage).setAntivirus(av);
            }
        }
        FileValidator fileValidator = new QuotaFileSizeValidation();
        JsonArray blockedExtensions = fs.getJsonArray("blockedExtensions");
        if (blockedExtensions != null && blockedExtensions.size() > 0) {
            fileValidator.setNext(new ExtensionValidator(blockedExtensions));
        }
        ((FileStorage) storage).setValidator(fileValidator);
    } else {
        storage = new GridfsStorage(vertx, Server.getEventBus(vertx), gridfsAddress);
    }
    return storage;
}

From source file:org.entcore.common.user.RepositoryHandler.java

License:Open Source License

@Override
public void handle(Message<JsonObject> message) {
    String action = message.body().getString("action", "");
    switch (action) {
    case "export":
        final String exportId = message.body().getString("exportId", "");
        String userId = message.body().getString("userId", "");
        String path = message.body().getString("path", "");
        final String locale = message.body().getString("locale", "fr");
        final String host = message.body().getString("host", "");
        JsonArray groupIds = message.body().getJsonArray("groups",
                new fr.wseduc.webutils.collections.JsonArray());
        repositoryEvents.exportResources(exportId, userId, groupIds, path, locale, host,
                new Handler<Boolean>() {
                    @Override//from  w w w  .  j  av a  2  s. c  o  m
                    public void handle(Boolean isExported) {
                        JsonObject exported = new JsonObject().put("action", "exported")
                                .put("status", (isExported ? "ok" : "error")).put("exportId", exportId)
                                .put("locale", locale).put("host", host);
                        eb.publish("entcore.export", exported);
                    }
                });
        break;
    case "delete-groups":
        JsonArray groups = message.body().getJsonArray("old-groups",
                new fr.wseduc.webutils.collections.JsonArray());
        repositoryEvents.deleteGroups(groups);
        break;
    case "delete-users":
        JsonArray users = message.body().getJsonArray("old-users",
                new fr.wseduc.webutils.collections.JsonArray());
        repositoryEvents.deleteUsers(users);
        break;
    case "users-classes-update":
        JsonArray updates = message.body().getJsonArray("users-classes-update",
                new fr.wseduc.webutils.collections.JsonArray());
        repositoryEvents.usersClassesUpdated(updates);
        break;
    case "transition":
        JsonObject structure = message.body().getJsonObject("structure");
        repositoryEvents.transition(structure);
        break;
    case "merge-users":
        JsonObject body = message.body();
        repositoryEvents.mergeUsers(body.getString("keepedUserId"), body.getString("deletedUserId"));
        break;
    default:
        message.reply(new JsonObject().put("status", "error").put("message", "invalid.action"));
    }
}

From source file:org.entcore.common.user.UserUtils.java

License:Open Source License

private static void findUsers(final EventBus eb, HttpServerRequest request, final JsonObject query,
        final Handler<JsonArray> handler) {
    getSession(eb, request, new Handler<JsonObject>() {

        @Override// w w w. j  av a 2 s .c  o  m
        public void handle(JsonObject session) {
            if (session != null && session.getString("userId") != null
                    && !session.getString("userId").trim().isEmpty()) {
                findUsers(eb, session.getString("userId"), query, handler);
            } else {
                handler.handle(new fr.wseduc.webutils.collections.JsonArray());
            }
        }
    });
}

From source file:org.entcore.common.user.UserUtils.java

License:Open Source License

public static void translateGroupsNames(JsonArray groups, String acceptLanguage) {
    for (Object u : groups) {
        if (!(u instanceof JsonObject))
            continue;
        JsonObject group = (JsonObject) u;
        if (group.getString("name") != null) {
            groupDisplayName(group, acceptLanguage);
        }/*from ww  w . jav  a  2 s.c om*/
    }
}

From source file:org.entcore.common.user.UserUtils.java

License:Open Source License

public static void groupDisplayName(JsonObject group, String acceptLanguage) {
    String name = group.getString("name");
    int idx = name.lastIndexOf('-');
    if (idx < 0) {
        return;//from  ww  w  .j  ava2  s  .c o  m
    }
    final String arg = name.substring(0, idx);
    String type = name.substring(idx + 1);
    String displayName = getOrElse(group.getString("groupDisplayName"), "group." + type);
    String translatedName = i18n.translate(displayName, I18n.DEFAULT_DOMAIN, acceptLanguage, arg);
    if (!translatedName.equals(displayName))
        group.put("name", translatedName);
}

From source file:org.entcore.common.user.UserUtils.java

License:Open Source License

public static JsonObject translateAndGroupVisible(JsonArray visibles, String acceptLanguage,
        boolean returnGroupType) {
    final JsonObject visible = new JsonObject();
    final JsonArray users = new fr.wseduc.webutils.collections.JsonArray();
    final JsonArray groups = new fr.wseduc.webutils.collections.JsonArray();
    visible.put("groups", groups).put("users", users);
    for (Object o : visibles) {
        if (!(o instanceof JsonObject))
            continue;
        JsonObject j = (JsonObject) o;
        if (j.getString("name") != null) {
            j.remove("displayName");
            j.remove("profile");
            j.remove("mood");
            if (returnGroupType) {
                Object gt = j.remove("groupType");
                Object gp = j.remove("groupProfile");
                if (gt instanceof Iterable) {
                    for (Object gti : (Iterable) gt) {
                        if (gti != null && !"Group".equals(gti) && gti.toString().endsWith("Group")) {
                            j.put("groupType", gti);
                            if ("ProfileGroup".equals(gti)) {
                                j.put("profile", gp);
                            }/*from ww w.j  a  v  a 2s  .c  o m*/
                            break;
                        }
                    }
                }
            }
            j.put("sortName", j.getString("name"));
            UserUtils.groupDisplayName(j, acceptLanguage);
            groups.add(j);
        } else {
            if (returnGroupType) {
                j.remove("groupProfile");
                j.remove("groupType");
            }
            j.remove("name");
            j.remove("nbUsers");
            users.add(j);
        }
    }
    return visible;
}

From source file:org.entcore.communication.services.impl.DefaultCommunicationService.java

License:Open Source License

private void initDefaultRules(JsonArray structureIds, String attr, JsonObject defaultRules,
        final StatementsBuilder existingGroups, final StatementsBuilder newGroups) {
    final String[] a = attr.split("\\-");
    final String c = "Class".equals(a[0]) ? "*2" : "";
    String relativeStudent = defaultRules.getString("Relative-Student"); // TODO check type in enum
    if (relativeStudent != null && "Relative".equals(a[1])) {
        String query = "MATCH (s:Structure)<-[:DEPENDS" + c + "]-(cg:ProfileGroup) "
                + "WHERE s.id IN {structures} AND NOT(HAS(cg.communiqueWith)) " + "AND cg.name =~ {profile} "
                + "SET cg.relativeCommuniqueStudent = {direction} ";
        JsonObject params = new JsonObject().put("structures", structureIds).put("direction", relativeStudent)
                .put("profile", "^.*?" + a[1] + "$");
        newGroups.add(query, params);/*from   w  w  w  .  ja v a  2  s.c o  m*/
    }
    String users = defaultRules.getString("users"); // TODO check type in enum
    if (users != null) {
        String query = "MATCH (s:Structure)<-[:DEPENDS" + c + "]-(cg:ProfileGroup) "
                + "WHERE s.id IN {structures} AND NOT(HAS(cg.communiqueWith)) " + "AND cg.name =~ {profile} "
                + "SET cg.users = {direction} ";
        JsonObject params = new JsonObject().put("structures", structureIds).put("direction", users)
                .put("profile", "^.*?" + a[1] + "$");
        newGroups.add(query, params);
    }
    JsonArray communiqueWith = defaultRules.getJsonArray("communiqueWith",
            new fr.wseduc.webutils.collections.JsonArray());
    Set<String> classes = new HashSet<>();
    Set<String> structures = new HashSet<>();
    StringBuilder groupLabelSB = new StringBuilder("g:ProfileGroup");
    for (Object o : communiqueWith) {
        if (!(o instanceof String))
            continue;
        String[] s = ((String) o).split("\\-");
        if ("Class".equals(s[0]) && "Structure".equals(a[0])) {
            log.warn("Invalid default configuration " + attr + "->" + o.toString());
        } else if ("Class".equals(s[0])) {
            if ("HeadTeacher".equals(s[1])) {
                groupLabelSB.append(" OR g:HTGroup");
            }
            classes.add(s[1]);
        } else {
            if ("Func".equals(s[1]) || "Discipline".equals(s[1])) {
                groupLabelSB.append(" OR g:FunctionGroup");
            } else if ("HeadTeacher".equals(s[1])) {
                groupLabelSB.append(" OR g:HTGroup");
            }
            structures.add(s[1]);
        }
    }
    final String groupLabel = groupLabelSB.toString();
    JsonObject params = new JsonObject().put("structures", structureIds).put("profile", "^.*?" + a[1] + "$");
    if (!classes.isEmpty()) {
        String query = "MATCH (s:Structure)<-[:DEPENDS" + c + "]-(cg:ProfileGroup)-[:DEPENDS]->(c:Class) "
                + "WHERE s.id IN {structures} AND HAS(cg.communiqueWith) AND cg.name =~ {profile} "
                + "WITH cg, c " + "MATCH c<-[:DEPENDS]-(g) " + "WHERE (" + groupLabel
                + ") AND NOT(HAS(g.communiqueWith)) AND g.name =~ {otherProfile} "
                + "SET cg.communiqueWith = FILTER(gId IN cg.communiqueWith WHERE gId <> g.id) + g.id ";
        String query2 = "MATCH (s:Structure)<-[:DEPENDS" + c + "]-(cg:ProfileGroup)-[:DEPENDS]->(c:Class) "
                + "WHERE s.id IN {structures} AND NOT(HAS(cg.communiqueWith)) AND cg.name =~ {profile} "
                + "WITH cg, c, s " + "MATCH c<-[:DEPENDS]-(g) " + "WHERE  (" + groupLabel
                + ") AND g.name =~ {otherProfile} "
                + "SET cg.communiqueWith = coalesce(cg.communiqueWith, []) + g.id ";
        if (!structures.isEmpty()) {
            query2 += "WITH DISTINCT s, cg " + "MATCH s<-[:DEPENDS]-(sg:ProfileGroup) "
                    + "WHERE sg.name =~ {structureProfile} "
                    + "SET cg.communiqueWith = coalesce(cg.communiqueWith, []) + sg.id ";
        }
        JsonObject p = params.copy();
        p.put("otherProfile", "^.*?(" + Joiner.on("|").join(classes) + ")$");
        p.put("structureProfile", "^.*?(" + Joiner.on("|").join(structures) + ")$");
        existingGroups.add(query, p);
        newGroups.add(query2, p);
    }
    if (!structures.isEmpty() && "Structure".equals(a[0])) {
        String query = "MATCH (s:Structure)<-[:DEPENDS" + c + "]-(cg:ProfileGroup), s<-[:DEPENDS]-(g) "
                + "WHERE s.id IN {structures} AND HAS(cg.communiqueWith) AND cg.name =~ {profile} " + "AND  ("
                + groupLabel + ") AND NOT(HAS(g.communiqueWith)) AND g.name =~ {otherProfile} "
                + "SET cg.communiqueWith = FILTER(gId IN cg.communiqueWith WHERE gId <> g.id) + g.id ";
        String query2 = "MATCH (s:Structure)<-[:DEPENDS" + c + "]-(cg:ProfileGroup), s<-[:DEPENDS]-(g) "
                + "WHERE s.id IN {structures} AND NOT(HAS(cg.communiqueWith)) AND cg.name =~ {profile} "
                + "AND (" + groupLabel + ") AND g.name =~ {otherProfile} "
                + "SET cg.communiqueWith = coalesce(cg.communiqueWith, []) + g.id ";
        params.put("otherProfile", "^.*?(" + Joiner.on("|").join(structures) + ")$");
        existingGroups.add(query, params);
        newGroups.add(query2, params);
    }
}

From source file:org.entcore.conversation.controllers.ConversationController.java

License:Open Source License

@Post("draft")
@SecuredAction("conversation.create.draft")
public void createDraft(final HttpServerRequest request) {
    getUserInfos(eb, request, new Handler<UserInfos>() {
        @Override/*from  ww  w  . j av a 2s .  co m*/
        public void handle(final UserInfos user) {
            if (user != null) {
                final String parentMessageId = request.params().get("In-Reply-To");
                bodyToJson(request, new Handler<JsonObject>() {
                    @Override
                    public void handle(final JsonObject message) {

                        if (!message.containsKey("from")) {
                            message.put("from", user.getUserId());
                        }

                        final Handler<JsonObject> parentHandler = new Handler<JsonObject>() {
                            @Override
                            public void handle(JsonObject parent) {
                                final String threadId;
                                if (parent != null) {
                                    threadId = parent.getString("thread_id");
                                } else {
                                    threadId = null;
                                }
                                neoConversationService.addDisplayNames(message, parent,
                                        new Handler<JsonObject>() {
                                            public void handle(JsonObject message) {
                                                conversationService.saveDraft(parentMessageId, threadId,
                                                        message, user, defaultResponseHandler(request, 201));
                                            }
                                        });
                            }
                        };

                        if (parentMessageId != null && !parentMessageId.trim().isEmpty()) {
                            conversationService.get(parentMessageId, user,
                                    new Handler<Either<String, JsonObject>>() {
                                        public void handle(Either<String, JsonObject> event) {
                                            if (event.isLeft()) {
                                                badRequest(request);
                                                return;
                                            }

                                            parentHandler.handle(event.right().getValue());
                                        }
                                    });
                        } else {
                            parentHandler.handle(null);
                        }

                    }
                });
            } else {
                unauthorized(request);
            }
        }
    });
}

From source file:org.entcore.conversation.controllers.ConversationController.java

License:Open Source License

@Post("send")
@SecuredAction(value = "", type = ActionType.RESOURCE)
@ResourceFilter(VisiblesFilter.class)
public void send(final HttpServerRequest request) {
    final String messageId = request.params().get("id");

    getUserInfos(eb, request, new Handler<UserInfos>() {
        @Override//from w  w w.j  av  a2s  . c  o  m
        public void handle(final UserInfos user) {
            if (user != null) {
                final String parentMessageId = request.params().get("In-Reply-To");
                bodyToJson(request, new Handler<JsonObject>() {
                    @Override
                    public void handle(final JsonObject message) {
                        if (!message.containsKey("from")) {
                            message.put("from", user.getUserId());
                        }

                        final Handler<JsonObject> parentHandler = new Handler<JsonObject>() {
                            public void handle(JsonObject parentMsg) {
                                final String threadId;
                                if (parentMsg != null) {
                                    threadId = parentMsg.getString("thread_id");
                                } else {
                                    threadId = null;
                                }
                                neoConversationService.addDisplayNames(message, parentMsg,
                                        new Handler<JsonObject>() {
                                            public void handle(final JsonObject message) {
                                                saveAndSend(messageId, message, user, parentMessageId, threadId,
                                                        new Handler<Either<String, JsonObject>>() {
                                                            @Override
                                                            public void handle(
                                                                    Either<String, JsonObject> event) {
                                                                if (event.isRight()) {
                                                                    JsonObject result = event.right()
                                                                            .getValue();
                                                                    JsonObject timelineParams = new JsonObject()
                                                                            .put("subject",
                                                                                    result.getString("subject"))
                                                                            .put("body",
                                                                                    StringUtils.stripHtmlTag(
                                                                                            result.getString(
                                                                                                    "body")))
                                                                            .put("id", result.getString("id"))
                                                                            .put("sentIds",
                                                                                    message.getJsonArray(
                                                                                            "allUsers",
                                                                                            new fr.wseduc.webutils.collections.JsonArray()));
                                                                    timelineNotification(request,
                                                                            timelineParams, user);
                                                                    renderJson(request, result.put("inactive",
                                                                            message.getJsonArray("inactives",
                                                                                    new fr.wseduc.webutils.collections.JsonArray()))
                                                                            .put("undelivered",
                                                                                    message.getJsonArray(
                                                                                            "undelivered",
                                                                                            new fr.wseduc.webutils.collections.JsonArray()))
                                                                            .put("sent", message.getJsonArray(
                                                                                    "allUsers",
                                                                                    new fr.wseduc.webutils.collections.JsonArray())
                                                                                    .size()));
                                                                } else {
                                                                    JsonObject error = new JsonObject().put(
                                                                            "error", event.left().getValue());
                                                                    renderJson(request, error, 400);
                                                                }
                                                            }
                                                        });
                                            }
                                        });
                            }
                        };

                        if (parentMessageId != null && !parentMessageId.trim().isEmpty()) {
                            conversationService.get(parentMessageId, user,
                                    new Handler<Either<String, JsonObject>>() {
                                        public void handle(Either<String, JsonObject> event) {
                                            if (event.isLeft()) {
                                                badRequest(request);
                                                return;
                                            }

                                            parentHandler.handle(event.right().getValue());
                                        }
                                    });
                        } else {
                            parentHandler.handle(null);
                        }
                    }
                });
            } else {
                unauthorized(request);
            }
        }
    });
}