Search in sources :

Example 1 with LdapAttributesList

use of org.gluu.site.ldap.persistence.annotation.LdapAttributesList in project oxCore by GluuFederation.

the class AbstractEntryManager method merge.

@SuppressWarnings("unchecked")
private <T> T merge(T entry, boolean isSchemaUpdate, AttributeModificationType schemaModificationType) {
    if (entry == null) {
        throw new MappingException("Entry to persist is null");
    }
    Class<?> entryClass = entry.getClass();
    List<PropertyAnnotation> propertiesAnnotations = getEntryPropertyAnnotations(entryClass);
    Object dnValue = getDNValue(entry, entryClass);
    List<AttributeData> attributesToPersist = getAttributesListForPersist(entry, propertiesAnnotations);
    Map<String, AttributeData> attributesToPersistMap = getAttributesMap(attributesToPersist);
    // Load entry
    List<AttributeData> attributesFromLdap;
    if (isSchemaUpdate) {
        // If it's schema modification request we don't need to load
        // attributes from LDAP
        attributesFromLdap = new ArrayList<AttributeData>();
    } else {
        List<String> currentLdapReturnAttributesList = getLdapAttributesList(entry, propertiesAnnotations, false);
        currentLdapReturnAttributesList.add("objectClass");
        attributesFromLdap = find(dnValue.toString(), currentLdapReturnAttributesList.toArray(EMPTY_STRING_ARRAY));
    }
    Map<String, AttributeData> attributesFromLdapMap = getAttributesMap(attributesFromLdap);
    // Prepare list of modifications
    // Process properties with LdapAttribute annotation
    List<AttributeDataModification> attributeDataModifications = new ArrayList<AttributeDataModification>();
    for (PropertyAnnotation propertiesAnnotation : propertiesAnnotations) {
        String propertyName = propertiesAnnotation.getPropertyName();
        Annotation ldapAttribute;
        ldapAttribute = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), LdapAttribute.class);
        if (ldapAttribute != null) {
            String ldapAttributeName = ((LdapAttribute) ldapAttribute).name();
            if (StringHelper.isEmpty(ldapAttributeName)) {
                ldapAttributeName = propertyName;
            }
            ldapAttributeName = ldapAttributeName.toLowerCase();
            AttributeData attributeToPersist = attributesToPersistMap.get(ldapAttributeName);
            AttributeData attributeFromLdap = attributesFromLdapMap.get(ldapAttributeName);
            // Remove processed attributes
            attributesToPersistMap.remove(ldapAttributeName);
            attributesFromLdapMap.remove(ldapAttributeName);
            LdapAttribute ldapAttributeAnnotation = (LdapAttribute) ldapAttribute;
            if (ldapAttributeAnnotation.ignoreDuringUpdate()) {
                continue;
            }
            if (attributeFromLdap != null && attributeToPersist != null) {
                // Modify DN entry attribute in DS
                if (!attributeFromLdap.equals(attributeToPersist)) {
                    attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REPLACE, attributeToPersist, attributeFromLdap));
                }
            } else if ((attributeFromLdap == null) && (attributeToPersist != null)) {
                // Add entry attribute or change schema
                if (isSchemaUpdate && (attributeToPersist.getValue() == null && Arrays.equals(attributeToPersist.getValues(), new String[] {}))) {
                    continue;
                }
                AttributeModificationType modType = isSchemaUpdate ? schemaModificationType : AttributeModificationType.ADD;
                if (AttributeModificationType.ADD.equals(modType)) {
                    attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.ADD, attributeToPersist));
                } else {
                    attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REMOVE, null, attributeToPersist));
                }
            } else if ((attributeFromLdap != null) && (attributeToPersist == null)) {
                // or updateOnly = true
                if (!ldapAttributeAnnotation.ignoreDuringRead() && !ldapAttributeAnnotation.updateOnly()) {
                    attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REMOVE, null, attributeFromLdap));
                }
            }
        }
    }
    // Process properties with LdapAttributesList annotation
    for (PropertyAnnotation propertiesAnnotation : propertiesAnnotations) {
        Annotation ldapAttribute;
        ldapAttribute = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), LdapAttributesList.class);
        if (ldapAttribute != null) {
            Map<String, LdapAttribute> ldapAttributesConfiguration = new HashMap<String, LdapAttribute>();
            for (LdapAttribute ldapAttributeConfiguration : ((LdapAttributesList) ldapAttribute).attributesConfiguration()) {
                ldapAttributesConfiguration.put(ldapAttributeConfiguration.name(), ldapAttributeConfiguration);
            }
            // Prepare attributes for removal
            for (AttributeData attributeFromLdap : attributesFromLdapMap.values()) {
                String attributeName = attributeFromLdap.getName();
                if (OBJECT_CLASS.equalsIgnoreCase(attributeName)) {
                    continue;
                }
                LdapAttribute ldapAttributeConfiguration = ldapAttributesConfiguration.get(attributeName);
                if ((ldapAttributeConfiguration != null) && ldapAttributeConfiguration.ignoreDuringUpdate()) {
                    continue;
                }
                if (!attributesToPersistMap.containsKey(attributeName.toLowerCase())) {
                    // true
                    if ((ldapAttributeConfiguration == null) || ((ldapAttributeConfiguration != null) && !ldapAttributeConfiguration.ignoreDuringRead())) {
                        attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REMOVE, null, attributeFromLdap));
                    }
                }
            }
            // Prepare attributes for adding and replace
            for (AttributeData attributeToPersist : attributesToPersistMap.values()) {
                String attributeName = attributeToPersist.getName();
                LdapAttribute ldapAttributeConfiguration = ldapAttributesConfiguration.get(attributeName);
                if ((ldapAttributeConfiguration != null) && ldapAttributeConfiguration.ignoreDuringUpdate()) {
                    continue;
                }
                AttributeData attributeFromLdap = attributesFromLdapMap.get(attributeName.toLowerCase());
                if (attributeFromLdap == null) {
                    // Add entry attribute or change schema
                    AttributeModificationType modType = isSchemaUpdate ? schemaModificationType : AttributeModificationType.ADD;
                    if (AttributeModificationType.ADD.equals(modType)) {
                        String[] attributeToPersistValues = attributeToPersist.getValues();
                        if (!(ArrayHelper.isEmpty(attributeToPersistValues) || ((attributeToPersistValues.length == 1) && StringHelper.isEmpty(attributeToPersistValues[0])))) {
                            attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.ADD, attributeToPersist));
                        }
                    } else {
                        attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REMOVE, null, attributeToPersist));
                    }
                } else if ((attributeFromLdap != null) && (Arrays.equals(attributeToPersist.getValues(), new String[] {}))) {
                    attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REMOVE, null, attributeFromLdap));
                } else {
                    if (!attributeFromLdap.equals(attributeToPersist)) {
                        attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REPLACE, attributeToPersist, attributeFromLdap));
                    }
                }
            }
        }
    }
    // Update object classes if entry contains custom object classes
    if (getSupportedLDAPVersion() > 2) {
        if (!isSchemaUpdate) {
            String[] objectClasses = getObjectClasses(entry, entryClass);
            String[] objectClassesFromLdap = attributesFromLdapMap.get(OBJECT_CLASS.toLowerCase()).getValues();
            if (!Arrays.equals(objectClassesFromLdap, objectClasses)) {
                attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REPLACE, new AttributeData(OBJECT_CLASS, objectClasses), new AttributeData(OBJECT_CLASS, objectClassesFromLdap)));
            }
        }
    }
    log.debug(String.format("LDAP attributes for merge: %s", attributeDataModifications));
    merge(dnValue.toString(), attributeDataModifications);
    return (T) find(entryClass, dnValue.toString(), null, propertiesAnnotations);
}
Also used : AttributeModificationType(org.gluu.site.ldap.persistence.AttributeDataModification.AttributeModificationType) Annotation(java.lang.annotation.Annotation) MappingException(org.gluu.site.ldap.persistence.exception.MappingException) LdapAttributesList(org.gluu.site.ldap.persistence.annotation.LdapAttributesList) LdapAttribute(org.gluu.site.ldap.persistence.annotation.LdapAttribute) LdapJsonObject(org.gluu.site.ldap.persistence.annotation.LdapJsonObject)

