Search in sources :

Example 1 with AttributeData

use of org.gluu.persist.model.AttributeData in project oxCore by GluuFederation.

the class LdapEntryManager method persist.

@Override
protected void persist(String dn, List<AttributeData> attributes) {
    List<Attribute> ldapAttributes = new ArrayList<Attribute>(attributes.size());
    for (AttributeData attribute : attributes) {
        String attributeName = attribute.getName();
        String[] attributeValues = attribute.getValues();
        if (ArrayHelper.isNotEmpty(attributeValues) && StringHelper.isNotEmpty(attributeValues[0])) {
            if (ldapOperationService.isCertificateAttribute(attributeName)) {
                byte[][] binaryValues = toBinaryValues(attributeValues);
                ldapAttributes.add(new Attribute(attributeName + ";binary", binaryValues));
            } else {
                ldapAttributes.add(new Attribute(attributeName, attributeValues));
            }
        }
    }
    // Persist entry
    try {
        boolean result = this.ldapOperationService.addEntry(dn, ldapAttributes);
        if (!result) {
            throw new EntryPersistenceException(String.format("Failed to persist entry: %s", dn));
        }
    } catch (ConnectionException ex) {
        throw new EntryPersistenceException(String.format("Failed to persist entry: %s", dn), ex.getCause());
    } catch (Exception ex) {
        throw new EntryPersistenceException(String.format("Failed to persist entry: %s", dn), ex);
    }
}
Also used : Attribute(com.unboundid.ldap.sdk.Attribute) ArrayList(java.util.ArrayList) EntryPersistenceException(org.gluu.persist.exception.mapping.EntryPersistenceException) AttributeData(org.gluu.persist.model.AttributeData) ConnectionException(org.gluu.persist.exception.operation.ConnectionException) SearchException(org.gluu.persist.exception.operation.SearchException) AuthenticationException(org.gluu.persist.exception.operation.AuthenticationException) MappingException(org.gluu.persist.exception.mapping.MappingException) SearchScopeException(org.gluu.persist.exception.operation.SearchScopeException) ParseException(java.text.ParseException) EntryPersistenceException(org.gluu.persist.exception.mapping.EntryPersistenceException) ConnectionException(org.gluu.persist.exception.operation.ConnectionException)

Example 2 with AttributeData

use of org.gluu.persist.model.AttributeData in project oxCore by GluuFederation.

the class LdapEntryManager method createEntities.

protected <T> List<T> createEntities(Class<T> entryClass, List<PropertyAnnotation> propertiesAnnotations, SearchResultEntry... searchResultEntries) {
    List<T> result = new ArrayList<T>(searchResultEntries.length);
    Map<String, List<AttributeData>> entriesAttributes = new HashMap<String, List<AttributeData>>(100);
    int count = 0;
    for (int i = 0; i < searchResultEntries.length; i++) {
        count++;
        SearchResultEntry entry = searchResultEntries[i];
        entriesAttributes.put(entry.getDN(), getAttributeDataList(entry));
        // Remove reference to allow java clean up object
        searchResultEntries[i] = null;
        // Allow java to clean up temporary objects
        if (count >= 100) {
            List<T> currentResult = createEntities(entryClass, propertiesAnnotations, entriesAttributes);
            result.addAll(currentResult);
            entriesAttributes = new HashMap<String, List<AttributeData>>(100);
            count = 0;
        }
    }
    List<T> currentResult = createEntities(entryClass, propertiesAnnotations, entriesAttributes);
    result.addAll(currentResult);
    return result;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) AttributeData(org.gluu.persist.model.AttributeData) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry)

Example 3 with AttributeData

use of org.gluu.persist.model.AttributeData in project oxCore by GluuFederation.

the class LdapEntryManager method find.

@Override
protected List<AttributeData> find(String dn, String... ldapReturnAttributes) {
    try {
        SearchResultEntry entry = this.ldapOperationService.lookup(dn, ldapReturnAttributes);
        List<AttributeData> result = getAttributeDataList(entry);
        if (result != null) {
            return result;
        }
    } catch (Exception ex) {
        throw new EntryPersistenceException(String.format("Failed to find entry: %s", dn), ex);
    }
    throw new EntryPersistenceException(String.format("Failed to find entry: %s", dn));
}
Also used : EntryPersistenceException(org.gluu.persist.exception.mapping.EntryPersistenceException) AttributeData(org.gluu.persist.model.AttributeData) SearchException(org.gluu.persist.exception.operation.SearchException) AuthenticationException(org.gluu.persist.exception.operation.AuthenticationException) MappingException(org.gluu.persist.exception.mapping.MappingException) SearchScopeException(org.gluu.persist.exception.operation.SearchScopeException) ParseException(java.text.ParseException) EntryPersistenceException(org.gluu.persist.exception.mapping.EntryPersistenceException) ConnectionException(org.gluu.persist.exception.operation.ConnectionException) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry)

Example 4 with AttributeData

use of org.gluu.persist.model.AttributeData in project oxCore by GluuFederation.

the class LdapEntryManager method getAttributeDataList.

