View Javadoc
1   /*
2    * Copyright 2019 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 java.util.Collection;
20  import java.util.Collections;
21  import java.util.Objects;
22  import java.util.Optional;
23  import java.util.stream.Stream;
24  import org.ldaptive.AddRequest;
25  import org.ldaptive.BindRequest;
26  import org.ldaptive.CompareRequest;
27  import org.ldaptive.ConnectionFactory;
28  import org.ldaptive.DeleteRequest;
29  import org.ldaptive.LdapEntry;
30  import org.ldaptive.ModifyDnRequest;
31  import org.ldaptive.ModifyRequest;
32  import org.ldaptive.SearchRequest;
33  import org.ldaptive.SearchResponse;
34  import org.ldaptive.extended.ExtendedRequest;
35  import org.ldaptive.extended.ExtendedResponse;
36  import org.ldaptive.extended.PasswordModifyRequest;
37  import org.ldaptive.extended.PasswordModifyResponseParser;
38  
39  /**
40   * The interface for ldap operations.
41   *
42   * @author Christian Bremer
43   */
44  public interface LdaptiveOperations {
45  
46    /**
47     * Gets connection factory.
48     *
49     * @return the connection factory
50     */
51    ConnectionFactory getConnectionFactory();
52  
53    /**
54     * Returns a new instance of this ldap operations with the same connection factory and error
55     * handler.
56     *
57     * @return a new instance of this ldap operations
58     */
59    LdaptiveOperations copy();
60  
61    /**
62     * Returns a new instance of this ldap operations with the same connection factory and the given
63     * error handler.
64     *
65     * @param errorHandler the new error handler
66     * @return the new instance of this ldap operations
67     */
68    LdaptiveOperations copy(LdaptiveErrorHandler errorHandler);
69  
70  
71    /**
72     * Executes add operation.
73     *
74     * @param addRequest the add request
75     */
76    void add(AddRequest addRequest);
77  
78    /**
79     * Executes bind operation.
80     *
81     * @param request the request
82     * @return the boolean
83     */
84    boolean bind(BindRequest request);
85  
86    /**
87     * Executes compare operation.
88     *
89     * @param request the request
90     * @return the boolean
91     */
92    boolean compare(CompareRequest request);
93  
94    /**
95     * Executes delete operation.
96     *
97     * @param request the request
98     */
99    void delete(DeleteRequest request);
100 
101   /**
102    * Executes an extended request.
103    *
104    * @param request the request
105    * @return the extended response
106    */
107   ExtendedResponse executeExtension(ExtendedRequest request);
108 
109   /**
110    * Generate user password.
111    *
112    * @param dn the dn
113    * @return the generated user password
114    */
115   default String generateUserPassword(String dn) {
116     ExtendedResponse response = executeExtension(new PasswordModifyRequest(dn));
117     return PasswordModifyResponseParser.parse(response);
118   }
119 
120   /**
121    * Executes modify operation.
122    *
123    * @param request the request
124    */
125   void modify(ModifyRequest request);
126 
127   /**
128    * Executes modify dn operation.
129    *
130    * @param request the request
131    */
132   void modifyDn(ModifyDnRequest request);
133 
134   /**
135    * Modifies user password.
136    *
137    * @param dn the dn
138    * @param oldPass the old pass
139    * @param newPass the new pass
140    */
141   default void modifyUserPassword(String dn, String oldPass,
142       String newPass) {
143     executeExtension(new PasswordModifyRequest(dn, oldPass, newPass));
144   }
145 
146   /**
147    * Executes search operation.
148    *
149    * @param request the request
150    * @return the search response
151    */
152   SearchResponse search(SearchRequest request);
153 
154   /**
155    * Find one.
156    *
157    * @param request the request
158    * @return the optional ldap entry
159    */
160   default Optional<LdapEntry> findOne(SearchRequest request) {
161     return Optional.ofNullable(search(request))
162         .map(SearchResponse::getEntry);
163   }
164 
165   /**
166    * Find one.
167    *
168    * @param <T> the type parameter
169    * @param request the request
170    * @param entryMapper the entry mapper
171    * @return the optional domain object
172    */
173   default <T> Optional<T> findOne(
174       SearchRequest request,
175       LdaptiveEntryMapper<T> entryMapper) {
176     return findOne(request)
177         .map(entryMapper::map);
178   }
179 
180   /**
181    * Find all.
182    *
183    * @param request the request
184    * @return the collection
185    */
186   default Collection<LdapEntry> findAll(SearchRequest request) {
187     return Optional.ofNullable(search(request))
188         .map(SearchResponse::getEntries)
189         .orElseGet(Collections::emptyList);
190   }
191 
192   /**
193    * Find all.
194    *
195    * @param <T> the type parameter
196    * @param request the request
197    * @param entryMapper the entry mapper
198    * @return the stream
199    */
200   default <T> Stream<T> findAll(
201       SearchRequest request,
202       LdaptiveEntryMapper<T> entryMapper) {
203     return Optional.ofNullable(search(request))
204         .map(SearchResponse::getEntries)
205         .orElseGet(Collections::emptyList)
206         .stream()
207         .map(entryMapper::map);
208   }
209 
210   /**
211    * Exists.
212    *
213    * @param dn the dn
214    * @return the boolean
215    */
216   boolean exists(String dn);
217 
218   /**
219    * Exists.
220    *
221    * @param <T> the type parameter
222    * @param domainObject the domain object
223    * @param entryMapper the entry mapper
224    * @return the boolean
225    */
226   default <T> boolean exists(T domainObject, LdaptiveEntryMapper<T> entryMapper) {
227     return exists(entryMapper.mapDn(domainObject));
228   }
229 
230   /**
231    * Save t.
232    *
233    * @param <T> the type parameter
234    * @param domainObject the domain object
235    * @param entryMapper the entry mapper
236    * @return the t
237    */
238   <T> T save(T domainObject, LdaptiveEntryMapper<T> entryMapper);
239 
240   /**
241    * Save all stream.
242    *
243    * @param <T> the type parameter
244    * @param domainObjects the domain objects
245    * @param entryMapper the entry mapper
246    * @return the stream
247    */
248   default <T> Stream<T> saveAll(
249       Collection<T> domainObjects,
250       LdaptiveEntryMapper<T> entryMapper) {
251 
252     return Stream.ofNullable(domainObjects)
253         .flatMap(Collection::stream)
254         .filter(Objects::nonNull)
255         .map(domainObject -> save(domainObject, entryMapper));
256   }
257 
258   /**
259    * Remove.
260    *
261    * @param <T> the type parameter
262    * @param domainObject the domain object
263    * @param entryMapper the entry mapper
264    */
265   default <T> void remove(
266       T domainObject,
267       LdaptiveEntryMapper<T> entryMapper) {
268     delete(DeleteRequest.builder().dn(entryMapper.mapDn(domainObject)).build());
269   }
270 
271   /**
272    * Remove all.
273    *
274    * @param <T> the type parameter
275    * @param domainObjects the domain objects
276    * @param entryMapper the entry mapper
277    */
278   default <T> void removeAll(
279       Collection<T> domainObjects,
280       LdaptiveEntryMapper<T> entryMapper) {
281     Optional.ofNullable(domainObjects)
282         .ifPresent(col -> col.forEach(domainObject -> remove(domainObject, entryMapper)));
283   }
284 
285 }