Search in sources :

Example 66 with JsonNode

use of org.codehaus.jackson.JsonNode in project oxTrust by GluuFederation.

the class SchemaTypeUserSerializer method serialize.

@Override
public void serialize(User user, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
    log.info(" serialize() ");
    try {
        ObjectMapper mapper = new ObjectMapper();
        mapper.disable(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS);
        JsonNode rootNode = mapper.convertValue(user, JsonNode.class);
        Iterator<Map.Entry<String, JsonNode>> iterator = rootNode.getFields();
        while (iterator.hasNext()) {
            Map.Entry<String, JsonNode> rootNodeEntry = iterator.next();
            if (!(SchemaTypeMapping.getSchemaTypeInstance(rootNodeEntry.getKey()) instanceof UserExtensionSchema)) {
                if (rootNodeEntry.getValue() instanceof ObjectNode) {
                    if (rootNodeEntry.getKey().equalsIgnoreCase("name")) {
                        AttributeHolder attributeHolder = new AttributeHolder();
                        attributeHolder.setName(rootNodeEntry.getKey());
                        attributeHolder.setType("string");
                        attributeHolder.setDescription("Name object");
                        attributeHolder.setRequired(Boolean.FALSE);
                        List<AttributeHolder> nameAttributeHolders = new ArrayList<AttributeHolder>();
                        Iterator<Map.Entry<String, JsonNode>> nameIterator = rootNodeEntry.getValue().getFields();
                        while (nameIterator.hasNext()) {
                            Map.Entry<String, JsonNode> nameRootNodeEntry = nameIterator.next();
                            AttributeHolder nameAttributeHolder = new AttributeHolder();
                            nameAttributeHolder.setName(nameRootNodeEntry.getKey());
                            nameAttributeHolder.setType("string");
                            if (nameRootNodeEntry.getKey().equalsIgnoreCase("formatted")) {
                                nameAttributeHolder.setDescription("Formatted name on-the-fly for display. Using this in a query filter is not supported.");
                                nameAttributeHolder.setMutability("readOnly");
                            } else {
                                nameAttributeHolder.setDescription(nameRootNodeEntry.getKey());
                            }
                            if (nameRootNodeEntry.getKey().equalsIgnoreCase("givenName") || nameRootNodeEntry.getKey().equalsIgnoreCase("familyName")) {
                                nameAttributeHolder.setRequired(true);
                            } else {
                                nameAttributeHolder.setRequired(false);
                            }
                            nameAttributeHolders.add(nameAttributeHolder);
                        }
                        attributeHolder.setSubAttributes(nameAttributeHolders);
                        attributeHolders.add(attributeHolder);
                    }
                } else if (rootNodeEntry.getValue() instanceof ArrayNode) {
                    AttributeHolder arrayNodeAttributeHolder = new AttributeHolder();
                    arrayNodeAttributeHolder.setName(rootNodeEntry.getKey());
                    if (rootNodeEntry.getKey().equalsIgnoreCase("groups")) {
                        arrayNodeAttributeHolder.setDescription(rootNodeEntry.getKey() + " list; using sub-attributes in a query filter is not supported (cross-querying)");
                        arrayNodeAttributeHolder.setCaseExact(Boolean.TRUE);
                        List<String> referenceTypes = new ArrayList<String>();
                        referenceTypes.add("Group");
                        arrayNodeAttributeHolder.setReferenceTypes(referenceTypes);
                    } else {
                        arrayNodeAttributeHolder.setDescription(rootNodeEntry.getKey() + " list");
                        arrayNodeAttributeHolder.setCaseExact(Boolean.FALSE);
                    }
                    arrayNodeAttributeHolder.setRequired(Boolean.FALSE);
                    arrayNodeAttributeHolder.setMultiValued(Boolean.TRUE);
                    if (rootNodeEntry.getKey().equalsIgnoreCase("schemas")) {
                        arrayNodeAttributeHolder.setUniqueness("server");
                        arrayNodeAttributeHolder.setType("string");
                        arrayNodeAttributeHolder.setCaseExact(Boolean.TRUE);
                        arrayNodeAttributeHolder.setReturned("always");
                    } else {
                        arrayNodeAttributeHolder.setType("complex");
                    }
                    if (rootNodeEntry.getKey().equalsIgnoreCase("photos")) {
                        arrayNodeAttributeHolder.setType("reference");
                        List<String> referenceTypes = new ArrayList<String>();
                        referenceTypes.add("uri");
                        arrayNodeAttributeHolder.setReferenceTypes(referenceTypes);
                    }
                    List<AttributeHolder> arrayNodeMapAttributeHolders = new ArrayList<AttributeHolder>();
                    Iterator<JsonNode> arrayNodeIterator = rootNodeEntry.getValue().getElements();
                    while (arrayNodeIterator.hasNext()) {
                        JsonNode jsonNode = arrayNodeIterator.next();
                        Iterator<Map.Entry<String, JsonNode>> arrayNodeMapIterator = jsonNode.getFields();
                        while (arrayNodeMapIterator.hasNext()) {
                            Map.Entry<String, JsonNode> arrayNodeMapRootNodeEntry = arrayNodeMapIterator.next();
                            AttributeHolder arrayNodeMapAttributeHolder = new AttributeHolder();
                            if (rootNodeEntry.getKey().equalsIgnoreCase("groups") && arrayNodeMapRootNodeEntry.getKey().equalsIgnoreCase("reference")) {
                                arrayNodeMapAttributeHolder.setName("$ref");
                            } else {
                                arrayNodeMapAttributeHolder.setName(arrayNodeMapRootNodeEntry.getKey());
                            }
                            arrayNodeMapAttributeHolder.setType("string");
                            arrayNodeMapAttributeHolder.setDescription(arrayNodeMapRootNodeEntry.getKey());
                            if (arrayNodeMapRootNodeEntry.getKey().equalsIgnoreCase("value") || arrayNodeMapRootNodeEntry.getKey().equalsIgnoreCase("type")) {
                                arrayNodeMapAttributeHolder.setRequired(Boolean.TRUE);
                            } else {
                                arrayNodeMapAttributeHolder.setRequired(Boolean.FALSE);
                            }
                            if (arrayNodeMapRootNodeEntry.getKey().equalsIgnoreCase("valueAsImageDataURI") || arrayNodeMapRootNodeEntry.getKey().equalsIgnoreCase("valueAsURI")) {
                                arrayNodeMapAttributeHolder.setMutability("readOnly");
                                arrayNodeMapAttributeHolder.setType("reference");
                                List<String> referenceTypes = new ArrayList<String>();
                                referenceTypes.add("uri");
                                arrayNodeMapAttributeHolder.setReferenceTypes(referenceTypes);
                            }
                            arrayNodeMapAttributeHolders.add(arrayNodeMapAttributeHolder);
                        }
                        arrayNodeAttributeHolder.setSubAttributes(arrayNodeMapAttributeHolders);
                        attributeHolders.add(arrayNodeAttributeHolder);
                    }
                } else {
                    AttributeHolder attributeHolder = new AttributeHolder();
                    attributeHolder.setName(rootNodeEntry.getKey());
                    if (rootNodeEntry.getValue().isBoolean()) {
                        attributeHolder.setType("boolean");
                    } else {
                        attributeHolder.setType("string");
                    }
                    attributeHolder.setDescription(rootNodeEntry.getKey());
                    if (rootNodeEntry.getKey().equalsIgnoreCase("userName") || rootNodeEntry.getKey().equalsIgnoreCase("displayName")) {
                        attributeHolder.setRequired(Boolean.TRUE);
                    } else {
                        attributeHolder.setRequired(Boolean.FALSE);
                    }
                    if (rootNodeEntry.getKey().equalsIgnoreCase("id") || rootNodeEntry.getKey().equalsIgnoreCase("userName")) {
                        attributeHolder.setUniqueness("server");
                        attributeHolder.setReturned("always");
                    }
                    if (rootNodeEntry.getKey().equalsIgnoreCase("id") || rootNodeEntry.getKey().equalsIgnoreCase("externalId") || rootNodeEntry.getKey().equalsIgnoreCase("password")) {
                        attributeHolder.setCaseExact(Boolean.TRUE);
                    }
                    if (rootNodeEntry.getKey().equalsIgnoreCase("id")) {
                        attributeHolder.setMutability("readOnly");
                    }
                    attributeHolders.add(attributeHolder);
                }
            }
        }
        UserCoreSchema userCoreSchema = (UserCoreSchema) schemaType;
        userCoreSchema.setAttributeHolders(attributeHolders);
        schemaType = userCoreSchema;
    } catch (Exception e) {
        e.printStackTrace();
        throw new IOException("Unexpected processing error; please check the User class structure.");
    }
}
Also used : AttributeHolder(org.gluu.oxtrust.model.scim2.schema.AttributeHolder) ObjectNode(org.codehaus.jackson.node.ObjectNode) ArrayList(java.util.ArrayList) JsonNode(org.codehaus.jackson.JsonNode) IOException(java.io.IOException) IOException(java.io.IOException) UserExtensionSchema(org.gluu.oxtrust.model.scim2.schema.extension.UserExtensionSchema) Iterator(java.util.Iterator) UserCoreSchema(org.gluu.oxtrust.model.scim2.schema.core.UserCoreSchema) ArrayList(java.util.ArrayList) List(java.util.List) ArrayNode(org.codehaus.jackson.node.ArrayNode) Map(java.util.Map) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 67 with JsonNode

