diff --git a/core/src/main/java/org/springframework/ldap/core/LdapTemplate.java b/core/src/main/java/org/springframework/ldap/core/LdapTemplate.java index cf44eac2..3a875c11 100644 --- a/core/src/main/java/org/springframework/ldap/core/LdapTemplate.java +++ b/core/src/main/java/org/springframework/ldap/core/LdapTemplate.java @@ -37,6 +37,7 @@ import javax.naming.NameClassPair; import javax.naming.NameNotFoundException; import javax.naming.NamingEnumeration; import javax.naming.PartialResultException; +import javax.naming.SizeLimitExceededException; import javax.naming.directory.Attributes; import javax.naming.directory.DirContext; import javax.naming.directory.ModificationItem; @@ -81,6 +82,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean { private boolean ignoreNameNotFoundException = false; + private boolean ignoreSizeLimitExceededException = true; + private int defaultSearchScope = SearchControls.SUBTREE_SCOPE; private int defaultTimeLimit = 0; @@ -177,6 +180,19 @@ public class LdapTemplate implements LdapOperations, InitializingBean { this.ignorePartialResultException = ignore; } + /** + * Specify whether SizeLimitExceededException should be ignored in searches. + * This is typically what you want if you specify count limit in your search controls. + * + * @param ignore true if SizeLimitExceededException + * should be ignored in searches, false otherwise. Default is + * true. + * @since 2.0 + */ + public void setIgnoreSizeLimitExceededException(boolean ignore) { + this.ignoreSizeLimitExceededException = ignore; + } + /** * Set the default scope to be used in searches if not explicitly specified. * Default is {@link SearchControls.SUBTREE_SCOPE}. @@ -379,6 +395,14 @@ public class LdapTemplate implements LdapOperations, InitializingBean { ex = LdapUtils.convertLdapException(e); } } + catch(SizeLimitExceededException e) { + if(ignoreSizeLimitExceededException) { + log.debug("SizeLimitExceededException encountered and ignored", e); + } + else { + ex = LdapUtils.convertLdapException(e); + } + } catch (javax.naming.NamingException e) { ex = LdapUtils.convertLdapException(e); } @@ -1714,8 +1738,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean { searchControls.setCountLimit(query.countLimit()); } - if(query.countLimit() != null) { - searchControls.setCountLimit(query.timeLimit()); + if(query.timeLimit() != null) { + searchControls.setTimeLimit(query.timeLimit()); } return searchControls; } diff --git a/test/integration-tests/src/test/java/org/springframework/ldap/itest/LdapTemplateSearchCountITest.xls b/test/integration-tests/src/test/java/org/springframework/ldap/itest/LdapTemplateSearchCountITest.xls deleted file mode 100644 index a7f7e40c..00000000 Binary files a/test/integration-tests/src/test/java/org/springframework/ldap/itest/LdapTemplateSearchCountITest.xls and /dev/null differ diff --git a/test/integration-tests/src/test/java/org/springframework/ldap/itest/LdapTemplateSearchResultITest.java b/test/integration-tests/src/test/java/org/springframework/ldap/itest/LdapTemplateSearchResultITest.java index 416521c9..5d87a422 100644 --- a/test/integration-tests/src/test/java/org/springframework/ldap/itest/LdapTemplateSearchResultITest.java +++ b/test/integration-tests/src/test/java/org/springframework/ldap/itest/LdapTemplateSearchResultITest.java @@ -22,6 +22,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.dao.IncorrectResultSizeDataAccessException; import org.springframework.ldap.NameNotFoundException; +import org.springframework.ldap.SizeLimitExceededException; +import org.springframework.ldap.core.ContextMapper; import org.springframework.ldap.core.DirContextAdapter; import org.springframework.ldap.core.DirContextOperations; import org.springframework.ldap.core.LdapTemplate; @@ -33,6 +35,7 @@ import org.springframework.ldap.test.AttributeCheckContextMapper; import org.springframework.test.context.ContextConfiguration; import javax.naming.Name; +import javax.naming.NamingException; import javax.naming.directory.SearchControls; import java.util.List; @@ -388,4 +391,31 @@ public class LdapTemplateSearchResultITest extends AbstractLdapTemplateIntegrati contextMapper); assertEquals(0, list.size()); } + + @Test + public void verifyThatSearchWithCountLimitReturnsTheEntriesFoundSoFar() { + List result = tested.search(query() + .countLimit(3) + .where("objectclass").is("person"), new ContextMapper() { + @Override + public Object mapFromContext(Object ctx) throws NamingException { + return new Object(); + } + }); + + assertEquals(3, result.size()); + } + + @Test(expected = SizeLimitExceededException.class) + public void verifyThatSearchWithCountLimitWithFlagToFalseThrowsException() { + tested.setIgnoreSizeLimitExceededException(false); + tested.search(query() + .countLimit(3) + .where("objectclass").is("person"), new ContextMapper() { + @Override + public Object mapFromContext(Object ctx) throws NamingException { + return new Object(); + } + }); + } }