use of com.unboundid.ldap.sdk.SearchResult in project zm-mailbox by Zimbra.
the class UBIDLdapContext method countEntries.
@Override
public long countEntries(String baseDN, ZLdapFilter filter, ZSearchControls searchControls) throws LdapException {
UBIDSearchControls sc = (UBIDSearchControls) searchControls;
try {
SearchRequest searchRequest = new SearchRequest(baseDN, sc.getSearchScope(), derefAliasPolicy, sc.getSizeLimit(), sc.getTimeLimit(), sc.getTypesOnly(), ((UBIDLdapFilter) filter).getNative());
NoopSearchControl noopSearchCtl = new NoopSearchControl();
searchRequest.addControl(noopSearchCtl);
// no point to request attributes
if (sc.getReturnAttrs() != null) {
throw LdapException.INVALID_REQUEST("return attributes are not allowed for countEntries", null);
}
SearchResult result = UBIDLdapOperation.SEARCH.execute(this, searchRequest, filter);
NoopSearchControl control = NoopSearchControl.get(result);
if (control == null) {
throw LdapException.LDAP_ERROR("Noop search control is not present in response", null);
}
return control.getCount();
} catch (LDAPException e) {
throw mapToLdapException("unable to search ldap", e);
}
}
use of com.unboundid.ldap.sdk.SearchResult in project zm-mailbox by Zimbra.
the class UBIDLdapContext method moveChildren.
@Override
public void moveChildren(String oldDn, String newDn) throws ServiceException {
try {
// use ZLdapFilter instead of just the native Filter so it's
// convenient for stating
ZLdapFilter filter = ZLdapFilterFactory.getInstance().anyEntry();
// Filter filter = Filter.createPresenceFilter(LdapConstants.ATTR_OBJECTCLASS);
SearchRequest searchRequest = new SearchRequest(oldDn, SearchScope.ONE, derefAliasPolicy, // size limit
0, // time limit
0, // getTypesOnly
false, ((UBIDLdapFilter) filter).getNative());
searchRequest.setAttributes("dn");
SearchResult result = UBIDLdapOperation.SEARCH.execute(this, searchRequest, filter);
List<SearchResultEntry> entries = result.getSearchEntries();
for (SearchResultEntry entry : entries) {
DN entryDN = entry.getParsedDN();
String childDn = entryDN.toNormalizedString();
String childRdn = entryDN.getRDNString();
UBIDLdapOperation.MODIFY_DN.execute(this, childDn, childRdn, true, newDn);
}
} catch (LDAPException e) {
throw mapToLdapException("unable to move children", e);
}
}
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