Example 2 with LdapAttributesList

use of org.gluu.site.ldap.persistence.annotation.LdapAttributesList in project oxCore by GluuFederation.

the class AbstractEntryManager method getAttributesListForPersist.

protected List<AttributeData> getAttributesListForPersist(Object entry, List<PropertyAnnotation> propertiesAnnotations) {
    // Prepare list of properties to persist
    List<AttributeData> attributes = new ArrayList<AttributeData>();
    for (PropertyAnnotation propertiesAnnotation : propertiesAnnotations) {
        String propertyName = propertiesAnnotation.getPropertyName();
        Annotation ldapAttribute;
        // Process properties with LdapAttribute annotation
        ldapAttribute = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), LdapAttribute.class);
        if (ldapAttribute != null) {
            AttributeData attribute = getAttributeFromLdapAttribute(entry, ldapAttribute, propertiesAnnotation, propertyName);
            if (attribute != null) {
                attributes.add(attribute);
            }
            continue;
        }
        // Process properties with LdapAttributesList annotation
        ldapAttribute = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), LdapAttributesList.class);
        if (ldapAttribute != null) {
            List<AttributeData> listAttributes = getAttributesFromLdapAttributesList(entry, ldapAttribute, propertyName);
            if (listAttributes != null) {
                attributes.addAll(listAttributes);
            }
            continue;
        }
    }
    return attributes;
}
Also used : LdapAttribute(org.gluu.site.ldap.persistence.annotation.LdapAttribute) Annotation(java.lang.annotation.Annotation) LdapAttributesList(org.gluu.site.ldap.persistence.annotation.LdapAttributesList)

