Skip to content

Commit 30efe4e

Browse files
committed
Add LdapTemplate to LdapClient Adapter
Closes gh-774
1 parent cb083d6 commit 30efe4e

File tree

6 files changed

+75
-6
lines changed

6 files changed

+75
-6
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -70,6 +70,8 @@ class DefaultLdapClient implements LdapClient {
7070

7171
private static final boolean RETURN_OBJ_FLAG = true;
7272

73+
private final Builder builder;
74+
7375
private final ContextSource contextSource;
7476

7577
private final Supplier<SearchControls> searchControlsSupplier;
@@ -80,9 +82,11 @@ class DefaultLdapClient implements LdapClient {
8082

8183
private boolean ignoreSizeLimitExceededException = true;
8284

83-
DefaultLdapClient(ContextSource contextSource, Supplier<SearchControls> searchControlsSupplier) {
85+
DefaultLdapClient(ContextSource contextSource, Supplier<SearchControls> searchControlsSupplier,
86+
LdapClient.Builder builder) {
8487
this.contextSource = contextSource;
8588
this.searchControlsSupplier = searchControlsSupplier;
89+
this.builder = builder;
8690
}
8791

8892
@Override
@@ -153,7 +157,7 @@ public UnbindSpec unbind(Name name) {
153157
*/
154158
@Override
155159
public Builder mutate() {
156-
return new DefaultLdapClientBuilder(this.contextSource, this.searchControlsSupplier);
160+
return this.builder;
157161
}
158162

159163
/**

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,20 @@ class DefaultLdapClientBuilder implements LdapClient.Builder {
4747
this.searchControlsSupplier = searchControlsSupplier;
4848
}
4949

50+
DefaultLdapClientBuilder(LdapTemplate ldap) {
51+
this.contextSource = ldap.getContextSource();
52+
this.searchControlsSupplier = () -> {
53+
SearchControls controls = new SearchControls();
54+
controls.setSearchScope(ldap.getDefaultSearchScope());
55+
controls.setCountLimit(ldap.getDefaultCountLimit());
56+
controls.setTimeLimit(ldap.getDefaultTimeLimit());
57+
return controls;
58+
};
59+
this.ignoreNameNotFoundException = ldap.isIgnoreNameNotFoundException();
60+
this.ignoreSizeLimitExceededException = ldap.isIgnoreSizeLimitExceededException();
61+
this.ignorePartialResultException = ldap.isIgnorePartialResultException();
62+
}
63+
5064
@Override
5165
public DefaultLdapClientBuilder contextSource(ContextSource contextSource) {
5266
this.contextSource = contextSource;
@@ -99,7 +113,7 @@ public DefaultLdapClientBuilder clone() {
99113

100114
@Override
101115
public LdapClient build() {
102-
DefaultLdapClient client = new DefaultLdapClient(this.contextSource, this.searchControlsSupplier);
116+
DefaultLdapClient client = new DefaultLdapClient(this.contextSource, this.searchControlsSupplier, this);
103117
client.setIgnorePartialResultException(this.ignorePartialResultException);
104118
client.setIgnoreSizeLimitExceededException(this.ignoreSizeLimitExceededException);
105119
client.setIgnoreNameNotFoundException(this.ignoreNameNotFoundException);

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2005-2023 the original author or authors.
2+
* Copyright 2005-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -167,6 +167,16 @@ static LdapClient create(ContextSource contextSource) {
167167
return new DefaultLdapClientBuilder().contextSource(contextSource).build();
168168
}
169169

170+
/**
171+
* Create an instance of {@link LdapClient}
172+
* @param ldap the {@link LdapTemplate} to base this client off of
173+
* @since 3.3
174+
* @see #builder()
175+
*/
176+
static LdapClient create(LdapTemplate ldap) {
177+
return new DefaultLdapClientBuilder(ldap).build();
178+
}
179+
170180
/**
171181
* Obtain a {@code LdapClient} builder.
172182
*/

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ public void setIgnoreNameNotFoundException(boolean ignore) {
166166
this.ignoreNameNotFoundException = ignore;
167167
}
168168

169+
boolean isIgnoreNameNotFoundException() {
170+
return this.ignoreNameNotFoundException;
171+
}
172+
169173
/**
170174
* Specify whether <code>PartialResultException</code> should be ignored in searches.
171175
* AD servers typically have a problem with referrals. Normally a referral should be
@@ -182,6 +186,10 @@ public void setIgnorePartialResultException(boolean ignore) {
182186
this.ignorePartialResultException = ignore;
183187
}
184188

189+
boolean isIgnorePartialResultException() {
190+
return this.ignorePartialResultException;
191+
}
192+
185193
/**
186194
* Specify whether <code>SizeLimitExceededException</code> should be ignored in
187195
* searches. This is typically what you want if you specify count limit in your search
@@ -194,6 +202,10 @@ public void setIgnoreSizeLimitExceededException(boolean ignore) {
194202
this.ignoreSizeLimitExceededException = ignore;
195203
}
196204

205+
boolean isIgnoreSizeLimitExceededException() {
206+
return this.ignoreSizeLimitExceededException;
207+
}
208+
197209
/**
198210
* Set the default scope to be used in searches if not explicitly specified. Default
199211
* is {@link javax.naming.directory.SearchControls#SUBTREE_SCOPE}.
@@ -206,6 +218,10 @@ public void setDefaultSearchScope(int defaultSearchScope) {
206218
this.defaultSearchScope = defaultSearchScope;
207219
}
208220

221+
int getDefaultSearchScope() {
222+
return this.defaultSearchScope;
223+
}
224+
209225
/**
210226
* Set the default time limit be used in searches if not explicitly specified. Default
211227
* is 0, indicating no time limit.
@@ -216,6 +232,10 @@ public void setDefaultTimeLimit(int defaultTimeLimit) {
216232
this.defaultTimeLimit = defaultTimeLimit;
217233
}
218234

235+
int getDefaultTimeLimit() {
236+
return this.defaultTimeLimit;
237+
}
238+
219239
/**
220240
* Set the default count limit be used in searches if not explicitly specified.
221241
* Default is 0, indicating no count limit.
@@ -226,6 +246,10 @@ public void setDefaultCountLimit(int defaultCountLimit) {
226246
this.defaultCountLimit = defaultCountLimit;
227247
}
228248

249+
int getDefaultCountLimit() {
250+
return this.defaultCountLimit;
251+
}
252+
229253
/**
230254
* {@inheritDoc}
231255
*/

core/src/test/java/org/springframework/ldap/core/DefaultLdapClientTests.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2005-2023 the original author or authors.
2+
* Copyright 2005-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -645,6 +645,21 @@ public void testAuthenticateWithFailedAuthenticationShouldFail() throws Exceptio
645645
verify(this.dirContextMock).close();
646646
}
647647

648+
@Test
649+
public void createWhenLdapTemplateThenUses() {
650+
LdapTemplate ldap = mock(LdapTemplate.class);
651+
given(ldap.getContextSource()).willReturn(this.contextSourceMock);
652+
given(this.contextSourceMock.getReadOnlyContext()).willReturn(this.dirContextMock);
653+
LdapClient.create(ldap).search().name("dc=name").toEntryStream();
654+
verify(ldap).getContextSource();
655+
verify(ldap).isIgnoreNameNotFoundException();
656+
verify(ldap).isIgnorePartialResultException();
657+
verify(ldap).isIgnoreSizeLimitExceededException();
658+
verify(ldap).getDefaultCountLimit();
659+
verify(ldap).getDefaultSearchScope();
660+
verify(ldap).getDefaultTimeLimit();
661+
}
662+
648663
private void noSearchResults(SearchControls controls) throws Exception {
649664
given(this.dirContextMock.search(eq(this.nameMock), eq("(ou=somevalue)"),
650665
argThat(new SearchControlsMatcher(controls))))

modules/ROOT/pages/introduction.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ The following list briefly describes the most important changes in Spring LDAP 2
214214
* The https://github.com/spring-projects/spring-ldap/tree/main/samples[samples] have been polished and updated to make use of the features in 2.0.
215215
Quite a bit of effort has been put into providing a useful example of an https://github.com/spring-projects/spring-ldap/tree/main/samples/user-admin[LDAP user management application].
216216

217+
* Added `LdapClient.create(LdapTemplate)` to simplify constructing an `LdapClient` from an `LdapTemplate`
218+
217219
[[spring-ldap-packaging-overview]]
218220
== Packaging Overview
219221

0 commit comments

Comments
 (0)