| 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) 2001 |
| 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 | import java.io.IOException; |
| 42 | import java.io.UnsupportedEncodingException; |
| 43 | |
| 44 | import org.ietf.ldap.LDAPControl; |
| 45 | import org.ietf.ldap.LDAPException; |
| 46 | import org.ietf.ldap.ber.stream.BERElement; |
| 47 | import org.ietf.ldap.ber.stream.BEREnumerated; |
| 48 | import org.ietf.ldap.ber.stream.BERInteger; |
| 49 | import org.ietf.ldap.ber.stream.BEROctetString; |
| 50 | import org.ietf.ldap.ber.stream.BERSequence; |
| 51 | import org.ietf.ldap.client.JDAPBERTagDecoder; |
| 52 | |
| 53 | /** |
| 54 | * Represents an LDAP v3 server control that specifies information |
| 55 | * about a change to an entry in the directory. (The OID for this |
| 56 | * control is 2.16.840.1.113730.3.4.7.) You need to use this control in |
| 57 | * conjunction with a "persistent search" control (represented |
| 58 | * by <CODE>LDAPPersistentSearchControl</CODE> object. |
| 59 | * <P> |
| 60 | * |
| 61 | * To use persistent searching for change notification, you create a |
| 62 | * "persistent search" control that specifies the types of changes that |
| 63 | * you want to track. When an entry is changed, the server sends that |
| 64 | * entry back to your client and may include an "entry change notification" |
| 65 | * control that specifies additional information about the change. |
| 66 | * <P> |
| 67 | * |
| 68 | * Typically, you use the <CODE>getResponseControls</CODE> method of |
| 69 | * the <CODE>LDAPConnection</CODE> object to get any |
| 70 | * <CODE>LDAPEntryChangeControl</CODE> objects returned by the server. |
| 71 | * <P> |
| 72 | * |
| 73 | * Once you retrieve an <CODE>LDAPEntryChangeControl</CODE> object from |
| 74 | * the server, you can get the following additional information about |
| 75 | * the change made to the entry: |
| 76 | * <P> |
| 77 | * |
| 78 | * <UL> |
| 79 | * <LI>The type of change made (add, modify, delete, or modify DN) |
| 80 | * <LI>The change number identifying the record of the change in the |
| 81 | * change log (if the server supports change logs) |
| 82 | * <LI>If the entry was renamed, the old DN of the entry |
| 83 | * </UL> |
| 84 | * <P> |
| 85 | * |
| 86 | * @see org.ietf.ldap.controls.LDAPPersistSearchControl |
| 87 | * @see org.ietf.ldap.LDAPConnection#getResponseControls |
| 88 | */ |
| 89 | |
| 90 | public class LDAPEntryChangeControl extends LDAPControl { |
| 91 | |
| 92 | /** |
| 93 | * Constructs a new <CODE>LDAPEntryChangeControl</CODE> object. |
| 94 | * @see org.ietf.ldap.LDAPControl |
| 95 | * @see org.ietf.ldap.controls.LDAPPersistSearchControl |
| 96 | */ |
| 97 | public LDAPEntryChangeControl() { |
| 98 | super(ENTRYCHANGED, false, null); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Contructs an <CODE>LDAPEntryChangedControl</CODE> object. |
| 103 | * This constructor is used by <CODE>LDAPControl.register</CODE> to |
| 104 | * instantiate entry change controls. |
| 105 | * @param oid this parameter must be |
| 106 | * <CODE>LDAPEntryChangeControl.ENTRYCHANGED</CODE> |
| 107 | * or an <CODE>LDAPException</CODE> is thrown |
| 108 | * @param critical <code>true</code> if this control is critical |
| 109 | * @param value the value associated with this control |
| 110 | * @exception org.ietf.ldap.LDAPException If oid is not |
| 111 | * <CODE>LDAPEntryChangeControl.ENTRYCHANGED</CODE>. |
| 112 | * @exception java.io.IOException If value is not a valid BER sequence. |
| 113 | * @see org.ietf.ldap.LDAPControl#register |
| 114 | */ |
| 115 | public LDAPEntryChangeControl(String oid, boolean critical, byte[] value) |
| 116 | throws LDAPException, IOException { |
| 117 | super(ENTRYCHANGED, false, value); |
| 118 | |
| 119 | if (!oid.equals( ENTRYCHANGED )) { |
| 120 | throw new LDAPException("oid must be LDAPEntryChangeControl." + |
| 121 | "ENTRYCHANGED", LDAPException.PARAM_ERROR); |
| 122 | } |
| 123 | |
| 124 | |
| 125 | ByteArrayInputStream inStream = new ByteArrayInputStream(_value); |
| 126 | BERSequence seq = new BERSequence(); |
| 127 | JDAPBERTagDecoder decoder = new JDAPBERTagDecoder(); |
| 128 | int[] numRead = new int[1]; |
| 129 | numRead[0] = 0; |
| 130 | |
| 131 | BERSequence s = (BERSequence)BERElement.getElement(decoder, inStream, |
| 132 | numRead); |
| 133 | |
| 134 | BEREnumerated itr = (BEREnumerated)s.elementAt(0); |
| 135 | |
| 136 | _changeTypes = itr.getValue(); |
| 137 | |
| 138 | if (s.size() > 1) { |
| 139 | if (s.elementAt(1) instanceof BEROctetString) { |
| 140 | BEROctetString str = (BEROctetString)s.elementAt(1); |
| 141 | try { |
| 142 | _previousDN = new String(str.getValue(), "UTF8"); |
| 143 | } catch (UnsupportedEncodingException e) { |
| 144 | } |
| 145 | } else if (s.elementAt(1) instanceof BERInteger) { |
| 146 | BERInteger num = (BERInteger)s.elementAt(1); |
| 147 | _changeNumber = num.getValue(); |
| 148 | } |
| 149 | } |
| 150 | if (s.size() > 2) { |
| 151 | BERInteger num = (BERInteger)s.elementAt(2); |
| 152 | _changeNumber = num.getValue(); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Sets the change number (which identifies the record of the change |
| 158 | * in the server's change log) in this "entry change notification" |
| 159 | * control. |
| 160 | * @param num change number to set |
| 161 | * @see org.ietf.ldap.controls.LDAPEntryChangeControl#getChangeNumber |
| 162 | */ |
| 163 | public void setChangeNumber(int num) { |
| 164 | _changeNumber = num; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Sets the change type (which identifies the type of change |
| 169 | * that occurred) in this "entry change notification" control. |
| 170 | * @param num change type to set. This can be one of |
| 171 | * the following values: |
| 172 | * <P> |
| 173 | * |
| 174 | * <UL> |
| 175 | * <LI><CODE>LDAPPersistSearchControl.ADD</CODE> (a new entry was |
| 176 | * added to the directory) |
| 177 | * <LI><CODE>LDAPPersistSearchControl.DELETE</CODE> (an entry was |
| 178 | * removed from the directory) |
| 179 | * <LI><CODE>LDAPPersistSearchControl.MODIFY</CODE> (an entry was |
| 180 | * modified) |
| 181 | * <LI><CODE>LDAPPersistSearchControl.MODDN</CODE> (an entry was |
| 182 | * renamed) |
| 183 | * </UL> |
| 184 | * <P> |
| 185 | * |
| 186 | * @see org.ietf.ldap.controls.LDAPEntryChangeControl#getChangeType |
| 187 | */ |
| 188 | public void setChangeType(int num) { |
| 189 | _changeTypes = num; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Sets the previous DN of the entry (if the entry was renamed) |
| 194 | * in the "entry change notification control". |
| 195 | * @param dn the previous distinguished name of the entry |
| 196 | * @see org.ietf.ldap.controls.LDAPEntryChangeControl#getPreviousDN |
| 197 | */ |
| 198 | public void setPreviousDN(String dn) { |
| 199 | _previousDN = dn; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Gets the change number, which identifies the record of the change |
| 204 | * in the server's change log. |
| 205 | * @return change number identifying the change made. |
| 206 | * @see org.ietf.ldap.controls.LDAPEntryChangeControl#setChangeNumber |
| 207 | */ |
| 208 | public int getChangeNumber() { |
| 209 | return _changeNumber; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Gets the change type, which identifies the type of change |
| 214 | * that occurred. |
| 215 | * @return change type identifying the type of change that |
| 216 | * occurred. This can be one of the following values: |
| 217 | * <P> |
| 218 | * |
| 219 | * <UL> |
| 220 | * <LI><CODE>LDAPPersistSearchControl.ADD</CODE> (a new entry was |
| 221 | * added to the directory) |
| 222 | * <LI><CODE>LDAPPersistSearchControl.DELETE</CODE> (an entry was |
| 223 | * removed from the directory) |
| 224 | * <LI><CODE>LDAPPersistSearchControl.MODIFY</CODE> (an entry was |
| 225 | * modified) |
| 226 | * <LI><CODE>LDAPPersistSearchControl.MODDN</CODE> (an entry was |
| 227 | * renamed) |
| 228 | * </UL> |
| 229 | * <P> |
| 230 | * |
| 231 | * @see org.ietf.ldap.controls.LDAPEntryChangeControl#setChangeType |
| 232 | */ |
| 233 | public int getChangeType() { |
| 234 | return _changeTypes; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Gets the previous DN of the entry (if the entry was renamed). |
| 239 | * @return the previous distinguished name of the entry. |
| 240 | * @see org.ietf.ldap.controls.LDAPEntryChangeControl#setPreviousDN |
| 241 | */ |
| 242 | public String getPreviousDN() { |
| 243 | return _previousDN; |
| 244 | } |
| 245 | |
| 246 | public String toString() { |
| 247 | StringBuffer sb = new StringBuffer("{EntryChangedCtrl:"); |
| 248 | |
| 249 | sb.append(" isCritical="); |
| 250 | sb.append(isCritical()); |
| 251 | |
| 252 | sb.append(" changeTypes="); |
| 253 | sb.append(LDAPPersistSearchControl.typesToString(_changeTypes)); |
| 254 | |
| 255 | sb.append(" previousDN="); |
| 256 | sb.append(_previousDN); |
| 257 | |
| 258 | sb.append(" changeNumber="); |
| 259 | sb.append(_changeNumber); |
| 260 | |
| 261 | sb.append("}"); |
| 262 | |
| 263 | return sb.toString(); |
| 264 | } |
| 265 | |
| 266 | private int _changeNumber = -1; |
| 267 | private int _changeTypes = -1; |
| 268 | private String _previousDN = null; |
| 269 | public final static String ENTRYCHANGED = "2.16.840.1.113730.3.4.7"; |
| 270 | |
| 271 | } |
| 272 |