File: LDAPPersistSearchControl.java

package info (click to toggle)
ldapjdk 4.20.0%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 9,708 kB
  • sloc: ansic: 44,727; java: 39,132; xml: 7,419; sh: 4,185; perl: 3,774; makefile: 1,681; cpp: 979
file content (523 lines) | stat: -rw-r--r-- 20,657 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 *
 * ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is mozilla.org code.
 *
 * The Initial Developer of the Original Code is
 * Netscape Communications Corporation.
 * Portions created by the Initial Developer are Copyright (C) 1999
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */
package org.ietf.ldap.controls;

import java.io.*;
import org.ietf.ldap.client.JDAPBERTagDecoder;
import org.ietf.ldap.LDAPControl;
import org.ietf.ldap.ber.stream.*;

/**
 * Represents an LDAP v3 server control that specifies a persistent
 * search (an ongoing search operation), which allows your LDAP client
 * to get notification of changes to the directory.  (The OID for this
 * control is 2.16.840.1.113730.3.4.3.)  You can use this control in
 * conjunction with an "entry change notification" control (represented
 * by <CODE>LDAPEntryChangeControl</CODE> object.
 * <P>
 *
 * To use persistent searching for change notification, you create a
 * "persistent search" control that specifies the types of changes that
 * you want to track.  You include the control in a search request.
 * If an entry in the directory is changed, the server determines if
 * the entry matches the search criteria in your request and if the
 * change is the type of change that you are tracking.  If both of
 * these are true, the server sends the entry to your client.
 * <P>
 *
 * The server can also include an "entry change notification" control
 * with the entry.  (The OID for this control is 2.16.840.1.113730.3.4.7.)
 * This control contains additional information about the
 * change made to the entry, including the type of change made,
 * the change number (which corresponds to an item in the server's
 * change log, if the server supports a change log), and, if the
 * entry was renamed, the old DN of the entry.
 * <P>
 *
 * When constructing an <CODE>LDAPPersistSearchControl</CODE> object,
 * you can specify the following information:
 * <P>
 *
 * <UL>
 * <LI>the type of change you want to track (added, modified, deleted,
 * or renamed entries)
 * <LI>a preference indicating whether or not you want the server to
 * return all entries that initially matched the search criteria
 * (rather than only the entries that change)
 * <LI>a preference indicating whether or not you want entry change
 * notification controls included with every entry returned by the
 * server
 * </UL>
 * <P>
 *
 * For example:
 * <PRE>
 * ...
 * LDAPConnection ld = new LDAPConnection();
 *
 * try {
 *     // Connect to server.
 *     ld.connect( 3, hostname, portnumber, "", "" );
 *
 *     // Create a persistent search control.
 *     int op = LDAPPersistSearchControl.ADD |
 *         LDAPPersistSearchControl.MODIFY |
 *         LDAPPersistSearchControl.DELETE |
 *         LDAPPersistSearchControl.MODDN;
 *     boolean changesOnly = true;
 *     boolean returnControls = true;
 *     boolean isCritical = true;
 *     LDAPPersistSearchControl persistCtrl = new
 *         LDAPPersistSearchControl( op, changesOnly,
 *         returnControls, isCritical );
 *
 *     // Set the search constraints to use that control.
 *     LDAPSearchConstraints cons = ld.getSearchConstraints();
 *     cons.setBatchSize( 1 );
 *     cons.setServerControls( persistCtrl );
 *
 *     // Start the persistent search.
 *     LDAPSearchResults res = ld.search( "o=Airius.com",
 *               LDAPConnection.SCOPE_SUB, "(cn=Barbara*)", null, false, cons );
 *
 *     // Loop through the incoming results.
 *     while ( res.hasMore() ) {
 *     ...
 *     }
 * ...
 * }
 * </PRE>
 *
 * @see org.ietf.ldap.LDAPControl
 * @see org.ietf.ldap.controls.LDAPEntryChangeControl
 */

public class LDAPPersistSearchControl extends LDAPControl {

    /**
     * Default constructor
     */
    public LDAPPersistSearchControl()
    {
        super(PERSISTENTSEARCH, true, null);
    }

