Search in sources :

Example 11 with Type

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

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

Example 13 with Type

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

the class Scim2PatchService method applyPatchOperation.

public BaseScimResource applyPatchOperation(BaseScimResource resource, PatchOperation operation) throws Exception {
    BaseScimResource result = null;
    Map<String, Object> genericMap = null;
    PatchOperationType opType = operation.getType();
    Class<? extends BaseScimResource> clazz = resource.getClass();
    String path = operation.getPath();
    log.debug("applyPatchOperation of type {}", opType);
    // Determine if operation is with value filter
    if (StringUtils.isNotEmpty(path) && !operation.getType().equals(PatchOperationType.ADD)) {
        Pair<Boolean, String> pair = validateBracketedPath(path);
        if (pair.getFirst()) {
            String valSelFilter = pair.getSecond();
            if (valSelFilter == null)
                throw new SCIMException("Unexpected syntax in value selection filter");
            else {
                int i = path.indexOf("[");
                String attribute = path.substring(0, i);
                i = path.lastIndexOf("].");
                String subAttribute = i == -1 ? "" : path.substring(i + 2);
                // Abort earlier
                return applyPatchOperationWithValueFilter(resource, operation, valSelFilter, attribute, subAttribute);
            }
        }
    }
    if (!opType.equals(PatchOperationType.REMOVE)) {
        Object value = operation.getValue();
        List<String> extensionUrns = extService.getUrnsOfExtensions(clazz);
        if (value instanceof Map)
            genericMap = IntrospectUtil.strObjMap(value);
        else {
            // It's an atomic value or an array
            if (StringUtils.isEmpty(path))
                throw new SCIMException("Value(s) supplied for resource not parseable");
            // Create a simple map and trim the last part of path
            String[] subPaths = ScimResourceUtil.splitPath(path, extensionUrns);
            genericMap = Collections.singletonMap(subPaths[subPaths.length - 1], value);
            if (subPaths.length == 1)
                path = "";
            else
                path = path.substring(0, path.lastIndexOf("."));
        }
        if (StringUtils.isNotEmpty(path)) {
            // Visit backwards creating a composite map
            String[] subPaths = ScimResourceUtil.splitPath(path, extensionUrns);
            for (int i = subPaths.length - 1; i >= 0; i--) {
                // Create a string consisting of all subpaths until the i-th
                StringBuilder sb = new StringBuilder();
                for (int j = 0; j <= i; j++) sb.append(subPaths[j]).append(".");
                Attribute annot = IntrospectUtil.getFieldAnnotation(sb.substring(0, sb.length() - 1), clazz, Attribute.class);
                boolean multivalued = !(annot == null || annot.multiValueClass().equals(NullType.class));
                Map<String, Object> genericBiggerMap = new HashMap<String, Object>();
                genericBiggerMap.put(subPaths[i], multivalued ? Collections.singletonList(genericMap) : genericMap);
                genericMap = genericBiggerMap;
            }
        }
        log.debug("applyPatchOperation. Generating a ScimResource from generic map: {}", genericMap.toString());
    }
    // Try parse genericMap as an instance of the resource
    ObjectMapper mapper = new ObjectMapper();
    BaseScimResource alter = opType.equals(PatchOperationType.REMOVE) ? resource : mapper.convertValue(genericMap, clazz);
    List<Extension> extensions = extService.getResourceExtensions(clazz);
    switch(operation.getType()) {
        case REPLACE:
            result = ScimResourceUtil.transferToResourceReplace(alter, resource, extensions);
            break;
        case ADD:
            result = ScimResourceUtil.transferToResourceAdd(alter, resource, extensions);
            break;
        case REMOVE:
            result = ScimResourceUtil.deleteFromResource(alter, operation.getPath(), extensions);
            break;
    }
    return result;
}
Also used : PatchOperationType(org.gluu.oxtrust.model.scim2.patch.PatchOperationType) Attribute(org.gluu.oxtrust.model.scim2.annotations.Attribute) Extension(org.gluu.oxtrust.model.scim2.extensions.Extension) SCIMException(org.gluu.oxtrust.model.exception.SCIMException) BaseScimResource(org.gluu.oxtrust.model.scim2.BaseScimResource) NullType(javax.lang.model.type.NullType) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

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