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

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

Introduction

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

Prototype

public boolean containsKey(String key) 

Source Link

Document

Does the JSON object contain the specified key?

Usage

From source file:org.entcore.blog.services.impl.DefaultPostService.java

License:Open Source License

@Override
public void update(String postId, final JsonObject post, final UserInfos user,
        final Handler<Either<String, JsonObject>> result) {

    final JsonObject jQuery = MongoQueryBuilder.build(QueryBuilder.start("_id").is(postId));
    mongo.findOne(POST_COLLECTION, jQuery,
            MongoDbResult.validActionResultHandler(new Handler<Either<String, JsonObject>>() {
                public void handle(Either<String, JsonObject> event) {
                    if (event.isLeft()) {
                        result.handle(event);
                        return;
                    } else {
                        final JsonObject postFromDb = event.right().getValue().getJsonObject("result",
                                new JsonObject());
                        final JsonObject now = MongoDb.now();
                        post.put("modified", now);
                        final JsonObject b = Utils.validAndGet(post, UPDATABLE_FIELDS,
                                Collections.<String>emptyList());

                        if (validationError(result, b))
                            return;
                        if (b.containsKey("content")) {
                            b.put("contentPlain", StringUtils.stripHtmlTag(b.getString("content", "")));
                        }/* ww w.ja v  a  2 s . c o  m*/

                        if (postFromDb.getJsonObject("firstPublishDate") != null) {
                            b.put("sorted", postFromDb.getJsonObject("firstPublishDate"));
                        } else {
                            b.put("sorted", now);
                        }

                        //if user is author, draft state
                        if (user.getUserId().equals(
                                postFromDb.getJsonObject("author", new JsonObject()).getString("userId"))) {
                            b.put("state", StateType.DRAFT.name());
                        }

                        MongoUpdateBuilder modifier = new MongoUpdateBuilder();
                        for (String attr : b.fieldNames()) {
                            modifier.set(attr, b.getValue(attr));
                        }
                        mongo.update(POST_COLLECTION, jQuery, modifier.build(),
                                new Handler<Message<JsonObject>>() {
                                    @Override
                                    public void handle(Message<JsonObject> event) {
                                        if ("ok".equals(event.body().getString("status"))) {
                                            final JsonObject r = new JsonObject().put("state",
                                                    b.getString("state", postFromDb.getString("state")));
                                            result.handle(new Either.Right<String, JsonObject>(r));
                                        } else {
                                            result.handle(new Either.Left<String, JsonObject>(
                                                    event.body().getString("message", "")));
                                        }
                                    }
                                });
                    }
                }
            }));

}

From source file:org.entcore.cas.services.AcademicSuffixRegisteredService.java

License:Open Source License

/**
 * Replace or format cas-user attribute response with formated externalId
 * In all case : remove prefix ENT$  if is at the beginning
 * Split externalId with - character, if we have only one - : remove the prefix and add the correct suffix
 * In other case, keep the same externalId
 * @param user : authenticated user/*from ww  w. ja  v a2s  .  c  o  m*/
 * @param userId
 * @param service
 * @param data
 * @param doc
 * @param additionnalAttributes
 */
@Override
protected void prepareUserCas20(User user, String userId, String service, JsonObject data, Document doc,
        List<Element> additionnalAttributes) {
    user.setUser(data.getString(principalAttributeName));
    log.debug("conf : eip : " + externalIdPrefix + ", acp : " + academicConfPattern + ", map : "
            + ACADEMIC_SUFFIX.toString());

    try {
        // Define attribute : cas:user
        if (data.containsKey("externalId")) {
            String initialExternalId = data.getString("externalId");

            log.debug("PrepareUserCas20 : ExternalId Initial : " + initialExternalId);

            // Replace first conf prefix if it's at the beginning.
            initialExternalId = initialExternalId.replaceFirst(this.externalIdPrefix, "$1");

            // We verify REXEXP
            Matcher academyMatcher = this.academicPattern.matcher(initialExternalId);
            String externalId = "";
            if (academyMatcher.matches()) {
                externalId = academyMatcher.group(2) + ACADEMIC_SUFFIX.get(academyMatcher.group(1));
                log.debug("ExternalId target : " + externalId);
            } else {
                externalId = initialExternalId;
                log.debug("External id could not be processed, Initial External id is used : "
                        + initialExternalId);
            }
            log.debug("ExternalId Final : " + externalId);
            user.setUser(externalId);
        }

    } catch (Exception e) {
        log.error("Failed to transform User for Uid service : ", e);
    }
}

