Skip to content

fixes #31 #411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1775,6 +1775,22 @@ boolean authenticate(String base, String filter, String password,
*/
<T> List<T> findAll(Class<T> 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 <T> 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
*/

<T> List<T> findAll(Name base, Class<T> 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.
Expand Down
20 changes: 20 additions & 0 deletions core/src/main/java/org/springframework/ldap/core/LdapTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -1857,6 +1857,26 @@ public <T> List<T> find(LdapQuery query, Class<T> 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 <T> 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 <T> List<T> findAll(Name base, Class<T> clazz) {
return findAll(base,
getDefaultSearchControls(defaultSearchScope, RETURN_OBJ_FLAG, ALL_ATTRIBUTES),
clazz);
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -38,6 +41,7 @@
public class QueryDslLdapQuery<K> implements FilteredClause<QueryDslLdapQuery<K>> {
private final LdapOperations ldapOperations;
private final Class<? extends K> clazz;
private final String base;

private QueryMixin<QueryDslLdapQuery<K>> queryMixin =
new QueryMixin<QueryDslLdapQuery<K>>(this, new DefaultQueryMetadata().noValidate());
Expand All @@ -53,6 +57,7 @@ public QueryDslLdapQuery(LdapOperations ldapOperations, Class<K> 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
Expand All @@ -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()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public Iterable<T> findAll(Predicate predicate, Sort sort) {
throw new UnsupportedOperationException();
}

private QueryDslLdapQuery<T> queryFor(Predicate predicate) {
protected QueryDslLdapQuery<T> queryFor(Predicate predicate) {
return new QueryDslLdapQuery<T>(getLdapOperations(), getClazz())
.where(predicate);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -45,11 +48,13 @@ public class SimpleLdapRepository<T> implements LdapRepository<T> {
private final LdapOperations ldapOperations;
private final ObjectDirectoryMapper odm;
private final Class<T> clazz;
private final String base;

public SimpleLdapRepository(LdapOperations ldapOperations, ObjectDirectoryMapper odm, Class<T> 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() {
Expand All @@ -64,7 +69,7 @@ protected Class<T> 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();
Expand Down Expand Up @@ -137,7 +142,11 @@ public boolean exists(Name name) {

@Override
public List<T> findAll() {
return ldapOperations.findAll(clazz);
if (StringUtils.hasText(base)) {
return ldapOperations.findAll(LdapUtils.newLdapName(base), clazz);
} else {
return ldapOperations.findAll(clazz);
}
}

@Override
Expand Down