Skip to content

Commit 1720617

Browse files
elecharnydavidcoutadeur
authored andcommitted
Fix the controls addition (#375)
1 parent ce5eb96 commit 1720617

File tree

1 file changed

+50
-37
lines changed

1 file changed

+50
-37
lines changed

src/main/java/org/lsc/jndi/JndiServices.java

Lines changed: 50 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -754,24 +754,31 @@ public List<String> getDnList(final String base, final String filter, final int
754754
private List<String> doGetDnList(final String base, final String filter, final int scope) throws NamingException {
755755
NamingEnumeration<SearchResult> namingEnumeration = null;
756756
List<String> list = new ArrayList<String>();
757+
758+
// Create a new context for the search operation
759+
LdapContext searchContext = (LdapContext)ctx.lookup("");
760+
757761
try {
758-
contextRequestControls();
762+
setRequestControls(searchContext);
763+
759764
SearchControls sc = new SearchControls();
760765
sc.setDerefLinkFlag(false);
761766
sc.setReturningAttributes(new String[] { "1.1" });
762767
sc.setSearchScope(scope);
763768
sc.setReturningObjFlag(true);
769+
764770
byte[] pagedResultsResponse = null;
765771
do {
766-
namingEnumeration = ctx.search(base, filter, sc);
772+
namingEnumeration = searchContext.search(base, filter, sc);
767773
String completedBaseDn = "";
768774
if (base.length() > 0) {
769775
completedBaseDn = "," + base;
770776
}
771777
while (namingEnumeration.hasMoreElements()) {
772778
list.add(namingEnumeration.next().getName() + completedBaseDn);
773779
}
774-
pagedResultsResponse = pagination();
780+
781+
pagedResultsResponse = pagination(searchContext);
775782
} while (pagedResultsResponse != null);
776783
} catch (NamingException e) {
777784
LOGGER.error(e.toString());
@@ -784,7 +791,7 @@ private List<String> doGetDnList(final String base, final String filter, final i
784791
LOGGER.error(e.toString());
785792
LOGGER.debug(e.toString(), e);
786793
} finally {
787-
ctx.setRequestControls(defaultRequestControls);
794+
searchContext.close();
788795
}
789796

790797
namingEnumeration.close();
@@ -1166,33 +1173,44 @@ public Map<String, LscDatasets> doGetAttrsList(final String base, final String f
11661173
constraints.setReturningObjFlag(true);
11671174
try {
11681175
byte[] pagedResultsResponse;
1169-
contextRequestControls();
1170-
do {
1171-
NamingEnumeration<SearchResult> results = ctx.search(searchBase, searchFilter, constraints);
11721176

1173-
if (results != null) {
1174-
Map<String, Object> attrsValues = null;
1175-
while (results.hasMoreElements()) {
1176-
attrsValues = new HashMap<String, Object>();
1177+
// Create a new context for the search operation
1178+
LdapContext searchContext = (LdapContext)ctx.lookup("");
1179+
1180+
try {
1181+
setRequestControls(searchContext);
1182+
1183+
do {
1184+
NamingEnumeration<SearchResult> results = searchContext.search(searchBase, searchFilter, constraints);
1185+
1186+
if (results != null) {
1187+
Map<String, Object> attrsValues = null;
1188+
while (results.hasMoreElements()) {
1189+
attrsValues = new HashMap<String, Object>();
11771190

1178-
SearchResult ldapResult = (SearchResult) results.next();
1191+
SearchResult ldapResult = (SearchResult) results.next();
11791192

1180-
// get the value for each attribute requested
1181-
for (String attributeName : attrsNames) {
1182-
Attribute attr = ldapResult.getAttributes().get(attributeName);
1183-
if (attr != null && attr.get() != null) {
1184-
attrsValues.put(attributeName, attr.get());
1193+
// get the value for each attribute requested
1194+
for (String attributeName : attrsNames) {
1195+
Attribute attr = ldapResult.getAttributes().get(attributeName);
1196+
if (attr != null && attr.get() != null) {
1197+
attrsValues.put(attributeName, attr.get());
1198+
}
11851199
}
1200+
1201+
res.put(ldapResult.getNameInNamespace(), new LscDatasets(attrsValues));
11861202
}
11871203

1188-
res.put(ldapResult.getNameInNamespace(), new LscDatasets(attrsValues));
1204+
11891205
}
1190-
}
11911206

1192-
results.close();
1207+
results.close();
11931208

1194-
pagedResultsResponse = pagination();
1195-
} while (pagedResultsResponse != null);
1209+
pagedResultsResponse = pagination(searchContext);
1210+
} while (pagedResultsResponse != null);
1211+
} finally {
1212+
searchContext.close();
1213+
}
11961214
}
11971215
catch (CommunicationException e) {
11981216
// Avoid handling the communication exception as a generic one
@@ -1215,9 +1233,9 @@ public Map<String, LscDatasets> doGetAttrsList(final String base, final String f
12151233
* @throws IOException
12161234
* @throws NamingException
12171235
*/
1218-
public byte[] pagination() throws IOException, NamingException {
1236+
public byte[] pagination(LdapContext ldapContext) throws IOException, NamingException {
12191237
byte[] pagedResultsResponse = null;
1220-
Control[] respCtls = ctx.getResponseControls();
1238+
Control[] respCtls = ldapContext.getResponseControls();
12211239
if (respCtls != null) {
12221240
for(Control respCtl : respCtls) {
12231241
if (respCtl instanceof PagedResultsResponseControl) {
@@ -1226,43 +1244,38 @@ public byte[] pagination() throws IOException, NamingException {
12261244
}
12271245
}
12281246
if (pagedResultsResponse != null) {
1229-
ctx.setRequestControls(new Control[]{
1247+
ldapContext.setRequestControls(new Control[]{
12301248
new PagedResultsControl(pageSize, pagedResultsResponse, Control.CRITICAL)});
12311249
}
12321250
return pagedResultsResponse;
12331251
}
12341252

12351253
/**
1236-
* Applying request controls such as pageSize and sortedBy for LDAP Context.
1254+
* Returning the request controls such as pageSize and sortedBy for a LDAP Context.
1255+
*
1256+
* @param ldapContext The Ldap Context that will receive the controls
12371257
*/
1238-
public void contextRequestControls() {
1239-
try {
1240-
// Storing default request controls
1241-
defaultRequestControls = ctx.getRequestControls();
1242-
} catch (NamingException e) {
1243-
throw new RuntimeException(e);
1244-
}
1245-
1258+
public void setRequestControls(LdapContext ldapContext) {
12461259
try {
12471260
List<BasicControl> requestControls = new ArrayList<>();
12481261

12491262
// Setting global pageSize variable
1250-
String pageSizeStr = (String) ctx.getEnvironment().get("java.naming.ldap.pageSize");
1263+
String pageSizeStr = (String) ldapContext.getEnvironment().get("java.naming.ldap.pageSize");
12511264

12521265
if (pageSizeStr != null && Integer.parseInt(pageSizeStr) > -1) {
12531266
pageSize = Integer.parseInt(pageSizeStr);
12541267
requestControls.add(new PagedResultsControl(pageSize, Control.CRITICAL));
12551268
}
12561269

12571270
// Setting global sortedBy variable
1258-
String sortedBy = (String) ctx.getEnvironment().get("java.naming.ldap.sortedBy");
1271+
String sortedBy = (String) ldapContext.getEnvironment().get("java.naming.ldap.sortedBy");
12591272

12601273
if (sortedBy != null) {
12611274
requestControls.add(new SortControl(sortedBy, Control.CRITICAL));
12621275
}
12631276

12641277
if (requestControls.size() > 0) {
1265-
ctx.setRequestControls(requestControls.toArray(new Control[requestControls.size()]));
1278+
ldapContext.setRequestControls(requestControls.toArray(new Control[requestControls.size()]));
12661279
}
12671280
} catch (NamingException | IOException e) {
12681281
throw new RuntimeException(e);

0 commit comments

Comments
 (0)