Skip to content

Commit

Permalink
Make CertRecordPagedList generic
Browse files Browse the repository at this point in the history
The class `CertRecordPagedList` is renamed to `RecordPagedList` and made
generic to be used with all type of entries available.
  • Loading branch information
fmarco76 committed Jan 29, 2024
1 parent cd68b1b commit 45200b4
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1164,7 +1164,7 @@ public int countCertificates(String filter, int timeLimit)
logger.debug("countCertificates filter {}", filter);

try (DBSSession s = dbSubsystem.createSession()) {
return s.countCertificates(mBaseDN, filter, timeLimit);
return s.countEntries(CertRecord.class, mBaseDN, filter, timeLimit);
}
}

Expand Down Expand Up @@ -1252,20 +1252,21 @@ public Enumeration<CertRecord> findCertRecords(String filter)
* @return a list of certificates
* @exception EBaseException failed to search
*/
public CertRecordPagedList findPagedCertRecords(String filter,
public RecordPagedList<CertRecord> findPagedCertRecords(String filter,
String[] attrs, String sortKey)
throws EBaseException {

logger.debug("CertificateRepository.findCertRecordsInList()");

try (DBSSession session = dbSubsystem.createSession()) {
DBPagedSearch<CertRecord> page = session.<CertRecord>createPagedSearch(
CertRecord.class,
mBaseDN,
filter,
attrs,
sortKey);

return new CertRecordPagedList(page);
return new RecordPagedList<>(page);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
import com.netscape.cmscore.base.ConfigStorage;
import com.netscape.cmscore.base.FileConfigStorage;
import com.netscape.cmscore.dbs.CertRecord;
import com.netscape.cmscore.dbs.CertRecordPagedList;
import com.netscape.cmscore.dbs.CertificateRepository;
import com.netscape.cmscore.dbs.DBSubsystem;
import com.netscape.cmscore.dbs.RecordPagedList;
import com.netscape.cmscore.ldapconn.LDAPConfig;
import com.netscape.cmscore.ldapconn.PKISocketConfig;
import com.netscape.cmscore.security.SecureRandomConfig;
Expand Down Expand Up @@ -118,7 +118,7 @@ public void execute(CommandLine cmd) throws Exception {
CertificateRepository certificateRepository = new CertificateRepository(secureRandom, dbSubsystem);
certificateRepository.init();

CertRecordPagedList certPages = certificateRepository.findPagedCertRecords(filter, null, "serialno");
RecordPagedList<CertRecord> certPages = certificateRepository.findPagedCertRecords(filter, null, "serialno");
boolean follow = false;
for (CertRecord cRec: certPages) {
CertId id = new CertId(cRec.getSerialNumber());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.List;

import com.netscape.certsrv.base.EBaseException;
import com.netscape.cmscore.dbs.CertRecord;

/**
* A class represents a paged search.
Expand All @@ -29,8 +28,8 @@
*/
public abstract class DBPagedSearch<E extends IDBObj> {

public abstract List<CertRecord> getPage() throws EBaseException;
public abstract List<E> getPage() throws EBaseException;

public abstract List<CertRecord> getPage(int size) throws EBaseException;
public abstract List<E> getPage(int size) throws EBaseException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -258,13 +258,15 @@ public DBSearchResults pagedSearch(
* Retrieves a list of object that satisfies the given
* filter.
*
* @param classResults the class representing the entries in the paged list
* @param base starting point of the search
* @param filter search filter
* @param timeLimit timeout limit
* @return the number of certificates
* @exception EBaseException failed to search
*/
public int countCertificates(
public <T extends IDBObj> int countEntries(
Class<T> classResults,
String base,
String filter,
int timeLimit
Expand Down Expand Up @@ -375,15 +377,15 @@ public <T extends IDBObj> DBVirtualList<T> createVirtualList(
/**
* Retrieves a paged search of objects.
*
* @param classResults the class representing the entries in the paged list
* @param base starting point of the search
* @param filter search filter
* @param attrs selected attributes
* @param startFrom starting point
* @param sortKey key used to sort the list
* @return search results in virtual list
* @exception EBaseException failed to search
*/
public <T extends IDBObj> DBPagedSearch<T> createPagedSearch(String base, String filter, String[] attrs,
public <T extends IDBObj> DBPagedSearch<T> createPagedSearch(Class<T> classResults, String base, String filter, String[] attrs,
String sortKey) throws EBaseException {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,19 @@ public boolean hasMoreElements() {
*/
@Override
public Object nextElement() {
LDAPEntry entry = null;

try {
Object o = mRes.nextElement();

if (o instanceof LDAPEntry) {
entry = (LDAPEntry) o;
if (o instanceof LDAPEntry entry) {
return mRegistry.createObject(entry.getAttributeSet());
}
if (o instanceof LDAPException)
;
// doing nothing because the last object in the search
// results is always LDAPException
else
logger.warn("DBSearchResults: result format error class=" + o.getClass().getName());
logger.warn("DBSearchResults: result format error class={}", o.getClass().getName());
} catch (Exception e) {

/*LogDoc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
public class LDAPPagedSearch<E extends IDBObj> extends DBPagedSearch<E> {
public static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(LDAPPagedSearch.class);

private Class<E> contentClassType;
private DBRegistry registry;
private LDAPConnection conn = null;
private String base = null;
Expand All @@ -53,8 +54,9 @@ public class LDAPPagedSearch<E extends IDBObj> extends DBPagedSearch<E> {
private String sortKey = null;
private LDAPSearchResults res = null;

public LDAPPagedSearch(DBRegistry registry, LDAPConnection conn, String base, String filter, String[] attrs,
public LDAPPagedSearch(Class<E> contentClassType, DBRegistry registry, LDAPConnection conn, String base, String filter, String[] attrs,
String sortKey) throws EBaseException {
this.contentClassType = contentClassType;
this.registry = registry;
this.base = base;
this.filter = filter;
Expand All @@ -69,13 +71,13 @@ public LDAPPagedSearch(DBRegistry registry, LDAPConnection conn, String base, St
}

@Override
public List<CertRecord> getPage()
public List<E> getPage()
throws EBaseException {
return getPage(LDAPSession.MAX_PAGED_SEARCH_SIZE);
}

@Override
public List<CertRecord> getPage(int size)
public List<E> getPage(int size)
throws EBaseException {
try {
logger.info("LDAPSession.continuousPagedSearch(): Searching {} for {}", base, filter);
Expand All @@ -95,7 +97,7 @@ public List<CertRecord> getPage(int size)
String ldapfilter = registry.getFilter(filter);

byte[] cookie = null;
ArrayList<CertRecord> entries = new ArrayList<>();
ArrayList<E> entries = new ArrayList<>();
if (res != null) {
for (LDAPControl c: res.getResponseControls()){
if(c instanceof LDAPPagedResultsControl resC){
Expand All @@ -117,7 +119,7 @@ public List<CertRecord> getPage(int size)
LDAPv3.SCOPE_ONE, ldapfilter, ldapattrs, false, cons);
DBSearchResults sr = new DBSearchResults(registry, res);
while (sr.hasMoreElements()) {
entries.add((CertRecord) sr.nextElement());
entries.add(contentClassType.cast(sr.nextElement()));
}
return entries;
} catch (LDAPException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -535,13 +535,13 @@ public DBSearchResults pagedSearch(String base, String filter, int start, int si
}

@Override
public int countCertificates(String base, String filter, int timeLimit)
public <T extends IDBObj> int countEntries(Class<T> classResults, String base, String filter, int timeLimit)
throws EBaseException {
String[] attrs = {"objectclass"};
DBPagedSearch<CertRecord> search = createPagedSearch(base, filter, attrs, null);
CertRecordPagedList list = new CertRecordPagedList(search);
DBPagedSearch<T> search = createPagedSearch(classResults, base, filter, attrs, null);
RecordPagedList<T> list = new RecordPagedList<>(search);
int count = 0;
for(CertRecord c: list) {
for(T c: list) {
count++;
}

Expand Down Expand Up @@ -688,11 +688,11 @@ public <T extends IDBObj> DBVirtualList<T> createVirtualList(String base, String
}

@Override
public <T extends IDBObj> DBPagedSearch<T> createPagedSearch(String base, String filter,
String attrs[], String sortKey) throws EBaseException {
public <T extends IDBObj> DBPagedSearch<T> createPagedSearch(Class<T> classResults, String base, String filter,
String[] attrs, String sortKey) throws EBaseException {
logger.debug("LDAPSession: createPagedSearch({}, {})", base, filter);

return new LDAPPagedSearch<>(dbSubsystem.getRegistry(),mConn, base,
return new LDAPPagedSearch<>(classResults, dbSubsystem.getRegistry(),mConn, base,
filter, attrs, sortKey);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,22 @@

import com.netscape.certsrv.base.EBaseException;
import com.netscape.certsrv.dbs.DBPagedSearch;
import com.netscape.certsrv.dbs.IDBObj;

/**
* Contain all records in a page for a paged search.
*
* @author Marco Fargetta {@literal <mfargett@redhat.com>}
*/
public class CertRecordPagedList implements Iterable<CertRecord> {
public static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(CertRecordPagedList.class);
public class RecordPagedList<T extends IDBObj> implements Iterable<T> {
public static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(RecordPagedList.class);

private DBPagedSearch<CertRecord> pages;
private Iterator<CertRecord> pageEntries;
private DBPagedSearch<T> pages;
private Iterator<T> pageEntries;
/**
* Constructs a request paged.
*/
public CertRecordPagedList(DBPagedSearch<CertRecord> pages) {
public RecordPagedList(DBPagedSearch<T> pages) {
this.pages = pages;
try {
pageEntries = pages.getPage().iterator();
Expand All @@ -46,17 +47,17 @@ public CertRecordPagedList(DBPagedSearch<CertRecord> pages) {
}

@Override
public Iterator<CertRecord> iterator() {
return new CertRecordPageIterator();
public Iterator<T> iterator() {
return new RecordPageIterator();
}

class CertRecordPageIterator implements Iterator<CertRecord> {
class RecordPageIterator implements Iterator<T> {

@Override
public boolean hasNext() {
if (!pageEntries.hasNext()) {
try {
List<CertRecord> newPage = pages.getPage();
List<T> newPage = pages.getPage();
pageEntries = newPage.iterator();
} catch (EBaseException e) {
throw new RuntimeException("CertRecordPagedList: Error to get a new page", e);
Expand All @@ -66,7 +67,7 @@ public boolean hasNext() {
}

@Override
public CertRecord next() {
public T next() {
if (hasNext()) {
return pageEntries.next();
}
Expand Down

0 comments on commit 45200b4

Please sign in to comment.