From source file:org.entcore.cas.services.CIDJRegisteredService.java

License:Open Source License

@Override
protected void prepareUserCas20(User user, String userId, String service, JsonObject data, Document doc,
        List<Element> additionnalAttributes) {
    user.setUser(data.getString(principalAttributeName));

    try {/*from w  ww.  j a  v  a 2  s  .  c  o  m*/

        // Uid
        if (data.containsKey("externalId")) {
            additionnalAttributes.add(createTextElement(CIDJ_ID, data.getString("externalId"), doc));
        }

        // Structures
        for (Object o : data.getJsonArray("structures", new fr.wseduc.webutils.collections.JsonArray())
                .getList()) {
            if (o == null || !(o instanceof JsonObject))
                continue;
            JsonObject structure = (JsonObject) o;
            if (structure.containsKey("UAI")) {
                additionnalAttributes
                        .add(createTextElement(CIDJ_STRUCTURE_CODERNE, structure.getString("UAI"), doc));
            }
        }

    } catch (Exception e) {
        log.error("Failed to transform User for CIDJ", e);
    }
}

From source file:org.entcore.cas.services.EducagriRegisteredService.java

License:Open Source License

@Override
protected void prepareUserCas20(User user, String userId, String service, JsonObject data, Document doc,
        List<Element> additionnalAttributes) {
    user.setUser(data.getString(principalAttributeName));
    try {/*from  ww w . ja va 2  s.  c o  m*/
        //uid
        if (data.containsKey("externalId")) {
            additionnalAttributes.add(createTextElement(EA_ID, data.getString("externalId"), doc));
        }

        // Structures
        for (Object o : data.getJsonArray("structures", new fr.wseduc.webutils.collections.JsonArray())
                .getList()) {
            if (o == null || !(o instanceof JsonObject))
                continue;
            JsonObject structure = (JsonObject) o;
            if (structure.containsKey("UAI")) {
                additionnalAttributes
                        .add(createTextElement(EA_STRUCTURE_UAI, structure.getString("UAI").toString(), doc));
            }
        }

        // Profile
        switch (data.getString("type")) {
        case "Student":
            additionnalAttributes.add(createTextElement(EA_PROFILES, "National_1", doc));
            break;
        case "Teacher":
            additionnalAttributes.add(createTextElement(EA_PROFILES, "National_3", doc));
            break;
        case "Personnel":
            additionnalAttributes.add(createTextElement(EA_PROFILES, "National_4", doc));
            break;
        }
    } catch (Exception e) {
        log.error("Failed to transform User for Educagri", e);
    }
}

From source file:org.entcore.cas.services.EduMediaRegisteredService.java

License:Open Source License

@Override
protected void prepareUserCas20(User user, String userId, String service, JsonObject data, Document doc,
        List<Element> additionnalAttributes) {
    user.setUser(data.getString(principalAttributeName));
    try {/*from  w ww. j  a  v a  2s  .c  om*/
        //uid
        if (data.containsKey("externalId")) {
            additionnalAttributes.add(createTextElement(EM_ID, data.getString("externalId"), doc));
        }

        // Structures
        for (Object o : data.getJsonArray("structures", new fr.wseduc.webutils.collections.JsonArray())
                .getList()) {
            if (o == null || !(o instanceof JsonObject))
                continue;
            JsonObject structure = (JsonObject) o;
            if (structure.containsKey("UAI")) {
                additionnalAttributes.add(createTextElement(EM_STRUCTURE_UAI, structure.getString("UAI"), doc));
            }
        }

        // Profile
        switch (data.getString("type")) {
        case "Student":
            additionnalAttributes.add(createTextElement(EM_PROFILES, "National_1", doc));
            break;
        case "Teacher":
            additionnalAttributes.add(createTextElement(EM_PROFILES, "National_3", doc));
            break;
        case "Personnel":
            additionnalAttributes.add(createTextElement(EM_PROFILES, "National_4", doc));
            break;
        }
    } catch (Exception e) {
        log.error("Failed to transform User for Edumedia", e);
    }

}

