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