Search in sources :

Example 1 with EntryPersistenceException

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

the class LdapEntryManager method removeSubtreeThroughIteration.

private void removeSubtreeThroughIteration(String dn) {
    SearchResult searchResult = null;
    try {
        searchResult = this.ldapOperationService.search(dn, Filter.createPresenceFilter("objectClass"), 0, 0, null, "dn");
        if (!ResultCode.SUCCESS.equals(searchResult.getResultCode())) {
            throw new EntryPersistenceException(String.format("Failed to find sub-entries of entry '%s' for removal", dn));
        }
    } catch (LDAPSearchException ex) {
        throw new EntryPersistenceException(String.format("Failed to find sub-entries of entry '%s' for removal", dn), ex);
    }
    List<String> removeEntriesDn = new ArrayList<String>(searchResult.getEntryCount());
    for (SearchResultEntry searchResultEntry : searchResult.getSearchEntries()) {
        removeEntriesDn.add(searchResultEntry.getDN());
    }
    Collections.sort(removeEntriesDn, LINE_LENGHT_COMPARATOR);
    for (String removeEntryDn : removeEntriesDn) {
        remove(removeEntryDn);
    }
}
Also used : EmptyEntryPersistenceException(org.gluu.site.ldap.persistence.exception.EmptyEntryPersistenceException) EntryPersistenceException(org.gluu.site.ldap.persistence.exception.EntryPersistenceException) ASN1OctetString(com.unboundid.asn1.ASN1OctetString)

Example 2 with EntryPersistenceException

use of org.gluu.site.ldap.persistence.exception.EntryPersistenceException 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 3 with EntryPersistenceException

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

the class LdapEntryManager method countEntries.

public <T> int countEntries(String baseDN, Class<T> entryClass, Filter filter) {
    if (StringHelper.isEmptyString(baseDN)) {
        throw new MappingException("Base DN to find entries is null");
    }
    // Check entry class
    checkEntryClass(entryClass, false);
    String[] objectClasses = getTypeObjectClasses(entryClass);
    // Don't load
    String[] ldapReturnAttributes = new String[] { "" };
    // attributes
    // Find entries
    Filter searchFilter;
    if (objectClasses.length > 0) {
        searchFilter = addObjectClassFilter(filter, objectClasses);
    } else {
        searchFilter = filter;
    }
    int countEntries = 0;
    ASN1OctetString cookie = null;
    SearchResult searchResult = null;
    do {
        Control[] controls = new Control[] { new SimplePagedResultsControl(100, cookie) };
        try {
            searchResult = this.ldapOperationService.search(baseDN, searchFilter, 0, 0, controls, ldapReturnAttributes);
            if (!ResultCode.SUCCESS.equals(searchResult.getResultCode())) {
                throw new EntryPersistenceException(String.format("Failed to calculate count entries with baseDN: %s, filter: %s", baseDN, searchFilter));
            }
        } catch (Exception ex) {
            throw new EntryPersistenceException(String.format("Failed to calculate count entries with baseDN: %s, filter: %s", baseDN, searchFilter), ex);
        }
        countEntries += searchResult.getEntryCount();
        // list
        if ((countEntries == 0) || ((countEntries % 100) != 0)) {
            break;
        }
        cookie = null;
        for (Control control : searchResult.getResponseControls()) {
            if (control instanceof SimplePagedResultsControl) {
                cookie = ((SimplePagedResultsControl) control).getCookie();
                break;
            }
        }
    } while (cookie != null);
    return countEntries;
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) SimplePagedResultsControl(com.unboundid.ldap.sdk.controls.SimplePagedResultsControl) EmptyEntryPersistenceException(org.gluu.site.ldap.persistence.exception.EmptyEntryPersistenceException) EntryPersistenceException(org.gluu.site.ldap.persistence.exception.EntryPersistenceException) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) SimplePagedResultsControl(com.unboundid.ldap.sdk.controls.SimplePagedResultsControl) 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) MappingException(org.gluu.site.ldap.persistence.exception.MappingException)

Example 4 with EntryPersistenceException

use of org.gluu.site.ldap.persistence.exception.EntryPersistenceException 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 : EmptyEntryPersistenceException(org.gluu.site.ldap.persistence.exception.EmptyEntryPersistenceException) EntryPersistenceException(org.gluu.site.ldap.persistence.exception.EntryPersistenceException) 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)

Example 5 with EntryPersistenceException

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

the class LdapEntryManager method getLDIF.

public List<String[]> getLDIF(String dn, String[] attributes) {
    SearchResult searchResult;
    try {
        searchResult = this.ldapOperationService.search(dn, Filter.create("objectclass=*"), SearchScope.BASE, -1, 0, null, attributes);
        if (!ResultCode.SUCCESS.equals(searchResult.getResultCode())) {
            throw new EntryPersistenceException(String.format("Failed to find entries with baseDN: %s", dn));
        }
    } catch (Exception ex) {
        throw new EntryPersistenceException(String.format("Failed to find entries with baseDN: %s, filter: %s", dn, null), ex);
    }
    List<String[]> result = new ArrayList<String[]>();
    if (searchResult.getEntryCount() == 0) {
        return result;
    }
    for (SearchResultEntry searchResultEntry : searchResult.getSearchEntries()) {
        result.add(searchResultEntry.toLDIF());
    }
    return result;
}
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)

Aggregations

EntryPersistenceException (org.gluu.site.ldap.persistence.exception.EntryPersistenceException)51 Response (javax.ws.rs.core.Response)20 DuplicateEntryException (org.gluu.site.ldap.exception.DuplicateEntryException)20 Path (javax.ws.rs.Path)19 Produces (javax.ws.rs.Produces)19 URI (java.net.URI)17 VirtualListViewResponse (org.xdi.ldap.model.VirtualListViewResponse)15 GluuCustomPerson (org.gluu.oxtrust.model.GluuCustomPerson)12 EmptyEntryPersistenceException (org.gluu.site.ldap.persistence.exception.EmptyEntryPersistenceException)11 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)9 DefaultValue (javax.ws.rs.DefaultValue)9 HeaderParam (javax.ws.rs.HeaderParam)9 GluuGroup (org.gluu.oxtrust.model.GluuGroup)9 ListResponse (org.gluu.oxtrust.model.scim2.ListResponse)9 MappingException (org.gluu.site.ldap.persistence.exception.MappingException)9 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)8 ParseException (java.text.ParseException)8 PersonRequiredFieldsException (org.gluu.oxtrust.exception.PersonRequiredFieldsException)8 ConnectionException (org.gluu.site.ldap.exception.ConnectionException)8 AuthenticationException (org.gluu.site.ldap.persistence.exception.AuthenticationException)8