From source file:org.entcore.cas.services.EnglishAttackRegisteredService.java

License:Open Source License

@Override
protected void prepareUserCas20(User user, final String userId, String service, final JsonObject data,
        final Document doc, final List<Element> additionnalAttributes) {
    user.setUser(data.getString(principalAttributeName));

    if (log.isDebugEnabled()) {
        log.debug("START : prepareUserCas20 userId : " + userId);
    }/*from w  ww . j a  v  a  2  s . co  m*/

    try {
        if (log.isDebugEnabled()) {
            log.debug("DATA : prepareUserCas20 data : " + data);
        }

        String query = "MATCH (u:`User` { id : {id}}) return u";
        JsonObject params = new JsonObject().put("id", userId);
        neo.execute(query, params, new Handler<Message<JsonObject>>() {
            @Override
            public void handle(Message<JsonObject> m) {

                // Uid
                if (data.containsKey("externalId")) {
                    additionnalAttributes.add(createTextElement(EA_ID, data.getString("externalId"), doc));
                }

                // Structures
                Element rootStructures = createElement(EA_STRUCTURE + "s", doc);
                for (Object o : data.getJsonArray("structures", new fr.wseduc.webutils.collections.JsonArray())
                        .getList()) {
                    if (o == null || !(o instanceof JsonObject))
                        continue;
                    JsonObject structure = (JsonObject) o;
                    Element rootStructure = createElement(EA_STRUCTURE, doc);

                    if (structure.containsKey("UAI")) {
                        rootStructure.appendChild(
                                createTextElement(EA_STRUCTURE_UAI, structure.getString("UAI"), doc));
                    }
                    if (structure.containsKey("name")) {
                        rootStructure.appendChild(
                                createTextElement(EA_STRUCTURE_NAME, structure.getString("name"), doc));
                    }
                    rootStructures.appendChild(rootStructure);
                }
                additionnalAttributes.add(rootStructures);

                // Profile
                switch (data.getString("type")) {
                case "Student":
                    additionnalAttributes.add(createTextElement(EA_PROFILES, "National_1", doc));
                    addStringArray(EA_CLASSE, "classes", "name", data, doc, additionnalAttributes,
                            new Mapper<String, String>() {
                                String map(String input) {
                                    Matcher m = classGroupPattern.matcher(input);
                                    if (m.matches() && m.groupCount() >= 1) {
                                        return m.group(1);
                                    }
                                    return input;
                                }
                            });
                    // Email
                    if (data.containsKey("email")) {
                        additionnalAttributes.add(createTextElement(EA_EMAIL, data.getString("email"), doc));
                    }
                    break;
                case "Teacher":
                    additionnalAttributes.add(createTextElement(EA_PROFILES, "National_3", doc));
                    addStringArray(EA_CLASSE, "classes", "name", data, doc, additionnalAttributes,
                            new Mapper<String, String>() {
                                String map(String input) {
                                    Matcher m = classGroupPattern.matcher(input);
                                    if (m.matches() && m.groupCount() >= 1) {
                                        return m.group(1);
                                    }
                                    return input;
                                }
                            });

                    //Discipline
                    Either<String, JsonObject> res = validUniqueResult(m);
                    if (res.isRight()) {
                        JsonObject j = res.right().getValue();
                        if (null != j.getJsonObject("u") && null != j.getJsonObject("u").getJsonObject("data")
                                && null != j.getJsonObject("u").getJsonObject("data")
                                        .getJsonArray("functions")) {
                            JsonArray jsonArrayFunctions = j.getJsonObject("u").getJsonObject("data")
                                    .getJsonArray("functions");
                            if (jsonArrayFunctions.size() > 0) {
                                Element rootDisciplines = createElement(EA_DISCIPLINE + "s", doc);
                                List<String> vTempListDiscipline = new ArrayList<>();
                                for (int i = 0; i < jsonArrayFunctions.size(); i++) {
                                    String fonction = jsonArrayFunctions.getString(i);
                                    String[] elements = fonction.split("\\$");
                                    if (elements.length > 1) {
                                        String discipline = elements[elements.length - 2] + "$"
                                                + elements[elements.length - 1];
                                        if (!vTempListDiscipline.contains(discipline)) {
                                            vTempListDiscipline.add(discipline);
                                            rootDisciplines.appendChild(
                                                    createTextElement(EA_DISCIPLINE, discipline, doc));
                                        }
                                    } else {
                                        log.error("Failed to get User functions userID, : " + userId
                                                + " fonction : " + fonction);
                                    }
                                }
                                additionnalAttributes.add(rootDisciplines);
                            }
                        } else {
                            log.error("Failed to get User functions userID, user empty : " + userId + " j : "
                                    + j);
                        }
                    } else {
                        log.error("Failed to get User functions userID : " + userId);
                    }
                    // Email
                    if (data.containsKey("emailAcademy")) {
                        additionnalAttributes
                                .add(createTextElement(EA_EMAIL, data.getString("emailAcademy"), doc));
                    } else if (data.containsKey("email")) {
                        additionnalAttributes.add(createTextElement(EA_EMAIL, data.getString("email"), doc));

                    }
                    break;
                case "Relative":
                    additionnalAttributes.add(createTextElement(EA_PROFILES, "National_2", doc));
                    break;
                case "Personnel":
                    additionnalAttributes.add(createTextElement(EA_PROFILES, "National_4", doc));
                    break;
                }

                // Lastname
                if (data.containsKey("lastName")) {
                    additionnalAttributes.add(createTextElement(EA_LASTNAME, data.getString("lastName"), doc));
                }

                // Firstname
                if (data.containsKey("firstName")) {
                    additionnalAttributes
                            .add(createTextElement(EA_FIRSTNAME, data.getString("firstName"), doc));
                }
            }
        });

    } catch (Exception e) {
        log.error("Failed to transform User for EnglishAttack", e);
    }

    if (log.isDebugEnabled()) {
        log.debug("START : prepareUserCas20 userId : " + userId);
    }
}

