Skip to content

Commit 1de2daa

Browse files
committed
Fix LDAPSession.countEntries() warning with page results
The method LDAPSession.countEntries() is updated to use raw paged search. It was counting the object associated with the retrieved ldap query but the query in this case retrieve only objectClass so the object mapping was failing. The count was correct but there were additional logs for the mapping error and it requires extra time for object mapping. Assisted-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 9168f30 commit 1de2daa

1 file changed

Lines changed: 40 additions & 8 deletions

File tree

base/server/src/main/java/com/netscape/cmscore/dbs/LDAPSession.java

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -541,15 +541,47 @@ public DBSearchResults pagedSearch(String base, String filter, String[] sortKeys
541541
@Override
542542
public <T extends IDBObj> int countEntries(Class<T> classResults, String base, String filter, int timeLimit)
543543
throws EBaseException {
544-
String[] attrs = {"objectclass"};
545-
DBPagedSearch<T> search = createPagedSearch(classResults, base, filter, attrs, (String) null);
546-
RecordPagedList<T> list = new RecordPagedList<>(search);
547-
int count = 0;
548-
for(T c: list) {
549-
count++;
550-
}
544+
try {
545+
String ldapfilter = dbSubsystem.getRegistry().getFilter(filter);
546+
logger.info("LDAPSession.countEntries(): Searching {} for {}", base, ldapfilter);
547+
548+
LDAPSearchConstraints cons = new LDAPSearchConstraints();
549+
if (timeLimit > 0) {
550+
cons.setServerTimeLimit(timeLimit);
551+
}
552+
553+
String[] attrs = {"objectclass"};
554+
LDAPPagedResultsControl pagecon = new LDAPPagedResultsControl(false, MAX_PAGED_SEARCH_SIZE);
555+
int count = 0;
556+
byte[] cookie;
551557

552-
return count;
558+
do {
559+
cons.setServerControls(pagecon);
560+
LDAPSearchResults res = mConn.search(base,
561+
LDAPv3.SCOPE_ONE, ldapfilter, attrs, false, cons);
562+
while (res.hasMoreElements()) {
563+
res.next();
564+
count++;
565+
}
566+
cookie = null;
567+
for (LDAPControl c : res.getResponseControls()) {
568+
if (c instanceof LDAPPagedResultsControl resC) {
569+
cookie = resC.getCookie();
570+
}
571+
}
572+
if (cookie != null) {
573+
pagecon = new LDAPPagedResultsControl(false, MAX_PAGED_SEARCH_SIZE, cookie);
574+
}
575+
} while (cookie != null);
576+
577+
return count;
578+
579+
} catch (LDAPException e) {
580+
if (e.getLDAPResultCode() == LDAPException.UNAVAILABLE)
581+
throw new DBNotAvailableException(
582+
CMS.getUserMessage("CMS_DBS_INTERNAL_DIR_UNAVAILABLE"));
583+
throw new DBException("Unable to search LDAP record: " + e.getMessage(), e);
584+
}
553585
}
554586

555587
@Override

0 commit comments

Comments
 (0)