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.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   * The interface for ldap operations.
44   *
45   * @author Christian Bremer
46   */
47  @Validated
48  public interface LdaptiveOperations {
49  
50    /**
51     * Gets connection factory.
52     *
53     * @return the connection factory
54     */
55    @NotNull
56    ConnectionFactory getConnectionFactory();
57  
58    /**
59     * Executes add operation.
60     *
61     * @param addRequest the add request
62     */
63    void add(@NotNull AddRequest addRequest);
64  
65    /**
66     * Executes bind operation.
67     *
68     * @param request the request
69     * @return the boolean
70     */
71    boolean bind(@NotNull BindRequest request);
72  
73    /**
74     * Executes compare operation.
75     *
76     * @param request the request
77     * @return the boolean
78     */
79    boolean compare(@NotNull CompareRequest request);
80  
81    /**
82     * Executes delete operation.
83     *
84     * @param request the request
85     */
86    void delete(@NotNull DeleteRequest request);
87  
88    /**
89     * Executes an extended request.
90     *
91     * @param request the request
92     * @return the extended response
93     */
94    @NotNull
95    ExtendedResponse executeExtension(@NotNull ExtendedRequest request);
96  
97    /**
98     * Generate user password.
99     *
100    * @param dn the dn
101    * @return the generated user password
102    */
103   default String generateUserPassword(@NotNull String dn) {
104     ExtendedResponse response = executeExtension(new PasswordModifyRequest(dn));
105     return PasswordModifyResponseParser.parse(response);
106   }
107 
108   /**
109    * Executes modify operation.
110    *
111    * @param request the request
112    */
113   void modify(@NotNull ModifyRequest request);
114 
115   /**
116    * Executes modify dn operation.
117    *
118    * @param request the request
119    */
120   void modifyDn(@NotNull ModifyDnRequest request);
121 
122   /**
123    * Modifies user password.
124    *
125    * @param dn the dn
126    * @param oldPass the old pass
127    * @param newPass the new pass
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    * Executes search operation.
135    *
136    * @param request the request
137    * @return the search response
138    */
139   @NotNull
140   SearchResponse search(@NotNull SearchRequest request);
141 
142   /**
143    * Find one.
144    *
145    * @param request the request
146    * @return the optional ldap entry
147    */
148   default Optional<LdapEntry> findOne(@NotNull SearchRequest request) {
149     return Optional.ofNullable(search(request))
150         .map(SearchResponse::getEntry);
151   }
152 
153   /**
154    * Find one.
155    *
156    * @param <T> the type parameter
157    * @param request the request
158    * @param entryMapper the entry mapper
159    * @return the optional domain object
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    * Find all.
171    *
172    * @param request the request
173    * @return the collection
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    * Find all.
183    *
184    * @param <T> the type parameter
185    * @param request the request
186    * @param entryMapper the entry mapper
187    * @return the stream
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    * Exists.
201    *
202    * @param dn the dn
203    * @return the boolean
204    */
205   boolean exists(@NotNull String dn);
206 
207   /**
208    * Exists.
209    *
210    * @param <T> the type parameter
211    * @param domainObject the domain object
212    * @param entryMapper the entry mapper
213    * @return the boolean
214    */
215   default <T> boolean exists(@NotNull T domainObject, @NotNull LdaptiveEntryMapper<T> entryMapper) {
216     return exists(entryMapper.mapDn(domainObject));
217   }
218 
219   /**
220    * Save t.
221    *
222    * @param <T> the type parameter
223    * @param domainObject the domain object
224    * @param entryMapper the entry mapper
225    * @return the t
226    */
227   <T> T save(T domainObject, LdaptiveEntryMapper<T> entryMapper);
228 
229   /**
230    * Save all stream.
231    *
232    * @param <T> the type parameter
233    * @param domainObjects the domain objects
234    * @param entryMapper the entry mapper
235    * @return the stream
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    * Remove.
250    *
251    * @param <T> the type parameter
252    * @param domainObject the domain object
253    * @param entryMapper the entry mapper
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    * Remove all.
263    *
264    * @param <T> the type parameter
265    * @param domainObjects the domain objects
266    * @param entryMapper the entry mapper
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 }