View Javadoc
1   /*
2    * Copyright 2019-2026 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.bremersee.ldaptive;
18  
19  import static org.springframework.util.ObjectUtils.isEmpty;
20  
21  import java.util.Arrays;
22  import java.util.Collection;
23  import java.util.Collections;
24  import java.util.Objects;
25  import java.util.Optional;
26  import java.util.stream.Stream;
27  import org.ldaptive.AddRequest;
28  import org.ldaptive.BindConnectionInitializer;
29  import org.ldaptive.BindRequest;
30  import org.ldaptive.CompareRequest;
31  import org.ldaptive.ConnectionFactory;
32  import org.ldaptive.ConnectionInitializer;
33  import org.ldaptive.DeleteRequest;
34  import org.ldaptive.LdapEntry;
35  import org.ldaptive.ModifyDnRequest;
36  import org.ldaptive.ModifyRequest;
37  import org.ldaptive.SearchRequest;
38  import org.ldaptive.SearchResponse;
39  import org.ldaptive.extended.ExtendedRequest;
40  import org.ldaptive.extended.ExtendedResponse;
41  import org.ldaptive.extended.PasswordModifyRequest;
42  import org.ldaptive.extended.PasswordModifyResponseParser;
43  
44  /**
45   * The interface for ldap operations.
46   *
47   * @author Christian Bremer
48   */
49  public interface LdaptiveOperations {
50  
51    /**
52     * Gets connection factory.
53     *
54     * @return the connection factory
55     */
56    ConnectionFactory getConnectionFactory();
57  
58    /**
59     * Returns a new instance of this ldap operations with the same connection factory and error
60     * handler.
61     *
62     * @return a new instance of this ldap operations
63     */
64    LdaptiveOperations copy();
65  
66    /**
67     * Returns a new instance of this ldap operations with the same connection factory and the given
68     * error handler.
69     *
70     * @param errorHandler the new error handler
71     * @return the new instance of this ldap operations
72     */
73    LdaptiveOperations copy(LdaptiveErrorHandler errorHandler);
74  
75    /**
76     * Determines whether a bind connection initializer is used or not.
77     *
78     * @return {@code true} if there is a bind connection initializer, otherwise {@code false}
79     */
80    default boolean hasBindConnectionInitializer() {
81      ConnectionInitializer[] initializers = getConnectionFactory().getConnectionConfig()
82          .getConnectionInitializers();
83      if (isEmpty(initializers)) {
84        return false;
85      }
86      return Arrays.stream(initializers)
87          .anyMatch(BindConnectionInitializer.class::isInstance);
88    }
89  
90  
91    /**
92     * Executes add operation.
93     *
94     * @param addRequest the add request
95     */
96    void add(AddRequest addRequest);
97  
98    /**
99     * Executes bind operation.
100    *
101    * @param request the request
102    * @return the boolean
103    */
104   boolean bind(BindRequest request);
105 
106   /**
107    * Executes compare operation.
108    *
109    * @param request the request
110    * @return the boolean
111    */
112   boolean compare(CompareRequest request);
113 
114   /**
115    * Executes delete operation.
116    *
117    * @param request the request
118    */
119   void delete(DeleteRequest request);
120 
121   /**
122    * Executes an extended request.
123    *
124    * @param request the request
125    * @return the extended response
126    */
127   ExtendedResponse executeExtension(ExtendedRequest request);
128 
129   /**
130    * Generate user password.
131    *
132    * @param dn the dn
133    * @return the generated user password
134    */
135   default String generateUserPassword(String dn) {
136     ExtendedResponse response = executeExtension(new PasswordModifyRequest(dn));
137     return PasswordModifyResponseParser.parse(response);
138   }
139 
140   /**
141    * Executes modify operation.
142    *
143    * @param request the request
144    */
145   void modify(ModifyRequest request);
146 
147   /**
148    * Executes modify dn operation.
149    *
150    * @param request the request
151    */
152   void modifyDn(ModifyDnRequest request);
153 
154   /**
155    * Modifies user password.
156    *
157    * @param dn the dn
158    * @param oldPass the old pass
159    * @param newPass the new pass
160    */
161   default void modifyUserPassword(String dn, String oldPass,
162       String newPass) {
163     executeExtension(new PasswordModifyRequest(dn, oldPass, newPass));
164   }
165 
166   /**
167    * Executes search operation.
168    *
169    * @param request the request
170    * @return the search response
171    */
172   SearchResponse search(SearchRequest request);
173 
174   /**
175    * Find one.
176    *
177    * @param request the request
178    * @return the optional ldap entry
179    */
180   default Optional<LdapEntry> findOne(SearchRequest request) {
181     return Optional.ofNullable(search(request))
182         .map(SearchResponse::getEntry);
183   }
184 
185   /**
186    * Find one.
187    *
188    * @param <T> the type parameter
189    * @param request the request
190    * @param entryMapper the entry mapper
191    * @return the optional domain object
192    */
193   default <T> Optional<T> findOne(
194       SearchRequest request,
195       LdaptiveEntryMapper<T> entryMapper) {
196     return findOne(request)
197         .map(entryMapper::map);
198   }
199 
200   /**
201    * Find all.
202    *
203    * @param request the request
204    * @return the collection
205    */
206   default Collection<LdapEntry> findAll(SearchRequest request) {
207     return Optional.ofNullable(search(request))
208         .map(SearchResponse::getEntries)
209         .orElseGet(Collections::emptyList);
210   }
211 
212   /**
213    * Find all.
214    *
215    * @param <T> the type parameter
216    * @param request the request
217    * @param entryMapper the entry mapper
218    * @return the stream
219    */
220   default <T> Stream<T> findAll(
221       SearchRequest request,
222       LdaptiveEntryMapper<T> entryMapper) {
223     return Optional.ofNullable(search(request))
224         .map(SearchResponse::getEntries)
225         .orElseGet(Collections::emptyList)
226         .stream()
227         .map(entryMapper::map);
228   }
229 
230   /**
231    * Exists.
232    *
233    * @param dn the dn
234    * @return the boolean
235    */
236   boolean exists(String dn);
237 
238   /**
239    * Exists.
240    *
241    * @param <T> the type parameter
242    * @param domainObject the domain object
243    * @param entryMapper the entry mapper
244    * @return the boolean
245    */
246   default <T> boolean exists(T domainObject, LdaptiveEntryMapper<T> entryMapper) {
247     return exists(entryMapper.mapDn(domainObject));
248   }
249 
250   /**
251    * Save t.
252    *
253    * @param <T> the type parameter
254    * @param domainObject the domain object
255    * @param entryMapper the entry mapper
256    * @return the t
257    */
258   <T> T save(T domainObject, LdaptiveEntryMapper<T> entryMapper);
259 
260   /**
261    * Save all stream.
262    *
263    * @param <T> the type parameter
264    * @param domainObjects the domain objects
265    * @param entryMapper the entry mapper
266    * @return the stream
267    */
268   default <T> Stream<T> saveAll(
269       Collection<T> domainObjects,
270       LdaptiveEntryMapper<T> entryMapper) {
271 
272     return Stream.ofNullable(domainObjects)
273         .flatMap(Collection::stream)
274         .filter(Objects::nonNull)
275         .map(domainObject -> save(domainObject, entryMapper));
276   }
277 
278   /**
279    * Remove.
280    *
281    * @param <T> the type parameter
282    * @param domainObject the domain object
283    * @param entryMapper the entry mapper
284    */
285   default <T> void remove(
286       T domainObject,
287       LdaptiveEntryMapper<T> entryMapper) {
288     delete(DeleteRequest.builder().dn(entryMapper.mapDn(domainObject)).build());
289   }
290 
291   /**
292    * Remove all.
293    *
294    * @param <T> the type parameter
295    * @param domainObjects the domain objects
296    * @param entryMapper the entry mapper
297    */
298   default <T> void removeAll(
299       Collection<T> domainObjects,
300       LdaptiveEntryMapper<T> entryMapper) {
301     Optional.ofNullable(domainObjects)
302         .ifPresent(col -> col.forEach(domainObject -> remove(domainObject, entryMapper)));
303   }
304 
305 }