Search in sources :

Example 1 with ConnectionException

use of org.gluu.site.ldap.exception.ConnectionException in project oxCore by GluuFederation.

the class LdapEntryManager method merge.

@Override
public void merge(String dn, List<AttributeDataModification> attributeDataModifications) {
    // Update entry
    try {
        List<Modification> modifications = new ArrayList<Modification>(attributeDataModifications.size());
        for (AttributeDataModification attributeDataModification : attributeDataModifications) {
            AttributeData attribute = attributeDataModification.getAttribute();
            AttributeData oldAttribute = attributeDataModification.getOldAttribute();
            Modification modification = null;
            if (AttributeModificationType.ADD.equals(attributeDataModification.getModificationType())) {
                modification = new Modification(ModificationType.ADD, attribute.getName(), attribute.getValues());
            } else if (AttributeModificationType.REMOVE.equals(attributeDataModification.getModificationType())) {
                modification = new Modification(ModificationType.DELETE, oldAttribute.getName(), oldAttribute.getValues());
            } else if (AttributeModificationType.REPLACE.equals(attributeDataModification.getModificationType())) {
                if (attribute.getValues().length == 1) {
                    modification = new Modification(ModificationType.REPLACE, attribute.getName(), attribute.getValues());
                } else {
                    String[] oldValues = ArrayHelper.arrayClone(oldAttribute.getValues());
                    String[] newValues = ArrayHelper.arrayClone(attribute.getValues());
                    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 = new Modification(ModificationType.DELETE, attribute.getName(), removeValues.toArray(new String[removeValues.size()]));
                        modifications.add(removeModification);
                    }
                    if (addValues.size() > 0) {
                        Modification addModification = new Modification(ModificationType.ADD, attribute.getName(), addValues.toArray(new String[addValues.size()]));
                        modifications.add(addModification);
                    }
                }
            }
            if (modification != null) {
                modifications.add(modification);
            }
        }
        if (modifications.size() > 0) {
            boolean result = this.ldapOperationService.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 : EmptyEntryPersistenceException(org.gluu.site.ldap.persistence.exception.EmptyEntryPersistenceException) EntryPersistenceException(org.gluu.site.ldap.persistence.exception.EntryPersistenceException) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) EmptyEntryPersistenceException(org.gluu.site.ldap.persistence.exception.EmptyEntryPersistenceException) MappingException(org.gluu.site.ldap.persistence.exception.MappingException) AuthenticationException(org.gluu.site.ldap.persistence.exception.AuthenticationException) InvalidArgumentException(org.gluu.site.ldap.persistence.exception.InvalidArgumentException) ParseException(java.text.ParseException) EntryPersistenceException(org.gluu.site.ldap.persistence.exception.EntryPersistenceException) ConnectionException(org.gluu.site.ldap.exception.ConnectionException) ConnectionException(org.gluu.site.ldap.exception.ConnectionException)

Example 2 with ConnectionException

use of org.gluu.site.ldap.exception.ConnectionException in project oxCore by GluuFederation.

the class OperationsFacade method deleteWithSubtree.

/**
   	 * Delete entry from the directory
   	 *
   	 * @param dn
   	 * @throws ConnectionException
   	 */
public void deleteWithSubtree(String dn) throws ConnectionException {
    try {
        final DeleteRequest deleteRequest = new DeleteRequest(dn);
        deleteRequest.addControl(new SubtreeDeleteRequestControl());
        getConnectionPool().delete(deleteRequest);
    } catch (Exception ex) {
        throw new ConnectionException("Failed to delete entry", ex);
    }
}
Also used : InvalidSimplePageControlException(org.gluu.site.ldap.exception.InvalidSimplePageControlException) MappingException(org.gluu.site.ldap.persistence.exception.MappingException) DuplicateEntryException(org.gluu.site.ldap.exception.DuplicateEntryException) ConnectionException(org.gluu.site.ldap.exception.ConnectionException) InvalidArgumentException(org.gluu.site.ldap.persistence.exception.InvalidArgumentException) ConnectionException(org.gluu.site.ldap.exception.ConnectionException)

Aggregations

ConnectionException (org.gluu.site.ldap.exception.ConnectionException)2 InvalidArgumentException (org.gluu.site.ldap.persistence.exception.InvalidArgumentException)2 MappingException (org.gluu.site.ldap.persistence.exception.MappingException)2 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)1 ParseException (java.text.ParseException)1 DuplicateEntryException (org.gluu.site.ldap.exception.DuplicateEntryException)1 InvalidSimplePageControlException (org.gluu.site.ldap.exception.InvalidSimplePageControlException)1 AuthenticationException (org.gluu.site.ldap.persistence.exception.AuthenticationException)1 EmptyEntryPersistenceException (org.gluu.site.ldap.persistence.exception.EmptyEntryPersistenceException)1 EntryPersistenceException (org.gluu.site.ldap.persistence.exception.EntryPersistenceException)1