From source file:org.entcore.cas.services.EnglishAttackRegisteredService.java

License:Open Source License

private void addStringArray(String casLabel, String entLabel, String entLabel2, JsonObject data, Document doc,
        List<Element> additionalAttributes, Mapper<String, String> mapper) {
    Element root = createElement(casLabel + "s", doc);
    if (data.containsKey(entLabel)) {
        for (Object item : data.getJsonArray(entLabel)) {
            if (item.getClass().getName().equalsIgnoreCase(JsonObject.class.getName())) {
                root.appendChild(createTextElement(casLabel, ((JsonObject) item).getString(entLabel2), doc));
            } else {
                root.appendChild(createTextElement(casLabel, mapper.map((String) item), doc));
            }//from   w  w  w.j ava 2s.c o m
        }
    }
    additionalAttributes.add(root);
}

From source file:org.entcore.cas.services.GepiRegisteredService.java

License:Open Source License

@Override
protected void prepareUserCas20(User user, String userId, String service, JsonObject data, Document doc,
        List<Element> additionnalAttributes) {
    user.setUser(data.getString(principalAttributeName));
    try {/*from ww  w .j  a v a  2  s. co  m*/
        //uid
        if (data.containsKey("externalId")) {
            additionnalAttributes.add(createTextElement(GP_ID, data.getString("externalId"), doc));
        }

        // Profile
        switch (data.getString("type")) {
        case "Student":
            additionnalAttributes.add(createTextElement(GP_PROFILES, "National_1", doc));
            break;
        case "Teacher":
            additionnalAttributes.add(createTextElement(GP_PROFILES, "National_3", doc));
            break;
        case "Relative":
            additionnalAttributes.add(createTextElement(GP_PROFILES, "National_2", doc));
            break;
        case "Personnel":
            additionnalAttributes.add(createTextElement(GP_PROFILES, "National_4", doc));
            break;
        }
    } catch (Exception e) {
        log.error("Failed to transform User for GEPI", e);
    }

}

From source file:org.entcore.cas.services.KneRegisteredService.java

License:Open Source License

