Search in sources :

Example 51 with AttributeData

use of io.jans.orm.model.AttributeData in project jans by JanssenProject.

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: " + getOperationService().isBinaryAttribute(attributeName));
            }
        }
        attributeValueStrings = attribute.getValues();
        if (attribute.needsBase64Encoding()) {
            boolean binaryAttribute = getOperationService().isBinaryAttribute(attributeName);
            boolean certificateAttribute = getOperationService().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 = getOperationService().getCertificateAttributeName(attributeName);
            }
        }
        boolean multiValued = attributeValueStrings.length > 1;
        AttributeData tmpAttribute = new AttributeData(attributeName, attributeValueStrings, multiValued);
        result.add(tmpAttribute);
    }
    return result;
}
Also used : Attribute(com.unboundid.ldap.sdk.Attribute) ArrayList(java.util.ArrayList) AttributeData(io.jans.orm.model.AttributeData)

Example 52 with AttributeData

use of io.jans.orm.model.AttributeData in project jans by JanssenProject.

the class LdapEntryManager method merge.

@Override
public void merge(String dn, String[] objectClasses, List<AttributeDataModification> attributeDataModifications, Integer expiration) {
    // Update entry
    try {
        List<Modification> modifications = new ArrayList<Modification>(attributeDataModifications.size());
        for (AttributeDataModification attributeDataModification : attributeDataModifications) {
            AttributeData attribute = attributeDataModification.getAttribute();
            AttributeData oldAttribute = attributeDataModification.getOldAttribute();
            String attributeName = null;
            String[] attributeValues = null;
            if (attribute != null) {
                attributeName = attribute.getName();
                attributeValues = attribute.getStringValues();
            }
            String oldAttributeName = null;
            String[] oldAttributeValues = null;
            if (oldAttribute != null) {
                oldAttributeName = oldAttribute.getName();
                oldAttributeValues = oldAttribute.getStringValues();
            }
            Modification modification = null;
            if (AttributeModificationType.ADD.equals(attributeDataModification.getModificationType())) {
                modification = createModification(ModificationType.ADD, attributeName, attributeValues);
            } else {
                if (AttributeModificationType.REMOVE.equals(attributeDataModification.getModificationType())) {
                    modification = createModification(ModificationType.DELETE, oldAttributeName, oldAttributeValues);
                } else if (AttributeModificationType.REPLACE.equals(attributeDataModification.getModificationType())) {
                    if (attributeValues.length == 1) {
                        modification = createModification(ModificationType.REPLACE, attributeName, attributeValues);
                    } else {
                        String[] oldValues = ArrayHelper.arrayClone(oldAttributeValues);
                        String[] newValues = ArrayHelper.arrayClone(attributeValues);
                        Arrays.sort(oldValues);
                        Arrays.sort(newValues);
                        boolean[] retainOldValues = new boolean[oldValues.length];
                        Arrays.fill(retainOldValues, false);
                        List<String> addValues = new ArrayList<String>();
                        List<String> removeValues = new ArrayList<String>();
                        // Add new values
                        for (String value : newValues) {
                            int idx = Arrays.binarySearch(oldValues, value, new Comparator<String>() {

                                @Override
                                public int compare(String o1, String o2) {
                                    return o1.toLowerCase().compareTo(o2.toLowerCase());
                                }
                            });
                            if (idx >= 0) {
                                // Old values array contains new value. Retain
                                // old value
                                retainOldValues[idx] = true;
                            } else {
                                // This is new value
                                addValues.add(value);
                            }
                        }
                        // Remove values which we don't have in new values
                        for (int i = 0; i < oldValues.length; i++) {
                            if (!retainOldValues[i]) {
                                removeValues.add(oldValues[i]);
                            }
                        }
                        if (removeValues.size() > 0) {
                            Modification removeModification = createModification(ModificationType.DELETE, attributeName, removeValues.toArray(new String[removeValues.size()]));
                            modifications.add(removeModification);
                        }
                        if (addValues.size() > 0) {
                            Modification addModification = createModification(ModificationType.ADD, attributeName, addValues.toArray(new String[addValues.size()]));
                            modifications.add(addModification);
                        }
                    }
                }
            }
            if (modification != null) {
                modifications.add(modification);
            }
        }
        if (modifications.size() > 0) {
            boolean result = getOperationService().updateEntry(dn, modifications);
            if (!result) {
                throw new EntryPersistenceException(String.format("Failed to update entry: %s", dn));
            }
        }
    } catch (ConnectionException ex) {
        throw new EntryPersistenceException(String.format("Failed to update entry: %s", dn), ex.getCause());
    } catch (Exception ex) {
        throw new EntryPersistenceException(String.format("Failed to update entry: %s", dn), ex);
    }
}
Also used : Modification(com.unboundid.ldap.sdk.Modification) AttributeDataModification(io.jans.orm.model.AttributeDataModification) ArrayList(java.util.ArrayList) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) MappingException(io.jans.orm.exception.MappingException) ParseException(java.text.ParseException) EntryDeleteException(io.jans.orm.exception.EntryDeleteException) SearchScopeException(io.jans.orm.exception.operation.SearchScopeException) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) ConnectionException(io.jans.orm.exception.operation.ConnectionException) SearchException(io.jans.orm.exception.operation.SearchException) AuthenticationException(io.jans.orm.exception.AuthenticationException) Comparator(java.util.Comparator) AttributeDataModification(io.jans.orm.model.AttributeDataModification) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) AttributeData(io.jans.orm.model.AttributeData) ConnectionException(io.jans.orm.exception.operation.ConnectionException)

