1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
46
47
48
49 public interface LdaptiveOperations {
50
51
52
53
54
55
56 ConnectionFactory getConnectionFactory();
57
58
59
60
61
62
63
64 LdaptiveOperations copy();
65
66
67
68
69
70
71
72
73 LdaptiveOperations copy(LdaptiveErrorHandler errorHandler);
74
75
76
77
78
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
93
94
95
96 void add(AddRequest addRequest);
97
98
99
100
101
102
103
104 boolean bind(BindRequest request);
105
106
107
108
109
110
111
112 boolean compare(CompareRequest request);
113
114
115
116
117
118
119 void delete(DeleteRequest request);
120
121
122
123
124
125
126
127 ExtendedResponse executeExtension(ExtendedRequest request);
128
129
130
131
132
133
134
135 default String generateUserPassword(String dn) {
136 ExtendedResponse response = executeExtension(new PasswordModifyRequest(dn));
137 return PasswordModifyResponseParser.parse(response);
138 }
139
140
141
142
143
144
145 void modify(ModifyRequest request);
146
147
148
149
150
151
152 void modifyDn(ModifyDnRequest request);
153
154
155
156
157
158
159
160
161 default void modifyUserPassword(String dn, String oldPass,
162 String newPass) {
163 executeExtension(new PasswordModifyRequest(dn, oldPass, newPass));
164 }
165
166
167
168
169
170
171
172 SearchResponse search(SearchRequest request);
173
174
175
176
177
178
179
180 default Optional<LdapEntry> findOne(SearchRequest request) {
181 return Optional.ofNullable(search(request))
182 .map(SearchResponse::getEntry);
183 }
184
185
186
187
188
189
190
191
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
202
203
204
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
214
215
216
217
218
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
232
233
234
235
236 boolean exists(String dn);
237
238
239
240
241
242
243
244
245
246 default <T> boolean exists(T domainObject, LdaptiveEntryMapper<T> entryMapper) {
247 return exists(entryMapper.mapDn(domainObject));
248 }
249
250
251
252
253
254
255
256
257
258 <T> T save(T domainObject, LdaptiveEntryMapper<T> entryMapper);
259
260
261
262
263
264
265
266
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
280
281
282
283
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
293
294
295
296
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 }