Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 98e4db9

Browse files
committed
LDAP-264: Handle SizeLimitException explicitly, ignoring and aborting unless explicitly specified otherwise.
1 parent 74e6e62 commit 98e4db9

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

core/src/main/java/org/springframework/ldap/core/LdapTemplate.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import javax.naming.NameNotFoundException;
3838
import javax.naming.NamingEnumeration;
3939
import javax.naming.PartialResultException;
40+
import javax.naming.SizeLimitExceededException;
4041
import javax.naming.directory.Attributes;
4142
import javax.naming.directory.DirContext;
4243
import javax.naming.directory.ModificationItem;
@@ -81,6 +82,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
8182

8283
private boolean ignoreNameNotFoundException = false;
8384

85+
private boolean ignoreSizeLimitExceededException = true;
86+
8487
private int defaultSearchScope = SearchControls.SUBTREE_SCOPE;
8588

8689
private int defaultTimeLimit = 0;
@@ -177,6 +180,19 @@ public void setIgnorePartialResultException(boolean ignore) {
177180
this.ignorePartialResultException = ignore;
178181
}
179182

183+
/**
184+
* Specify whether <code>SizeLimitExceededException</code> should be ignored in searches.
185+
* This is typically what you want if you specify count limit in your search controls.
186+
*
187+
* @param ignore <code>true</code> if <code>SizeLimitExceededException</code>
188+
* should be ignored in searches, <code>false</code> otherwise. Default is
189+
* <code>true</code>.
190+
* @since 2.0
191+
*/
192+
public void setIgnoreSizeLimitExceededException(boolean ignore) {
193+
this.ignoreSizeLimitExceededException = ignore;
194+
}
195+
180196
/**
181197
* Set the default scope to be used in searches if not explicitly specified.
182198
* Default is {@link SearchControls.SUBTREE_SCOPE}.
@@ -379,6 +395,14 @@ public void search(SearchExecutor se, NameClassPairCallbackHandler handler, DirC
379395
ex = LdapUtils.convertLdapException(e);
380396
}
381397
}
398+
catch(SizeLimitExceededException e) {
399+
if(ignoreSizeLimitExceededException) {
400+
log.debug("SizeLimitExceededException encountered and ignored", e);
401+
}
402+
else {
403+
ex = LdapUtils.convertLdapException(e);
404+
}
405+
}
382406
catch (javax.naming.NamingException e) {
383407
ex = LdapUtils.convertLdapException(e);
384408
}
@@ -1714,8 +1738,8 @@ private SearchControls searchControlsForQuery(LdapQuery query, boolean returnObj
17141738
searchControls.setCountLimit(query.countLimit());
17151739
}
17161740

1717-
if(query.countLimit() != null) {
1718-
searchControls.setCountLimit(query.timeLimit());
1741+
if(query.timeLimit() != null) {
1742+
searchControls.setTimeLimit(query.timeLimit());
17191743
}
17201744
return searchControls;
17211745
}

test/integration-tests/src/test/java/org/springframework/ldap/itest/LdapTemplateSearchResultITest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import org.springframework.dao.EmptyResultDataAccessException;
2323
import org.springframework.dao.IncorrectResultSizeDataAccessException;
2424
import org.springframework.ldap.NameNotFoundException;
25+
import org.springframework.ldap.SizeLimitExceededException;
26+
import org.springframework.ldap.core.ContextMapper;
2527
import org.springframework.ldap.core.DirContextAdapter;
2628
import org.springframework.ldap.core.DirContextOperations;
2729
import org.springframework.ldap.core.LdapTemplate;
@@ -33,6 +35,7 @@
3335
import org.springframework.test.context.ContextConfiguration;
3436

3537
import javax.naming.Name;
38+
import javax.naming.NamingException;
3639
import javax.naming.directory.SearchControls;
3740
import java.util.List;
3841

@@ -388,4 +391,31 @@ public void testSearchWithInvalidSearchBaseCanBeConfiguredToSwallowException() {
388391
contextMapper);
389392
assertEquals(0, list.size());
390393
}
394+
395+
@Test
396+
public void verifyThatSearchWithCountLimitReturnsTheEntriesFoundSoFar() {
397+
List<Object> result = tested.search(query()
398+
.countLimit(3)
399+
.where("objectclass").is("person"), new ContextMapper<Object>() {
400+
@Override
401+
public Object mapFromContext(Object ctx) throws NamingException {
402+
return new Object();
403+
}
404+
});
405+
406+
assertEquals(3, result.size());
407+
}
408+
409+
@Test(expected = SizeLimitExceededException.class)
410+
public void verifyThatSearchWithCountLimitWithFlagToFalseThrowsException() {
411+
tested.setIgnoreSizeLimitExceededException(false);
412+
tested.search(query()
413+
.countLimit(3)
414+
.where("objectclass").is("person"), new ContextMapper<Object>() {
415+
@Override
416+
public Object mapFromContext(Object ctx) throws NamingException {
417+
return new Object();
418+
}
419+
});
420+
}
391421
}

0 commit comments

Comments
 (0)