    /**
     * Constructs an <CODE>LDAPPersistSearchControl</CODE> object
     * that specifies a persistent search.
     *
     * @param changeTypes the change types to monitor. You can perform
     * a bitwise OR on any of the following values and specify the result as
     * the change types:
     * <UL>
     * <LI><CODE>LDAPPersistSearchControl.ADD</CODE> (to track new entries
     * added to the directory)
     * <LI><CODE>LDAPPersistSearchControl.DELETE</CODE> (to track entries
     * removed from the directory)
     * <LI><CODE>LDAPPersistSearchControl.MODIFY</CODE> (to track entries
     * that have been modified)
     * <LI><CODE>LDAPPersistSearchControl.MODDN</CODE> (to track entries
     * that have been renamed)
     * </UL>
     * @param changesOnly <code>true</code> if you do not want the server
     * to return all existing entries in the directory that match the
     * search criteria.  (Use this if you just want the changed entries 
     * to be returned.)
     * @param returnControls <code>true</code> if you want the server to return
     * entry change controls with each entry in the search results
     * @param isCritical <code>true</code> if this control is critical to
     * the search operation. (If the server does not support
     * this control, you may not want the server to perform the search
     * at all.)
     * @see org.ietf.ldap.LDAPControl
     * @see org.ietf.ldap.controls.LDAPEntryChangeControl
     */
    public LDAPPersistSearchControl(int changeTypes, boolean changesOnly,
        boolean returnControls, boolean isCritical) {
        super(PERSISTENTSEARCH, isCritical, null);
        _value = createPersistSearchSpecification(changeTypes,
          changesOnly, returnControls);
        _changeTypes = changeTypes;
        _changesOnly = changesOnly;
        _returnECs = returnControls;
    }

    /**
     * Gets the change types monitored by this control.
     * @return integer representing the change types to monitor.
     * This value can be the bitwise OR of <code>ADD, DELETE, MODIFY,</code>
     * and/or <code>MODDN</code>. If the change type is unknown,
     * this method returns -1.
     * @see org.ietf.ldap.controls.LDAPPersistSearchControl#setChangeTypes
     */
    public int getChangeTypes() {
        return _changeTypes;
    }

    /**
     * Indicates whether you want the server to send any existing
     * entries that already match the search criteria or only the
     * entries that have changed.
     * @return if <code>true</code>, the server returns only the
     * entries that have changed.  If <code>false</code>, the server
     * also returns any existing entries that match the search criteria
     * but have not changed.
     * @see org.ietf.ldap.controls.LDAPPersistSearchControl#setChangesOnly
     */
    public boolean getChangesOnly() {
        return _changesOnly;
    }

    /**
     * Indicates whether or not the server includes an "entry change
     * notification" control with each entry it sends back to the client
     * during the persistent search.
     * @return <code>true</code> if the server includes "entry change
     * notification" controls with the entries it sends during the
     * persistent search.
     * @see org.ietf.ldap.controls.LDAPEntryChangeControl
     * @see org.ietf.ldap.controls.LDAPPersistSearchControl#setReturnControls
     */
    public boolean getReturnControls() {
        return _returnECs;
    }

    /**
     * Sets the change types that you want monitored by this control.
     * @param types integer representing the change types to monitor
     * This value can be the bitwise OR of <code>ADD, DELETE, MODIFY,</code>
     * and/or <code>MODDN</code>.
     * @see org.ietf.ldap.controls.LDAPPersistSearchControl#getChangeTypes
     */
    public void setChangeTypes(int types) {
        _changeTypes = types;
    }

    /**
     * Specifies whether you want the server to send any existing
     * entries that already match the search criteria or only the
     * entries that have changed.
     * @param changesOnly if <code>true</code>, the server returns only the
     * entries that have changed.  If <code>false</code>, the server
     * also returns any existing entries that match the search criteria
     * but have not changed.
     * @see org.ietf.ldap.controls.LDAPPersistSearchControl#getChangesOnly
     */
    public void setChangesOnly(boolean changesOnly) {
        _changesOnly = changesOnly;
    }

    /**
     * Specifies whether you want the server to include an "entry change
     * notification" control with each entry it sends back to the client
     * during the persistent search.
     * @param returnControls if <code>true</code>, the server includes
     * "entry change notification" controls with the entries it sends
     * during the persistent search
     * @see org.ietf.ldap.controls.LDAPEntryChangeControl
     * @see org.ietf.ldap.controls.LDAPPersistSearchControl#setReturnControls
     */
    public void setReturnControls(boolean returnControls) {
        _returnECs = returnControls;
    }

