From 2a91a07ee0702ce3c575f2057e1b827e2b671e56 Mon Sep 17 00:00:00 2001 From: Dustin Sweigart Date: Sat, 28 Oct 2017 09:18:30 -0400 Subject: [PATCH] updated findAll method to check if base dn should be used --- .../ldap/core/LdapTemplate.java | 51 ++++++++++++------- 1 file changed, 32 insertions(+), 19 deletions(-) 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 2cf3ead18f..edd252b94d 100644 --- a/core/src/main/java/org/springframework/ldap/core/LdapTemplate.java +++ b/core/src/main/java/org/springframework/ldap/core/LdapTemplate.java @@ -24,6 +24,7 @@ import org.springframework.ldap.NamingException; import org.springframework.ldap.UncategorizedLdapException; import org.springframework.ldap.filter.Filter; +import org.springframework.ldap.odm.annotations.Entry; import org.springframework.ldap.odm.core.ObjectDirectoryMapper; import org.springframework.ldap.odm.core.OdmException; import org.springframework.ldap.odm.core.impl.DefaultObjectDirectoryMapper; @@ -43,6 +44,8 @@ import javax.naming.directory.ModificationItem; import javax.naming.directory.SearchControls; import javax.naming.ldap.LdapName; +import java.lang.annotation.Annotation; +import java.util.Arrays; import java.util.List; /** @@ -60,9 +63,9 @@ * results are lost) or all referrals are ignored (if the server is unable to * handle them properly. Neither is there any simple way to get notified that a * PartialResultException has been ignored (other than in the log). - * + * * @see org.springframework.ldap.core.ContextSource - * + * * @author Mattias Hellborg Arthursson * @author Ulrik Sandberg */ @@ -100,7 +103,7 @@ public LdapTemplate() { /** * Constructor to setup instance directly. - * + * * @param contextSource the ContextSource to use. */ public LdapTemplate(ContextSource contextSource) { @@ -110,7 +113,7 @@ public LdapTemplate(ContextSource contextSource) { /** * Set the ContextSource. Call this method when the default constructor has * been used. - * + * * @param contextSource the ContextSource. */ public void setContextSource(ContextSource contextSource) { @@ -137,7 +140,7 @@ public void setObjectDirectoryMapper(ObjectDirectoryMapper odm) { /** * Get the ContextSource. - * + * * @return the ContextSource. */ public ContextSource getContextSource() { @@ -152,11 +155,11 @@ public ContextSource getContextSource() { * 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) { @@ -174,7 +177,7 @@ public void setIgnoreNameNotFoundException(boolean ignore) { * PartialResultException to be ignored, so that the search * method returns normally. Default value of this parameter is * false. - * + * * @param ignore true if PartialResultException * should be ignored in searches, false otherwise. Default is * false. @@ -340,7 +343,7 @@ public NamingEnumeration executeSearch(DirContext ctx) throws javax.naming.Namin * {@link NameClassPair} (this might be a NameClassPair or a subclass * thereof) is passed to the CallbackHandler. Any encountered * NamingException will be translated using the NamingExceptionTranslator. - * + * * @param se the SearchExecutor to use for performing the actual list. * @param handler the NameClassPairCallbackHandler to which each found entry * will be passed. @@ -427,7 +430,7 @@ public void search(SearchExecutor se, NameClassPairCallbackHandler handler, DirC * {@link NameClassPair} (this might be a NameClassPair or a subclass * thereof) is passed to the CallbackHandler. Any encountered * NamingException will be translated using the NamingExceptionTranslator. - * + * * @param se the SearchExecutor to use for performing the actual list. * @param handler the NameClassPairCallbackHandler to which each found entry * will be passed. @@ -1088,7 +1091,7 @@ public Object executeWithContext(DirContext ctx) throws javax.naming.NamingExcep /** * Delete all subcontexts including the current one recursively. - * + * * @param ctx The context to use for deleting. * @param name The starting point to delete recursively. * @throws NamingException if any error occurs @@ -1191,7 +1194,7 @@ private void closeContextAndNamingEnumeration(DirContext ctx, NamingEnumeration /** * Close the supplied DirContext if it is not null. Swallow any exceptions, * as this is only for cleanup. - * + * * @param ctx the context to close. */ private void closeContext(DirContext ctx) { @@ -1208,7 +1211,7 @@ private void closeContext(DirContext ctx) { /** * Close the supplied NamingEnumeration if it is not null. Swallow any * exceptions, as this is only for cleanup. - * + * * @param results the NamingEnumeration to close. */ private void closeNamingEnumeration(NamingEnumeration results) { @@ -1235,7 +1238,7 @@ private SearchControls getDefaultSearchControls(int searchScope, boolean returni /** * Make sure the returnObjFlag is set in the supplied SearchControls. Set it * and log if it's not set. - * + * * @param controls the SearchControls to check. */ private void assureReturnObjFlagSet(SearchControls controls) { @@ -1266,7 +1269,7 @@ public void preProcess(DirContext ctx) { /** * A {@link NameClassPairCallbackHandler} that passes the NameClassPairs * found to a NameClassPairMapper and collects the results in a list. - * + * * @author Mattias Hellborg Arthursson */ public final static class MappingCollectingNameClassPairCallbackHandler extends @@ -1811,9 +1814,19 @@ public List findAll(Name base, SearchControls searchControls, final Class */ @Override public List findAll(Class clazz) { - return findAll(LdapUtils.emptyLdapName(), - getDefaultSearchControls(defaultSearchScope, RETURN_OBJ_FLAG, ALL_ATTRIBUTES), - clazz); + Name base = null; + + for (Annotation annotation : clazz.getAnnotations()) { + if (Entry.class.isAssignableFrom(annotation.annotationType())) { + base = LdapUtils.newLdapName(((Entry) annotation).base()); + } + } + + if (base == null) { + base = LdapUtils.emptyLdapName(); + } + + return findAll(base, getDefaultSearchControls(defaultSearchScope, RETURN_OBJ_FLAG, ALL_ATTRIBUTES), clazz); } /** @@ -1832,7 +1845,7 @@ public List find(Name base, Filter filter, SearchControls searchControls, // extend search controls with the attributes to return String[] attributes = odm.manageClass(clazz); searchControls.setReturningAttributes(attributes); - + if (LOG.isDebugEnabled()) { LOG.debug(String.format("Searching - base=%1$s, finalFilter=%2$s, scope=%3$s", base, finalFilter, searchControls)); }