Search in sources :

Example 26 with Attribute

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

the class IntrospectUtil method traverseClassForNames.

private static void traverseClassForNames(Class clazz, String prefix, List<Field> extraFields, boolean prune) throws Exception {
    List<Field> fields = new ArrayList<Field>();
    fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
    fields.addAll(extraFields);
    for (Field f : fields) {
        Attribute attrAnnot = f.getAnnotation(Attribute.class);
        if (attrAnnot != null) {
            String name = f.getName();
            if (prefix.length() > 0)
                name = prefix + "." + name;
            switch(attrAnnot.returned()) {
                case ALWAYS:
                    alwaysAttrsNames.add(name);
                    break;
                case DEFAULT:
                    defaultAttrsNames.add(name);
                    break;
                case NEVER:
                    neverAttrsNames.add(name);
                    break;
                case REQUEST:
                    requestAttrsNames.add(name);
                    break;
            }
            if (attrAnnot.isRequired())
                requiredAttrsNames.add(name);
            if (attrAnnot.canonicalValues().length > 0)
                canonicalizedAttrsNames.add(name);
            Validator vAnnot = f.getAnnotation(Validator.class);
            if (vAnnot != null)
                validableAttrsNames.add(name);
            if (!prune && attrAnnot.type().equals(AttributeDefinition.Type.COMPLEX)) {
                // Use <T> parameter of Collection if present
                Class cls = attrAnnot.multiValueClass();
                if (cls.equals(NullType.class))
                    cls = f.getType();
                if (// Prevent infinite loop
                clazz.equals(cls))
                    prune = true;
                traverseClassForNames(cls.equals(NullType.class) ? f.getType() : cls, name, new ArrayList<Field>(), prune);
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) Attribute(org.gluu.oxtrust.model.scim2.annotations.Attribute) Validator(org.gluu.oxtrust.model.scim2.annotations.Validator)

Example 27 with Attribute

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

the class IntrospectUtil method computeGettersMap.

private static Map<String, List<Method>> computeGettersMap(List<String> attrNames, Class baseClass) throws Exception {
    Map<String, List<Method>> map = new HashMap<String, List<Method>>();
    for (String attrName : attrNames) {
        List<Method> list = new ArrayList<Method>();
        Class clazz = baseClass;
        for (String prop : attrName.split("\\.")) {
            Method method = getGetter(prop, clazz);
            list.add(method);
            if (isCollection(method.getReturnType())) {
                // Use class of parameter in collection
                Field f = findField(clazz, prop);
                Attribute attrAnnot = f.getAnnotation(Attribute.class);
                if (attrAnnot != null)
                    clazz = attrAnnot.multiValueClass();
            } else
                clazz = method.getReturnType();
        }
        map.put(attrName, list);
    }
    return map;
}
Also used : Field(java.lang.reflect.Field) Attribute(org.gluu.oxtrust.model.scim2.annotations.Attribute) Method(java.lang.reflect.Method)

Example 28 with Attribute

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

the class ScimResourceUtil method deleteFromResource.

/**
 * Returns a SCIM resource with the same data found in <code>origin</code> object, except for the attribute referenced
 * by <code>path</code> being removed from the output. In other words, this method nullifies an attribute.
 * @param origin The resource having the the original data
 * @param path An attribute path (in dot notation). Examples could be: <code>displayName, emails.type, addresses,
 *             meta.lastModified</code>.
 * @param extensions A list of <code>Extension</code>s associated to <code>origin</code> Object
 * @return The resulting object: data in origin without the attribute referenced by <code>path</code>
 * @throws InvalidAttributeValueException If there is an attempt to remove an attribute annotated as {@link Attribute#isRequired()
 * required} or {@link org.gluu.oxtrust.model.scim2.AttributeDefinition.Mutability#READ_ONLY read-only}
 */
public static BaseScimResource deleteFromResource(BaseScimResource origin, String path, List<Extension> extensions) throws InvalidAttributeValueException {
    Field f = IntrospectUtil.findFieldFromPath(origin.getClass(), path);
    if (f != null) {
        Attribute attrAnnot = f.getAnnotation(Attribute.class);
        if (attrAnnot != null && (attrAnnot.mutability().equals(READ_ONLY) || attrAnnot.isRequired()))
            throw new InvalidAttributeValueException("Cannot remove read-only or required attribute " + path);
    }
    Map<String, Object> map = mapper.convertValue(origin, new TypeReference<Map<String, Object>>() {
    });
    traversalClass tclass = new traversalClass(origin.getClass());
    if (// Extensions stuff
    f == null)
        deleteCustomAttribute(map, path, extensions);
    else
        tclass.traverseDelete(map, path);
    return mapper.convertValue(map, origin.getClass());
}
Also used : ExtensionField(org.gluu.oxtrust.model.scim2.extensions.ExtensionField) Field(java.lang.reflect.Field) Attribute(org.gluu.oxtrust.model.scim2.annotations.Attribute) InvalidAttributeValueException(javax.management.InvalidAttributeValueException)

Example 29 with Attribute

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

the class LdapFilterListener method enterAttrexp.

@Override
public void enterAttrexp(ScimFilterParser.AttrexpContext ctx) {
    if (StringUtils.isEmpty(error)) {
        log.trace("enterAttrexp.");
        String path = ctx.attrpath().getText();
        ScimFilterParser.CompvalueContext compValueCtx = ctx.compvalue();
        boolean isPrRule = compValueCtx == null && ctx.getChild(1).getText().equals("pr");
        Type attrType = null;
        Attribute attrAnnot = IntrospectUtil.getFieldAnnotation(path, resourceClass, Attribute.class);
        String ldapAttribute = null;
        boolean isNested = false;
        if (attrAnnot == null) {
            ExtensionField field = extService.getFieldOfExtendedAttribute(resourceClass, path);
            if (field == null)
                error = String.format("Attribute path '%s' is not recognized in %s", path, resourceClass.getSimpleName());
            else {
                attrType = field.getAttributeDefinitionType();
                ldapAttribute = path.substring(path.lastIndexOf(":") + 1);
            }
        } else {
            attrType = attrAnnot.type();
            Pair<String, Boolean> pair = FilterUtil.getLdapAttributeOfResourceAttribute(path, resourceClass);
            ldapAttribute = pair.getFirst();
            isNested = pair.getSecond();
        }
        if (error != null)
            // Intentionally left empty
            ;
        else if (attrType == null)
            error = String.format("Could not determine type of attribute path '%s' in %s", path, resourceClass.getSimpleName());
        else if (ldapAttribute == null)
            error = String.format("Could not determine LDAP attribute for path '%s' in %s", path, resourceClass.getSimpleName());
        else {
            String subattr = isNested ? path.substring(path.lastIndexOf(".") + 1) : null;
            String subFilth;
            CompValueType type;
            ScimOperator operator;
            if (isPrRule) {
                type = CompValueType.NULL;
                operator = ScimOperator.NOT_EQUAL;
            } else {
                type = FilterUtil.getCompValueType(compValueCtx);
                operator = ScimOperator.getByValue(ctx.compareop().getText());
            }
            error = FilterUtil.checkFilterConsistency(path, attrType, type, operator);
            if (error == null) {
                subFilth = getSubFilter(subattr, ldapAttribute, isPrRule ? null : compValueCtx.getText(), attrType, type, operator);
                if (subFilth == null) {
                    if (error == null)
                        error = String.format("Operator '%s' is not supported for attribute %s", operator.getValue(), path);
                } else
                    filter.append(subFilth);
            }
        }
    }
}
Also used : Type(org.gluu.oxtrust.model.scim2.AttributeDefinition.Type) CompValueType(org.gluu.oxtrust.service.antlr.scimFilter.enums.CompValueType) ExtensionField(org.gluu.oxtrust.model.scim2.extensions.ExtensionField) Attribute(org.gluu.oxtrust.model.scim2.annotations.Attribute) ScimOperator(org.gluu.oxtrust.service.antlr.scimFilter.enums.ScimOperator) ScimFilterParser(org.gluu.oxtrust.service.antlr.scimFilter.antlr4.ScimFilterParser) CompValueType(org.gluu.oxtrust.service.antlr.scimFilter.enums.CompValueType)

Example 30 with Attribute

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

the class SimpleExpression method evaluate.

public Boolean evaluate(Map<String, Object> item) {
    /*
        There are 3 categories for attribute operators:
        - eq, ne (applicable to all types)
        - co, sw, ew (applicable to STRING, REFERENCE)
        - gt, ge, lt, le (applicable to STRING, DECIMAL, REFERENCE, DATETIME)
         */
    Boolean val = null;
    Type attrType = null;
    log.trace("SimpleExpression.evaluate.");
    String msg = String.format("%s%s", StringUtils.isEmpty(parentAttribute) ? "" : (parentAttribute + "."), resourceClass.getSimpleName());
    Attribute attrAnnot = getAttributeAnnotation();
    if (attrAnnot == null) {
        if (extService != null) {
            ExtensionField field = extService.getFieldOfExtendedAttribute(resourceClass, attribute);
            if (field == null)
                log.error("SimpleExpression.evaluate. Attribute '{}' is not recognized in {}", attribute, msg);
            else
                attrType = field.getAttributeDefinitionType();
        }
    } else
        attrType = attrAnnot.type();
    if (attrType == null) {
        log.error("SimpleExpression.evaluate. Could not determine type of attribute '{}' in {}", attribute, msg);
    } else {
        String errMsg = FilterUtil.checkFilterConsistency(attribute, attrType, type, operator);
        if (errMsg == null) {
            Object currentAttrValue = item.get(attribute);
            if (type.equals(CompValueType.NULL)) {
                // implies attributeValue==null
                log.trace("SimpleExpression.evaluate. Using null as compare value");
                val = operator.equals(ScimOperator.EQUAL) ? currentAttrValue == null : currentAttrValue != null;
            } else if (currentAttrValue == null) {
                // If value is absent, filter won't match against anything (only when comparing with null as in previous case)
                log.trace("SimpleExpression.evaluate. Attribute \"{}\" is absent in resource data", attribute);
                val = false;
            } else if (// check it's a string or reference
            Type.STRING.equals(attrType) || Type.REFERENCE.equals(attrType))
                val = evaluateStringAttribute(attrAnnot != null && attrAnnot.isCaseExact(), currentAttrValue);
            else if (Type.INTEGER.equals(attrType) || Type.DECIMAL.equals(attrType))
                val = evaluateNumericAttribute(attrType, currentAttrValue);
            else if (Type.BOOLEAN.equals(attrType))
                val = evaluateBooleanAttribute(attrType, currentAttrValue);
            else if (Type.DATETIME.equals(attrType))
                val = evaluateDateTimeAttribute(attrType, currentAttrValue);
        } else
            log.error("SimpleExpression.evaluate. {}", errMsg);
    }
    return val;
}
Also used : Type(org.gluu.oxtrust.model.scim2.AttributeDefinition.Type) CompValueType(org.gluu.oxtrust.service.antlr.scimFilter.enums.CompValueType) ExtensionField(org.gluu.oxtrust.model.scim2.extensions.ExtensionField) 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