    /**
     * Takes an input byte array and extracts the ber elements, assigning
     * them to appropriate fields in the entry change control.
     *
     * @param c byte array that contains BER elements
     * @return the entry change control.
     * @deprecated LDAPEntryChangeControl controls are now automatically 
     * instantiated.
     */
    public LDAPEntryChangeControl parseResponse(byte[] c) {
        LDAPEntryChangeControl con = new LDAPEntryChangeControl();

        ByteArrayInputStream inStream = new ByteArrayInputStream(c);
        BERSequence seq = new BERSequence();
        JDAPBERTagDecoder decoder = new JDAPBERTagDecoder();
        int[] numRead = new int[1];
        numRead[0] = 0;

        try {
            /* a sequence */
            BERSequence s = (BERSequence)BERElement.getElement(decoder, inStream,
              numRead);

            BEREnumerated itr = (BEREnumerated)s.elementAt(0);

            con.setChangeType(itr.getValue());

            if (s.size() > 1) {
                if (s.elementAt(1) instanceof BEROctetString) {
                    BEROctetString str = (BEROctetString)s.elementAt(1);
                    con.setPreviousDN(new String(str.getValue(), "UTF8"));
                } else if (s.elementAt(1) instanceof BERInteger) {
                    BERInteger num = (BERInteger)s.elementAt(1);
                    con.setChangeNumber(num.getValue());
                }
            }
            if (s.size() > 2) {
                BERInteger num = (BERInteger)s.elementAt(2);
                con.setChangeNumber(num.getValue());
            }
        } catch (Exception e) {
            return null;
        }

        return con;
    }

    /**
     * Returns an "entry change notification" control if the control is in
     * the specified array of controls.  Use this method to retrieve an "entry
     * change notification" control included with an entry sent by the server.
     * <P>
     *
     * You can get the controls returned by the server by using the
     * <CODE>getResponseControls</CODE> method of the
     * <CODE>LDAPConnection</CODE> class.
     * <P>
     *
     * For example:
     * <PRE>
     * ...
     * LDAPConnection ld = new LDAPConnection();
     * try {
     *     // Connect to the server, set up the persistent search control,
     *     // and set up the search constraints.
     *     ...
     *
     *     // Search the directory.
     *     LDAPSearchResults res = ld.search( "o=Airius.com",
     *         LDAPConnection.SCOPE_SUB, "(cn=Barbara*)", attrs, false, cons );
     *
     *     // Determine if the server sent a control back to you.
     *     LDAPControl[] returnedControls = ld.getResponseControls();
     *     if ( returnedControls != null ) {
     *
     *         // Get the entry change control.
     *         LDAPEntryChangeControl entryCtrl = null;
     *         for ( int i = 0; i < returnedControls.length; i++ ) {
     *             if ( returnedControls[i] instanceof LDAPEntryChangeControl ) {
     *                 entryCtrl = (LDAPEntryChangeControl)returnedControls[i];
     *                 break;
     *             }
     *         }
     *         if ( entryCtrl != null ) {
     *
     *             // Get and print the type of change made to the entry.
     *             int changeType = entryCtrl.getChangeType();
     *             if ( changeType != -1 ) {
     *                 System.out.print( "Change made: " );
     *                 switch ( changeType ) {
     *                 case LDAPPersistSearchControl.ADD:
     *                     System.out.println( "Added new entry." );
     *             break;
     *                 case LDAPPersistSearchControl.MODIFY:
     *                     System.out.println( "Modified entry." );
     *             break;
     *                 case LDAPPersistSearchControl.DELETE:
     *                     System.out.println( "Deleted entry." );
     *             break;
     *                 case LDAPPersistSearchControl.MODDN:
     *                     System.out.println( "Renamed entry." );
     *             break;
     *                 }
     *             }
     *
     *             // Get and print the change number corresponding
     *             // to the change.
     *             int changeNumber = entryCtrl.getChangeNumber();
     *             if ( changeNumber != -1 )
     *                 System.out.println( "Change log number: " + changeNumber);
     *
     *             // Get and print the previous DN of the entry,
     *             // if the entry was renamed.
     *             LDAPDN oldDN = entryCtrl.getPreviousDN();
     *             if ( oldDN != null )
     *                     System.out.println( "Previous DN: " + oldDN );
     *
     *         } else {
     *
     *             System.out.println( "No entry change control." );
     *         }
     *     }
     *     ...
     * }
     * ...
     *
     * </PRE>
     *
     * @param controls an array of <CODE>LDAPControl</CODE> objects,
     * representing the controls returned by the server
     * with an entry. To get these controls, use the
     * <CODE>getResponseControls</CODE> method of the
     * <CODE>LDAPConnection</CODE> class.
     * @return an <CODE>LDAPEntryChangeControl</CODE> object representing
     * the entry change control sent by the server.  If no entry change
     * control was sent, this method returns null.
     * @see org.ietf.ldap.controls.LDAPEntryChangeControl
     * @see org.ietf.ldap.LDAPConnection#getResponseControls
     * @deprecated LDAPEntryChangeControl controls are now automatically 
     * instantiated.
     */
    public static LDAPEntryChangeControl parseResponse(LDAPControl[] controls) {

        LDAPPersistSearchControl con = new LDAPPersistSearchControl();

        for (int i=0; (controls != null) && (i < controls.length); i++) {

            // get the entry change control
            if (controls[i].getID().equals(LDAPEntryChangeControl.ENTRYCHANGED)) {
                return con.parseResponse(controls[i].getValue());
            }
        }

        return null;
    }