Example 3 with LdapAttributesList

use of org.gluu.site.ldap.persistence.annotation.LdapAttributesList in project oxCore by GluuFederation.

the class BaseEntryManager method createEntities.

protected <T> List<T> createEntities(Class<T> entryClass, List<PropertyAnnotation> propertiesAnnotations, Map<String, List<AttributeData>> entriesAttributes, boolean doSort) {
    // Check if entry has DN property
    String dnProperty = getDNPropertyName(entryClass);
    // Get DN value
    Setter dnSetter = getSetter(entryClass, dnProperty);
    if (dnSetter == null) {
        throw new MappingException("Entry should has getter for property " + dnProperty);
    }
    // Type object classes
    String[] typeObjectClasses = getTypeObjectClasses(entryClass);
    Arrays.sort(typeObjectClasses);
    List<T> results = new ArrayList<T>(entriesAttributes.size());
    for (Entry<String, List<AttributeData>> entryAttributes : entriesAttributes.entrySet()) {
        String dn = entryAttributes.getKey();
        List<AttributeData> attributes = entryAttributes.getValue();
        Map<String, AttributeData> attributesMap = getAttributesMap(attributes);
        T entry;
        List<String> customObjectClasses = null;
        try {
            entry = ReflectHelper.createObjectByDefaultConstructor(entryClass);
        } catch (Exception ex) {
            throw new MappingException(String.format("Entry %s should has default constructor", entryClass));
        }
        results.add(entry);
        dnSetter.set(entry, dn);
        // Process properties with LdapAttribute annotation
        for (PropertyAnnotation propertiesAnnotation : propertiesAnnotations) {
            String propertyName = propertiesAnnotation.getPropertyName();
            Annotation ldapAttribute;
            ldapAttribute = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), LdapAttribute.class);
            if (ldapAttribute != null) {
                String ldapAttributeName = ((LdapAttribute) ldapAttribute).name();
                if (StringHelper.isEmpty(ldapAttributeName)) {
                    ldapAttributeName = propertyName;
                }
                ldapAttributeName = ldapAttributeName.toLowerCase();
                AttributeData attributeData = attributesMap.get(ldapAttributeName);
                // Remove processed attributes
                attributesMap.remove(ldapAttributeName);
                if (((LdapAttribute) ldapAttribute).ignoreDuringRead()) {
                    continue;
                }
                Setter setter = getSetter(entryClass, propertyName);
                if (setter == null) {
                    throw new MappingException("Entry should has setter for property " + propertyName);
                }
                Annotation ldapJsonObject = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), LdapJsonObject.class);
                boolean jsonObject = ldapJsonObject != null;
                setPropertyValue(propertyName, setter, entry, attributeData, jsonObject);
            }
        }
        // Process properties with LdapAttributesList annotation
        for (PropertyAnnotation propertiesAnnotation : propertiesAnnotations) {
            String propertyName = propertiesAnnotation.getPropertyName();
            Annotation ldapAttribute;
            ldapAttribute = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), LdapAttributesList.class);
            if (ldapAttribute != null) {
                Map<String, LdapAttribute> ldapAttributesConfiguration = new HashMap<String, LdapAttribute>();
                for (LdapAttribute ldapAttributeConfiguration : ((LdapAttributesList) ldapAttribute).attributesConfiguration()) {
                    ldapAttributesConfiguration.put(ldapAttributeConfiguration.name(), ldapAttributeConfiguration);
                }
                Setter setter = getSetter(entryClass, propertyName);
                if (setter == null) {
                    throw new MappingException("Entry should has setter for property " + propertyName);
                }
                List<Object> propertyValue = new ArrayList<Object>();
                setter.set(entry, propertyValue);
                Class<?> entryItemType = ReflectHelper.getListType(setter);
                if (entryItemType == null) {
                    throw new MappingException("Entry property " + propertyName + " should has setter with specified element type");
                }
                String entryPropertyName = ((LdapAttributesList) ldapAttribute).name();
                Setter entryPropertyNameSetter = getSetter(entryItemType, entryPropertyName);
                if (entryPropertyNameSetter == null) {
                    throw new MappingException("Entry should has setter for property " + propertyName + "." + entryPropertyName);
                }
                String entryPropertyValue = ((LdapAttributesList) ldapAttribute).value();
                Setter entryPropertyValueSetter = getSetter(entryItemType, entryPropertyValue);
                if (entryPropertyValueSetter == null) {
                    throw new MappingException("Entry should has getter for property " + propertyName + "." + entryPropertyValue);
                }
                for (AttributeData entryAttribute : attributesMap.values()) {
                    if (OBJECT_CLASS.equalsIgnoreCase(entryAttribute.getName())) {
                        String[] objectClasses = entryAttribute.getValues();
                        if (ArrayHelper.isEmpty(objectClasses)) {
                            continue;
                        }
                        if (customObjectClasses == null) {
                            customObjectClasses = new ArrayList<String>();
                        }
                        for (String objectClass : objectClasses) {
                            int idx = Arrays.binarySearch(typeObjectClasses, objectClass, new Comparator<String>() {

                                public int compare(String o1, String o2) {
                                    return o1.toLowerCase().compareTo(o2.toLowerCase());
                                }
                            });
                            if (idx < 0) {
                                customObjectClasses.add(objectClass);
                            }
                        }
                        continue;
                    }
                    LdapAttribute ldapAttributeConfiguration = ldapAttributesConfiguration.get(entryAttribute.getName());
                    if ((ldapAttributeConfiguration != null) && ldapAttributeConfiguration.ignoreDuringRead()) {
                        continue;
                    }
                    Object listItem = getListItem(propertyName, entryPropertyNameSetter, entryPropertyValueSetter, entryItemType, entryAttribute);
                    if (listItem != null) {
                        propertyValue.add(listItem);
                    }
                }
                if (doSort) {
                    sortLdapAttributesListIfNeeded((LdapAttributesList) ldapAttribute, entryItemType, propertyValue);
                }
            }
        }
        if ((customObjectClasses != null) && (customObjectClasses.size() > 0)) {
            setCustomObjectClasses(entry, entryClass, customObjectClasses.toArray(new String[0]));
        }
    }
    return results;
}
Also used : HashMap(java.util.HashMap) IdentityHashMap(java.util.IdentityHashMap) ArrayList(java.util.ArrayList) MappingException(org.gluu.persist.exception.mapping.MappingException) PropertyAnnotation(org.gluu.persist.model.PropertyAnnotation) LdapAttribute(org.gluu.site.ldap.persistence.annotation.LdapAttribute) LdapAttributesList(org.gluu.site.ldap.persistence.annotation.LdapAttributesList) ArrayList(java.util.ArrayList) List(java.util.List) MappingException(org.gluu.persist.exception.mapping.MappingException) InvalidArgumentException(org.gluu.persist.exception.mapping.InvalidArgumentException) EntryPersistenceException(org.gluu.persist.exception.mapping.EntryPersistenceException) PropertyAnnotation(org.gluu.persist.model.PropertyAnnotation) Annotation(java.lang.annotation.Annotation) LdapAttributesList(org.gluu.site.ldap.persistence.annotation.LdapAttributesList) Setter(org.gluu.persist.reflect.property.Setter) LdapJsonObject(org.gluu.site.ldap.persistence.annotation.LdapJsonObject) AttributeData(org.gluu.persist.model.AttributeData)

