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
30
31
32
33 @Validated
34 public interface ReactiveLdaptiveOperations {
35
36
37
38
39
40
41 @NotNull
42 ConnectionFactory getConnectionFactory();
43
44
45
46
47
48
49
50 Mono<Result> add(@NotNull AddRequest addRequest);
51
52
53
54
55
56
57
58 Mono<Boolean> bind(@NotNull BindRequest bindRequest);
59
60
61
62
63
64
65
66 Mono<Boolean> compare(@NotNull CompareRequest compareRequest);
67
68
69
70
71
72
73
74 Mono<Result> delete(@NotNull DeleteRequest deleteRequest);
75
76
77
78
79
80
81
82 Mono<ExtendedResponse> executeExtension(@NotNull ExtendedRequest request);
83
84
85
86
87
88
89
90 default Mono<String> generateUserPassword(@NotNull String dn) {
91 return executeExtension(new PasswordModifyRequest(dn))
92 .map(PasswordModifyResponseParser::parse);
93 }
94
95
96
97
98
99
100
101 Mono<Result> modify(@NotNull ModifyRequest modifyRequest);
102
103
104
105
106
107
108
109 Mono<Result> modifyDn(@NotNull ModifyDnRequest modifyDnRequest);
110
111
112
113
114
115
116
117
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
128
129
130
131
132 Mono<LdapEntry> findOne(@NotNull SearchRequest searchRequest);
133
134
135
136
137
138
139
140
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
149
150
151
152
153 Flux<LdapEntry> findAll(@NotNull SearchRequest searchRequest);
154
155
156
157
158
159
160
161
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
170
171
172
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
187
188
189
190
191
192
193 default <T> Mono<Boolean> exists(@NotNull T domainObject, @NotNull LdaptiveEntryMapper<T> entryMapper) {
194 return exists(entryMapper.mapDn(domainObject));
195 }
196
197
198
199
200
201
202
203
204
205 <T> Mono<T> save(@NotNull T domainObject, @NotNull LdaptiveEntryMapper<T> entryMapper);
206
207
208
209
210
211
212
213
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
222
223
224
225
226
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
236
237
238
239
240
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 }