No longer silently ignoring a NameNotFoundException due to an invalid search base. LDAP-134

This commit is contained in:
Ulrik Sandberg
2008-10-11 15:52:09 +00:00
parent 8b99097cb1
commit a76523b66d
5 changed files with 72 additions and 21 deletions

View File

@@ -73,6 +73,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
private boolean ignorePartialResultException = false;
private boolean ignoreNameNotFoundException = false;
/**
* Constructor for bean usage.
*/
@@ -107,6 +109,25 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
return contextSource;
}
/**
* Specify whether <code>NameNotFoundException</code> should be ignored in
* searches. In previous version, <code>NameNotFoundException</code> caused
* by the search base not being found was silently ignored. The default
* behavior is now to treat this as an error (as it should), and to convert
* and re-throw the exception. The ability to revert to the previous
* behavior still exists. The only difference is that the incident is in
* that case no longer silently ignored, but logged as a warning.
*
* @param ignore <code>true</code> if <code>NameNotFoundException</code>
* should be ignored in searches, <code>false</code> otherwise. Default is
* <code>false</code>.
*
* @since 1.3
*/
public void setIgnoreNameNotFoundException(boolean ignore) {
this.ignoreNameNotFoundException = ignore;
}
/**
* Specify whether <code>PartialResultException</code> should be ignored
* in searches. AD servers typically have a problem with referrals. Normally
@@ -262,14 +283,13 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
}
catch (NameNotFoundException e) {
// The base context was not found, which basically means
// that the search did not return any results. Just clean up and
// exit.
// Note that this may present problems if a DirContextProcessor was
// supplied - there's no guarantee that the postProcess() operation
// will go well after a NamingException has been thrown. It is
// however quite possible that information will be available for
// retrieval either way.
// It is possible to ignore errors caused by base not found
if (ignoreNameNotFoundException) {
log.warn("Base context not found, ignoring: " + e.getMessage());
}
else {
ex = LdapUtils.convertLdapException(e);
}
}
catch (PartialResultException e) {
// Workaround for AD servers not handling referrals correctly.

View File

@@ -349,7 +349,7 @@ public class LdapTemplateTest extends TestCase {
controls.setReturningObjFlag(false);
dirContextControl.setDefaultMatcher(new SearchControlsMatcher());
javax.naming.NameNotFoundException ne = new javax.naming.NameNotFoundException();
javax.naming.NameNotFoundException ne = new javax.naming.NameNotFoundException("some text");
dirContextControl.expectAndThrow(dirContextMock.search(nameMock,
"(ou=somevalue)", controls), ne);
@@ -357,7 +357,13 @@ public class LdapTemplateTest extends TestCase {
replay();
tested.search(nameMock, "(ou=somevalue)", handlerMock);
try {
tested.search(nameMock, "(ou=somevalue)", handlerMock);
fail("NameNotFoundException expected");
}
catch (NameNotFoundException expected) {
assertTrue(true);
}
verify();
}
@@ -1605,7 +1611,13 @@ public class LdapTemplateTest extends TestCase {
replay();
tested.search(searchExecutorMock, handlerMock);
try {
tested.search(searchExecutorMock, handlerMock);
fail("NameNotFoundException expected");
}
catch (NameNotFoundException expected) {
assertTrue(true);
}
verify();
}

View File

@@ -1,9 +0,0 @@
log4j.rootCategory=WARN, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n
#Enable debug logging
log4j.category.net.sf.ldaptemplate=INFO
log4j.category.net.sf.ldaptemplate.LdapTemplate=DEBUG

View File

@@ -16,6 +16,8 @@
package org.springframework.ldap;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.fail;
import java.util.List;
@@ -179,4 +181,27 @@ public class LdapTemplateSearchResultITest extends AbstractLdapTemplateIntegrati
List list = tested.search(BASE_NAME, FILTER_STRING, SearchControls.SUBTREE_SCOPE, CN_SN_ATTRS, contextMapper);
assertEquals(1, list.size());
}
@Test
public void testSearchWithInvalidSearchBaseShouldByDefaultThrowException() {
try {
tested.search(BASE_NAME + "ou=unknown", FILTER_STRING, SearchControls.SUBTREE_SCOPE, CN_SN_ATTRS,
contextMapper);
fail("NameNotFoundException expected");
}
catch (NameNotFoundException expected) {
assertTrue(true);
}
}
@Test
public void testSearchWithInvalidSearchBaseCanBeConfiguredToSwallowException() {
tested.setIgnoreNameNotFoundException(true);
contextMapper.setExpectedAttributes(CN_SN_ATTRS);
contextMapper.setExpectedValues(CN_SN_VALUES);
contextMapper.setAbsentAttributes(ABSENT_ATTRIBUTES);
List list = tested.search(BASE_NAME + "ou=unknown", FILTER_STRING, SearchControls.SUBTREE_SCOPE, CN_SN_ATTRS,
contextMapper);
assertEquals(0, list.size());
}
}

View File

@@ -1,6 +1,9 @@
log4j.rootCategory=warn, stdout
log4j.rootCategory=WARN, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout.ConversionPattern=%r [%t] %-5p\: %-15c{2} - %m%n
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Enable info on Spring LDAP
#log4j.logger.org.springframework.ldap=INFO
log4j.logger.org.apache.directory.server.schema.registries.DefaultSyntaxRegistry=WARN