Search in sources :

Example 1 with Attribute

use of io.jans.scim.model.scim2.annotations.Attribute in project oxTrust by GluuFederation.

the class SchemaWebService method getSchemaInstance.

private SchemaResource getSchemaInstance(Class<? extends BaseScimResource> clazz) throws Exception {
    SchemaResource resource;
    Class<? extends BaseScimResource> schemaCls = SchemaResource.class;
    Schema annotation = ScimResourceUtil.getSchemaAnnotation(clazz);
    if (!clazz.equals(schemaCls) && annotation != null) {
        Meta meta = new Meta();
        meta.setResourceType(ScimResourceUtil.getType(schemaCls));
        meta.setLocation(endpointUrl + "/" + annotation.id());
        resource = new SchemaResource();
        resource.setId(annotation.id());
        resource.setName(annotation.name());
        resource.setDescription(annotation.description());
        resource.setMeta(meta);
        List<SchemaAttribute> attribs = new ArrayList<SchemaAttribute>();
        // paths are, happily alphabetically sorted :)
        for (String path : IntrospectUtil.allAttrs.get(clazz)) {
            SchemaAttribute schAttr = new SchemaAttribute();
            Field f = IntrospectUtil.findFieldFromPath(clazz, path);
            Attribute attrAnnot = f.getAnnotation(Attribute.class);
            if (attrAnnot != null) {
                JsonProperty jsonAnnot = f.getAnnotation(JsonProperty.class);
                schAttr.setName(jsonAnnot == null ? f.getName() : jsonAnnot.value());
                schAttr.setType(attrAnnot.type().getName());
                schAttr.setMultiValued(!attrAnnot.multiValueClass().equals(NullType.class) || IntrospectUtil.isCollection(f.getType()));
                schAttr.setDescription(attrAnnot.description());
                schAttr.setRequired(attrAnnot.isRequired());
                schAttr.setCanonicalValues(attrAnnot.canonicalValues().length == 0 ? null : Arrays.asList(attrAnnot.canonicalValues()));
                schAttr.setCaseExact(attrAnnot.isCaseExact());
                schAttr.setMutability(attrAnnot.mutability().getName());
                schAttr.setReturned(attrAnnot.returned().getName());
                schAttr.setUniqueness(attrAnnot.uniqueness().getName());
                schAttr.setReferenceTypes(attrAnnot.referenceTypes().length == 0 ? null : Arrays.asList(attrAnnot.referenceTypes()));
                if (attrAnnot.type().equals(AttributeDefinition.Type.COMPLEX))
                    schAttr.setSubAttributes(new ArrayList<SchemaAttribute>());
                // root list
                List<SchemaAttribute> list = attribs;
                String[] parts = path.split("\\.");
                for (int i = 0; i < parts.length - 1; i++) {
                    // skip last part (real attribute name)
                    int j = list.indexOf(new SchemaAttribute(parts[i]));
                    list = list.get(j).getSubAttributes();
                }
                list.add(schAttr);
            }
        }
        resource.setAttributes(attribs);
    } else
        resource = null;
    return resource;
}
Also used : Meta(org.gluu.oxtrust.model.scim2.Meta) JsonProperty(org.codehaus.jackson.annotate.JsonProperty) Attribute(org.gluu.oxtrust.model.scim2.annotations.Attribute) SchemaAttribute(org.gluu.oxtrust.model.scim2.provider.schema.SchemaAttribute) Schema(org.gluu.oxtrust.model.scim2.annotations.Schema) ExtensionField(org.gluu.oxtrust.model.scim2.extensions.ExtensionField) Field(java.lang.reflect.Field) NullType(javax.lang.model.type.NullType) SchemaAttribute(org.gluu.oxtrust.model.scim2.provider.schema.SchemaAttribute) SchemaResource(org.gluu.oxtrust.model.scim2.provider.schema.SchemaResource)

Example 2 with Attribute

use of io.jans.scim.model.scim2.annotations.Attribute in project oxTrust by GluuFederation.

the class Scim2PatchService method applyPatchOperationWithValueFilter.

