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