LDAP-264: Handle SizeLimitException explicitly, ignoring and aborting unless explicitly specified otherwise.

This commit is contained in:
Mattias Hellborg Arthursson
2013-11-18 11:51:14 +01:00
parent 74e6e62fa3
commit 98e4db9ae5
3 changed files with 56 additions and 2 deletions

View File

@@ -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 <code>SizeLimitExceededException</code> should be ignored in searches.
* This is typically what you want if you specify count limit in your search controls.
*
* @param ignore <code>true</code> if <code>SizeLimitExceededException</code>
* should be ignored in searches, <code>false</code> otherwise. Default is
* <code>true</code>.
* @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;
}

View File

@@ -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<Object> result = tested.search(query()
.countLimit(3)
.where("objectclass").is("person"), new ContextMapper<Object>() {
@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<Object>() {
@Override
public Object mapFromContext(Object ctx) throws NamingException {
return new Object();
}
});
}
}