@@ -754,24 +754,31 @@ public List<String> getDnList(final String base, final String filter, final int
754
754
private List <String > doGetDnList (final String base , final String filter , final int scope ) throws NamingException {
755
755
NamingEnumeration <SearchResult > namingEnumeration = null ;
756
756
List <String > list = new ArrayList <String >();
757
+
758
+ // Create a new context for the search operation
759
+ LdapContext searchContext = (LdapContext )ctx .lookup ("" );
760
+
757
761
try {
758
- contextRequestControls ();
762
+ setRequestControls (searchContext );
763
+
759
764
SearchControls sc = new SearchControls ();
760
765
sc .setDerefLinkFlag (false );
761
766
sc .setReturningAttributes (new String [] { "1.1" });
762
767
sc .setSearchScope (scope );
763
768
sc .setReturningObjFlag (true );
769
+
764
770
byte [] pagedResultsResponse = null ;
765
771
do {
766
- namingEnumeration = ctx .search (base , filter , sc );
772
+ namingEnumeration = searchContext .search (base , filter , sc );
767
773
String completedBaseDn = "" ;
768
774
if (base .length () > 0 ) {
769
775
completedBaseDn = "," + base ;
770
776
}
771
777
while (namingEnumeration .hasMoreElements ()) {
772
778
list .add (namingEnumeration .next ().getName () + completedBaseDn );
773
779
}
774
- pagedResultsResponse = pagination ();
780
+
781
+ pagedResultsResponse = pagination (searchContext );
775
782
} while (pagedResultsResponse != null );
776
783
} catch (NamingException e ) {
777
784
LOGGER .error (e .toString ());
@@ -784,7 +791,7 @@ private List<String> doGetDnList(final String base, final String filter, final i
784
791
LOGGER .error (e .toString ());
785
792
LOGGER .debug (e .toString (), e );
786
793
} finally {
787
- ctx . setRequestControls ( defaultRequestControls );
794
+ searchContext . close ( );
788
795
}
789
796
790
797
namingEnumeration .close ();
@@ -1166,33 +1173,44 @@ public Map<String, LscDatasets> doGetAttrsList(final String base, final String f
1166
1173
constraints .setReturningObjFlag (true );
1167
1174
try {
1168
1175
byte [] pagedResultsResponse ;
1169
- contextRequestControls ();
1170
- do {
1171
- NamingEnumeration <SearchResult > results = ctx .search (searchBase , searchFilter , constraints );
1172
1176
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 >();
1177
1190
1178
- SearchResult ldapResult = (SearchResult ) results .next ();
1191
+ SearchResult ldapResult = (SearchResult ) results .next ();
1179
1192
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
+ }
1185
1199
}
1200
+
1201
+ res .put (ldapResult .getNameInNamespace (), new LscDatasets (attrsValues ));
1186
1202
}
1187
1203
1188
- res . put ( ldapResult . getNameInNamespace (), new LscDatasets ( attrsValues ));
1204
+
1189
1205
}
1190
- }
1191
1206
1192
- results .close ();
1207
+ results .close ();
1193
1208
1194
- pagedResultsResponse = pagination ();
1195
- } while (pagedResultsResponse != null );
1209
+ pagedResultsResponse = pagination (searchContext );
1210
+ } while (pagedResultsResponse != null );
1211
+ } finally {
1212
+ searchContext .close ();
1213
+ }
1196
1214
}
1197
1215
catch (CommunicationException e ) {
1198
1216
// Avoid handling the communication exception as a generic one
@@ -1215,9 +1233,9 @@ public Map<String, LscDatasets> doGetAttrsList(final String base, final String f
1215
1233
* @throws IOException
1216
1234
* @throws NamingException
1217
1235
*/
1218
- public byte [] pagination () throws IOException , NamingException {
1236
+ public byte [] pagination (LdapContext ldapContext ) throws IOException , NamingException {
1219
1237
byte [] pagedResultsResponse = null ;
1220
- Control [] respCtls = ctx .getResponseControls ();
1238
+ Control [] respCtls = ldapContext .getResponseControls ();
1221
1239
if (respCtls != null ) {
1222
1240
for (Control respCtl : respCtls ) {
1223
1241
if (respCtl instanceof PagedResultsResponseControl ) {
@@ -1226,43 +1244,38 @@ public byte[] pagination() throws IOException, NamingException {
1226
1244
}
1227
1245
}
1228
1246
if (pagedResultsResponse != null ) {
1229
- ctx .setRequestControls (new Control []{
1247
+ ldapContext .setRequestControls (new Control []{
1230
1248
new PagedResultsControl (pageSize , pagedResultsResponse , Control .CRITICAL )});
1231
1249
}
1232
1250
return pagedResultsResponse ;
1233
1251
}
1234
1252
1235
1253
/**
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
1237
1257
*/
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 ) {
1246
1259
try {
1247
1260
List <BasicControl > requestControls = new ArrayList <>();
1248
1261
1249
1262
// 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" );
1251
1264
1252
1265
if (pageSizeStr != null && Integer .parseInt (pageSizeStr ) > -1 ) {
1253
1266
pageSize = Integer .parseInt (pageSizeStr );
1254
1267
requestControls .add (new PagedResultsControl (pageSize , Control .CRITICAL ));
1255
1268
}
1256
1269
1257
1270
// 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" );
1259
1272
1260
1273
if (sortedBy != null ) {
1261
1274
requestControls .add (new SortControl (sortedBy , Control .CRITICAL ));
1262
1275
}
1263
1276
1264
1277
if (requestControls .size () > 0 ) {
1265
- ctx .setRequestControls (requestControls .toArray (new Control [requestControls .size ()]));
1278
+ ldapContext .setRequestControls (requestControls .toArray (new Control [requestControls .size ()]));
1266
1279
}
1267
1280
} catch (NamingException | IOException e ) {
1268
1281
throw new RuntimeException (e );
0 commit comments