Search in sources :

Example 1 with SearchResult

use of com.unboundid.ldap.sdk.SearchResult in project oxCore by GluuFederation.

the class LdifDataUtility method checkIfSerrverHasEntryFromLDIFFile.

/**
	 * Check if DS has at least one DN simular to specified in ldif file.
	 * 
	 * @param connection
	 *            Connection to LDAP server
	 * @param ldifFileName
	 *            LDIF file
	 * @return true if server contains at least one DN simular to specified in
	 *         ldif file.
	 */
public boolean checkIfSerrverHasEntryFromLDIFFile(LDAPConnection connection, String ldifFileName) {
    // Set up the LDIF reader that will be used to read the changes to apply
    LDIFReader ldifReader = createLdifReader(ldifFileName);
    if (ldifReader == null) {
        return true;
    }
    // Check all ldif entries
    while (true) {
        // Read the next change to process.
        Entry entry = null;
        try {
            entry = ldifReader.readEntry();
        } catch (LDIFException le) {
            log.error("Malformed ldif record", le);
            if (!le.mayContinueReading()) {
                return true;
            }
        } catch (IOException ioe) {
            log.error("I/O error encountered while reading a change record", ioe);
            return true;
        }
        // changes to be processed.
        if (entry == null) {
            break;
        }
        // Search entry in the server.
        try {
            SearchResult sr = connection.search(entry.getDN(), SearchScope.BASE, "objectClass=*");
            if ((sr != null) && (sr.getEntryCount() > 0)) {
                return true;
            }
        } catch (LDAPException le) {
            if (le.getResultCode() != ResultCode.NO_SUCH_OBJECT) {
                log.error("Failed to search ldif record", le);
                return true;
            }
        }
    }
    disposeLdifReader(ldifReader);
    return false;
}
Also used : Entry(com.unboundid.ldap.sdk.Entry) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry) LDAPException(com.unboundid.ldap.sdk.LDAPException) LDIFReader(com.unboundid.ldif.LDIFReader) LDIFException(com.unboundid.ldif.LDIFException) SearchResult(com.unboundid.ldap.sdk.SearchResult) IOException(java.io.IOException)

Example 2 with SearchResult

use of com.unboundid.ldap.sdk.SearchResult in project oxCore by GluuFederation.

the class LdifDataUtility method deleteEntryWithAllSubs.

/**
	 * Remove base entry with all sub entries
	 * 
	 * @param connection
	 *            Connection to LDAP server
	 * @param baseDN
	 *            Base DN entry
	 * @return The result code for the processing that was performed.
	 */
public ResultCode deleteEntryWithAllSubs(LDAPConnection connection, String baseDN) {
    ResultCode resultCode = ResultCode.SUCCESS;
    SearchResult searchResult = null;
    try {
        searchResult = connection.search(baseDN, SearchScope.SUB, "objectClass=*");
        if ((searchResult == null) || (searchResult.getEntryCount() == 0)) {
            return ResultCode.LOCAL_ERROR;
        }
    } catch (LDAPSearchException le) {
        log.error("Failed to search subordinate entries", le);
        return ResultCode.LOCAL_ERROR;
    }
    LinkedList<String> dns = new LinkedList<String>();
    for (SearchResultEntry entry : searchResult.getSearchEntries()) {
        dns.add(entry.getDN());
    }
    ListIterator<String> listIterator = dns.listIterator(dns.size());
    while (listIterator.hasPrevious()) {
        try {
            connection.delete(listIterator.previous());
        } catch (LDAPException le) {
            log.error("Failed to delete entry", le);
            resultCode = ResultCode.LOCAL_ERROR;
            break;
        }
    }
    return resultCode;
}
Also used : LDAPException(com.unboundid.ldap.sdk.LDAPException) LDAPSearchException(com.unboundid.ldap.sdk.LDAPSearchException) SearchResult(com.unboundid.ldap.sdk.SearchResult) ResultCode(com.unboundid.ldap.sdk.ResultCode) LinkedList(java.util.LinkedList) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry)

Example 3 with SearchResult

use of com.unboundid.ldap.sdk.SearchResult in project oxCore by GluuFederation.

the class LdapEntryManager method removeSubtreeThroughIteration.

