Search in sources :

Example 11 with Extension

use of org.gluu.oxtrust.model.scim2.extensions.Extension 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)

Example 12 with Extension

use of org.gluu.oxtrust.model.scim2.extensions.Extension in project oxTrust by GluuFederation.

the class ScimResourceSerializer method buildIncludeSet.

private void buildIncludeSet(SortedSet<String> include, Class<? extends BaseScimResource> resourceClass, List<String> schemas, String attributes, String exclussions) {
    Set<String> tempSet;
    Set<String> alwaysSet = IntrospectUtil.alwaysCoreAttrs.get(resourceClass).keySet();
    Set<String> neverSet = IntrospectUtil.neverCoreAttrs.get(resourceClass).keySet();
    Set<String> defaultSet = new HashSet<String>();
    // Here we assume all attributes part of extensions have returnability="default"...
    SortedSet<String> extendedSet = new TreeSet<String>();
    for (Extension ext : extService.getResourceExtensions(resourceClass)) {
        extendedSet.add(ext.getUrn());
        extendedSet.addAll(IntrospectUtil.getPathsInExtension(ext));
    }
    defaultSet.addAll(IntrospectUtil.defaultCoreAttrs.get(resourceClass).keySet());
    defaultSet.addAll(extendedSet);
    String defaultSchema = ScimResourceUtil.getDefaultSchemaUrn(resourceClass);
    if (attributes != null) {
        log.info("buildIncludeSet. Processing attributes query param (excludedAttributes ignored)");
        extendedSet.addAll(IntrospectUtil.allAttrs.get(resourceClass));
        tempSet = expandAttributesPaths(attributes, defaultSchema, schemas, extendedSet);
        tempSet.removeAll(neverSet);
        include.addAll(tempSet);
    } else if (exclussions != null) {
        log.info("buildIncludeSet. Processing excludedAttributes query param");
        extendedSet.addAll(IntrospectUtil.allAttrs.get(resourceClass));
        tempSet = defaultSet;
        tempSet.removeAll(expandAttributesPaths(exclussions, defaultSchema, schemas, extendedSet));
        include.addAll(tempSet);
    } else {
        log.info("buildIncludeSet. No attributes neither excludedAttributes query param were passed");
        include.addAll(defaultSet);
    }
    include.addAll(alwaysSet);
}
Also used : Extension(org.gluu.oxtrust.model.scim2.extensions.Extension)

Example 13 with Extension

use of org.gluu.oxtrust.model.scim2.extensions.Extension in project oxTrust by GluuFederation.

the class ResourceValidator method validateExtendedAttributes.

/**
 * Inspects the resource passed in the constructor and for every extended attribute (see {@link BaseScimResource#getCustomAttributes()},
 * the attribute's value is checked to see if it complies with the data type it is supposed to belong to. This
 * information is obtained from the list of <code>Extension</code>s passed in the constructor (every {@link ExtensionField}
 * has an associated {@link ExtensionField#getType() type}.
 * <p>When an attribute is {@link ExtensionField#isMultiValued() multi-valued}, every single item inside the collection
 * is validated.</p>
 * @throws SCIMException When any of the validations do not pass or an attribute seems not to be part of a known schema.
 */