private BaseScimResource applyPatchOperationWithValueFilter(BaseScimResource resource, PatchOperation operation, String valSelFilter, String attribute, String subAttribute) throws SCIMException, InvalidAttributeValueException {
    String path = operation.getPath();
    ObjectMapper mapper = new ObjectMapper();
    Class<? extends BaseScimResource> cls = resource.getClass();
    Map<String, Object> resourceAsMap = mapper.convertValue(resource, new TypeReference<Map<String, Object>>() {
    });
    List<Map<String, Object>> list;
    Attribute attrAnnot = IntrospectUtil.getFieldAnnotation(attribute, cls, Attribute.class);
    if (attrAnnot != null) {
        if (!attrAnnot.multiValueClass().equals(NullType.class) && attrAnnot.type().equals(AttributeDefinition.Type.COMPLEX)) {
            Object colObject = resourceAsMap.get(attribute);
            list = colObject == null ? null : new ArrayList<Map<String, Object>>((Collection<Map<String, Object>>) colObject);
        } else
            throw new SCIMException(String.format("Attribute '%s' expected to be complex multi-valued", attribute));
    } else
        throw new SCIMException(String.format("Attribute '%s' not recognized or expected to be complex multi-valued", attribute));
    if (list == null)
        log.info("applyPatchOperationWithValueFilter. List of values for {} is empty. Operation has no effect", attribute);
    else {
        try {
            valSelFilter = FilterUtil.preprocess(valSelFilter, cls);
            ParseTree parseTree = filterService.getParseTree(valSelFilter);
            List<Integer> matchingIndexes = new ArrayList<Integer>();
            for (int i = 0; i < list.size(); i++) {
                if (filterService.complexAttributeMatch(parseTree, list.get(i), attribute, cls))
                    // Important: add so that resulting list is reverse-ordered
                    matchingIndexes.add(0, i);
            }
            if (subAttribute.length() > 0 && matchingIndexes.size() > 0 && operation.getType().equals(PatchOperationType.REMOVE)) {
                // per spec (section 3.5.2.2 RFC 7644) subAttribute must not be required or read-only
                Attribute subAttrAnnot = IntrospectUtil.getFieldAnnotation(attribute + "." + subAttribute, cls, Attribute.class);
                if (subAttrAnnot != null && (subAttrAnnot.mutability().equals(READ_ONLY) || subAttrAnnot.isRequired()))
                    throw new InvalidAttributeValueException("Cannot remove read-only or required attribute " + attribute + "." + subAttribute);
            }
            /*
                Here we differ from spec (see section 3.5.2.3/4 of RFC7644. If no record match is made, we are supposed to
                return error 400 with scimType of noTarget. But this is clearly inconvenient
                */
            log.info("There are {} entries matching the filter '{}'", matchingIndexes.size(), path);
            for (Integer index : matchingIndexes) {
                if (operation.getType().equals(PatchOperationType.REMOVE)) {
                    if (// Remove the whole item
                    subAttribute.length() == 0)
                        // If intValue is not used, the remove(Object) method is called!
                        list.remove(index.intValue());
                    else
                        // remove subattribute only
                        list.get(index).remove(subAttribute);
                } else
                    applyPartialUpdate(attribute, subAttribute, list, index, operation.getValue(), cls);
            }
            log.trace("New {} list is:\n{}", attribute, mapper.writeValueAsString(list));
            resourceAsMap.put(attribute, list.size() == 0 ? null : list);
            resource = mapper.convertValue(resourceAsMap, cls);
        } catch (InvalidAttributeValueException ei) {
            throw ei;
        } catch (Exception e) {
            log.info("Error processing Patch operation with value selection path '{}'", path);
            log.error(e.getMessage(), e);
            throw new SCIMException(e.getMessage(), e);
        }
    }
    return resource;
}
Also used : Attribute(org.gluu.oxtrust.model.scim2.annotations.Attribute) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) SCIMException(org.gluu.oxtrust.model.exception.SCIMException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) SCIMException(org.gluu.oxtrust.model.exception.SCIMException) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 3 with Attribute

use of io.jans.scim.model.scim2.annotations.Attribute in project oxTrust by GluuFederation.

the class IntrospectUtil method findFieldFromPath.

/**
 * Inspects a class to search for a field that corresponds to the path passed using dot notation. Every piece of the
 * path (separated by the a dot '.') is expected to have a field with the same name in the class inspected. When such
 * a field is found, the remainder of the path is processed using the class associated to the field, until the path is
 * fully consumed.
 * <p>This method starts from an initial class and visits ascendingly the class hierarchy with a route determined
 * by the components found in the path parameter.</p>
 * @param initcls Class to start the search from
 * @param path A string denoting a path to a target attribute. Examples of valid paths can be: displayName, name.givenName,
 *            addresses.locality
 * @return A Field that represents the terminal portion of the path, for instance "locality" field for "addresses.locality".
 * If no such field is found (because at some point, there was no route to go), null is returned.
 */
public static Field findFieldFromPath(Class<?> initcls, String path) {
    Class cls = initcls;
    Field f = null;
    for (String prop : path.split("\\.")) {
        f = findField(cls, prop);
        if (f != null) {
            cls = f.getType();
            if (isCollection(cls)) {
                Attribute attrAnnot = f.getAnnotation(Attribute.class);
                if (attrAnnot != null)
                    cls = attrAnnot.multiValueClass();
            }
        } else
            break;
    }
    return f;
}
Also used : Field(java.lang.reflect.Field) Attribute(org.gluu.oxtrust.model.scim2.annotations.Attribute)

Example 4 with Attribute

use of io.jans.scim.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 5 with Attribute

use of io.jans.scim.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)

Aggregations

Response (javax.ws.rs.core.Response)19 UserResource (io.jans.scim.model.scim2.user.UserResource)13 ListResponse (io.jans.scim.model.scim2.ListResponse)12 Test (org.testng.annotations.Test)12 SCIMException (io.jans.scim.model.exception.SCIMException)11 Attribute (io.jans.scim.model.scim2.annotations.Attribute)11 Field (java.lang.reflect.Field)11 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)11 Attribute (org.gluu.oxtrust.model.scim2.annotations.Attribute)11 ExtensionField (io.jans.scim.model.scim2.extensions.ExtensionField)10 Extension (io.jans.scim.model.scim2.extensions.Extension)9 UserBaseTest (io.jans.scim2.client.UserBaseTest)7 BaseScimResource (io.jans.scim.model.scim2.BaseScimResource)5 NullType (javax.lang.model.type.NullType)5 Path (javax.ws.rs.Path)5 SearchRequest (io.jans.scim.model.scim2.SearchRequest)4 ProtectedApi (io.jans.scim.service.filter.ProtectedApi)4 RefAdjusted (io.jans.scim.service.scim2.interceptor.RefAdjusted)4 BaseTest (io.jans.scim2.client.BaseTest)4 URI (java.net.URI)4