private void removeSubtreeThroughIteration(String dn) {
    SearchResult searchResult = null;
    try {
        searchResult = this.ldapOperationService.search(dn, toLdapFilter(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 (SearchException 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 : ArrayList(java.util.ArrayList) EntryPersistenceException(org.gluu.persist.exception.mapping.EntryPersistenceException) SearchException(org.gluu.persist.exception.operation.SearchException) SearchResult(com.unboundid.ldap.sdk.SearchResult) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry)

Example 4 with SearchResult

use of com.unboundid.ldap.sdk.SearchResult in project oxCore by GluuFederation.

the class LdapEntryManager method getLDIFTree.

@Override
public List<String[]> getLDIFTree(String baseDN, Filter searchFilter, String... attributes) {
    SearchResult searchResult;
    try {
        searchResult = this.ldapOperationService.search(baseDN, toLdapFilter(searchFilter), -1, 0, null, attributes);
        if (!ResultCode.SUCCESS.equals(searchResult.getResultCode())) {
            throw new EntryPersistenceException(String.format("Failed to find entries with baseDN: %s, filter: %s", baseDN, searchFilter));
        }
    } catch (Exception ex) {
        throw new EntryPersistenceException(String.format("Failed to find entries with baseDN: %s, filter: %s", baseDN, searchFilter), 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 : ArrayList(java.util.ArrayList) EntryPersistenceException(org.gluu.persist.exception.mapping.EntryPersistenceException) SearchResult(com.unboundid.ldap.sdk.SearchResult) 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 5 with SearchResult

use of com.unboundid.ldap.sdk.SearchResult in project oxCore by GluuFederation.

the class LdapEntryManager method findEntries.

@Override
public <T> List<T> findEntries(String baseDN, Class<T> entryClass, Filter filter, SearchScope scope, String[] ldapReturnAttributes, BatchOperation<T> batchOperation, int startIndex, int sizeLimit, int chunkSize) {
    if (StringHelper.isEmptyString(baseDN)) {
        throw new MappingException("Base DN to find entries is null");
    }
    // Check entry class
    checkEntryClass(entryClass, false);
    String[] objectClasses = getTypeObjectClasses(entryClass);
    List<PropertyAnnotation> propertiesAnnotations = getEntryPropertyAnnotations(entryClass);
    String[] currentLdapReturnAttributes = ldapReturnAttributes;
    if (ArrayHelper.isEmpty(currentLdapReturnAttributes)) {
        currentLdapReturnAttributes = getLdapAttributes(null, propertiesAnnotations, false);
    }
    // Find entries
    Filter searchFilter;
    if (objectClasses.length > 0) {
        searchFilter = addObjectClassFilter(filter, objectClasses);
    } else {
        searchFilter = filter;
    }
    SearchResult searchResult = null;
    try {
        LdapBatchOperationWraper<T> batchOperationWraper = new LdapBatchOperationWraper<T>(batchOperation, this, entryClass, propertiesAnnotations);
        searchResult = this.ldapOperationService.search(baseDN, toLdapFilter(searchFilter), toLdapSearchScope(scope), batchOperationWraper, startIndex, chunkSize, sizeLimit, null, currentLdapReturnAttributes);
        if (!ResultCode.SUCCESS.equals(searchResult.getResultCode())) {
            throw new EntryPersistenceException(String.format("Failed to find entries with baseDN: %s, filter: %s", baseDN, searchFilter));
        }
    } catch (Exception ex) {
        throw new EntryPersistenceException(String.format("Failed to find entries with baseDN: %s, filter: %s", baseDN, searchFilter), ex);
    }
    if (searchResult.getEntryCount() == 0) {
        return new ArrayList<T>(0);
    }
    List<T> entries = createEntities(entryClass, propertiesAnnotations, searchResult.getSearchEntries().toArray(new SearchResultEntry[searchResult.getSearchEntries().size()]));
    // Default sort if needed
    sortEntriesIfNeeded(entryClass, entries);
    return entries;
}
Also used : ArrayList(java.util.ArrayList) EntryPersistenceException(org.gluu.persist.exception.mapping.EntryPersistenceException) SearchResult(com.unboundid.ldap.sdk.SearchResult) 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) MappingException(org.gluu.persist.exception.mapping.MappingException) PropertyAnnotation(org.gluu.persist.model.PropertyAnnotation) Filter(org.gluu.search.filter.Filter) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry)

Aggregations

SearchResult (com.unboundid.ldap.sdk.SearchResult)38 SearchResultEntry (com.unboundid.ldap.sdk.SearchResultEntry)24 LDAPException (com.unboundid.ldap.sdk.LDAPException)17 SearchRequest (com.unboundid.ldap.sdk.SearchRequest)15 SearchException (org.gluu.persist.exception.operation.SearchException)10 ArrayList (java.util.ArrayList)9 MappingException (org.gluu.persist.exception.mapping.MappingException)9 ConnectionException (org.gluu.persist.exception.operation.ConnectionException)8 LdapConnection (com.gitblit.ldap.LdapConnection)6 BindResult (com.unboundid.ldap.sdk.BindResult)6 EntryPersistenceException (org.gluu.persist.exception.mapping.EntryPersistenceException)6 Test (org.junit.Test)6 LDAPConnection (com.unboundid.ldap.sdk.LDAPConnection)5 LDAPSearchException (com.unboundid.ldap.sdk.LDAPSearchException)5 TeamModel (com.gitblit.models.TeamModel)4 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)4 SimplePagedResultsControl (com.unboundid.ldap.sdk.controls.SimplePagedResultsControl)4 ParseException (java.text.ParseException)4 AuthenticationException (org.gluu.persist.exception.operation.AuthenticationException)4 SearchScopeException (org.gluu.persist.exception.operation.SearchScopeException)4