From a76523b66dd9693fab30e717ff572993f4294562 Mon Sep 17 00:00:00 2001 From: Ulrik Sandberg Date: Sat, 11 Oct 2008 15:52:09 +0000 Subject: [PATCH] No longer silently ignoring a NameNotFoundException due to an invalid search base. LDAP-134 --- .../ldap/core/LdapTemplate.java | 36 ++++++++++++++----- .../ldap/core/LdapTemplateTest.java | 18 ++++++++-- .../src/test/java/log4j.properties | 9 ----- .../ldap/LdapTemplateSearchResultITest.java | 25 +++++++++++++ .../src/test/resources/log4j.properties | 5 ++- 5 files changed, 72 insertions(+), 21 deletions(-) delete mode 100644 mvn-build/test/integration-tests/src/test/java/log4j.properties diff --git a/mvn-build/core/src/main/java/org/springframework/ldap/core/LdapTemplate.java b/mvn-build/core/src/main/java/org/springframework/ldap/core/LdapTemplate.java index d4519495..80380d14 100644 --- a/mvn-build/core/src/main/java/org/springframework/ldap/core/LdapTemplate.java +++ b/mvn-build/core/src/main/java/org/springframework/ldap/core/LdapTemplate.java @@ -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 NameNotFoundException should be ignored in + * searches. In previous version, NameNotFoundException 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 true if NameNotFoundException + * should be ignored in searches, false otherwise. Default is + * false. + * + * @since 1.3 + */ + public void setIgnoreNameNotFoundException(boolean ignore) { + this.ignoreNameNotFoundException = ignore; + } + /** * Specify whether PartialResultException 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. diff --git a/mvn-build/core/src/test/java/org/springframework/ldap/core/LdapTemplateTest.java b/mvn-build/core/src/test/java/org/springframework/ldap/core/LdapTemplateTest.java index 2b69f7a3..077d0e34 100644 --- a/mvn-build/core/src/test/java/org/springframework/ldap/core/LdapTemplateTest.java +++ b/mvn-build/core/src/test/java/org/springframework/ldap/core/LdapTemplateTest.java @@ -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(); } diff --git a/mvn-build/test/integration-tests/src/test/java/log4j.properties b/mvn-build/test/integration-tests/src/test/java/log4j.properties deleted file mode 100644 index f57302e8..00000000 --- a/mvn-build/test/integration-tests/src/test/java/log4j.properties +++ /dev/null @@ -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 diff --git a/mvn-build/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateSearchResultITest.java b/mvn-build/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateSearchResultITest.java index bf57b524..88bf183e 100644 --- a/mvn-build/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateSearchResultITest.java +++ b/mvn-build/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateSearchResultITest.java @@ -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()); + } } diff --git a/mvn-build/test/integration-tests/src/test/resources/log4j.properties b/mvn-build/test/integration-tests/src/test/resources/log4j.properties index 12c4b1c8..ee4b44e7 100644 --- a/mvn-build/test/integration-tests/src/test/resources/log4j.properties +++ b/mvn-build/test/integration-tests/src/test/resources/log4j.properties @@ -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 \ No newline at end of file