Debian Codegraph @

java-sdk/ietfldap/org/ietf/ldap/controls/LDAPPersistSearchControl.java

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * The Original Code is mozilla.org code.
17 *
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1999
21 * the Initial Developer. All Rights Reserved.
22 *
23 * Contributor(s):
24 *
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
36 *
37 * ***** END LICENSE BLOCK ***** */
38 package org.ietf.ldap.controls;
39
40 import java.io.ByteArrayInputStream;
41
42 import org.ietf.ldap.LDAPControl;
43 import org.ietf.ldap.ber.stream.BERBoolean;
44 import org.ietf.ldap.ber.stream.BERElement;
45 import org.ietf.ldap.ber.stream.BEREnumerated;
46 import org.ietf.ldap.ber.stream.BERInteger;
47 import org.ietf.ldap.ber.stream.BEROctetString;
48 import org.ietf.ldap.ber.stream.BERSequence;
49 import org.ietf.ldap.client.JDAPBERTagDecoder;
50
51 /**
52 * Represents an LDAP v3 server control that specifies a persistent
53 * search (an ongoing search operation), which allows your LDAP client
54 * to get notification of changes to the directory. (The OID for this
55 * control is 2.16.840.1.113730.3.4.3.) You can use this control in
56 * conjunction with an "entry change notification" control (represented
57 * by <CODE>LDAPEntryChangeControl</CODE> object.
58 * <P>
59 *
60 * To use persistent searching for change notification, you create a
61 * "persistent search" control that specifies the types of changes that
62 * you want to track. You include the control in a search request.
63 * If an entry in the directory is changed, the server determines if
64 * the entry matches the search criteria in your request and if the
65 * change is the type of change that you are tracking. If both of
66 * these are true, the server sends the entry to your client.
67 * <P>
68 *
69 * The server can also include an "entry change notification" control
70 * with the entry. (The OID for this control is 2.16.840.1.113730.3.4.7.)
71 * This control contains additional information about the
72 * change made to the entry, including the type of change made,
73 * the change number (which corresponds to an item in the server's
74 * change log, if the server supports a change log), and, if the
75 * entry was renamed, the old DN of the entry.
76 * <P>
77 *
78 * When constructing an <CODE>LDAPPersistSearchControl</CODE> object,
79 * you can specify the following information:
80 * <P>
81 *
82 * <UL>
83 * <LI>the type of change you want to track (added, modified, deleted,
84 * or renamed entries)
85 * <LI>a preference indicating whether or not you want the server to
86 * return all entries that initially matched the search criteria
87 * (rather than only the entries that change)
88 * <LI>a preference indicating whether or not you want entry change
89 * notification controls included with every entry returned by the
90 * server
91 * </UL>
92 * <P>
93 *
94 * For example:
95 * <PRE>
96 * ...
97 * LDAPConnection ld = new LDAPConnection();
98 *
99 * try {
100 * // Connect to server.
101 * ld.connect( 3, hostname, portnumber, "", "" );
102 *
103 * // Create a persistent search control.
104 * int op = LDAPPersistSearchControl.ADD |
105 * LDAPPersistSearchControl.MODIFY |
106 * LDAPPersistSearchControl.DELETE |
107 * LDAPPersistSearchControl.MODDN;
108 * boolean changesOnly = true;
109 * boolean returnControls = true;
110 * boolean isCritical = true;
111 * LDAPPersistSearchControl persistCtrl = new
112 * LDAPPersistSearchControl( op, changesOnly,
113 * returnControls, isCritical );
114 *
115 * // Set the search constraints to use that control.
116 * LDAPSearchConstraints cons = ld.getSearchConstraints();
117 * cons.setBatchSize( 1 );
118 * cons.setServerControls( persistCtrl );
119 *
120 * // Start the persistent search.
121 * LDAPSearchResults res = ld.search( "o=Airius.com",
122 * LDAPConnection.SCOPE_SUB, "(cn=Barbara*)", null, false, cons );
123 *
124 * // Loop through the incoming results.
125 * while ( res.hasMore() ) {
126 * ...
127 * }
128 * ...
129 * }
130 * </PRE>
131 *
132 * @see org.ietf.ldap.LDAPControl
133 * @see org.ietf.ldap.controls.LDAPEntryChangeControl
134 */
135
136 public class LDAPPersistSearchControl extends LDAPControl {
137
138 /**
139 * Default constructor
140 */
141 public LDAPPersistSearchControl()
142 {
143 super(PERSISTENTSEARCH, true, null);
144 }
145
146 /**
147 * Constructs an <CODE>LDAPPersistSearchControl</CODE> object
148 * that specifies a persistent search.
149 *
150 * @param changeTypes the change types to monitor. You can perform
151 * a bitwise OR on any of the following values and specify the result as
152 * the change types:
153 * <UL>
154 * <LI><CODE>LDAPPersistSearchControl.ADD</CODE> (to track new entries
155 * added to the directory)
156 * <LI><CODE>LDAPPersistSearchControl.DELETE</CODE> (to track entries
157 * removed from the directory)
158 * <LI><CODE>LDAPPersistSearchControl.MODIFY</CODE> (to track entries
159 * that have been modified)
160 * <LI><CODE>LDAPPersistSearchControl.MODDN</CODE> (to track entries
161 * that have been renamed)
162 * </UL>
163 * @param changesOnly <code>true</code> if you do not want the server
164 * to return all existing entries in the directory that match the
165 * search criteria. (Use this if you just want the changed entries
166 * to be returned.)
167 * @param returnControls <code>true</code> if you want the server to return
168 * entry change controls with each entry in the search results
169 * @param isCritical <code>true</code> if this control is critical to
170 * the search operation. (If the server does not support
171 * this control, you may not want the server to perform the search
172 * at all.)
173 * @see org.ietf.ldap.LDAPControl
174 * @see org.ietf.ldap.controls.LDAPEntryChangeControl
175 */
176 public LDAPPersistSearchControl(int changeTypes, boolean changesOnly,
177 boolean returnControls, boolean isCritical) {
178 super(PERSISTENTSEARCH, isCritical, null);
179 _value = createPersistSearchSpecification(changeTypes,
180 changesOnly, returnControls);
181 _changeTypes = changeTypes;
182 _changesOnly = changesOnly;
183 _returnECs = returnControls;
184 }
185
186 /**
187 * Gets the change types monitored by this control.
188 * @return integer representing the change types to monitor.
189 * This value can be the bitwise OR of <code>ADD, DELETE, MODIFY,</code>
190 * and/or <code>MODDN</code>. If the change type is unknown,
191 * this method returns -1.
192 * @see org.ietf.ldap.controls.LDAPPersistSearchControl#setChangeTypes
193 */
194 public int getChangeTypes() {
195 return _changeTypes;
196 }
197
198 /**
199 * Indicates whether you want the server to send any existing
200 * entries that already match the search criteria or only the
201 * entries that have changed.
202 * @return if <code>true</code>, the server returns only the
203 * entries that have changed. If <code>false</code>, the server
204 * also returns any existing entries that match the search criteria
205 * but have not changed.
206 * @see org.ietf.ldap.controls.LDAPPersistSearchControl#setChangesOnly
207 */
208 public boolean getChangesOnly() {
209 return _changesOnly;
210 }
211
212 /**
213 * Indicates whether or not the server includes an "entry change
214 * notification" control with each entry it sends back to the client
215 * during the persistent search.
216 * @return <code>true</code> if the server includes "entry change
217 * notification" controls with the entries it sends during the
218 * persistent search.
219 * @see org.ietf.ldap.controls.LDAPEntryChangeControl
220 * @see org.ietf.ldap.controls.LDAPPersistSearchControl#setReturnControls
221 */
222 public boolean getReturnControls() {
223 return _returnECs;
224 }
225
226 /**
227 * Sets the change types that you want monitored by this control.
228 * @param types integer representing the change types to monitor
229 * This value can be the bitwise OR of <code>ADD, DELETE, MODIFY,</code>
230 * and/or <code>MODDN</code>.
231 * @see org.ietf.ldap.controls.LDAPPersistSearchControl#getChangeTypes
232 */
233 public void setChangeTypes(int types) {
234 _changeTypes = types;
235 }
236
237 /**
238 * Specifies whether you want the server to send any existing
239 * entries that already match the search criteria or only the
240 * entries that have changed.
241 * @param changesOnly if <code>true</code>, the server returns only the
242 * entries that have changed. If <code>false</code>, the server
243 * also returns any existing entries that match the search criteria
244 * but have not changed.
245 * @see org.ietf.ldap.controls.LDAPPersistSearchControl#getChangesOnly
246 */
247 public void setChangesOnly(boolean changesOnly) {
248 _changesOnly = changesOnly;
249 }
250
251 /**
252 * Specifies whether you want the server to include an "entry change
253 * notification" control with each entry it sends back to the client
254 * during the persistent search.
255 * @param returnControls if <code>true</code>, the server includes
256 * "entry change notification" controls with the entries it sends
257 * during the persistent search
258 * @see org.ietf.ldap.controls.LDAPEntryChangeControl
259 * @see org.ietf.ldap.controls.LDAPPersistSearchControl#setReturnControls
260 */
261 public void setReturnControls(boolean returnControls) {
262 _returnECs = returnControls;
263 }
264
265 /**
266 * Takes an input byte array and extracts the ber elements, assigning
267 * them to appropriate fields in the entry change control.
268 *
269 * @param c byte array that contains BER elements
270 * @return the entry change control.
271 * @deprecated LDAPEntryChangeControl controls are now automatically
272 * instantiated.
273 */
274 public LDAPEntryChangeControl parseResponse(byte[] c) {
275 LDAPEntryChangeControl con = new LDAPEntryChangeControl();
276
277 ByteArrayInputStream inStream = new ByteArrayInputStream(c);
278 BERSequence seq = new BERSequence();
279 JDAPBERTagDecoder decoder = new JDAPBERTagDecoder();
280 int[] numRead = new int[1];
281 numRead[0] = 0;
282
283 try {
284 /* a sequence */
285 BERSequence s = (BERSequence)BERElement.getElement(decoder, inStream,
286 numRead);
287
288 BEREnumerated itr = (BEREnumerated)s.elementAt(0);
289
290 con.setChangeType(itr.getValue());
291
292 if (s.size() > 1) {
293 if (s.elementAt(1) instanceof BEROctetString) {
294 BEROctetString str = (BEROctetString)s.elementAt(1);
295 con.setPreviousDN(new String(str.getValue(), "UTF8"));
296 } else if (s.elementAt(1) instanceof BERInteger) {
297 BERInteger num = (BERInteger)s.elementAt(1);
298 con.setChangeNumber(num.getValue());
299 }
300 }
301 if (s.size() > 2) {
302 BERInteger num = (BERInteger)s.elementAt(2);
303 con.setChangeNumber(num.getValue());
304 }
305 } catch (Exception e) {
306 return null;
307 }
308
309 return con;
310 }
311
312 /**
313 * Returns an "entry change notification" control if the control is in
314 * the specified array of controls. Use this method to retrieve an "entry
315 * change notification" control included with an entry sent by the server.
316 * <P>
317 *
318 * You can get the controls returned by the server by using the
319 * <CODE>getResponseControls</CODE> method of the
320 * <CODE>LDAPConnection</CODE> class.
321 * <P>
322 *
323 * For example:
324 * <PRE>
325 * ...
326 * LDAPConnection ld = new LDAPConnection();
327 * try {
328 * // Connect to the server, set up the persistent search control,
329 * // and set up the search constraints.
330 * ...
331 *
332 * // Search the directory.
333 * LDAPSearchResults res = ld.search( "o=Airius.com",
334 * LDAPConnection.SCOPE_SUB, "(cn=Barbara*)", attrs, false, cons );
335 *
336 * // Determine if the server sent a control back to you.
337 * LDAPControl[] returnedControls = ld.getResponseControls();
338 * if ( returnedControls != null ) {
339 *
340 * // Get the entry change control.
341 * LDAPEntryChangeControl entryCtrl = null;
342 * for ( int i = 0; i < returnedControls.length; i++ ) {
343 * if ( returnedControls[i] instanceof LDAPEntryChangeControl ) {
344 * entryCtrl = (LDAPEntryChangeControl)returnedControls[i];
345 * break;
346 * }
347 * }
348 * if ( entryCtrl != null ) {
349 *
350 * // Get and print the type of change made to the entry.
351 * int changeType = entryCtrl.getChangeType();
352 * if ( changeType != -1 ) {
353 * System.out.print( "Change made: " );
354 * switch ( changeType ) {
355 * case LDAPPersistSearchControl.ADD:
356 * System.out.println( "Added new entry." );
357 * break;
358 * case LDAPPersistSearchControl.MODIFY:
359 * System.out.println( "Modified entry." );
360 * break;
361 * case LDAPPersistSearchControl.DELETE:
362 * System.out.println( "Deleted entry." );
363 * break;
364 * case LDAPPersistSearchControl.MODDN:
365 * System.out.println( "Renamed entry." );
366 * break;
367 * }
368 * }
369 *
370 * // Get and print the change number corresponding
371 * // to the change.
372 * int changeNumber = entryCtrl.getChangeNumber();
373 * if ( changeNumber != -1 )
374 * System.out.println( "Change log number: " + changeNumber);
375 *
376 * // Get and print the previous DN of the entry,
377 * // if the entry was renamed.
378 * LDAPDN oldDN = entryCtrl.getPreviousDN();
379 * if ( oldDN != null )
380 * System.out.println( "Previous DN: " + oldDN );
381 *
382 * } else {
383 *
384 * System.out.println( "No entry change control." );
385 * }
386 * }
387 * ...
388 * }
389 * ...
390 *
391 * </PRE>
392 *
393 * @param controls an array of <CODE>LDAPControl</CODE> objects,
394 * representing the controls returned by the server
395 * with an entry. To get these controls, use the
396 * <CODE>getResponseControls</CODE> method of the
397 * <CODE>LDAPConnection</CODE> class.
398 * @return an <CODE>LDAPEntryChangeControl</CODE> object representing
399 * the entry change control sent by the server. If no entry change
400 * control was sent, this method returns null.
401 * @see org.ietf.ldap.controls.LDAPEntryChangeControl
402 * @see org.ietf.ldap.LDAPConnection#getResponseControls
403 * @deprecated LDAPEntryChangeControl controls are now automatically
404 * instantiated.
405 */
406 public static LDAPEntryChangeControl parseResponse(LDAPControl[] controls) {
407
408 LDAPPersistSearchControl con = new LDAPPersistSearchControl();
409
410 for (int i=0; (controls != null) && (i < controls.length); i++) {
411
412 // get the entry change control
413 if (controls[i].getID().equals(LDAPEntryChangeControl.ENTRYCHANGED)) {
414 return con.parseResponse(controls[i].getValue());
415 }
416 }
417
418 return null;
419 }
420
421 /**
422 * Creates a "flattened" BER encoding of the persistent search
423 * specifications and returns it as a byte array.
424 * @param changeTypes the change types to monitor on the server
425 * @param changesOnly <code>true</code> to skip the initial search
426 * @param returnECs <code>true</code> if entry change controls are to be
427 * returned with the search results
428 * @return the BER-encoded data.
429 */
430 private byte[] createPersistSearchSpecification(int changeTypes,
431 boolean changesOnly, boolean returnECs) {
432
433 /* A sequence */
434 BERSequence seq = new BERSequence();
435 seq.addElement(new BERInteger(changeTypes));
436 seq.addElement(new BERBoolean(changesOnly));
437 seq.addElement(new BERBoolean(returnECs));
438
439 /* return a byte array */
440 return flattenBER( seq );
441 }
442
443 public String toString() {
444 StringBuffer sb = new StringBuffer("{PersistSearchCtrl:");
445
446 sb.append(" isCritical=");
447 sb.append(isCritical());
448
449 sb.append(" returnEntryChangeCtrls=");
450 sb.append(_returnECs);
451
452 sb.append(" changesOnly=");
453 sb.append(_changesOnly);
454
455 sb.append(" changeTypes=");
456 sb.append(typesToString(_changeTypes));
457
458 sb.append("}");
459
460 return sb.toString();
461 }
462
463
464 /**
465 * This method is also used by LDAPentryChangeControl.toString()
466 */
467 static String typesToString(int changeTypes) {
468 String types = "";
469
470 if ((changeTypes & ADD) != 0) {
471 types += (types.length() > 0) ? "+ADD" : "ADD";
472 }
473 if ((changeTypes & DELETE) != 0) {
474 types += (types.length() > 0) ? "+DEL" : "DEL";
475 }
476 if ((changeTypes & MODIFY) != 0) {
477 types += (types.length() > 0) ? "+MOD" : "MOD";
478 }
479 if ((changeTypes & MODDN) != 0) {
480 types += (types.length() > 0) ? "+MODDN" : "MODDN";
481 }
482 return types;
483 }
484
485 private int _changeTypes = 1;
486 private boolean _changesOnly = false;
487 private boolean _returnECs = false;
488
489 /**
490 * Change type specifying that you want to track additions of new
491 * entries to the directory. You can either specify this change type
492 * when constructing an <CODE>LDAPPersistSearchControl</CODE> or
493 * by using the <CODE>setChangeTypes</CODE> method.
494 * @see org.ietf.ldap.controls.LDAPPersistSearchControl#getChangeTypes
495 * @see org.ietf.ldap.controls.LDAPPersistSearchControl#setChangeTypes
496 */
497 public static final int ADD = 1;
498
499 /**
500 * Change type specifying that you want to track removals of
501 * entries from the directory. You can either specify this change type
502 * when constructing an <CODE>LDAPPersistSearchControl</CODE> or
503 * by using the <CODE>setChangeTypes</CODE> method.
504 * @see org.ietf.ldap.controls.LDAPPersistSearchControl#getChangeTypes
505 * @see org.ietf.ldap.controls.LDAPPersistSearchControl#setChangeTypes
506 */
507 public static final int DELETE = 2;
508
509 /**
510 * Change type specifying that you want to track modifications of
511 * entries in the directory. You can either specify this change type
512 * when constructing an <CODE>LDAPPersistSearchControl</CODE> or
513 * by using the <CODE>setChangeTypes</CODE> method.
514 * @see org.ietf.ldap.controls.LDAPPersistSearchControl#getChangeTypes
515 * @see org.ietf.ldap.controls.LDAPPersistSearchControl#setChangeTypes
516 */
517 public static final int MODIFY = 4;
518
519 /**
520 * Change type specifying that you want to track modifications of the
521 * DNs of entries in the directory. You can either specify this change type
522 * when constructing an <CODE>LDAPPersistSearchControl</CODE> or
523 * by using the <CODE>setChangeTypes</CODE> method.
524 * @see org.ietf.ldap.controls.LDAPPersistSearchControl#getChangeTypes
525 * @see org.ietf.ldap.controls.LDAPPersistSearchControl#setChangeTypes
526 */
527 public static final int MODDN = 8;
528 public final static String PERSISTENTSEARCH = "2.16.840.1.113730.3.4.3";
529 }