use of org.codehaus.jackson.JsonNode in project oxTrust by GluuFederation.

the class UserDeserializer method deserialize.

@Override
public User deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
    log.info(" deserialize() ");
    try {
        JsonNode rootNode = jsonParser.readValueAsTree();
        ObjectMapper mapper = new ObjectMapper();
        mapper.disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);
        User user = mapper.readValue(rootNode.toString(), User.class);
        if (user.getSchemas() == null) {
            throw new IllegalArgumentException("Required field \"schemas\" is null or missing.");
        } else if (!user.getSchemas().contains(Constants.USER_CORE_SCHEMA_ID)) {
            throw new IllegalArgumentException("User Core schema is required.");
        } else if (user.getSchemas().contains(Constants.USER_EXT_SCHEMA_ID)) {
            JsonNode userExtensionNode = rootNode.get(Constants.USER_EXT_SCHEMA_ID);
            if (userExtensionNode != null) {
                ExtensionDeserializer deserializer = new ExtensionDeserializer();
                deserializer.setId(Constants.USER_EXT_SCHEMA_ID);
                SimpleModule deserializerModule = new SimpleModule("ExtensionDeserializerModule", new Version(1, 0, 0, ""));
                deserializerModule.addDeserializer(Extension.class, deserializer);
                mapper.registerModule(deserializerModule);
                Extension extension = mapper.readValue(userExtensionNode.toString(), Extension.class);
                user.addExtension(extension);
            } else {
                throw new IllegalArgumentException("User Extension schema is indicated, but value body is absent.");
            }
        }
        return user;
    } catch (Exception e) {
        e.printStackTrace();
        throw new IOException(INTERNAL_SERVER_ERROR_MESSAGE);
    }
}
Also used : Extension(org.gluu.oxtrust.model.scim2.Extension) User(org.gluu.oxtrust.model.scim2.User) Version(org.codehaus.jackson.Version) JsonNode(org.codehaus.jackson.JsonNode) IOException(java.io.IOException) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) SimpleModule(org.codehaus.jackson.map.module.SimpleModule) IOException(java.io.IOException)