Example 4 with LdapAttributesList

use of org.gluu.site.ldap.persistence.annotation.LdapAttributesList in project oxCore by GluuFederation.

the class BaseEntryManager method getAttributesListForPersist.

protected List<AttributeData> getAttributesListForPersist(Object entry, List<PropertyAnnotation> propertiesAnnotations) {
    // Prepare list of properties to persist
    List<AttributeData> attributes = new ArrayList<AttributeData>();
    for (PropertyAnnotation propertiesAnnotation : propertiesAnnotations) {
        String propertyName = propertiesAnnotation.getPropertyName();
        Annotation ldapAttribute;
        // Process properties with LdapAttribute annotation
        ldapAttribute = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), LdapAttribute.class);
        if (ldapAttribute != null) {
            AttributeData attribute = getAttributeFromLdapAttribute(entry, ldapAttribute, propertiesAnnotation, propertyName);
            if (attribute != null) {
                attributes.add(attribute);
            }
            continue;
        }
        // Process properties with LdapAttributesList annotation
        ldapAttribute = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), LdapAttributesList.class);
        if (ldapAttribute != null) {
            List<AttributeData> listAttributes = getAttributesFromLdapAttributesList(entry, ldapAttribute, propertyName);
            if (listAttributes != null) {
                attributes.addAll(listAttributes);
            }
            continue;
        }
    }
    return attributes;
}
Also used : ArrayList(java.util.ArrayList) LdapAttribute(org.gluu.site.ldap.persistence.annotation.LdapAttribute) AttributeData(org.gluu.persist.model.AttributeData) PropertyAnnotation(org.gluu.persist.model.PropertyAnnotation) Annotation(java.lang.annotation.Annotation) PropertyAnnotation(org.gluu.persist.model.PropertyAnnotation) LdapAttributesList(org.gluu.site.ldap.persistence.annotation.LdapAttributesList)