private List<AttributeData> getAttributeDataList(SearchResultEntry entry) {
    if (entry == null) {
        return null;
    }
    List<AttributeData> result = new ArrayList<AttributeData>();
    for (Attribute attribute : entry.getAttributes()) {
        String[] attributeValueStrings = NO_STRINGS;
        String attributeName = attribute.getName();
        if (LOG.isTraceEnabled()) {
            if (attribute.needsBase64Encoding()) {
                LOG.trace("Found binary attribute: " + attributeName + ". Is defined in LDAP config: " + ldapOperationService.isBinaryAttribute(attributeName));
            }
        }
        attributeValueStrings = attribute.getValues();
        if (attribute.needsBase64Encoding()) {
            boolean binaryAttribute = ldapOperationService.isBinaryAttribute(attributeName);
            boolean certificateAttribute = ldapOperationService.isCertificateAttribute(attributeName);
            if (binaryAttribute || certificateAttribute) {
                byte[][] attributeValues = attribute.getValueByteArrays();
                if (attributeValues != null) {
                    attributeValueStrings = new String[attributeValues.length];
                    for (int i = 0; i < attributeValues.length; i++) {
                        attributeValueStrings[i] = Base64.encodeBase64String(attributeValues[i]);
                        LOG.trace("Binary attribute: " + attribute.getName() + " value (hex): " + org.apache.commons.codec.binary.Hex.encodeHexString(attributeValues[i]) + " value (base64): " + attributeValueStrings[i]);
                    }
                }
            }
            if (certificateAttribute) {
                attributeName = ldapOperationService.getCertificateAttributeName(attributeName);
            }
        }
        AttributeData tmpAttribute = new AttributeData(attributeName, attributeValueStrings);
        result.add(tmpAttribute);
    }
    return result;
}
Also used : Attribute(com.unboundid.ldap.sdk.Attribute) ArrayList(java.util.ArrayList) AttributeData(org.gluu.persist.model.AttributeData)

Example 5 with AttributeData

use of org.gluu.persist.model.AttributeData in project oxCore by GluuFederation.

the class LdapEntryManager method createEntitiesVirtualListView.

@Deprecated
private <T> List<T> createEntitiesVirtualListView(Class<T> entryClass, List<PropertyAnnotation> propertiesAnnotations, SearchResultEntry... searchResultEntries) {
    List<T> result = new LinkedList<T>();
    Map<String, List<AttributeData>> entriesAttributes = new LinkedHashMap<String, List<AttributeData>>(100);
    int count = 0;
    for (int i = 0; i < searchResultEntries.length; i++) {
        count++;
        SearchResultEntry entry = searchResultEntries[i];
        LinkedList<AttributeData> attributeDataLinkedList = new LinkedList<AttributeData>();
        attributeDataLinkedList.addAll(getAttributeDataList(entry));
        entriesAttributes.put(entry.getDN(), attributeDataLinkedList);
        // Remove reference to allow java clean up object
        searchResultEntries[i] = null;
        // Allow java to clean up temporary objects
        if (count >= 100) {
            List<T> currentResult = new LinkedList<T>();
            currentResult.addAll(createEntities(entryClass, propertiesAnnotations, entriesAttributes, false));
            result.addAll(currentResult);
            entriesAttributes = new LinkedHashMap<String, List<AttributeData>>(100);
            count = 0;
        }
    }
    List<T> currentResult = createEntities(entryClass, propertiesAnnotations, entriesAttributes, false);
    result.addAll(currentResult);
    return result;
}
Also used : ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) LinkedList(java.util.LinkedList) AttributeData(org.gluu.persist.model.AttributeData) LinkedHashMap(java.util.LinkedHashMap) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry)

Aggregations

AttributeData (org.gluu.persist.model.AttributeData)27 ArrayList (java.util.ArrayList)16 MappingException (org.gluu.persist.exception.mapping.MappingException)15 PropertyAnnotation (org.gluu.persist.model.PropertyAnnotation)14 LdapJsonObject (org.gluu.site.ldap.persistence.annotation.LdapJsonObject)9 List (java.util.List)7 Annotation (java.lang.annotation.Annotation)6 LdapAttribute (org.gluu.site.ldap.persistence.annotation.LdapAttribute)6 LdapAttributesList (org.gluu.site.ldap.persistence.annotation.LdapAttributesList)6 HashMap (java.util.HashMap)4 EntryPersistenceException (org.gluu.persist.exception.mapping.EntryPersistenceException)4 AttributeDataModification (org.gluu.persist.model.AttributeDataModification)4 Attribute (com.unboundid.ldap.sdk.Attribute)3 SearchResultEntry (com.unboundid.ldap.sdk.SearchResultEntry)3 ParseException (java.text.ParseException)3 LinkedList (java.util.LinkedList)3 AuthenticationException (org.gluu.persist.exception.operation.AuthenticationException)3 ConnectionException (org.gluu.persist.exception.operation.ConnectionException)3 SearchException (org.gluu.persist.exception.operation.SearchException)3 SearchScopeException (org.gluu.persist.exception.operation.SearchScopeException)3