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;
}
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;
}
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);
}
}
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;
}
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;
}
Aggregations