1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.bremersee.data.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 javax.validation.constraints.NotNull;
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.SearchRequest;
34 import org.ldaptive.SearchResponse;
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 org.springframework.lang.Nullable;
40 import org.springframework.validation.annotation.Validated;
41
42
43
44
45
46
47 @Validated
48 public interface LdaptiveOperations {
49
50
51
52
53
54
55 @NotNull
56 ConnectionFactory getConnectionFactory();
57
58
59
60
61
62
63 void add(@NotNull AddRequest addRequest);
64
65
66
67
68
69
70
71 boolean bind(@NotNull BindRequest request);
72
73
74
75
76
77
78
79 boolean compare(@NotNull CompareRequest request);
80
81
82
83
84
85
86 void delete(@NotNull DeleteRequest request);
87
88
89
90
91
92
93
94 @NotNull
95 ExtendedResponse executeExtension(@NotNull ExtendedRequest request);
96
97
98
99
100
101
102
103 default String generateUserPassword(@NotNull String dn) {
104 ExtendedResponse response = executeExtension(new PasswordModifyRequest(dn));
105 return PasswordModifyResponseParser.parse(response);
106 }
107
108
109
110
111
112
113 void modify(@NotNull ModifyRequest request);
114
115
116
117
118
119
120 void modifyDn(@NotNull ModifyDnRequest request);
121
122
123
124
125
126
127
128
129 default void modifyUserPassword(@NotNull String dn, @Nullable String oldPass, @Nullable String newPass) {
130 executeExtension(new PasswordModifyRequest(dn, oldPass, newPass));
131 }
132
133
134
135
136
137
138
139 @NotNull
140 SearchResponse search(@NotNull SearchRequest request);
141
142
143
144
145
146
147
148 default Optional<LdapEntry> findOne(@NotNull SearchRequest request) {
149 return Optional.ofNullable(search(request))
150 .map(SearchResponse::getEntry);
151 }
152
153
154
155
156
157
158
159
160
161 default <T> Optional<T> findOne(
162 @NotNull SearchRequest request,
163 @NotNull LdaptiveEntryMapper<T> entryMapper) {
164 return Optional.ofNullable(search(request))
165 .map(SearchResponse::getEntry)
166 .map(entryMapper::map);
167 }
168
169
170
171
172
173
174
175 default Collection<LdapEntry> findAll(@NotNull SearchRequest request) {
176 return Optional.ofNullable(search(request))
177 .map(SearchResponse::getEntries)
178 .orElseGet(Collections::emptyList);
179 }
180
181
182
183
184
185
186
187
188
189 default <T> Stream<T> findAll(
190 @NotNull SearchRequest request,
191 @NotNull LdaptiveEntryMapper<T> entryMapper) {
192 return Optional.ofNullable(search(request))
193 .map(SearchResponse::getEntries)
194 .orElseGet(Collections::emptyList)
195 .stream()
196 .map(entryMapper::map);
197 }
198
199
200
201
202
203
204
205 boolean exists(@NotNull String dn);
206
207
208
209
210
211
212
213
214
215 default <T> boolean exists(@NotNull T domainObject, @NotNull LdaptiveEntryMapper<T> entryMapper) {
216 return exists(entryMapper.mapDn(domainObject));
217 }
218
219
220
221
222
223
224
225
226
227 <T> T save(T domainObject, LdaptiveEntryMapper<T> entryMapper);
228
229
230
231
232
233
234
235
236
237 default <T> Stream<T> saveAll(
238 @Nullable Collection<T> domainObjects,
239 @NotNull LdaptiveEntryMapper<T> entryMapper) {
240
241 return Optional.ofNullable(domainObjects)
242 .map(col -> col.stream()
243 .filter(Objects::nonNull)
244 .map(domainObject -> save(domainObject, entryMapper)))
245 .orElseGet(Stream::empty);
246 }
247
248
249
250
251
252
253
254
255 default <T> void remove(
256 @NotNull T domainObject,
257 @NotNull LdaptiveEntryMapper<T> entryMapper) {
258 delete(DeleteRequest.builder().dn(entryMapper.mapDn(domainObject)).build());
259 }
260
261
262
263
264
265
266
267
268 default <T> void removeAll(
269 @Nullable Collection<T> domainObjects,
270 @NotNull LdaptiveEntryMapper<T> entryMapper) {
271 Optional.ofNullable(domainObjects)
272 .ifPresent(col -> col.forEach(domainObject -> remove(domainObject, entryMapper)));
273 }
274
275 }