public void validateExtendedAttributes() throws SCIMException {
    // Note: throughout this method, we always ignore presence of nulls
    // Gets all extended attributes (see the @JsonAnySetter annotation in BaseScimResource)
    Map<String, Object> extendedAttributes = resource.getCustomAttributes();
    // Iterate over every extension of the resource object (in practice it will be just one at most)
    for (String schema : extendedAttributes.keySet()) {
        // Validate if the schema referenced in the extended attributes is contained in the valid set of extension
        Extension extension = null;
        for (Extension ext : extensions) if (ext.getUrn().equals(schema)) {
            extension = ext;
            break;
        }
        if (extension != null) {
            log.debug("validateExtendedAttributes. Revising attributes under schema {}", schema);
            try {
                // Obtains a generic map consisting of all name/value(s) pairs associated to this schema
                Map<String, Object> attrsMap = IntrospectUtil.strObjMap(extendedAttributes.get(schema));
                for (String attr : attrsMap.keySet()) {
                    Object value = attrsMap.get(attr);
                    if (value != null) {
                        /*
                             Gets the class associated to the value of current attribute. For extended attributes, we
                             should only see coming: String, Integer, Double, boolean, and Collection.
                             Different things will be rejected
                             */
                        Class cls = value.getClass();
                        boolean isCollection = IntrospectUtil.isCollection(cls);
                        // If the attribute coming is unknown, NPE will be thrown and we are covered
                        log.debug("validateExtendedAttributes. Got value(s) for attribute '{}'", attr);
                        // Check if the multivalued custom attribute is consistent with the nature of the value itself
                        if (isCollection == extension.getFields().get(attr).isMultiValued()) {
                            if (isCollection) {
                                for (Object elem : (Collection) value) if (elem != null)
                                    validateDataTypeExtendedAttr(extension, attr, elem);
                            } else
                                validateDataTypeExtendedAttr(extension, attr, value);
                        } else
                            throw new SCIMException(ERROR_PARSING_EXTENDED);
                    }
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                throw new SCIMException(ERROR_PARSING_EXTENDED);
            }
        } else
            throw new SCIMException(String.format(UNKNOWN_EXTENSION, schema));
    }
}
Also used : Extension(org.gluu.oxtrust.model.scim2.extensions.Extension) SCIMException(org.gluu.oxtrust.model.exception.SCIMException) SCIMException(org.gluu.oxtrust.model.exception.SCIMException)

Example 14 with Extension

use of org.gluu.oxtrust.model.scim2.extensions.Extension in project oxTrust by GluuFederation.

the class ResourceValidator method validateSchemasAttribute.

/**
 * Inspects the {@link BaseScimResource#getSchemas() schemas} attribute of the resource passed in the constructor and
 * checks the default schema <code>urn</code> associated to the resource type is present in the list. If some of the
 * <code>urn</code>s part of the <code>Extension</code>s passed in the constructor are contained in the list, the validation is also
 * successful.
 * <p>This method should be called after a successful call to {@link #validateRequiredAttributes()}.</p>
 * @throws SCIMException If there is no {@link BaseScimResource#getSchemas() schemas} in this resource or if some of
 * the <code>urn</code>s there are not known.
 */
public void validateSchemasAttribute() throws SCIMException {
    Set<String> schemaList = new HashSet<String>(resource.getSchemas());
    if (schemaList.size() == 0)
        throw new SCIMException(WRONG_SCHEMAS_ATTR);
    Set<String> allSchemas = new HashSet<String>();
    allSchemas.add(ScimResourceUtil.getDefaultSchemaUrn(resourceClass));
    for (Extension ext : extensions) allSchemas.add(ext.getUrn());
    schemaList.removeAll(allSchemas);
    if (// means that some wrong extension urn is there
    schemaList.size() > 0)
        throw new SCIMException(WRONG_SCHEMAS_ATTR);
}
Also used : Extension(org.gluu.oxtrust.model.scim2.extensions.Extension) SCIMException(org.gluu.oxtrust.model.exception.SCIMException)

Example 15 with Extension

use of org.gluu.oxtrust.model.scim2.extensions.Extension in project oxTrust by GluuFederation.

the class ExtensionDeserializer method deserialize.

@Override
public Extension deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
    log.info(" deserialize() ");
    try {
        if (id == null || id.isEmpty()) {
            throw new IllegalArgumentException("The URN cannot be null or empty");
        }
        JsonNode rootNode = jsonParser.readValueAsTree();
        if (!rootNode.isObject()) {
            throw new IllegalArgumentException("Extension is of wrong JSON type");
        }
        Extension.Builder extensionBuilder = new Extension.Builder(id);
        Iterator<Map.Entry<String, JsonNode>> fieldIterator = rootNode.getFields();
        while (fieldIterator.hasNext()) {
            Map.Entry<String, JsonNode> entry = fieldIterator.next();
            GluuAttribute gluuAttribute = attributeService.getAttributeByName(entry.getKey());
            if (gluuAttribute != null) {
                if (!(gluuAttribute.getOxSCIMCustomAttribute() != null && gluuAttribute.getOxSCIMCustomAttribute().equals(ScimCustomAtribute.TRUE))) {
                    log.info(" NOT A CUSTOM ATTRIBUTE: " + gluuAttribute.getName());
                    throw new IllegalArgumentException("NOT A CUSTOM ATTRIBUTE: " + gluuAttribute.getName());
                }
                GluuAttributeDataType attributeDataType = gluuAttribute.getDataType();
                if ((gluuAttribute.getOxMultivaluedAttribute() != null) && gluuAttribute.getOxMultivaluedAttribute().equals(OxMultivalued.TRUE)) {
                    if (entry.getValue() instanceof ArrayNode) {
                        ArrayNode arrayNode = (ArrayNode) entry.getValue();
                        ObjectMapper mapper = new ObjectMapper();
                        mapper.disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);
                        if (attributeDataType.equals(GluuAttributeDataType.STRING) || attributeDataType.equals(GluuAttributeDataType.PHOTO)) {
                            List<String> stringList = Arrays.asList(mapper.readValue(arrayNode, String[].class));
                            extensionBuilder.setFieldAsList(entry.getKey(), stringList);
                        } else if (attributeDataType.equals(GluuAttributeDataType.DATE)) {
                            // For validation
                            List<Date> dateList = Arrays.asList(mapper.readValue(arrayNode, Date[].class));
                            extensionBuilder.setFieldAsList(entry.getKey(), Arrays.asList(mapper.readValue(arrayNode, String[].class)));
                        } else if (attributeDataType.equals(GluuAttributeDataType.NUMERIC)) {
                            List<BigDecimal> numberList = Arrays.asList(mapper.readValue(arrayNode, BigDecimal[].class));
                            extensionBuilder.setFieldAsList(entry.getKey(), numberList);
                        } else {
                            log.info(" NO MATCH: attributeDataType.getDisplayName() = " + attributeDataType.getDisplayName());
                            throw new IllegalArgumentException("JSON type not supported: " + entry.getValue().toString());
                        }
                    } else {
                        throw new IllegalArgumentException("Attribute \"" + entry.getKey() + "\" is multi-valued but passed value is not of array type.");
                    }
                } else {
                    if (entry.getValue() instanceof ArrayNode) {
                        throw new IllegalArgumentException("Attribute \"" + entry.getKey() + "\" is not multi-valued but passed value is of array type.");
                    } else {
                        if (attributeDataType.equals(GluuAttributeDataType.STRING) || attributeDataType.equals(GluuAttributeDataType.PHOTO)) {
                            handleString(extensionBuilder, entry);
                        } else if (attributeDataType.equals(GluuAttributeDataType.DATE)) {
                            handleDateTime(extensionBuilder, entry);
                        } else if (attributeDataType.equals(GluuAttributeDataType.NUMERIC)) {
                            handleNumber(extensionBuilder, entry);
                        } else {
                            log.info(" NO MATCH: attributeDataType.getDisplayName() = " + attributeDataType.getDisplayName());
                            throw new IllegalArgumentException("JSON type not supported: " + entry.getValue().toString());
                        }
                    }
                }
            } else {
                throw new IllegalArgumentException("NOT FOUND: custom attribute = " + entry.getKey());
            }
        }
        return extensionBuilder.build();
    } catch (Exception e) {
        e.printStackTrace();
        throw new IOException(INTERNAL_SERVER_ERROR_MESSAGE);
    }
}
Also used : JsonNode(org.codehaus.jackson.JsonNode) GluuAttributeDataType(org.xdi.model.GluuAttributeDataType) IOException(java.io.IOException) Date(java.util.Date) BigDecimal(java.math.BigDecimal) IOException(java.io.IOException) GluuAttribute(org.xdi.model.GluuAttribute) Extension(org.gluu.oxtrust.model.scim2.Extension) List(java.util.List) ArrayNode(org.codehaus.jackson.node.ArrayNode) Map(java.util.Map) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Aggregations

Extension (org.gluu.oxtrust.model.scim2.extensions.Extension)14 Extension (org.gluu.oxtrust.model.scim2.Extension)8 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)6 ExtensionField (org.gluu.oxtrust.model.scim2.extensions.ExtensionField)6 GluuAttribute (org.xdi.model.GluuAttribute)6 BigDecimal (java.math.BigDecimal)5 Date (java.util.Date)5 HashMap (java.util.HashMap)4 User (org.gluu.oxtrust.model.scim2.User)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 GluuCustomPerson (org.gluu.oxtrust.model.GluuCustomPerson)3 SCIMException (org.gluu.oxtrust.model.exception.SCIMException)3 GluuAttributeDataType (org.xdi.model.GluuAttributeDataType)3 List (java.util.List)2 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)2 JsonNode (org.codehaus.jackson.JsonNode)2 Version (org.codehaus.jackson.Version)2 SimpleModule (org.codehaus.jackson.map.module.SimpleModule)2