diff --git a/core/src/main/java/org/springframework/ldap/core/LdapOperations.java b/core/src/main/java/org/springframework/ldap/core/LdapOperations.java index 38d5e87f7e..28257a056f 100644 --- a/core/src/main/java/org/springframework/ldap/core/LdapOperations.java +++ b/core/src/main/java/org/springframework/ldap/core/LdapOperations.java @@ -1775,6 +1775,22 @@ boolean authenticate(String base, String filter, String password, */ List findAll(Class clazz); + /** + * Find all entries in the LDAP directory of a given type. The referenced class must have object-directory mapping metadata + * specified using {@link org.springframework.ldap.odm.annotations.Entry} and associated annotations. + * + * @param The Java type to return + * @param base The root of the sub-tree at which to begin the search. + * @param clazz The Java type to return + * @return All entries that are of the type represented by the given + * Java class + * + * @throws org.springframework.ldap.NamingException on error. + * @since 3.0 + */ + + List findAll(Name base, Class clazz); + /** * Find all entries in the LDAP directory of a given type. The referenced class must have object-directory mapping metadata * specified using {@link org.springframework.ldap.odm.annotations.Entry} and associated annotations. 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 f149165d03..dfd54c152a 100644 --- a/core/src/main/java/org/springframework/ldap/core/LdapTemplate.java +++ b/core/src/main/java/org/springframework/ldap/core/LdapTemplate.java @@ -1857,6 +1857,26 @@ public List find(LdapQuery query, Class clazz) { return find(query.base(), query.filter(), searchControls, clazz); } + /** + * Find all entries in the LDAP directory of a given type. The referenced class must have object-directory mapping metadata + * specified using {@link org.springframework.ldap.odm.annotations.Entry} and associated annotations. + * + * @param The Java type to return + * @param base The root of the sub-tree at which to begin the search. + * @param clazz The Java type to return + * @return All entries that are of the type represented by the given + * Java class + * + * @throws org.springframework.ldap.NamingException on error. + * @since 3.0 + */ + + public List findAll(Name base, Class clazz) { + return findAll(base, + getDefaultSearchControls(defaultSearchScope, RETURN_OBJ_FLAG, ALL_ATTRIBUTES), + clazz); + } + /** * {@inheritDoc} */ diff --git a/core/src/main/java/org/springframework/ldap/repository/support/QueryDslLdapQuery.java b/core/src/main/java/org/springframework/ldap/repository/support/QueryDslLdapQuery.java index e9092f4844..43fdb690c9 100644 --- a/core/src/main/java/org/springframework/ldap/repository/support/QueryDslLdapQuery.java +++ b/core/src/main/java/org/springframework/ldap/repository/support/QueryDslLdapQuery.java @@ -22,7 +22,10 @@ import com.querydsl.core.types.EntityPath; import com.querydsl.core.types.Predicate; import org.springframework.ldap.core.LdapOperations; +import org.springframework.ldap.odm.annotations.Entry; import org.springframework.ldap.query.LdapQuery; +import org.springframework.ldap.query.LdapQueryBuilder; +import org.springframework.util.StringUtils; import java.util.List; @@ -38,6 +41,7 @@ public class QueryDslLdapQuery implements FilteredClause> { private final LdapOperations ldapOperations; private final Class clazz; + private final String base; private QueryMixin> queryMixin = new QueryMixin>(this, new DefaultQueryMetadata().noValidate()); @@ -53,6 +57,7 @@ public QueryDslLdapQuery(LdapOperations ldapOperations, Class clazz) { this.ldapOperations = ldapOperations; this.clazz = clazz; this.filterGenerator = new LdapSerializer(ldapOperations.getObjectDirectoryMapper(), clazz); + this.base = clazz.getAnnotation(Entry.class) != null ? clazz.getAnnotation(Entry.class).base() : null; } @Override @@ -70,7 +75,14 @@ public K uniqueResult() { } LdapQuery buildQuery() { - return query().filter(filterGenerator.handle(queryMixin.getMetadata().getWhere())); + + LdapQueryBuilder query = query(); + + if (StringUtils.hasText(base)) { + query = query().base(base); + } + + return query.filter(filterGenerator.handle(queryMixin.getMetadata().getWhere())); } } diff --git a/core/src/main/java/org/springframework/ldap/repository/support/QueryDslLdapRepository.java b/core/src/main/java/org/springframework/ldap/repository/support/QueryDslLdapRepository.java index 56914e1cfa..5c5f59366a 100644 --- a/core/src/main/java/org/springframework/ldap/repository/support/QueryDslLdapRepository.java +++ b/core/src/main/java/org/springframework/ldap/repository/support/QueryDslLdapRepository.java @@ -66,7 +66,7 @@ public Iterable findAll(Predicate predicate, Sort sort) { throw new UnsupportedOperationException(); } - private QueryDslLdapQuery queryFor(Predicate predicate) { + protected QueryDslLdapQuery queryFor(Predicate predicate) { return new QueryDslLdapQuery(getLdapOperations(), getClazz()) .where(predicate); } diff --git a/core/src/main/java/org/springframework/ldap/repository/support/SimpleLdapRepository.java b/core/src/main/java/org/springframework/ldap/repository/support/SimpleLdapRepository.java index d17b2af874..ea90ba5850 100644 --- a/core/src/main/java/org/springframework/ldap/repository/support/SimpleLdapRepository.java +++ b/core/src/main/java/org/springframework/ldap/repository/support/SimpleLdapRepository.java @@ -22,10 +22,13 @@ import org.springframework.ldap.core.LdapOperations; import org.springframework.ldap.core.support.CountNameClassPairCallbackHandler; import org.springframework.ldap.filter.Filter; +import org.springframework.ldap.odm.annotations.Entry; import org.springframework.ldap.odm.core.ObjectDirectoryMapper; import org.springframework.ldap.query.LdapQuery; import org.springframework.ldap.repository.LdapRepository; +import org.springframework.ldap.support.LdapUtils; import org.springframework.util.Assert; +import org.springframework.util.StringUtils; import javax.naming.Name; import java.util.Iterator; @@ -45,11 +48,13 @@ public class SimpleLdapRepository implements LdapRepository { private final LdapOperations ldapOperations; private final ObjectDirectoryMapper odm; private final Class clazz; + private final String base; public SimpleLdapRepository(LdapOperations ldapOperations, ObjectDirectoryMapper odm, Class clazz) { this.ldapOperations = ldapOperations; this.odm = odm; this.clazz = clazz; + this.base = clazz.getAnnotation(Entry.class) != null ? clazz.getAnnotation(Entry.class).base() : null; } protected LdapOperations getLdapOperations() { @@ -64,7 +69,7 @@ protected Class getClazz() { public long count() { Filter filter = odm.filterFor(clazz, null); CountNameClassPairCallbackHandler callback = new CountNameClassPairCallbackHandler(); - LdapQuery query = query().attributes(OBJECTCLASS_ATTRIBUTE).filter(filter); + LdapQuery query = query().base(base).attributes(OBJECTCLASS_ATTRIBUTE).filter(filter); ldapOperations.search(query, callback); return callback.getNoOfRows(); @@ -137,7 +142,11 @@ public boolean exists(Name name) { @Override public List findAll() { - return ldapOperations.findAll(clazz); + if (StringUtils.hasText(base)) { + return ldapOperations.findAll(LdapUtils.newLdapName(base), clazz); + } else { + return ldapOperations.findAll(clazz); + } } @Override