Example 5 with LdapAttributesList

use of org.gluu.site.ldap.persistence.annotation.LdapAttributesList in project oxCore by GluuFederation.

the class BaseEntryManager method getLdapAttributesList.

private <T> List<String> getLdapAttributesList(T entry, List<PropertyAnnotation> propertiesAnnotations, boolean isIgnoreLdapAttributesList) {
    List<String> attributes = new ArrayList<String>();
    for (PropertyAnnotation propertiesAnnotation : propertiesAnnotations) {
        String propertyName = propertiesAnnotation.getPropertyName();
        Annotation ldapAttribute;
        if (!isIgnoreLdapAttributesList) {
            ldapAttribute = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), LdapAttributesList.class);
            if (ldapAttribute != null) {
                if (entry == null) {
                    return null;
                } else {
                    List<AttributeData> ldapAttributesList = getAttributesFromLdapAttributesList(entry, ldapAttribute, propertyName);
                    for (AttributeData attributeData : ldapAttributesList) {
                        String ldapAttributeName = attributeData.getName();
                        attributes.add(ldapAttributeName);
                    }
                }
            }
        }
        // Process properties with LdapAttribute annotation
        ldapAttribute = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), LdapAttribute.class);
        if (ldapAttribute != null) {
            String ldapAttributeName = ((LdapAttribute) ldapAttribute).name();
            if (StringHelper.isEmpty(ldapAttributeName)) {
                ldapAttributeName = propertyName;
            }
            attributes.add(ldapAttributeName);
        }
    }
    if (attributes.size() == 0) {
        return null;
    }
    return attributes;
}
Also used : ArrayList(java.util.ArrayList) LdapAttribute(org.gluu.site.ldap.persistence.annotation.LdapAttribute) PropertyAnnotation(org.gluu.persist.model.PropertyAnnotation) Annotation(java.lang.annotation.Annotation) AttributeData(org.gluu.persist.model.AttributeData) PropertyAnnotation(org.gluu.persist.model.PropertyAnnotation) LdapAttributesList(org.gluu.site.ldap.persistence.annotation.LdapAttributesList)

Aggregations

LdapAttributesList (org.gluu.site.ldap.persistence.annotation.LdapAttributesList)10 Annotation (java.lang.annotation.Annotation)8 LdapAttribute (org.gluu.site.ldap.persistence.annotation.LdapAttribute)8 ArrayList (java.util.ArrayList)5 AttributeData (org.gluu.persist.model.AttributeData)5 LdapJsonObject (org.gluu.site.ldap.persistence.annotation.LdapJsonObject)5 PropertyAnnotation (org.gluu.persist.model.PropertyAnnotation)4 MappingException (org.gluu.site.ldap.persistence.exception.MappingException)3 HashMap (java.util.HashMap)2 IdentityHashMap (java.util.IdentityHashMap)2 List (java.util.List)2 MappingException (org.gluu.persist.exception.mapping.MappingException)2 EntryPersistenceException (org.gluu.persist.exception.mapping.EntryPersistenceException)1 InvalidArgumentException (org.gluu.persist.exception.mapping.InvalidArgumentException)1 AttributeDataModification (org.gluu.persist.model.AttributeDataModification)1 AttributeModificationType (org.gluu.persist.model.AttributeDataModification.AttributeModificationType)1 Getter (org.gluu.persist.reflect.property.Getter)1 Setter (org.gluu.persist.reflect.property.Setter)1 AttributeModificationType (org.gluu.site.ldap.persistence.AttributeDataModification.AttributeModificationType)1 EntryPersistenceException (org.gluu.site.ldap.persistence.exception.EntryPersistenceException)1