    /**
     * Creates a "flattened" BER encoding of the persistent search
     * specifications and returns it as a byte array.
     * @param changeTypes the change types to monitor on the server
     * @param changesOnly <code>true</code> to skip the initial search
     * @param returnECs <code>true</code> if entry change controls are to be
     * returned with the search results
     * @return the BER-encoded data.
     */
    private byte[] createPersistSearchSpecification(int changeTypes,
        boolean changesOnly, boolean returnECs) {

        /* A sequence */
        BERSequence seq = new BERSequence();
        seq.addElement(new BERInteger(changeTypes));
        seq.addElement(new BERBoolean(changesOnly));
        seq.addElement(new BERBoolean(returnECs));

        /* return a byte array */
        return flattenBER( seq );
    }

    public String toString() {
        StringBuffer sb = new StringBuffer("{PersistSearchCtrl:");
        
        sb.append(" isCritical=");
        sb.append(isCritical());
        
        sb.append(" returnEntryChangeCtrls=");
        sb.append(_returnECs);
        
        sb.append(" changesOnly=");
        sb.append(_changesOnly);

        sb.append(" changeTypes=");
        sb.append(typesToString(_changeTypes));
        
        sb.append("}");

        return sb.toString();
    }


    /**
     * This method is also used by LDAPentryChangeControl.toString()
     */
    static String typesToString(int changeTypes) {
        String types = "";

        if ((changeTypes & ADD) != 0) {
            types += (types.length() > 0) ? "+ADD" : "ADD";
        }
        if ((changeTypes & DELETE) != 0) {
            types += (types.length() > 0) ? "+DEL" : "DEL";
        }
        if ((changeTypes & MODIFY) != 0) {
            types += (types.length() > 0) ? "+MOD" : "MOD";
        }
        if ((changeTypes & MODDN) != 0) {
            types += (types.length() > 0) ? "+MODDN" : "MODDN";
        }
        return types;
    }        

    private int _changeTypes = 1;
    private boolean _changesOnly = false;
    private boolean _returnECs = false;

    /**
     * Change type specifying that you want to track additions of new
     * entries to the directory.  You can either specify this change type
     * when constructing an <CODE>LDAPPersistSearchControl</CODE> or
     * by using the <CODE>setChangeTypes</CODE> method.
     * @see org.ietf.ldap.controls.LDAPPersistSearchControl#getChangeTypes
     * @see org.ietf.ldap.controls.LDAPPersistSearchControl#setChangeTypes
     */
    public static final int ADD = 1;

    /**
     * Change type specifying that you want to track removals of
     * entries from the directory.  You can either specify this change type
     * when constructing an <CODE>LDAPPersistSearchControl</CODE> or
     * by using the <CODE>setChangeTypes</CODE> method.
     * @see org.ietf.ldap.controls.LDAPPersistSearchControl#getChangeTypes
     * @see org.ietf.ldap.controls.LDAPPersistSearchControl#setChangeTypes
     */
    public static final int DELETE = 2;

    /**
     * Change type specifying that you want to track modifications of
     * entries in the directory.  You can either specify this change type
     * when constructing an <CODE>LDAPPersistSearchControl</CODE> or
     * by using the <CODE>setChangeTypes</CODE> method.
     * @see org.ietf.ldap.controls.LDAPPersistSearchControl#getChangeTypes
     * @see org.ietf.ldap.controls.LDAPPersistSearchControl#setChangeTypes
     */
    public static final int MODIFY = 4;

    /**
     * Change type specifying that you want to track modifications of the
     * DNs of entries in the directory.  You can either specify this change type
     * when constructing an <CODE>LDAPPersistSearchControl</CODE> or
     * by using the <CODE>setChangeTypes</CODE> method.
     * @see org.ietf.ldap.controls.LDAPPersistSearchControl#getChangeTypes
     * @see org.ietf.ldap.controls.LDAPPersistSearchControl#setChangeTypes
     */
    public static final int MODDN = 8;
    public final static String PERSISTENTSEARCH  = "2.16.840.1.113730.3.4.3";
}