Example 53 with AttributeData

use of io.jans.orm.model.AttributeData in project jans by JanssenProject.

the class SpannerEntryManager method createModification.

private AttributeDataModification createModification(final AttributeModificationType type, final String attributeName, final Boolean multiValued, final Object[] attributeValues, final Object[] oldAttributeValues) {
    String realAttributeName = attributeName;
    Object[] realValues = attributeValues;
    if (StringHelper.equalsIgnoreCase(SpannerOperationService.USER_PASSWORD, realAttributeName)) {
        realValues = getOperationService().createStoragePassword(StringHelper.toStringArray(attributeValues));
    }
    escapeValues(realValues);
    if (AttributeModificationType.REPLACE == type) {
        escapeValues(oldAttributeValues);
        return new AttributeDataModification(type, new AttributeData(realAttributeName, realValues, multiValued), new AttributeData(realAttributeName, oldAttributeValues, multiValued));
    } else {
        return new AttributeDataModification(type, new AttributeData(realAttributeName, realValues, multiValued));
    }
}
Also used : AttributeDataModification(io.jans.orm.model.AttributeDataModification) AttributeData(io.jans.orm.model.AttributeData)

Example 54 with AttributeData

use of io.jans.orm.model.AttributeData in project jans by JanssenProject.

the class SpannerEntryManager method find.

@Override
protected List<AttributeData> find(String dn, String[] objectClasses, Map<String, PropertyAnnotation> propertiesAnnotationsMap, String... ldapReturnAttributes) {
    try {
        // Load entry
        ParsedKey keyWithInum = toSQLKey(dn);
        List<AttributeData> result = getOperationService().lookup(keyWithInum.getKey(), objectClasses[0], toInternalAttributes(ldapReturnAttributes));
        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 : ParsedKey(io.jans.orm.impl.model.ParsedKey) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) AttributeData(io.jans.orm.model.AttributeData) MappingException(io.jans.orm.exception.MappingException) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) EntryDeleteException(io.jans.orm.exception.EntryDeleteException) SearchException(io.jans.orm.exception.operation.SearchException) AuthenticationException(io.jans.orm.exception.AuthenticationException)

Example 55 with AttributeData

use of io.jans.orm.model.AttributeData in project jans by JanssenProject.

the class SpannerOperationServiceImpl method getEntryDataList.

private List<EntryData> getEntryDataList(String objectClass, ResultSet resultSet) throws EntryConvertationException {
    List<EntryData> entryDataList = new LinkedList<>();
    List<AttributeData> attributeDataList = null;
    do {
        attributeDataList = getAttributeDataList(objectClass, resultSet, false);
        if (attributeDataList != null) {
            EntryData entryData = new EntryData(attributeDataList);
            entryDataList.add(entryData);
        }
    } while (attributeDataList != null);
    return entryDataList;
}
Also used : EntryData(io.jans.orm.model.EntryData) LinkedList(java.util.LinkedList) AttributeData(io.jans.orm.model.AttributeData)

Aggregations

AttributeData (io.jans.orm.model.AttributeData)62 MappingException (io.jans.orm.exception.MappingException)29 ArrayList (java.util.ArrayList)23 SearchException (io.jans.orm.exception.operation.SearchException)22 EntryPersistenceException (io.jans.orm.exception.EntryPersistenceException)17 AuthenticationException (io.jans.orm.exception.AuthenticationException)15 EntryDeleteException (io.jans.orm.exception.EntryDeleteException)15 PropertyAnnotation (io.jans.orm.reflect.property.PropertyAnnotation)14 AttributeDataModification (io.jans.orm.model.AttributeDataModification)13 JsonObject (io.jans.orm.annotation.JsonObject)10 LinkedList (java.util.LinkedList)10 DateTimeParseException (java.time.format.DateTimeParseException)8 List (java.util.List)8 ParsedKey (io.jans.orm.impl.model.ParsedKey)7 JsonObject (com.couchbase.client.java.document.json.JsonObject)6 AttributeName (io.jans.orm.annotation.AttributeName)6 PersistenceException (io.jans.orm.exception.operation.PersistenceException)6 Annotation (java.lang.annotation.Annotation)6 AttributeModificationType (io.jans.orm.model.AttributeDataModification.AttributeModificationType)5 LinkedHashMap (java.util.LinkedHashMap)5