View Javadoc
1   /*
2    * Copyright (c) 1999, 2002, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  package com.sun.jndi.ldap;
27  
28  import java.io.IOException;
29  
30  /**
31   * This class implements the LDAPv3 Request Control for the persistent search
32   * mechanism as defined in
33   * <a href="http://www.ietf.org/internet-drafts/draft-ietf-ldapext-psearch-02.txt">draft-ietf-ldapext-psearch-02.txt</a>.
34   *
35   * The control's value has the following ASN.1 definition:
36   * <pre>
37   *
38   *     PersistentSearch ::= SEQUENCE {
39   *         changeTypes INTEGER,
40   *         changesOnly BOOLEAN,
41   *         returnECs BOOLEAN
42   *     }
43   *
44   * </pre>
45   *
46   * @see EntryChangeResponseControl
47   * @author Vincent Ryan
48   */
49  final public class PersistentSearchControl extends BasicControl {
50  
51      /**
52       * The persistent search control's assigned object identifier
53       * is 2.16.840.1.113730.3.4.3.
54       */
55      public static final String OID = "2.16.840.1.113730.3.4.3";
56  
57      /**
58       * Indicates interest in entries which have been added.
59       */
60      public static final int ADD = 1;
61  
62      /**
63       * Indicates interest in entries which have been deleted.
64       */
65      public static final int DELETE = 2;
66  
67      /**
68       * Indicates interest in entries which have been modified.
69       */
70      public static final int MODIFY = 4;
71  
72      /**
73       * Indicates interest in entries which have been renamed.
74       */
75      public static final int RENAME = 8;
76  
77      /**
78       * Indicates interest in entries which have been added, deleted,
79       * modified or renamed.
80       */
81      public static final int ANY = ADD | DELETE | MODIFY | RENAME;
82  
83      /**
84       * The change types of interest. All changes, by default.
85       *
86       * @serial
87       */
88      private int changeTypes = ANY;
89  
90      /**
91       * Return original entries and changed entries or only changed entries.
92       *
93       * @serial
94       */
95      private boolean changesOnly = false;
96  
97      /**
98       * Return entry change controls.
99       *
100      * @serial
101      */
102     private boolean returnControls = true;
103 
104     private static final long serialVersionUID = 6335140491154854116L;
105 
106     /**
107      * Constructs a persistent search non-critical control.
108      * The original entries, any changed entries (additions,
109      * deletions, modifications or renames) and entry change
110      * controls are requested.
111      *
112      * @exception IOException If a BER encoding error occurs.
113      */
114     public PersistentSearchControl() throws IOException {
115         super(OID);
116         super.value = setEncodedValue();
117     }
118 
119     /**
120      * Constructs a persistent search control.
121      *
122      * @param   changeTypes     The change types of interest.
123      * @param   changesOnly     Return original entries and changed entries
124      *                          or only the changed entries.
125      * @param   returnControls  Return entry change controls.
126      * @param   criticality     The control's criticality.
127      * @exception IOException If a BER encoding error occurs.
128      */
129     public PersistentSearchControl(int changeTypes, boolean changesOnly,
130         boolean returnControls, boolean criticality) throws IOException {
131 
132         super(OID, criticality, null);
133         this.changeTypes = changeTypes;
134         this.changesOnly = changesOnly;
135         this.returnControls = returnControls;
136         super.value = setEncodedValue();
137     }
138 
139     /*
140      * Sets the ASN.1 BER encoded value of the persistent search control.
141      * The result is the raw BER bytes including the tag and length of
142      * the control's value. It does not include the controls OID or criticality.
143      *
144      * @return A possibly null byte array representing the ASN.1 BER encoded
145      *         value of the LDAP persistent search control.
146      * @exception IOException If a BER encoding error occurs.
147      */
148     private byte[] setEncodedValue() throws IOException {
149 
150         // build the ASN.1 encoding
151         BerEncoder ber = new BerEncoder(32);
152 
153         ber.beginSeq(Ber.ASN_SEQUENCE | Ber.ASN_CONSTRUCTOR);
154             ber.encodeInt(changeTypes);
155             ber.encodeBoolean(changesOnly);
156             ber.encodeBoolean(returnControls);
157         ber.endSeq();
158 
159         return ber.getTrimmedBuf();
160     }
161 }