Skip to content

Commit

Permalink
lsc-project#176: Pagination fix for jScript.
Browse files Browse the repository at this point in the history
- Reverted changes to re-introduce functionality for sortedBy and pagination.
- Created a method to retrieve pagination cookie.
- Fixed pagination when searching via jScript.
  • Loading branch information
abpai94 committed Sep 2, 2024
1 parent bea2639 commit 8f16d5a
Showing 1 changed file with 61 additions and 29 deletions.
90 changes: 61 additions & 29 deletions src/main/java/org/lsc/jndi/JndiServices.java
Original file line number Diff line number Diff line change
Expand Up @@ -754,19 +754,25 @@ private List<String> doGetDnList(final String base, final String filter,
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()) {
list.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);
} 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 list;
}
Expand Down Expand Up @@ -1137,44 +1143,70 @@ public Map<String, LscDatasets> doGetAttrsList(final String base,
constraints.setReturningAttributes(attributes);
constraints.setSearchScope(scope);
constraints.setReturningObjFlag(true);

try {
NamingEnumeration<SearchResult> results = ctx.search(searchBase, searchFilter, constraints);

if (results != null) {
Map<String, Object> attrsValues = null;
while (results.hasMoreElements()) {
attrsValues = new HashMap<String, Object>();

SearchResult ldapResult = (SearchResult) results.next();

// get the value for each attribute requested
for (String attributeName : attrsNames) {
Attribute attr = ldapResult.getAttributes().get(attributeName);
if (attr != null && attr.get() != null) {
attrsValues.put(attributeName, attr.get());
byte[] pagedResultsResponse = null;
do {
NamingEnumeration<SearchResult> results = ctx.search(searchBase, searchFilter, constraints);

if (results != null) {
Map<String, Object> attrsValues = null;
while (results.hasMoreElements()) {
attrsValues = new HashMap<String, Object>();

SearchResult ldapResult = (SearchResult) results.next();

// get the value for each attribute requested
for (String attributeName : attrsNames) {
Attribute attr = ldapResult.getAttributes().get(attributeName);
if (attr != null && attr.get() != null) {
attrsValues.put(attributeName, attr.get());
}
}
}

res.put(ldapResult.getNameInNamespace(), new LscDatasets(attrsValues));
res.put(ldapResult.getNameInNamespace(), new LscDatasets(attrsValues));
}
}
}

pagedResultsResponse = pagination();
} while (pagedResultsResponse != null);
} 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);

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

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

0 comments on commit 8f16d5a

Please sign in to comment.