Skip to content

Commit

Permalink
- Added method to apply pagination globally
Browse files Browse the repository at this point in the history
- Removed code that resets pagination
- Maintains pagination and sortedBy for multiple context requests
  • Loading branch information
abpai94 committed Sep 4, 2024
1 parent 8ce90e1 commit 64e3dac
Showing 1 changed file with 74 additions and 69 deletions.
143 changes: 74 additions & 69 deletions src/main/java/org/lsc/jndi/JndiServices.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,15 +237,9 @@ private void initConnection()
throw new NamingException(e.getMessage());
}

String pageSizeStr = (String) ctx.getEnvironment().get("java.naming.ldap.pageSize");
if (pageSizeStr != null) {
pageSize = Integer.parseInt(pageSizeStr);
} else {
pageSize = -1;
}
// Setting global pageSize and sortedBy variable
contextRequestControls();

sortedBy = (String) ctx.getEnvironment().get("java.naming.ldap.sortedBy");

String recursiveDeleteStr = (String) ctx.getEnvironment().get("java.naming.recursivedelete");
if (recursiveDeleteStr != null) {
recursiveDelete = Boolean.parseBoolean(recursiveDeleteStr);
Expand Down Expand Up @@ -745,28 +739,35 @@ public List<String> getDnList(final String base, final String filter,
private List<String> doGetDnList(final String base, final String filter,
final int scope) throws NamingException {
NamingEnumeration<SearchResult> ne = null;
List<String> iist = new ArrayList<String>();
List<String> list = new ArrayList<String>();
try {
SearchControls sc = new SearchControls();
sc.setDerefLinkFlag(false);
sc.setReturningAttributes(new String[]{"1.1"});
sc.setSearchScope(scope);
sc.setReturningObjFlag(true);
ne = ctx.search(base, filter, sc);

String completedBaseDn = "";
if (base.length() > 0) {
completedBaseDn = "," + base;
}
while (ne.hasMoreElements()) {
iist.add(((SearchResult) ne.next()).getName() + completedBaseDn);
}
byte[] pagedResultsResponse = null;
do {
ne = ctx.search(base, filter, sc);
String completedBaseDn = "";
if (base.length() > 0) {
completedBaseDn = "," + base;
}
while (ne.hasMoreElements()) {
list.add(ne.next().getName() + completedBaseDn);
}
pagedResultsResponse = pagination();
} while (pagedResultsResponse != null);
contextRequestControls();
} catch (NamingException e) {
LOGGER.error(e.toString());
LOGGER.debug(e.toString(), e);
throw e;
} catch (IOException e) {
LOGGER.error(e.toString());
LOGGER.debug(e.toString(), e);
}
return iist;
return list;
}

/**
Expand Down Expand Up @@ -1135,30 +1136,8 @@ public Map<String, LscDatasets> doGetAttrsList(final String base,
constraints.setReturningAttributes(attributes);
constraints.setSearchScope(scope);
constraints.setReturningObjFlag(true);

try {
boolean requestPagedResults = false;

List<Control> extControls = new ArrayList<Control>();

if (pageSize > 0) {
requestPagedResults = true;
LOGGER.debug("Using pagedResults control for {} entries at a time", pageSize);
}

if (requestPagedResults) {
extControls.add(new PagedResultsControl(pageSize, Control.CRITICAL));
}

if(sortedBy != null) {
extControls.add(new SortControl(sortedBy, Control.CRITICAL));
}

if (extControls.size() > 0) {
ctx.setRequestControls(extControls.toArray(new Control[extControls.size()]));
}

byte[] pagedResultsResponse = null;
byte[] pagedResultsResponse;
do {
NamingEnumeration<SearchResult> results = ctx.search(searchBase, searchFilter, constraints);

Expand All @@ -1180,48 +1159,74 @@ public Map<String, LscDatasets> doGetAttrsList(final String base,
res.put(ldapResult.getNameInNamespace(), new LscDatasets(attrsValues));
}
}

Control[] respCtls = ctx.getResponseControls();
if (respCtls != null) {
for(Control respCtl : respCtls) {
if (requestPagedResults && respCtl instanceof PagedResultsResponseControl) {
pagedResultsResponse = ((PagedResultsResponseControl) respCtl).getCookie();
}
}
}

if (requestPagedResults && pagedResultsResponse != null) {
ctx.setRequestControls(new Control[]{
new PagedResultsControl(pageSize, pagedResultsResponse, Control.CRITICAL)});
}

pagedResultsResponse = pagination();
} while (pagedResultsResponse != null);

// clear requestControls for future use of the JNDI context
if (requestPagedResults) {
ctx.setRequestControls(null);
}
contextRequestControls();
} catch (CommunicationException e) {
// Avoid handling the communication exception as a generic one
throw e;
} catch (ServiceUnavailableException e) {
// Avoid handling the service unavailable exception as a generic one
throw e;
} catch (NamingException e) {
} catch (NamingException | IOException e) {
// clear requestControls for future use of the JNDI context
ctx.setRequestControls(null);
LOGGER.error(e.toString());
LOGGER.debug(e.toString(), e);

} catch (IOException e) {
// clear requestControls for future use of the JNDI context
ctx.setRequestControls(null);
contextRequestControls();
LOGGER.error(e.toString());
LOGGER.debug(e.toString(), e);
}
return res;
}

/**
* Obtain pagination cookie to retrieve all elements in search request.
* @return paging cookie
* @throws IOException
* @throws NamingException
*/
public byte[] pagination() throws IOException, NamingException {
byte[] pagedResultsResponse = null;
Control[] respCtls = ctx.getResponseControls();
if (respCtls != null) {
for(Control respCtl : respCtls) {
if (respCtl instanceof PagedResultsResponseControl) {
pagedResultsResponse = ((PagedResultsResponseControl) respCtl).getCookie();
}
}
}
if (pagedResultsResponse != null) {
ctx.setRequestControls(new Control[]{
new PagedResultsControl(pageSize, pagedResultsResponse, Control.CRITICAL)});
}
return pagedResultsResponse;
}

/**
* Applying request controls such as pageSize and sortedBy for LDAP Context.
*/
public void contextRequestControls() {
try {
// Setting global pageSize variable
String pageSizeStr = (String) ctx.getEnvironment().get("java.naming.ldap.pageSize");
if (pageSizeStr != null && Integer.parseInt(pageSizeStr) > -1) {
pageSize = Integer.parseInt(pageSizeStr);
List<PagedResultsControl> requestControls = new ArrayList<>();
requestControls.add(new PagedResultsControl(pageSize, Control.CRITICAL));
ctx.setRequestControls(requestControls.toArray(new Control[requestControls.size()]));
}

// Setting global sortedBy variable
sortedBy = (String) ctx.getEnvironment().get("java.naming.ldap.sortedBy");
if (sortedBy != null) {
List<SortControl> requestControls = new ArrayList<>();
requestControls.add(new SortControl(sortedBy, Control.CRITICAL));
ctx.setRequestControls(requestControls.toArray(new Control[requestControls.size()]));
}
} catch (NamingException | IOException e) {
throw new RuntimeException(e);
}
}

/**
* @return the contextDn
*/
Expand Down

0 comments on commit 64e3dac

Please sign in to comment.