Skip to content

Commit 8f48118

Browse files
committed
Add LdapClient
Closes spring-projectsgh-675
1 parent 19f0ecc commit 8f48118

21 files changed

+4784
-128
lines changed

core/src/main/java/org/springframework/ldap/core/LdapClient.java

Lines changed: 569 additions & 0 deletions
Large diffs are not rendered by default.

core/src/main/java/org/springframework/ldap/core/SpringLdapClient.java

Lines changed: 682 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package org.springframework.ldap.core;
2+
3+
import java.util.function.Consumer;
4+
import java.util.function.Supplier;
5+
6+
import javax.naming.NameNotFoundException;
7+
import javax.naming.SizeLimitExceededException;
8+
import javax.naming.directory.SearchControls;
9+
10+
class SpringLdapClientBuilder implements LdapClient.Builder {
11+
private ContextSource contextSource;
12+
13+
private Supplier<SearchControls> searchControlsSupplier = () -> {
14+
SearchControls controls = new SearchControls();
15+
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
16+
controls.setCountLimit(0);
17+
controls.setTimeLimit(0);
18+
return controls;
19+
};
20+
21+
private boolean ignorePartialResultException = false;
22+
23+
private boolean ignoreNameNotFoundException = false;
24+
25+
private boolean ignoreSizeLimitExceededException = true;
26+
27+
SpringLdapClientBuilder() {}
28+
29+
SpringLdapClientBuilder(ContextSource contextSource,
30+
Supplier<SearchControls> searchControlsSupplier) {
31+
this.contextSource = contextSource;
32+
this.searchControlsSupplier = searchControlsSupplier;
33+
}
34+
35+
@Override
36+
public SpringLdapClientBuilder contextSource(ContextSource contextSource) {
37+
this.contextSource = contextSource;
38+
return this;
39+
}
40+
41+
@Override
42+
public SpringLdapClientBuilder defaultSearchControls(Supplier<SearchControls> searchControlsSupplier) {
43+
this.searchControlsSupplier = searchControlsSupplier;
44+
return this;
45+
}
46+
47+
/**
48+
* {@inheritDoc}
49+
*/
50+
@Override
51+
public LdapClient.Builder ignorePartialResultException(boolean ignore) {
52+
this.ignorePartialResultException = ignore;
53+
return this;
54+
}
55+
56+
/**
57+
* {@inheritDoc}
58+
*/
59+
@Override
60+
public LdapClient.Builder ignoreNameNotFoundException(boolean ignore) {
61+
this.ignoreNameNotFoundException = ignore;
62+
return this;
63+
}
64+
65+
/**
66+
* {@inheritDoc}
67+
*/
68+
@Override
69+
public LdapClient.Builder ignoreSizeLimitExceededException(boolean ignore) {
70+
this.ignoreSizeLimitExceededException = ignore;
71+
return this;
72+
}
73+
74+
@Override
75+
public SpringLdapClientBuilder apply(Consumer<LdapClient.Builder> builderConsumer) {
76+
builderConsumer.accept(this);
77+
return this;
78+
}
79+
80+
@Override
81+
public SpringLdapClientBuilder clone() {
82+
return new SpringLdapClientBuilder(this.contextSource, this.searchControlsSupplier);
83+
}
84+
85+
@Override
86+
public LdapClient build() {
87+
SpringLdapClient client = new SpringLdapClient(this.contextSource, this.searchControlsSupplier);
88+
client.setIgnorePartialResultException(this.ignorePartialResultException);
89+
client.setIgnoreSizeLimitExceededException(this.ignoreSizeLimitExceededException);
90+
client.setIgnoreNameNotFoundException(this.ignoreNameNotFoundException);
91+
return client;
92+
}
93+
}

core/src/main/java/org/springframework/ldap/query/LdapQueryBuilder.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ public final class LdapQueryBuilder implements LdapQuery {
6262

6363
private DefaultContainerCriteria rootContainer = null;
6464

65+
private boolean isFilterStarted = false;
66+
6567
/**
6668
* Not to be instantiated directly - use static query() method.
6769
*/
@@ -79,15 +81,20 @@ public static LdapQueryBuilder query() {
7981
}
8082

8183
/**
82-
* Construct a new LdapQueryBuilder based on an existing {@link LdapQuery}
83-
* All non-filter fields are copied.
84+
* Construct a new {@link LdapQueryBuilder} based on an existing {@link LdapQuery}
85+
* All fields are copied, including giving the query a default filter.
86+
*
87+
* <p>
88+
* Note that all filter invariants are still enforced; an application cannot specify
89+
* any non-filter values after it specifies a filter.
90+
*
8491
* @return a new instance.
8592
* @since 3.0
8693
*/
8794
public static LdapQueryBuilder fromQuery(LdapQuery query) {
88-
LdapQueryBuilder builder = LdapQueryBuilder.query()
89-
.attributes(query.attributes())
90-
.base(query.base());
95+
LdapQueryBuilder builder = new LdapQueryBuilder();
96+
builder.rootContainer = new DefaultContainerCriteria(builder).append(query.filter());
97+
builder.attributes(query.attributes()).base(query.base());
9198
if (query.countLimit() != null) {
9299
builder.countLimit(query.countLimit());
93100
}
@@ -184,6 +191,7 @@ public ConditionCriteria where(String attribute) {
184191
private void initRootContainer() {
185192
assertFilterNotStarted();
186193
rootContainer = new DefaultContainerCriteria(this);
194+
isFilterStarted = true;
187195
}
188196

189197
/**
@@ -237,7 +245,7 @@ public LdapQuery filter(String filterFormat, Object... params) {
237245
}
238246

239247
private void assertFilterNotStarted() {
240-
if(rootContainer != null) {
248+
if (isFilterStarted) {
241249
throw new IllegalStateException("Invalid operation - filter condition specification already started");
242250
}
243251
}

0 commit comments

Comments
 (0)