Example 68 with JsonNode

use of org.codehaus.jackson.JsonNode in project oxTrust by GluuFederation.

the class SchemaTypeAbstractSerializer method serialize.

@Override
public void serialize(SchemaType schemaType, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
    log.info(" serialize() ");
    try {
        jsonGenerator.writeStartObject();
        ObjectMapper mapper = new ObjectMapper();
        mapper.disable(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS);
        JsonNode rootNode = mapper.convertValue(schemaType, JsonNode.class);
        Iterator<Map.Entry<String, JsonNode>> iterator = rootNode.getFields();
        while (iterator.hasNext()) {
            Map.Entry<String, JsonNode> rootNodeEntry = iterator.next();
            if (!rootNodeEntry.getKey().equalsIgnoreCase("schemas")) {
                jsonGenerator.writeFieldName(rootNodeEntry.getKey());
                jsonGenerator.writeObject(rootNodeEntry.getValue());
            }
        }
        jsonGenerator.writeEndObject();
    } catch (Exception e) {
        e.printStackTrace();
        throw new IOException(INTERNAL_SERVER_ERROR_MESSAGE);
    }
}
Also used : JsonNode(org.codehaus.jackson.JsonNode) IOException(java.io.IOException) Map(java.util.Map) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) IOException(java.io.IOException)

