From 8f16d5a35c41749361db0d6e11b8a043b37e8185 Mon Sep 17 00:00:00 2001 From: Abhishek Pai Date: Mon, 2 Sep 2024 17:06:40 +0200 Subject: [PATCH] #176: Pagination fix for jScript. - Reverted changes to re-introduce functionality for sortedBy and pagination. - Created a method to retrieve pagination cookie. - Fixed pagination when searching via jScript. --- src/main/java/org/lsc/jndi/JndiServices.java | 90 +++++++++++++------- 1 file changed, 61 insertions(+), 29 deletions(-) diff --git a/src/main/java/org/lsc/jndi/JndiServices.java b/src/main/java/org/lsc/jndi/JndiServices.java index 0da61977..083c6e63 100644 --- a/src/main/java/org/lsc/jndi/JndiServices.java +++ b/src/main/java/org/lsc/jndi/JndiServices.java @@ -754,19 +754,25 @@ private List 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; } @@ -1137,44 +1143,70 @@ public Map doGetAttrsList(final String base, constraints.setReturningAttributes(attributes); constraints.setSearchScope(scope); constraints.setReturningObjFlag(true); - try { - NamingEnumeration results = ctx.search(searchBase, searchFilter, constraints); - - if (results != null) { - Map attrsValues = null; - while (results.hasMoreElements()) { - attrsValues = new HashMap(); - - 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 results = ctx.search(searchBase, searchFilter, constraints); + + if (results != null) { + Map attrsValues = null; + while (results.hasMoreElements()) { + attrsValues = new HashMap(); + + 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 */