private void addArray(String casLabel, String entLabel, JsonObject data, Document doc,
        List<Element> additionalAttributes, Mapper<String, String> mapper) {
    Element root = createElement(casLabel + "s", doc);
    if (data.containsKey(entLabel)) {
        for (Object item : data.getJsonArray(entLabel)) {
            root.appendChild(createTextElement(casLabel, mapper.map((String) item), doc));
        }//from w w  w  . j  av a 2 s.c  o m
    }
    additionalAttributes.add(root);
}

From source file:org.entcore.cas.services.KneRegisteredService.java

License:Open Source License

@Override
protected void prepareUserCas20(User user, String userId, String service, JsonObject data, Document doc,
        List<Element> additionalAttributes) {
    user.setUser(data.getString(principalAttributeName));

    try {/*from w  w w .  jav  a2s .com*/

        String queryParams = new URI(service).getQuery();
        String[] pairs;
        if (queryParams != null && queryParams.length() > 0 && (pairs = queryParams.split("&")).length > 0) {
            for (String pair : pairs) {
                String key = pair.substring(0, pair.indexOf('='));
                if ("UAI".equals(key)) {
                    String value = pair.substring(pair.indexOf('=') + 1);
                    additionalAttributes.add(createTextElement("ENTPersonStructRattachUAI", value, doc));
                    for (Object o : data
                            .getJsonArray("structureNodes", new fr.wseduc.webutils.collections.JsonArray())
                            .getList()) {
                        if (o == null || !(o instanceof JsonObject))
                            continue;
                        JsonObject structure = (JsonObject) o;
                        if (value.equals(structure.getString("UAI"))) {
                            if (structure.containsKey("type")) {
                                String type = structure.getString("type");
                                switch (type) {
                                case "ECOLE DE NIVEAU ELEMENTAIRE":
                                    additionalAttributes
                                            .add(createTextElement("ENTStructureTypeStruct", "1ORD", doc));
                                    break;
                                case "COLLEGE":
                                case "COLLEGE CLIMATIQUE":
                                    additionalAttributes
                                            .add(createTextElement("ENTStructureTypeStruct", "CLG", doc));
                                    break;
                                case "LYCEE D ENSEIGNEMENT GENERAL":
                                case "LYCEE POLYVALENT":
                                    additionalAttributes
                                            .add(createTextElement("ENTStructureTypeStruct", "LYC", doc));
                                    break;
                                case "LYCEE PROFESSIONNEL":
                                    additionalAttributes
                                            .add(createTextElement("ENTStructureTypeStruct", "LP", doc));
                                    break;
                                default:
                                    additionalAttributes
                                            .add(createTextElement("ENTStructureTypeStruct", type, doc));
                                }
                            }
                            break;
                        }
                    }
                    break;
                }
            }
        }

        Element rootProfiles;
        String profile = data.getJsonArray("type", new fr.wseduc.webutils.collections.JsonArray()).size() > 0
                ? data.getJsonArray("type").getString(0)
                : "";
        switch (profile) {
        case "Student":
            rootProfiles = createElement("ENTPersonProfils", doc);
            rootProfiles.appendChild(createTextElement("ENTPersonProfil", "National_ELV", doc));
            additionalAttributes.add(rootProfiles);
            additionalAttributes.add(createTextElement("ENTEleveMEF", data.getString("module", ""), doc));
            addArray("ENTEleveCodeEnseignement", "fieldOfStudy", data, doc, additionalAttributes,
                    new DefaultMapper<String>());
            addArray("ENTEleveClasse", "classes", data, doc, additionalAttributes,
                    new Mapper<String, String>() {
                        String map(String input) {
                            Matcher m = classGroupPattern.matcher(input);
                            if (m.matches() && m.groupCount() >= 1) {
                                return m.group(1);
                            }
                            return input;
                        }
                    });
            addArray("ENTEleveGroupe", "groups", data, doc, additionalAttributes, new Mapper<String, String>() {
                String map(String input) {
                    Matcher m = classGroupPattern.matcher(input);
                    if (m.matches() && m.groupCount() >= 1) {
                        return m.group(1);
                    }
                    return input;
                }
            });
            additionalAttributes.add(createTextElement("ENTAuxEnsClassesMatieres", "", doc));
            additionalAttributes.add(createTextElement("ENTAuxEnsGroupes", "", doc));
            additionalAttributes.add(createTextElement("ENTAuxEnsClasses", "", doc));
            additionalAttributes.add(createTextElement("ENTAuxEnsMEFs", "", doc));
            break;
        case "Teacher":
            rootProfiles = createElement("ENTPersonProfils", doc);
            rootProfiles.appendChild(createTextElement("ENTPersonProfil", "National_ENS", doc));
            //rootProfiles.appendChild(createTextElement("ENTPersonProfil", "National_TUT", doc));
            additionalAttributes.add(rootProfiles);
            additionalAttributes.add(createTextElement("ENTEleveMEF", "", doc));
            additionalAttributes.add(createTextElement("ENTEleveCodeEnseignements", "", doc));
            additionalAttributes.add(createTextElement("ENTEleveClasses", "", doc));
            additionalAttributes.add(createTextElement("ENTEleveGroupes", "", doc));
            addArray("ENTAuxEnsClassesMatiere", "classesFieldOfStudy", data, doc, additionalAttributes,
                    new Mapper<String, String>() {
                        String map(String input) {
                            Matcher m = matPattern.matcher(input);
                            if (m.matches() && m.groupCount() >= 1) {
                                return m.group(1);
                            }
                            return input;
                        }
                    });
            addArray("ENTAuxEnsGroupe", "groups", data, doc, additionalAttributes,
                    new Mapper<String, String>() {
                        String map(String input) {
                            Matcher m = classGroupPattern.matcher(input);
                            if (m.matches() && m.groupCount() >= 1) {
                                return m.group(1);
                            }
                            return input;
                        }
                    });
            addArray("ENTAuxEnsClasse", "classes", data, doc, additionalAttributes,
                    new Mapper<String, String>() {
                        String map(String input) {
                            Matcher m = classGroupPattern.matcher(input);
                            if (m.matches() && m.groupCount() >= 1) {
                                return m.group(1);
                            }
                            return input;
                        }
                    });
            addArray("ENTAuxEnsMEF", "modules", data, doc, additionalAttributes, new Mapper<String, String>() {
                String map(String input) {
                    Matcher m = mefStatPattern.matcher(input);
                    if (m.matches() && m.groupCount() >= 1) {
                        return m.group(1);
                    }
                    return input;
                }
            });
            break;
        case "Relative":
            rootProfiles = createElement("ENTPersonProfils", doc);
            rootProfiles.appendChild(createTextElement("ENTPersonProfil", "National_TUT", doc));
            additionalAttributes.add(rootProfiles);
            additionalAttributes.add(createTextElement("ENTEleveMEF", "", doc));
            additionalAttributes.add(createTextElement("ENTEleveCodeEnseignements", "", doc));
            additionalAttributes.add(createTextElement("ENTEleveClasses", "", doc));
            additionalAttributes.add(createTextElement("ENTEleveGroupes", "", doc));
            additionalAttributes.add(createTextElement("ENTAuxEnsClassesMatieres", "", doc));
            additionalAttributes.add(createTextElement("ENTAuxEnsGroupes", "", doc));
            additionalAttributes.add(createTextElement("ENTAuxEnsClasses", "", doc));
            additionalAttributes.add(createTextElement("ENTAuxEnsMEFs", "", doc));
            break;
        case "Personnel":
            rootProfiles = createElement("ENTPersonProfils", doc);
            rootProfiles.appendChild(createTextElement("ENTPersonProfil", "National_DOC", doc));
            additionalAttributes.add(rootProfiles);
            additionalAttributes.add(createTextElement("ENTEleveMEF", "", doc));
            additionalAttributes.add(createTextElement("ENTEleveCodeEnseignements", "", doc));
            additionalAttributes.add(createTextElement("ENTEleveClasses", "", doc));
            additionalAttributes.add(createTextElement("ENTEleveGroupes", "", doc));
            additionalAttributes.add(createTextElement("ENTAuxEnsClassesMatieres", "", doc));
            additionalAttributes.add(createTextElement("ENTAuxEnsGroupes", "", doc));
            additionalAttributes.add(createTextElement("ENTAuxEnsClasses", "", doc));
            additionalAttributes.add(createTextElement("ENTAuxEnsMEFs", "", doc));
            break;
        }

    } catch (Exception e) {
        log.error("Failed to transform User for KNE", e);
    }
}