diff --git a/src/main/java/org/lsc/jndi/JndiServices.java b/src/main/java/org/lsc/jndi/JndiServices.java index 8e93340f..5e446262 100644 --- a/src/main/java/org/lsc/jndi/JndiServices.java +++ b/src/main/java/org/lsc/jndi/JndiServices.java @@ -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 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 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) { @@ -772,6 +758,7 @@ private List 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); @@ -1150,7 +1137,7 @@ public Map doGetAttrsList(final String base, constraints.setSearchScope(scope); constraints.setReturningObjFlag(true); try { - byte[] pagedResultsResponse = null; + byte[] pagedResultsResponse; do { NamingEnumeration results = ctx.search(searchBase, searchFilter, constraints); @@ -1175,6 +1162,7 @@ public Map doGetAttrsList(final String base, pagedResultsResponse = pagination(); } while (pagedResultsResponse != null); + contextRequestControls(); } catch (CommunicationException e) { // Avoid handling the communication exception as a generic one throw e; @@ -1183,7 +1171,7 @@ public Map 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); } @@ -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 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 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 */