Search in sources :

Example 1 with Type

use of org.gluu.oxtrust.model.scim2.AttributeDefinition.Type 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 2 with Type

use of org.gluu.oxtrust.model.scim2.AttributeDefinition.Type in project oxTrust by GluuFederation.

the class SchemaWebService method getSchemaInstance.

private SchemaResource getSchemaInstance(Class<? extends BaseScimResource> clazz, String urn) throws Exception {
    if (ScimResourceUtil.getDefaultSchemaUrn(clazz).equals(urn))
        // Process core attributes
        return getSchemaInstance(clazz);
    else {
        // process extension attributes
        SchemaResource resource = null;
        Class<? extends BaseScimResource> schemaCls = SchemaResource.class;
        // Find the appropriate extension
        List<Extension> extensions = extService.getResourceExtensions(clazz);
        for (Extension extension : extensions) {
            if (extension.getUrn().equals(urn)) {
                Meta meta = new Meta();
                meta.setResourceType(ScimResourceUtil.getType(schemaCls));
                meta.setLocation(endpointUrl + "/" + urn);
                resource = new SchemaResource();
                resource.setId(urn);
                resource.setName(extension.getName());
                resource.setDescription(extension.getDescription());
                resource.setMeta(meta);
                List<SchemaAttribute> attribs = new ArrayList<SchemaAttribute>();
                for (ExtensionField field : extension.getFields().values()) {
                    SchemaAttribute schAttr = new SchemaAttribute();
                    schAttr.setName(field.getName());
                    schAttr.setMultiValued(field.isMultiValued());
                    schAttr.setDescription(field.getDescription());
                    schAttr.setRequired(false);
                    schAttr.setCanonicalValues(null);
                    schAttr.setCaseExact(false);
                    schAttr.setMutability(AttributeDefinition.Mutability.READ_WRITE.getName());
                    schAttr.setReturned(AttributeDefinition.Returned.DEFAULT.getName());
                    schAttr.setUniqueness(AttributeDefinition.Uniqueness.NONE.getName());
                    schAttr.setReferenceTypes(null);
                    AttributeDefinition.Type type = field.getAttributeDefinitionType();
                    schAttr.setType(type == null ? null : type.getName());
                    schAttr.setSubAttributes(null);
                    attribs.add(schAttr);
                }
                resource.setAttributes(attribs);
                break;
            }
        }
        return resource;
    }
}
Also used : Extension(org.gluu.oxtrust.model.scim2.extensions.Extension) Meta(org.gluu.oxtrust.model.scim2.Meta) ExtensionField(org.gluu.oxtrust.model.scim2.extensions.ExtensionField) AttributeDefinition(org.gluu.oxtrust.model.scim2.AttributeDefinition) SchemaAttribute(org.gluu.oxtrust.model.scim2.provider.schema.SchemaAttribute) SchemaResource(org.gluu.oxtrust.model.scim2.provider.schema.SchemaResource)

Example 3 with Type

use of org.gluu.oxtrust.model.scim2.AttributeDefinition.Type in project oxTrust by GluuFederation.

the class SearchResourcesWebService method getListResponseTree.

/**
 * Returns a JsonNode with the response obtained from sending a POST to a search method given the SearchRequest passed
 * @param index Determines the concrete search method to be executed: (0 - user; 1 - group; 2 - fido device)
 * @param searchRequest
 * @return
 */
private JsonNode getListResponseTree(int index, SearchRequest searchRequest) {
    try {
        log.debug("getListResponseTree. Resource type is: {}", ScimResourceUtil.getType(resourceClasses[index]));
        Response r = null;
        switch(index) {
            case 0:
                r = userWS.searchUsersPost(searchRequest);
                break;
            case 1:
                r = groupWS.searchGroupsPost(searchRequest);
                break;
            case 2:
                r = fidoWS.searchDevicesPost(searchRequest, null);
                break;
        }
        if (r.getStatus() != OK.getStatusCode())
            throw new Exception("Intermediate POST search returned " + r.getStatus());
        // readEntity does not work here since data is not backed by an input stream, so we just get the raw entity
        String jsonStr = r.getEntity().toString();
        return mapper.readTree(jsonStr);
    } catch (Exception e) {
        log.error("Error in getListResponseTree {}", e.getMessage());
        log.error(e.getMessage(), e);
        return null;
    }
}
Also used : ListResponse(org.gluu.oxtrust.model.scim2.ListResponse) Response(javax.ws.rs.core.Response)

Example 4 with Type

use of org.gluu.oxtrust.model.scim2.AttributeDefinition.Type in project oxTrust by GluuFederation.

the class Scim2UserService method transferExtendedAttributesToResource.

