Search in sources :

Example 16 with Attribute

use of org.gluu.oxtrust.model.scim2.annotations.Attribute 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 17 with Attribute

use of org.gluu.oxtrust.model.scim2.annotations.Attribute in project oxTrust by GluuFederation.

the class ResourceValidator method validateCanonicalizedAttributes.

/**
 * Inspects the resource passed in the constructor and for every attribute annotated with a non-empty collection of
 * {@link Attribute#canonicalValues() canonical values}, it checks whether the attribute value matches any of the
 * canonical values supplied.
 * <p>This method should be called after a successful call to {@link #validateRequiredAttributes()}.</p>
 * @throws SCIMException When a validation does not pass (there is no match for any of the attributes inspected)
 */
public void validateCanonicalizedAttributes() throws SCIMException {
    Map<String, List<Method>> map = IntrospectUtil.canonicalCoreAttrs.get(resourceClass);
    for (String attributePath : map.keySet()) {
        Attribute attrAnnot = IntrospectUtil.getFieldAnnotation(attributePath, resourceClass, Attribute.class);
        List<String> canonicalVals = Arrays.asList(attrAnnot.canonicalValues());
        log.debug("Validating values of canonical attribute '{}'", attributePath);
        for (Object val : IntrospectUtil.getAttributeValues(resource, map.get(attributePath))) {
            if (!canonicalVals.contains(val.toString())) {
                log.error("Error validating canonical attribute '{}', wrong value supplied: '{}'", attributePath, val.toString());
                throw new SCIMException(String.format(ATTR_VALIDATION_FAILED, attributePath));
            }
        }
    }
}
Also used : SCIMException(org.gluu.oxtrust.model.exception.SCIMException) Attribute(org.gluu.oxtrust.model.scim2.annotations.Attribute)

Example 18 with Attribute

use of org.gluu.oxtrust.model.scim2.annotations.Attribute 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 19 with Attribute

use of org.gluu.oxtrust.model.scim2.annotations.Attribute in project oxTrust by GluuFederation.

the class ScimResourceUtil method traverse.

void traverse(String prefix, Map<String, Object> source, Map<String, Object> destination, boolean replacing) {
    for (String key : source.keySet()) {
        Object value = source.get(key);
        Object destValue = destination.get(key);
        if (value != null && error == null) {
            // Atributes related to extensions evaluate null here
            Attribute attrAnnot = IntrospectUtil.getFieldAnnotation(getNewPrefix(prefix, key), base, Attribute.class);
            if (attrAnnot != null && !attrAnnot.mutability().equals(READ_ONLY)) {
                if (value instanceof Map)
                    value = smallerMap(getNewPrefix(prefix, key), IntrospectUtil.strObjMap(value), destValue, replacing);
                else if (attrAnnot.mutability().equals(IMMUTABLE) && destValue != null && !value.equals(destValue)) {
                    // provokes no more traversals
                    error = "Invalid value passed for immutable attribute " + key;
                    value = null;
                }
                if (value != null) {
                    if (IntrospectUtil.isCollection(value.getClass())) {
                        Collection col = (Collection) value;
                        int size = col.size();
                        if (!replacing) {
                            // we need to add to the existing collection
                            if (destValue != null) {
                                if (!IntrospectUtil.isCollection(destValue.getClass()))
                                    log.warn("Value {} was expected to be a collection", destValue);
                                else
                                    col.addAll((Collection) destValue);
                            }
                        }
                        // Do the arrangement so that only one primary="true" can stay in data
                        value = col.size() == 0 ? null : adjustPrimarySubAttributes(col, size);
                    }
                    destination.put(key, value);
                }
            }
        }
    }
}
Also used : Attribute(org.gluu.oxtrust.model.scim2.annotations.Attribute)

Example 20 with Attribute

use of org.gluu.oxtrust.model.scim2.annotations.Attribute in project oxTrust by GluuFederation.

the class UpdatePersonAction method syncEmailReverse.

/**
 * One-way sync from "mail" to "oxTrustEmail". This method takes current values
 * of "oxTrustEmail" attribute, deletes those that do not match any of those in
 * "mail", and adds new ones that are missing.
 *
 * @param gluuCustomPerson
 * @param isScim2
 * @return
 * @throws Exception
 */
