Skip to content

Commit

Permalink
lsc-project#176: Creating function to apply request controls. Fixes b…
Browse files Browse the repository at this point in the history
…ug with outdated paging cookie.
  • Loading branch information
abpai94 committed Sep 3, 2024
1 parent 7539691 commit 7b8f695
Showing 1 changed file with 32 additions and 18 deletions.
50 changes: 32 additions & 18 deletions src/main/java/org/lsc/jndi/JndiServices.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,22 +237,8 @@ private void initConnection()
throw new NamingException(e.getMessage());
}

// 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()]));
}
// Setting global pageSize and sortedBy variable
contextRequestControls();

String recursiveDeleteStr = (String) ctx.getEnvironment().get("java.naming.recursivedelete");
if (recursiveDeleteStr != null) {
Expand Down Expand Up @@ -772,6 +758,7 @@ private List<String> doGetDnList(final String base, final String filter,
}
pagedResultsResponse = pagination();
} while (pagedResultsResponse != null);
contextRequestControls();
} catch (NamingException e) {
LOGGER.error(e.toString());
LOGGER.debug(e.toString(), e);
Expand Down Expand Up @@ -1150,7 +1137,7 @@ public Map<String, LscDatasets> doGetAttrsList(final String base,
constraints.setSearchScope(scope);
constraints.setReturningObjFlag(true);
try {
byte[] pagedResultsResponse = null;
byte[] pagedResultsResponse;
do {
NamingEnumeration<SearchResult> results = ctx.search(searchBase, searchFilter, constraints);

Expand All @@ -1175,6 +1162,7 @@ public Map<String, LscDatasets> doGetAttrsList(final String base,

pagedResultsResponse = pagination();
} while (pagedResultsResponse != null);
contextRequestControls();
} catch (CommunicationException e) {
// Avoid handling the communication exception as a generic one
throw e;
Expand All @@ -1183,7 +1171,7 @@ public Map<String, LscDatasets> doGetAttrsList(final String base,
throw e;
} catch (NamingException | IOException e) {
// clear requestControls for future use of the JNDI context
ctx.setRequestControls(null);
contextRequestControls();
LOGGER.error(e.toString());
LOGGER.debug(e.toString(), e);
}
Expand Down Expand Up @@ -1213,6 +1201,32 @@ public byte[] pagination() throws IOException, NamingException {
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 7b8f695

Please sign in to comment.