Search in sources :

Example 1 with LdapAttribute

use of org.gluu.site.ldap.persistence.annotation.LdapAttribute 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 LdapAttribute

use of org.gluu.site.ldap.persistence.annotation.LdapAttribute 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 LdapAttribute

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

the class AbstractEntryManager method getEntryKey.

protected String getEntryKey(Object dnValue, boolean caseSensetive, List<PropertyAnnotation> propertiesAnnotations, List<AttributeData> attributesData) {
    StringBuilder sb = new StringBuilder("_HASH__").append(((String) dnValue).toLowerCase()).append("__");
    List<String> processedProperties = new ArrayList<String>();
    Map<String, AttributeData> attributesDataMap = getAttributesDataMap(attributesData);
    for (PropertyAnnotation propertiesAnnotation : propertiesAnnotations) {
        Annotation ldapAttribute = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), LdapAttribute.class);
        if (ldapAttribute == null) {
            continue;
        }
        String ldapAttributeName = ((LdapAttribute) ldapAttribute).name();
        if (StringHelper.isEmpty(ldapAttributeName)) {
            ldapAttributeName = propertiesAnnotation.getPropertyName();
        }
        processedProperties.add(ldapAttributeName);
        String[] values = null;
        AttributeData attributeData = attributesDataMap.get(ldapAttributeName);
        if ((attributeData != null) && (attributeData.getValues() != null)) {
            values = attributeData.getValues().clone();
            Arrays.sort(values);
        }
        addPropertyWithValuesToKey(sb, ldapAttributeName, values);
    }
    for (AttributeData attributeData : attributesData) {
        if (processedProperties.contains(attributeData.getName())) {
            continue;
        }
        addPropertyWithValuesToKey(sb, attributeData.getName(), attributeData.getValues());
    }
    if (caseSensetive) {
        return sb.toString();
    } else {
        return sb.toString().toLowerCase();
    }
}
Also used : LdapAttribute(org.gluu.site.ldap.persistence.annotation.LdapAttribute) Annotation(java.lang.annotation.Annotation)

Example 4 with LdapAttribute

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

the class AbstractEntryManager method getAttributeFromLdapAttribute.

private AttributeData getAttributeFromLdapAttribute(Object entry, Annotation ldapAttribute, PropertyAnnotation propertiesAnnotation, String propertyName) {
    Class<?> entryClass = entry.getClass();
    String ldapAttributeName = ((LdapAttribute) ldapAttribute).name();
    if (StringHelper.isEmpty(ldapAttributeName)) {
        ldapAttributeName = propertyName;
    }
    Getter getter = getGetter(entryClass, propertyName);
    if (getter == null) {
        throw new MappingException("Entry should has getter for property " + propertyName);
    }
    Annotation ldapJsonObject = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), LdapJsonObject.class);
    boolean jsonObject = ldapJsonObject != null;
    AttributeData attribute = getAttribute(propertyName, ldapAttributeName, getter, entry, jsonObject);
    return attribute;
}
Also used : Getter(org.gluu.site.ldap.persistence.property.Getter) LdapAttribute(org.gluu.site.ldap.persistence.annotation.LdapAttribute) Annotation(java.lang.annotation.Annotation) MappingException(org.gluu.site.ldap.persistence.exception.MappingException)

Example 5 with LdapAttribute

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

the class AbstractEntryManager 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 : LdapAttribute(org.gluu.site.ldap.persistence.annotation.LdapAttribute) Annotation(java.lang.annotation.Annotation) LdapAttributesList(org.gluu.site.ldap.persistence.annotation.LdapAttributesList)

Aggregations

Annotation (java.lang.annotation.Annotation)6 LdapAttribute (org.gluu.site.ldap.persistence.annotation.LdapAttribute)6 LdapAttributesList (org.gluu.site.ldap.persistence.annotation.LdapAttributesList)4 MappingException (org.gluu.site.ldap.persistence.exception.MappingException)3 LdapJsonObject (org.gluu.site.ldap.persistence.annotation.LdapJsonObject)2 AttributeModificationType (org.gluu.site.ldap.persistence.AttributeDataModification.AttributeModificationType)1 EntryPersistenceException (org.gluu.site.ldap.persistence.exception.EntryPersistenceException)1 Getter (org.gluu.site.ldap.persistence.property.Getter)1 Setter (org.gluu.site.ldap.persistence.property.Setter)1