Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed bug override globally configured pageSize variable in the middle of the code #299

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 73 additions & 71 deletions src/main/java/org/lsc/jndi/JndiServices.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,15 +237,6 @@ 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;
}

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 +736,37 @@ 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 {
contextRequestControls();
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;
abpai94 marked this conversation as resolved.
Show resolved Hide resolved
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());
abpai94 marked this conversation as resolved.
Show resolved Hide resolved
LOGGER.debug(e.toString(), e);
throw e;
} catch (IOException e) {
LOGGER.error(e.toString());
LOGGER.debug(e.toString(), e);
} finally {
ctx.setRequestControls(null);
}
return iist;
return list;
}

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

abpai94 marked this conversation as resolved.
Show resolved Hide resolved
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;
abpai94 marked this conversation as resolved.
Show resolved Hide resolved
byte[] pagedResultsResponse;
contextRequestControls();
do {
NamingEnumeration<SearchResult> results = ctx.search(searchBase, searchFilter, constraints);

Expand All @@ -1180,48 +1159,71 @@ 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);
}
} catch (CommunicationException e) {
}
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) {
// clear requestControls for future use of the JNDI context
ctx.setRequestControls(null);
} catch (NamingException | IOException e) {
LOGGER.error(e.toString());
LOGGER.debug(e.toString(), e);

} catch (IOException e) {
// clear requestControls for future use of the JNDI context
} finally {
ctx.setRequestControls(null);
LOGGER.error(e.toString());
LOGGER.debug(e.toString(), e);
abpai94 marked this conversation as resolved.
Show resolved Hide resolved
}
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)});
abpai94 marked this conversation as resolved.
Show resolved Hide resolved
}
return pagedResultsResponse;
}

/**
* Applying request controls such as pageSize and sortedBy for LDAP Context.
*/
public void contextRequestControls() {
List<BasicControl> requestControls = new ArrayList<>();
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);
requestControls.add(new PagedResultsControl(pageSize, Control.CRITICAL));
}
// Setting global sortedBy variable
sortedBy = (String) ctx.getEnvironment().get("java.naming.ldap.sortedBy");
if (sortedBy != null) {
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
Loading