Skip to content

Commit

Permalink
Add LdapClient
Browse files Browse the repository at this point in the history
  • Loading branch information
jzheaux committed Mar 16, 2023
1 parent 19f0ecc commit 8f48118
Show file tree
Hide file tree
Showing 21 changed files with 4,784 additions and 128 deletions.
569 changes: 569 additions & 0 deletions core/src/main/java/org/springframework/ldap/core/LdapClient.java

Large diffs are not rendered by default.

682 changes: 682 additions & 0 deletions core/src/main/java/org/springframework/ldap/core/SpringLdapClient.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package org.springframework.ldap.core;

import java.util.function.Consumer;
import java.util.function.Supplier;

import javax.naming.NameNotFoundException;
import javax.naming.SizeLimitExceededException;
import javax.naming.directory.SearchControls;

class SpringLdapClientBuilder implements LdapClient.Builder {
private ContextSource contextSource;

private Supplier<SearchControls> searchControlsSupplier = () -> {
SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
controls.setCountLimit(0);
controls.setTimeLimit(0);
return controls;
};

private boolean ignorePartialResultException = false;

private boolean ignoreNameNotFoundException = false;

private boolean ignoreSizeLimitExceededException = true;

SpringLdapClientBuilder() {}

SpringLdapClientBuilder(ContextSource contextSource,
Supplier<SearchControls> searchControlsSupplier) {
this.contextSource = contextSource;
this.searchControlsSupplier = searchControlsSupplier;
}

@Override
public SpringLdapClientBuilder contextSource(ContextSource contextSource) {
this.contextSource = contextSource;
return this;
}

@Override
public SpringLdapClientBuilder defaultSearchControls(Supplier<SearchControls> searchControlsSupplier) {
this.searchControlsSupplier = searchControlsSupplier;
return this;
}

/**
* {@inheritDoc}
*/
@Override
public LdapClient.Builder ignorePartialResultException(boolean ignore) {
this.ignorePartialResultException = ignore;
return this;
}

/**
* {@inheritDoc}
*/
@Override
public LdapClient.Builder ignoreNameNotFoundException(boolean ignore) {
this.ignoreNameNotFoundException = ignore;
return this;
}

/**
* {@inheritDoc}
*/
@Override
public LdapClient.Builder ignoreSizeLimitExceededException(boolean ignore) {
this.ignoreSizeLimitExceededException = ignore;
return this;
}

@Override
public SpringLdapClientBuilder apply(Consumer<LdapClient.Builder> builderConsumer) {
builderConsumer.accept(this);
return this;
}

@Override
public SpringLdapClientBuilder clone() {
return new SpringLdapClientBuilder(this.contextSource, this.searchControlsSupplier);
}

@Override
public LdapClient build() {
SpringLdapClient client = new SpringLdapClient(this.contextSource, this.searchControlsSupplier);
client.setIgnorePartialResultException(this.ignorePartialResultException);
client.setIgnoreSizeLimitExceededException(this.ignoreSizeLimitExceededException);
client.setIgnoreNameNotFoundException(this.ignoreNameNotFoundException);
return client;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public final class LdapQueryBuilder implements LdapQuery {

private DefaultContainerCriteria rootContainer = null;

private boolean isFilterStarted = false;

/**
* Not to be instantiated directly - use static query() method.
*/
Expand All @@ -79,15 +81,20 @@ public static LdapQueryBuilder query() {
}

/**
* Construct a new LdapQueryBuilder based on an existing {@link LdapQuery}
* All non-filter fields are copied.
* Construct a new {@link LdapQueryBuilder} based on an existing {@link LdapQuery}
* All fields are copied, including giving the query a default filter.
*
* <p>
* Note that all filter invariants are still enforced; an application cannot specify
* any non-filter values after it specifies a filter.
*
* @return a new instance.
* @since 3.0
*/
public static LdapQueryBuilder fromQuery(LdapQuery query) {
LdapQueryBuilder builder = LdapQueryBuilder.query()
.attributes(query.attributes())
.base(query.base());
LdapQueryBuilder builder = new LdapQueryBuilder();
builder.rootContainer = new DefaultContainerCriteria(builder).append(query.filter());
builder.attributes(query.attributes()).base(query.base());
if (query.countLimit() != null) {
builder.countLimit(query.countLimit());
}
Expand Down Expand Up @@ -184,6 +191,7 @@ public ConditionCriteria where(String attribute) {
private void initRootContainer() {
assertFilterNotStarted();
rootContainer = new DefaultContainerCriteria(this);
isFilterStarted = true;
}

/**
Expand Down Expand Up @@ -237,7 +245,7 @@ public LdapQuery filter(String filterFormat, Object... params) {
}

private void assertFilterNotStarted() {
if(rootContainer != null) {
if (isFilterStarted) {
throw new IllegalStateException("Invalid operation - filter condition specification already started");
}
}
Expand Down
Loading

0 comments on commit 8f48118

Please sign in to comment.