private void transferExtendedAttributesToResource(GluuCustomPerson person, BaseScimResource resource) {
    log.debug("transferExtendedAttributesToResource of type {}", ScimResourceUtil.getType(resource.getClass()));
    // Gets the list of extensions associated to the resource passed. In practice, this will be at most a singleton list
    List<Extension> extensions = extService.getResourceExtensions(resource.getClass());
    // Iterate over every extension to copy extended attributes from person to resource
    for (Extension extension : extensions) {
        Map<String, ExtensionField> fields = extension.getFields();
        // Create empty map to store the values of the extended attributes found for current extension in object person
        Map<String, Object> map = new HashMap<String, Object>();
        log.debug("transferExtendedAttributesToResource. Revising attributes of extension '{}'", extension.getUrn());
        // Iterate over every attribute part of this extension
        for (String attr : fields.keySet()) {
            // Gets the values associated to this attribute that were found in LDAP
            String[] values = person.getAttributes(attr);
            if (values != null) {
                log.debug("transferExtendedAttributesToResource. Copying to resource the value(s) for attribute '{}'", attr);
                ExtensionField field = fields.get(attr);
                if (field.isMultiValued())
                    map.put(attr, extService.convertValues(field, values));
                else
                    map.put(attr, extService.convertValues(field, values).get(0));
            }
        }
        // Stores all extended attributes (with their values) in the resource object
        if (map.size() > 0) {
            resource.addCustomAttributes(extension.getUrn(), map);
        }
    }
    for (String urn : resource.getCustomAttributes().keySet()) resource.getSchemas().add(urn);
}
Also used : Extension(org.gluu.oxtrust.model.scim2.extensions.Extension) ExtensionField(org.gluu.oxtrust.model.scim2.extensions.ExtensionField) HashMap(java.util.HashMap)

Example 5 with Type

use of org.gluu.oxtrust.model.scim2.AttributeDefinition.Type in project oxTrust by GluuFederation.

the class Scim2UserService method transferExtendedAttributesToPerson.

/**
 * Takes all extended attributes found in the SCIM resource and copies them to a GluuCustomPerson
 * This method is called after validations take place (see associated decorator for User Service), so all inputs are
 * OK and can go straight to LDAP with no runtime surprises
 * @param resource A SCIM resource used as origin of data
 * @param person a GluuCustomPerson used as destination
 */
private void transferExtendedAttributesToPerson(BaseScimResource resource, GluuCustomPerson person) {
    try {
        // Gets all the extended attributes for this resource
        Map<String, Object> extendedAttrs = resource.getCustomAttributes();
        // Iterates over all extensions this type of resource might have
        for (Extension extension : extService.getResourceExtensions(resource.getClass())) {
            Object val = extendedAttrs.get(extension.getUrn());
            if (val != null) {
                // Obtains the attribute/value(s) pairs in the current extension
                Map<String, Object> attrsMap = IntrospectUtil.strObjMap(val);
                for (String attribute : attrsMap.keySet()) {
                    Object value = attrsMap.get(attribute);
                    // Ignore if the attribute is unassigned in this resource: destination will not be changed in this regard
                    if (value != null) {
                        // Get properly formatted string representations for the value(s) associated to the attribute
                        List<String> values = extService.getStringAttributeValues(extension.getFields().get(attribute), value);
                        log.debug("transferExtendedAttributesToPerson. Setting attribute '{}' with values {}", attribute, values.toString());
                        person.setAttribute(attribute, values.toArray(new String[] {}));
                    }
                }
            }
        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
}
Also used : Extension(org.gluu.oxtrust.model.scim2.extensions.Extension) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) WebApplicationException(javax.ws.rs.WebApplicationException)

Aggregations

Extension (org.gluu.oxtrust.model.scim2.extensions.Extension)6 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)5 ExtensionField (org.gluu.oxtrust.model.scim2.extensions.ExtensionField)4 IOException (java.io.IOException)3 List (java.util.List)3 Map (java.util.Map)3 JsonNode (org.codehaus.jackson.JsonNode)3 ArrayNode (org.codehaus.jackson.node.ArrayNode)3 SCIMException (org.gluu.oxtrust.model.exception.SCIMException)3 Attribute (org.gluu.oxtrust.model.scim2.annotations.Attribute)3 BigDecimal (java.math.BigDecimal)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 Iterator (java.util.Iterator)2 ObjectNode (org.codehaus.jackson.node.ObjectNode)2 Type (org.gluu.oxtrust.model.scim2.AttributeDefinition.Type)2 Extension (org.gluu.oxtrust.model.scim2.Extension)2 AttributeHolder (org.gluu.oxtrust.model.scim2.schema.AttributeHolder)2 CompValueType (org.gluu.oxtrust.service.antlr.scimFilter.enums.CompValueType)2 GluuAttribute (org.xdi.model.GluuAttribute)2