Search in sources :

Example 1 with Validator

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

the class ResourceValidator method validateValidableAttributes.

/**
 * Inspects the resource passed in the constructor and applies validations for every attribute annotated with
 * {@link Validator}. Validations are of different nature as seen{@link Validations here}.
 * @throws SCIMException When a validation does not pass (the {@link Validations#apply(Validations, Object) apply}
 * method returns false)
 */
public void validateValidableAttributes() throws SCIMException {
    Map<String, List<Method>> map = IntrospectUtil.validableCoreAttrs.get(resourceClass);
    for (String attributePath : map.keySet()) {
        Field f = IntrospectUtil.findFieldFromPath(resourceClass, attributePath);
        Validations valToApply = f.getAnnotation(Validator.class).value();
        log.debug("Validating value(s) of attribute '{}'", attributePath);
        for (Object val : IntrospectUtil.getAttributeValues(resource, map.get(attributePath))) {
            if (val != null && !Validations.apply(valToApply, val)) {
                log.error("Error validating attribute '{}', wrong value supplied: '{}'", attributePath, val.toString());
                throw new SCIMException(String.format(ATTR_VALIDATION_FAILED, attributePath));
            }
        }
    }
}
Also used : ExtensionField(org.gluu.oxtrust.model.scim2.extensions.ExtensionField) Field(java.lang.reflect.Field) Validations(org.gluu.oxtrust.model.scim2.Validations) SCIMException(org.gluu.oxtrust.model.exception.SCIMException) Validator(org.gluu.oxtrust.model.scim2.annotations.Validator)

Example 2 with Validator

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

Aggregations

Field (java.lang.reflect.Field)2 Validator (org.gluu.oxtrust.model.scim2.annotations.Validator)2 SCIMException (org.gluu.oxtrust.model.exception.SCIMException)1 Validations (org.gluu.oxtrust.model.scim2.Validations)1 Attribute (org.gluu.oxtrust.model.scim2.annotations.Attribute)1 ExtensionField (org.gluu.oxtrust.model.scim2.extensions.ExtensionField)1