Skip to content

Commit

Permalink
Add LdapTemplate to LdapClient Adapter
Browse files Browse the repository at this point in the history
Closes gh-774
  • Loading branch information
jzheaux committed Feb 12, 2025
1 parent cb083d6 commit 30efe4e
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -70,6 +70,8 @@ class DefaultLdapClient implements LdapClient {

private static final boolean RETURN_OBJ_FLAG = true;

private final Builder builder;

private final ContextSource contextSource;

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

private boolean ignoreSizeLimitExceededException = true;

DefaultLdapClient(ContextSource contextSource, Supplier<SearchControls> searchControlsSupplier) {
DefaultLdapClient(ContextSource contextSource, Supplier<SearchControls> searchControlsSupplier,
LdapClient.Builder builder) {
this.contextSource = contextSource;
this.searchControlsSupplier = searchControlsSupplier;
this.builder = builder;
}

@Override
Expand Down Expand Up @@ -153,7 +157,7 @@ public UnbindSpec unbind(Name name) {
*/
@Override
public Builder mutate() {
return new DefaultLdapClientBuilder(this.contextSource, this.searchControlsSupplier);
return this.builder;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ class DefaultLdapClientBuilder implements LdapClient.Builder {
this.searchControlsSupplier = searchControlsSupplier;
}

DefaultLdapClientBuilder(LdapTemplate ldap) {
this.contextSource = ldap.getContextSource();
this.searchControlsSupplier = () -> {
SearchControls controls = new SearchControls();
controls.setSearchScope(ldap.getDefaultSearchScope());
controls.setCountLimit(ldap.getDefaultCountLimit());
controls.setTimeLimit(ldap.getDefaultTimeLimit());
return controls;
};
this.ignoreNameNotFoundException = ldap.isIgnoreNameNotFoundException();
this.ignoreSizeLimitExceededException = ldap.isIgnoreSizeLimitExceededException();
this.ignorePartialResultException = ldap.isIgnorePartialResultException();
}

@Override
public DefaultLdapClientBuilder contextSource(ContextSource contextSource) {
this.contextSource = contextSource;
Expand Down Expand Up @@ -99,7 +113,7 @@ public DefaultLdapClientBuilder clone() {

@Override
public LdapClient build() {
DefaultLdapClient client = new DefaultLdapClient(this.contextSource, this.searchControlsSupplier);
DefaultLdapClient client = new DefaultLdapClient(this.contextSource, this.searchControlsSupplier, this);
client.setIgnorePartialResultException(this.ignorePartialResultException);
client.setIgnoreSizeLimitExceededException(this.ignoreSizeLimitExceededException);
client.setIgnoreNameNotFoundException(this.ignoreNameNotFoundException);
Expand Down
12 changes: 11 additions & 1 deletion core/src/main/java/org/springframework/ldap/core/LdapClient.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2005-2023 the original author or authors.
* Copyright 2005-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -167,6 +167,16 @@ static LdapClient create(ContextSource contextSource) {
return new DefaultLdapClientBuilder().contextSource(contextSource).build();
}

/**
* Create an instance of {@link LdapClient}
* @param ldap the {@link LdapTemplate} to base this client off of
* @since 3.3
* @see #builder()
*/
static LdapClient create(LdapTemplate ldap) {
return new DefaultLdapClientBuilder(ldap).build();
}

/**
* Obtain a {@code LdapClient} builder.
*/
Expand Down
24 changes: 24 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 @@ -166,6 +166,10 @@ public void setIgnoreNameNotFoundException(boolean ignore) {
this.ignoreNameNotFoundException = ignore;
}

boolean isIgnoreNameNotFoundException() {
return this.ignoreNameNotFoundException;
}

/**
* Specify whether <code>PartialResultException</code> should be ignored in searches.
* AD servers typically have a problem with referrals. Normally a referral should be
Expand All @@ -182,6 +186,10 @@ public void setIgnorePartialResultException(boolean ignore) {
this.ignorePartialResultException = ignore;
}

boolean isIgnorePartialResultException() {
return this.ignorePartialResultException;
}

/**
* Specify whether <code>SizeLimitExceededException</code> should be ignored in
* searches. This is typically what you want if you specify count limit in your search
Expand All @@ -194,6 +202,10 @@ public void setIgnoreSizeLimitExceededException(boolean ignore) {
this.ignoreSizeLimitExceededException = ignore;
}

boolean isIgnoreSizeLimitExceededException() {
return this.ignoreSizeLimitExceededException;
}

/**
* Set the default scope to be used in searches if not explicitly specified. Default
* is {@link javax.naming.directory.SearchControls#SUBTREE_SCOPE}.
Expand All @@ -206,6 +218,10 @@ public void setDefaultSearchScope(int defaultSearchScope) {
this.defaultSearchScope = defaultSearchScope;
}

int getDefaultSearchScope() {
return this.defaultSearchScope;
}

/**
* Set the default time limit be used in searches if not explicitly specified. Default
* is 0, indicating no time limit.
Expand All @@ -216,6 +232,10 @@ public void setDefaultTimeLimit(int defaultTimeLimit) {
this.defaultTimeLimit = defaultTimeLimit;
}

int getDefaultTimeLimit() {
return this.defaultTimeLimit;
}

/**
* Set the default count limit be used in searches if not explicitly specified.
* Default is 0, indicating no count limit.
Expand All @@ -226,6 +246,10 @@ public void setDefaultCountLimit(int defaultCountLimit) {
this.defaultCountLimit = defaultCountLimit;
}

int getDefaultCountLimit() {
return this.defaultCountLimit;
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2005-2023 the original author or authors.
* Copyright 2005-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -645,6 +645,21 @@ public void testAuthenticateWithFailedAuthenticationShouldFail() throws Exceptio
verify(this.dirContextMock).close();
}

@Test
public void createWhenLdapTemplateThenUses() {
LdapTemplate ldap = mock(LdapTemplate.class);
given(ldap.getContextSource()).willReturn(this.contextSourceMock);
given(this.contextSourceMock.getReadOnlyContext()).willReturn(this.dirContextMock);
LdapClient.create(ldap).search().name("dc=name").toEntryStream();
verify(ldap).getContextSource();
verify(ldap).isIgnoreNameNotFoundException();
verify(ldap).isIgnorePartialResultException();
verify(ldap).isIgnoreSizeLimitExceededException();
verify(ldap).getDefaultCountLimit();
verify(ldap).getDefaultSearchScope();
verify(ldap).getDefaultTimeLimit();
}

private void noSearchResults(SearchControls controls) throws Exception {
given(this.dirContextMock.search(eq(this.nameMock), eq("(ou=somevalue)"),
argThat(new SearchControlsMatcher(controls))))
Expand Down
2 changes: 2 additions & 0 deletions modules/ROOT/pages/introduction.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ The following list briefly describes the most important changes in Spring LDAP 2
* 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.
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].

* Added `LdapClient.create(LdapTemplate)` to simplify constructing an `LdapClient` from an `LdapTemplate`

[[spring-ldap-packaging-overview]]
== Packaging Overview

Expand Down

0 comments on commit 30efe4e

Please sign in to comment.