Example 69 with JsonNode

use of org.codehaus.jackson.JsonNode in project oxTrust by GluuFederation.

the class ListResponseUserSerializer method serialize.

@Override
public void serialize(User user, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
    log.info(" serialize() ");
    try {
        jsonGenerator.writeStartObject();
        ObjectMapper mapper = new ObjectMapper();
        mapper.disable(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS);
        attributes = (attributesArray != null && !attributesArray.isEmpty()) ? new LinkedHashSet<String>(Arrays.asList(attributesArray.split("\\,"))) : null;
        // attributes = (attributesArray != null && !attributesArray.isEmpty()) ? new LinkedHashSet<String>(Arrays.asList(mapper.readValue(attributesArray, String[].class))) : null;
        if (attributes != null && attributes.size() > 0) {
            attributes.add("schemas");
            attributes.add("id");
            attributes.add("userName");
            attributes.add("meta.created");
            attributes.add("meta.lastModified");
            attributes.add("meta.location");
            attributes.add("meta.version");
            attributes.add("meta.resourceType");
        }
        JsonNode rootNode = mapper.convertValue(user, JsonNode.class);
        processNodes(null, rootNode, mapper, user, jsonGenerator);
        jsonGenerator.writeEndObject();
    } catch (Exception e) {
        e.printStackTrace();
        throw new IOException(INTERNAL_SERVER_ERROR_MESSAGE);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) JsonNode(org.codehaus.jackson.JsonNode) IOException(java.io.IOException) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) IOException(java.io.IOException)

Example 70 with JsonNode

use of org.codehaus.jackson.JsonNode in project sling by apache.

the class BundleInfo method getProperties.

/**
     * @return the bundle properties in a {@link Map}
     */
public Map<String, String> getProperties() {
    JsonNode props = bundle.get("props");
    Map<String, String> entries = new HashMap<String, String>();
    if (props != null) {
        Iterator<JsonNode> it = props.getElements();
        while (it.hasNext()) {
            JsonNode n = it.next();
            entries.put(n.get("key").getTextValue(), n.get("value").getTextValue());
        }
    }
    return entries;
}
Also used : HashMap(java.util.HashMap) JsonNode(org.codehaus.jackson.JsonNode)

Aggregations

JsonNode (org.codehaus.jackson.JsonNode)200 Test (org.junit.Test)55 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)50 IOException (java.io.IOException)40 ArrayList (java.util.ArrayList)20 HashMap (java.util.HashMap)20 HTTP (org.neo4j.test.server.HTTP)18 Resource (com.netflix.simianarmy.Resource)17 AWSResource (com.netflix.simianarmy.aws.AWSResource)17 ObjectNode (org.codehaus.jackson.node.ObjectNode)15 Response (org.neo4j.test.server.HTTP.Response)12 Map (java.util.Map)9 ArrayNode (org.codehaus.jackson.node.ArrayNode)9 RpcException (cz.metacentrum.perun.core.api.exceptions.RpcException)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 JsonParseException (org.neo4j.server.rest.domain.JsonParseException)7 Date (java.util.Date)6 List (java.util.List)6 Description (org.hamcrest.Description)6 TypeSafeMatcher (org.hamcrest.TypeSafeMatcher)6