001/*
002 *   Licensed to the Apache Software Foundation (ASF) under one
003 *   or more contributor license agreements.  See the NOTICE file
004 *   distributed with this work for additional information
005 *   regarding copyright ownership.  The ASF licenses this file
006 *   to you under the Apache License, Version 2.0 (the
007 *   "License"); you may not use this file except in compliance
008 *   with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *   Unless required by applicable law or agreed to in writing,
013 *   software distributed under the License is distributed on an
014 *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *   KIND, either express or implied.  See the License for the
016 *   specific language governing permissions and limitations
017 *   under the License.
018 *
019 */
020package org.apache.directory.ldap.client.template;
021
022
023import java.util.List;
024
025import org.apache.directory.api.ldap.model.entry.Attribute;
026import org.apache.directory.api.ldap.model.message.AddRequest;
027import org.apache.directory.api.ldap.model.message.AddResponse;
028import org.apache.directory.api.ldap.model.message.DeleteRequest;
029import org.apache.directory.api.ldap.model.message.DeleteResponse;
030import org.apache.directory.api.ldap.model.message.ModifyRequest;
031import org.apache.directory.api.ldap.model.message.ModifyResponse;
032import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
033import org.apache.directory.api.ldap.model.message.ResultResponse;
034import org.apache.directory.api.ldap.model.message.SearchRequest;
035import org.apache.directory.api.ldap.model.message.SearchScope;
036import org.apache.directory.api.ldap.model.name.Dn;
037import org.apache.directory.ldap.client.template.exception.LdapRequestUnsuccessfulException;
038import org.apache.directory.ldap.client.template.exception.PasswordException;
039
040
041/**
042 * Specifies the set of operations available on
043 * {@link org.apache.directory.ldap.client.template.LdapConnectionTemplate
044 * LdapConnectionTemplate}.  This interface can be useful for unit testing
045 * in order to stub out methods.
046 * 
047 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
048 */
049public interface LdapConnectionOperations
050{
051
052    /**
053     * Adds an entry specified by an AddRequest to the LDAP server.
054     *
055     * @param addRequest The request
056     * @return An AddResponse
057     */
058    public abstract AddResponse add( AddRequest addRequest );
059
060
061    /**
062     * Adds an entry specified by a Dn and an array of Attribute's to the LDAP
063     * server.
064     *
065     * @param dn The distinguished name of the new entry
066     * @param attributes The attributes of the new entry
067     * @return An AddResponse
068     */
069    public abstract AddResponse add( Dn dn, Attribute... attributes );
070
071
072    /**
073     * Adds an entry specified by a Dn, to be filled out by a RequestBuilder,
074     * to the LDAP server.
075     *
076     * @param dn The distinguished name of the new entry
077     * @param requestBuilder The request builder
078     * @return An AddResponse
079     */
080    public abstract AddResponse add( Dn dn, RequestBuilder<AddRequest> requestBuilder );
081
082
083    /**
084     * Attempts to authenticate the supplied credentials against the first 
085     * entry found matching the search criteria.  If authentication fails, 
086     * a PasswordException is thrown.  If successful, the response is 
087     * checked for warnings, and if present, a PasswordWarning is returned.
088     * Otherwise, null is returned.
089     *
090     * @param baseDn
091     * @param filter
092     * @param scope
093     * @param password
094     * @return
095     * @throws PasswordException
096     * @see {@link #authenticate(Dn, char[])}
097     * @see {@link #searchFirst(String, String, SearchScope, EntryMapper)}
098     */
099    public PasswordWarning authenticate( String baseDn, String filter, SearchScope scope, char[] password ) throws PasswordException;
100
101
102    /**
103     * Attempts to authenticate the supplied credentials against the first 
104     * entry found matching the search criteria.  If authentication fails, 
105     * a PasswordException is thrown.  If successful, the response is 
106     * checked for warnings, and if present, a PasswordWarning is returned.
107     * Otherwise, null is returned.
108     *
109     * @param baseDn
110     * @param filter
111     * @param scope
112     * @param password
113     * @return
114     * @throws PasswordException
115     * @see {@link #authenticate(Dn, char[])}
116     * @see {@link #searchFirst(Dn, String, SearchScope, EntryMapper)}
117     */
118    public PasswordWarning authenticate( Dn baseDn, String filter, SearchScope scope, char[] password ) throws PasswordException;
119    
120    
121    /**
122     * Attempts to authenticate the supplied credentials against the first 
123     * entry found matching the search criteria.  If authentication fails, 
124     * a PasswordException is thrown.  If successful, the response is 
125     * checked for warnings, and if present, a PasswordWarning is returned.
126     * Otherwise, null is returned.
127     *
128     * @param baseDn
129     * @param filter
130     * @param scope
131     * @param password
132     * @return
133     * @throws PasswordException
134     * @see {@link #authenticate(Dn, char[])}
135     * @see {@link #searchFirst(SearchRequest, EntryMapper)}
136     */
137    public PasswordWarning authenticate( SearchRequest searchRequest, char[] password ) throws PasswordException;
138
139
140    /**
141     * Attempts to authenticate the supplied credentials.  If authentication
142     * fails, a PasswordException is thrown.  If successful, the response is 
143     * checked for warnings, and if present, a PasswordWarning is returned.
144     * Otherwise, null is returned.
145     *
146     * @param userDn The distinguished name of the user
147     * @param password The password
148     * @return A PasswordWarning or null
149     * @throws PasswordException If authentication fails
150     */
151    public abstract PasswordWarning authenticate( Dn userDn, char[] password ) throws PasswordException;
152
153
154    /**
155     * Deletes an entry specified by a DeleteRequest from the LDAP server.
156     *
157     * @param deleteRequest The request
158     * @return A DeleteResponse
159     */
160    public abstract DeleteResponse delete( DeleteRequest deleteRequest );
161
162
163    /**
164     * Deletes an entry specified by Dn from the LDAP server.
165     *
166     * @param dn The distinguished name of the entry
167     * @return A DeleteResponse
168     */
169    public abstract DeleteResponse delete( Dn dn );
170
171
172    /**
173     * Deletes an entry specified by Dn, and whose request is configured
174     * by a RequestBuilder, from the LDAP server.
175     *
176     * @param dn The distinguished name of the entry
177     * @param requestBuilder The RequestBuilder
178     * @return A DeleteResponse
179     */
180    public abstract DeleteResponse delete( Dn dn, RequestBuilder<DeleteRequest> requestBuilder );
181
182
183    /**
184     * Executes the <code>connectionCallback</code>, supplying it a managed
185     * connection.
186     *
187     * @param connectionCallback The callback
188     * @return Whatever the callback returns
189     */
190    public abstract <T> T execute( ConnectionCallback<T> connectionCallback );
191
192
193    /**
194     * Performs a lookup, and supplies the matching entry to the 
195     * <code>entryMapper</code>.
196     *
197     * @param dn The distinguished name of the entry
198     * @param entryMapper The mapper from entry to model object
199     * @return Whatever the <code>entryMapper</code> returns
200     */
201    public abstract <T> T lookup( Dn dn, EntryMapper<T> entryMapper );
202
203
204    /**
205     * Performs a lookup, requesting <code>attributes</code>, and supplies 
206     * the matching entry to the <code>entryMapper</code>.
207     *
208     * @param dn The distinguished name of the entry
209     * @param attributes The attributes to be fetched
210     * @param entryMapper The mapper from entry to model object
211     * @return Whatever the <code>entryMapper</code> returns
212     */
213    public abstract <T> T lookup( Dn dn, String[] attributes, EntryMapper<T> entryMapper );
214
215
216    /**
217     * Modifies the password for <code>userDn</code> to
218     * <code>newPassword</code> using the admin account.
219     *
220     * @param userDn
221     * @param newPassword
222     * @throws PasswordException
223     * @see {@link #modifyPassword(Dn, char[], char[], boolean)}
224     */
225    public void modifyPassword( Dn userDn, char[] newPassword ) 
226        throws PasswordException;
227
228
229    /**
230     * Modifies the password for <code>userDn</code> from 
231     * <code>oldPassword</code> to <code>newPassword</code>.
232     *
233     * @param userDn
234     * @param oldPassword
235     * @param newPassword
236     * @throws PasswordException
237     * @see {@link #modifyPassword(Dn, char[], char[], boolean)}
238     */
239    public void modifyPassword( Dn userDn, char[] oldPassword,
240        char[] newPassword ) throws PasswordException;
241
242
243    /**
244     * Modifies the password for <code>userDn</code> from 
245     * <code>oldPassword</code> to <code>newPassword</code>, optionally using
246     * an admin account.  If <code>asAdmin</code> is true, then the operation
247     * is performed in admin context which means <code>oldPassword</code> is
248     * may be <code>null</code>.
249     *
250     * @param userDn The distinguished name of the user
251     * @param oldPassword The users old password (optional if asAdmin is true)
252     * @param newPassword The users new password
253     * @param asAdmin If true, execute in admin context
254     * @throws PasswordException If the password modification fails
255     */
256    public abstract void modifyPassword( Dn userDn, char[] oldPassword, char[] newPassword,
257        boolean asAdmin ) throws PasswordException;
258
259
260    /**
261     * Modifies an entry specified by a ModifyRequest on the LDAP server.
262     *
263     * @param modifyRequest The request
264     * @return A ModifyResponse
265     */
266    public abstract ModifyResponse modify( ModifyRequest modifyRequest );
267
268
269    /**
270     * Modifies an entry specified by Dn, and whose request is configured
271     * by a RequestBuilder, on the LDAP server.
272     *
273     * @param dn The distinguished name of the entry
274     * @param requestBuilder The RequestBuilder
275     * @return A ModifyResponse
276     */
277    public abstract ModifyResponse modify( Dn dn, RequestBuilder<ModifyRequest> requestBuilder );
278
279    
280    /**
281     * Checks the supplied response for its result code, and if not 
282     * {@link ResultCodeEnum#SUCCESS}, an exception is thrown. This method is 
283     * intened to be used inline:
284     * 
285     * <pre>
286     * template.responseOrException( template.delete( dn ) );
287     * </pre>
288     *
289     * @param response The response to check for success
290     * @return The supplied <code>response</code>
291     * @throws LdapRequestUnsuccessfulException If the response is not
292     * {@link ResultCodeEnum#SUCCESS}
293     */
294    public abstract <T extends ResultResponse> T responseOrException( T response );
295
296
297    /**
298     * Searches for the entries matching the supplied criteria, feeding the 
299     * result into the <code>entryMapper</code>.
300     *
301     * @param baseDn
302     * @param filter
303     * @param scope
304     * @param entryMapper
305     * @return The mapped entries
306     * @see {@link #search(SearchRequest, EntryMapper)}
307     */
308    public abstract <T> List<T> search( String baseDn, String filter, SearchScope scope,
309        EntryMapper<T> entryMapper );
310
311
312    /**
313     * Searches for the entries matching the supplied criteria, feeding the 
314     * result into the <code>entryMapper</code>.
315     *
316     * @param baseDn
317     * @param filter
318     * @param scope
319     * @param entryMapper
320     * @return The mapped entries
321     * @see {@link #search(SearchRequest, EntryMapper)}
322     */
323    public abstract <T> List<T> search( Dn baseDn, String filter, SearchScope scope,
324        EntryMapper<T> entryMapper );
325
326
327    /**
328     * Searches for the entries matching the supplied criteria, feeding the 
329     * result into the <code>entryMapper</code>, querying only the requested 
330     * attributes.
331     *
332     * @param baseDn
333     * @param filter
334     * @param scope
335     * @param attributes
336     * @param entryMapper
337     * @return The mapped entries
338     * @see {@link #search(SearchRequest, EntryMapper)}
339     */
340    public abstract <T> List<T> search( String baseDn, String filter, SearchScope scope,
341        String[] attributes, EntryMapper<T> entryMapper );
342
343
344    /**
345     * Searches for the entries matching the supplied criteria, feeding the 
346     * result into the <code>entryMapper</code>, querying only the requested 
347     * attributes.
348     *
349     * @param baseDn
350     * @param filter
351     * @param scope
352     * @param attributes
353     * @param entryMapper
354     * @return The mapped entries
355     * @see {@link #search(SearchRequest, EntryMapper)}
356     */
357    public abstract <T> List<T> search( Dn baseDn, String filter, SearchScope scope,
358        String[] attributes, EntryMapper<T> entryMapper );
359
360
361    /**
362     * Searches for the entries matching the supplied 
363     * <code>searchRequest</code>, feeding the result into the 
364     * <code>entryMapper</code>.
365     *
366     * @param searchRequest The search request
367     * @param entryMapper The mapper
368     * @return The mapped entries
369     */
370    public abstract <T> List<T> search( SearchRequest searchRequest,
371        EntryMapper<T> entryMapper );
372    
373    
374    /**
375     * Searches for the first entry matching the supplied criteria, feeding the 
376     * result into the <code>entryMapper</code>.
377     *
378     * @param baseDn
379     * @param filter
380     * @param scope
381     * @param entryMapper
382     * @return The mapped entries
383     * @see {@link #searchFirst(SearchRequest, EntryMapper)}
384     */
385    public abstract <T> T searchFirst( String baseDn, String filter, SearchScope scope,
386        EntryMapper<T> entryMapper );
387
388
389    /**
390     * Searches for the first entry matching the supplied criteria, feeding the 
391     * result into the <code>entryMapper</code>.
392     *
393     * @param baseDn
394     * @param filter
395     * @param scope
396     * @param entryMapper
397     * @return The mapped entries
398     * @see {@link #searchFirst(SearchRequest, EntryMapper)}
399     */
400    public abstract <T> T searchFirst( Dn baseDn, String filter, SearchScope scope,
401        EntryMapper<T> entryMapper );
402
403
404    /**
405     * Searches for the first entry matching the supplied criteria, feeding the 
406     * result into the <code>entryMapper</code>, querying only the requested 
407     * attributes.
408     *
409     * @param baseDn
410     * @param filter
411     * @param scope
412     * @param attributes
413     * @param entryMapper
414     * @return The mapped entries
415     * @see {@link #searchFirst(SearchRequest, EntryMapper)}
416     */
417    public abstract <T> T searchFirst( String baseDn, String filter, SearchScope scope,
418        String[] attributes, EntryMapper<T> entryMapper );
419
420
421    /**
422     * Searches for the first entry matching the supplied criteria, feeding the 
423     * result into the <code>entryMapper</code>, querying only the requested 
424     * attributes.
425     *
426     * @param baseDn
427     * @param filter
428     * @param scope
429     * @param attributes
430     * @param entryMapper
431     * @return The mapped entries
432     * @see {@link #searchFirst(SearchRequest, EntryMapper)}
433     */
434    public abstract <T> T searchFirst( Dn baseDn, String filter, SearchScope scope,
435        String[] attributes, EntryMapper<T> entryMapper );
436    
437
438    /**
439     * Searches for the first entry matching the supplied 
440     * <code>searchRequest</code>, feeding the result into the 
441     * <code>entryMapper</code>. This is basically the same as 
442     * {@link #search(SearchRequest, EntryMapper)}, but is optimized by
443     * modifying the <code>searchRequest</code> to set its size limit to 1.
444     * The <code>searchRequest</code> is returned to its original size limit
445     * before this method returns (or throws an exception).
446     *
447     * @param searchRequest The search request
448     * @param entryMapper The mapper
449     * @return The mapped entry
450     */
451    public abstract <T> T searchFirst( SearchRequest searchRequest,
452        EntryMapper<T> entryMapper );
453
454}