public GluuCustomPerson syncEmailReverse(GluuCustomPerson gluuCustomPerson, boolean isScim2) throws Exception {
    /*
		 * Implementation of this method could not be simplified to creating a new empty
		 * list for oxTrustEmail and then do the respective additions based on current
		 * mail values since information such as display, primary, etc. would be lost.
		 * Instead, it uses set operations to know which existing entries must be
		 * removed or retained, and then apply additions of new data.
		 */
    log.info(" IN Utils.syncEmailReverse()...");
    GluuCustomAttribute mail = gluuCustomPerson.getGluuCustomAttribute("mail");
    GluuCustomAttribute oxTrustEmail = gluuCustomPerson.getGluuCustomAttribute("oxTrustEmail");
    if (mail == null) {
        gluuCustomPerson.setAttribute("oxTrustEmail", new String[0]);
    } else {
        Set<String> mailSet = new HashSet<String>();
        if (mail.getValues() != null)
            mailSet.addAll(Arrays.asList(mail.getValues()));
        Set<String> mailSetCopy = new HashSet<String>(mailSet);
        Set<String> oxTrustEmailSet = new HashSet<String>();
        List<Email> oxTrustEmails = new ArrayList<Email>();
        if (oxTrustEmail != null && oxTrustEmail.getValues() != null) {
            for (String oxTrustEmailJson : oxTrustEmail.getValues()) {
                oxTrustEmails.add(jsonService.jsonToObject(oxTrustEmailJson, Email.class));
            }
            for (Email email : oxTrustEmails) {
                oxTrustEmailSet.add(email.getValue());
            }
        }
        // Keep those in "mail" and not in oxTrustEmail
        mailSetCopy.removeAll(oxTrustEmailSet);
        // Keep those in oxTrustEmail and not in "mail"
        oxTrustEmailSet.removeAll(mailSet);
        List<Integer> delIndexes = new ArrayList<Integer>();
        // Build a list of indexes that should be removed in oxTrustEmails
        for (int i = 0; i < oxTrustEmails.size(); i++) {
            if (oxTrustEmailSet.contains(oxTrustEmails.get(i).getValue())) {
                delIndexes.add(0, i);
            }
        }
        // Delete unmatched oxTrustEmail entries from highest index to lowest
        for (Integer idx : delIndexes) {
            // must not pass an Integer directly
            oxTrustEmails.remove(idx.intValue());
        }
        List<String> newValues = new ArrayList<String>();
        for (Email email : oxTrustEmails) {
            newValues.add(jsonService.objectToPerttyJson(email));
        }
        for (String mailStr : mailSetCopy) {
            Email email = new Email();
            email.setValue(mailStr);
            email.setPrimary(false);
            newValues.add(jsonService.objectToPerttyJson(email));
        }
        gluuCustomPerson.setAttribute("oxTrustEmail", newValues.toArray(new String[0]));
    }
    log.info(" LEAVING Utils.syncEmailReverse()...");
    return gluuCustomPerson;
}
Also used : GluuCustomAttribute(org.gluu.oxtrust.model.GluuCustomAttribute) Email(org.gluu.oxtrust.model.scim2.user.Email) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Aggregations

Attribute (org.gluu.oxtrust.model.scim2.annotations.Attribute)11 SCIMException (org.gluu.oxtrust.model.exception.SCIMException)9 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)8 Extension (org.gluu.oxtrust.model.scim2.extensions.Extension)8 ExtensionField (org.gluu.oxtrust.model.scim2.extensions.ExtensionField)8 Field (java.lang.reflect.Field)6 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)6 GluuCustomPerson (org.gluu.oxtrust.model.GluuCustomPerson)6 ArrayList (java.util.ArrayList)5 Date (java.util.Date)5 GluuAttribute (org.xdi.model.GluuAttribute)5 BigDecimal (java.math.BigDecimal)4 Response (javax.ws.rs.core.Response)4 Extension (org.gluu.oxtrust.model.scim2.Extension)4 ListResponse (org.gluu.oxtrust.model.scim2.ListResponse)4 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)3 URI (java.net.URI)3 Consumes (javax.ws.rs.Consumes)3 DefaultValue (javax.ws.rs.DefaultValue)3 HeaderParam (javax.ws.rs.HeaderParam)3