001/* 002 * Copyright 2007-2020 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2007-2020 Ping Identity Corporation 007 * 008 * Licensed under the Apache License, Version 2.0 (the "License"); 009 * you may not use this file except in compliance with the License. 010 * You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, software 015 * distributed under the License is distributed on an "AS IS" BASIS, 016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 017 * See the License for the specific language governing permissions and 018 * limitations under the License. 019 */ 020/* 021 * Copyright (C) 2008-2020 Ping Identity Corporation 022 * 023 * This program is free software; you can redistribute it and/or modify 024 * it under the terms of the GNU General Public License (GPLv2 only) 025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only) 026 * as published by the Free Software Foundation. 027 * 028 * This program is distributed in the hope that it will be useful, 029 * but WITHOUT ANY WARRANTY; without even the implied warranty of 030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 031 * GNU General Public License for more details. 032 * 033 * You should have received a copy of the GNU General Public License 034 * along with this program; if not, see <http://www.gnu.org/licenses>. 035 */ 036package com.unboundid.ldap.sdk.controls; 037 038 039 040import java.util.ArrayList; 041 042import com.unboundid.asn1.ASN1Constants; 043import com.unboundid.asn1.ASN1Element; 044import com.unboundid.asn1.ASN1Enumerated; 045import com.unboundid.asn1.ASN1Exception; 046import com.unboundid.asn1.ASN1Long; 047import com.unboundid.asn1.ASN1OctetString; 048import com.unboundid.asn1.ASN1Sequence; 049import com.unboundid.ldap.sdk.Control; 050import com.unboundid.ldap.sdk.DecodeableControl; 051import com.unboundid.ldap.sdk.LDAPException; 052import com.unboundid.ldap.sdk.ResultCode; 053import com.unboundid.ldap.sdk.SearchResultEntry; 054import com.unboundid.util.Debug; 055import com.unboundid.util.NotMutable; 056import com.unboundid.util.StaticUtils; 057import com.unboundid.util.ThreadSafety; 058import com.unboundid.util.ThreadSafetyLevel; 059import com.unboundid.util.Validator; 060 061import static com.unboundid.ldap.sdk.controls.ControlMessages.*; 062 063 064 065/** 066 * This class provides an implementation of the entry change notification 067 * control as defined in draft-ietf-ldapext-psearch. It will be returned in 068 * search result entries that match the criteria associated with a persistent 069 * search (see the {@link PersistentSearchRequestControl} class) and have been 070 * changed in a way associated with the registered change types for that search. 071 * <BR><BR> 072 * The information that can be included in an entry change notification control 073 * includes: 074 * <UL> 075 * <LI>A change type, which indicates the type of operation that was performed 076 * to trigger this entry change notification control. It will be one of 077 * the values of the {@link PersistentSearchChangeType} enum.</LI> 078 * <LI>An optional previous DN, which indicates the DN that the entry had 079 * before the associated operation was processed. It will only be present 080 * if the associated operation was a modify DN operation.</LI> 081 * <LI>An optional change number, which may be used to retrieve additional 082 * information about the associated operation from the server. This may 083 * not be available in all directory server implementations.</LI> 084 * </UL> 085 * Note that the entry change notification control should only be included in 086 * search result entries that are associated with a search request that included 087 * the persistent search request control, and only if that persistent search 088 * request control had the {@code returnECs} flag set to {@code true} to 089 * indicate that entry change notification controls should be included in 090 * resulting entries. Further, the entry change notification control will only 091 * be included in entries that are returned as the result of a change in the 092 * server and not any of the preliminary entries that may be returned if the 093 * corresponding persistent search request had the {@code changesOnly} flag set 094 * to {@code false}. 095 */ 096@NotMutable() 097@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 098public final class EntryChangeNotificationControl 099 extends Control 100 implements DecodeableControl 101{ 102 /** 103 * The OID (2.16.840.1.113730.3.4.7) for the entry change notification 104 * control. 105 */ 106 public static final String ENTRY_CHANGE_NOTIFICATION_OID = 107 "2.16.840.1.113730.3.4.7"; 108 109 110 111 /** 112 * The serial version UID for this serializable class. 113 */ 114 private static final long serialVersionUID = -1305357948140939303L; 115 116 117 118 // The change number for the change, if available. 119 private final long changeNumber; 120 121 // The change type for the change. 122 private final PersistentSearchChangeType changeType; 123 124 // The previous DN of the entry, if applicable. 125 private final String previousDN; 126 127 128 129 /** 130 * Creates a new empty control instance that is intended to be used only for 131 * decoding controls via the {@code DecodeableControl} interface. 132 */ 133 EntryChangeNotificationControl() 134 { 135 changeNumber = -1; 136 changeType = null; 137 previousDN = null; 138 } 139 140 141 142 /** 143 * Creates a new entry change notification control with the provided 144 * information. It will not be critical. 145 * 146 * @param changeType The change type for the change. It must not be 147 * {@code null}. 148 * @param previousDN The previous DN of the entry, if applicable. 149 * @param changeNumber The change number to include in this control, or 150 * -1 if there should not be a change number. 151 */ 152 public EntryChangeNotificationControl( 153 final PersistentSearchChangeType changeType, 154 final String previousDN, final long changeNumber) 155 { 156 this(changeType, previousDN, changeNumber, false); 157 } 158 159 160 161 /** 162 * Creates a new entry change notification control with the provided 163 * information. 164 * 165 * @param changeType The change type for the change. It must not be 166 * {@code null}. 167 * @param previousDN The previous DN of the entry, if applicable. 168 * @param changeNumber The change number to include in this control, or 169 * -1 if there should not be a change number. 170 * @param isCritical Indicates whether this control should be marked 171 * critical. Response controls should generally not be 172 * critical. 173 */ 174 public EntryChangeNotificationControl( 175 final PersistentSearchChangeType changeType, 176 final String previousDN, final long changeNumber, 177 final boolean isCritical) 178 { 179 super(ENTRY_CHANGE_NOTIFICATION_OID, isCritical, 180 encodeValue(changeType, previousDN, changeNumber)); 181 182 this.changeType = changeType; 183 this.previousDN = previousDN; 184 this.changeNumber = changeNumber; 185 } 186 187 188 189 /** 190 * Creates a new entry change notification control with the provided 191 * information. 192 * 193 * @param oid The OID for the control. 194 * @param isCritical Indicates whether the control should be marked 195 * critical. 196 * @param value The encoded value for the control. This may be 197 * {@code null} if no value was provided. 198 * 199 * @throws LDAPException If the provided control cannot be decoded as an 200 * entry change notification control. 201 */ 202 public EntryChangeNotificationControl(final String oid, 203 final boolean isCritical, 204 final ASN1OctetString value) 205 throws LDAPException 206 { 207 super(oid, isCritical, value); 208 209 if (value == null) 210 { 211 throw new LDAPException(ResultCode.DECODING_ERROR, 212 ERR_ECN_NO_VALUE.get()); 213 } 214 215 final ASN1Sequence ecnSequence; 216 try 217 { 218 final ASN1Element element = ASN1Element.decode(value.getValue()); 219 ecnSequence = ASN1Sequence.decodeAsSequence(element); 220 } 221 catch (final ASN1Exception ae) 222 { 223 Debug.debugException(ae); 224 throw new LDAPException(ResultCode.DECODING_ERROR, 225 ERR_ECN_VALUE_NOT_SEQUENCE.get(ae), ae); 226 } 227 228 final ASN1Element[] ecnElements = ecnSequence.elements(); 229 if ((ecnElements.length < 1) || (ecnElements.length > 3)) 230 { 231 throw new LDAPException(ResultCode.DECODING_ERROR, 232 ERR_ECN_INVALID_ELEMENT_COUNT.get( 233 ecnElements.length)); 234 } 235 236 final ASN1Enumerated ecnEnumerated; 237 try 238 { 239 ecnEnumerated = ASN1Enumerated.decodeAsEnumerated(ecnElements[0]); 240 } 241 catch (final ASN1Exception ae) 242 { 243 Debug.debugException(ae); 244 throw new LDAPException(ResultCode.DECODING_ERROR, 245 ERR_ECN_FIRST_NOT_ENUMERATED.get(ae), ae); 246 } 247 248 changeType = PersistentSearchChangeType.valueOf(ecnEnumerated.intValue()); 249 if (changeType == null) 250 { 251 throw new LDAPException(ResultCode.DECODING_ERROR, 252 ERR_ECN_INVALID_CHANGE_TYPE.get( 253 ecnEnumerated.intValue())); 254 } 255 256 257 String prevDN = null; 258 long chgNum = -1; 259 for (int i=1; i < ecnElements.length; i++) 260 { 261 switch (ecnElements[i].getType()) 262 { 263 case ASN1Constants.UNIVERSAL_OCTET_STRING_TYPE: 264 prevDN = ASN1OctetString.decodeAsOctetString( 265 ecnElements[i]).stringValue(); 266 break; 267 268 case ASN1Constants.UNIVERSAL_INTEGER_TYPE: 269 try 270 { 271 chgNum = ASN1Long.decodeAsLong(ecnElements[i]).longValue(); 272 } 273 catch (final ASN1Exception ae) 274 { 275 Debug.debugException(ae); 276 throw new LDAPException(ResultCode.DECODING_ERROR, 277 ERR_ECN_CANNOT_DECODE_CHANGE_NUMBER.get(ae), ae); 278 } 279 break; 280 281 default: 282 throw new LDAPException(ResultCode.DECODING_ERROR, 283 ERR_ECN_INVALID_ELEMENT_TYPE.get( 284 StaticUtils.toHex(ecnElements[i].getType()))); 285 } 286 } 287 288 previousDN = prevDN; 289 changeNumber = chgNum; 290 } 291 292 293 294 /** 295 * {@inheritDoc} 296 */ 297 @Override() 298 public EntryChangeNotificationControl 299 decodeControl(final String oid, final boolean isCritical, 300 final ASN1OctetString value) 301 throws LDAPException 302 { 303 return new EntryChangeNotificationControl(oid, isCritical, value); 304 } 305 306 307 308 /** 309 * Extracts an entry change notification control from the provided search 310 * result entry. 311 * 312 * @param entry The search result entry from which to retrieve the entry 313 * change notification control. 314 * 315 * @return The entry change notification control contained in the provided 316 * search result entry, or {@code null} if the entry did not contain 317 * an entry change notification control. 318 * 319 * @throws LDAPException If a problem is encountered while attempting to 320 * decode the entry change notification control 321 * contained in the provided entry. 322 */ 323 public static EntryChangeNotificationControl 324 get(final SearchResultEntry entry) 325 throws LDAPException 326 { 327 final Control c = entry.getControl(ENTRY_CHANGE_NOTIFICATION_OID); 328 if (c == null) 329 { 330 return null; 331 } 332 333 if (c instanceof EntryChangeNotificationControl) 334 { 335 return (EntryChangeNotificationControl) c; 336 } 337 else 338 { 339 return new EntryChangeNotificationControl(c.getOID(), c.isCritical(), 340 c.getValue()); 341 } 342 } 343 344 345 346 /** 347 * Encodes the provided information into an octet string that can be used as 348 * the value for this control. 349 * 350 * @param changeType The change type for the change. It must not be 351 * {@code null}. 352 * @param previousDN The previous DN of the entry, if applicable. 353 * @param changeNumber The change number to include in this control, or 354 * -1 if there should not be a change number. 355 * 356 * @return An ASN.1 octet string that can be used as the value for this 357 * control. 358 */ 359 private static ASN1OctetString encodeValue( 360 final PersistentSearchChangeType changeType, 361 final String previousDN, final long changeNumber) 362 { 363 Validator.ensureNotNull(changeType); 364 365 final ArrayList<ASN1Element> elementList = new ArrayList<>(3); 366 elementList.add(new ASN1Enumerated(changeType.intValue())); 367 368 if (previousDN != null) 369 { 370 elementList.add(new ASN1OctetString(previousDN)); 371 } 372 373 if (changeNumber > 0) 374 { 375 elementList.add(new ASN1Long(changeNumber)); 376 } 377 378 return new ASN1OctetString(new ASN1Sequence(elementList).encode()); 379 } 380 381 382 383 /** 384 * Retrieves the change type for this entry change notification control. 385 * 386 * @return The change type for this entry change notification control. 387 */ 388 public PersistentSearchChangeType getChangeType() 389 { 390 return changeType; 391 } 392 393 394 395 /** 396 * Retrieves the previous DN for the entry, if applicable. 397 * 398 * @return The previous DN for the entry, or {@code null} if there is none. 399 */ 400 public String getPreviousDN() 401 { 402 return previousDN; 403 } 404 405 406 407 /** 408 * Retrieves the change number for the associated change, if available. 409 * 410 * @return The change number for the associated change, or -1 if none was 411 * provided. 412 */ 413 public long getChangeNumber() 414 { 415 return changeNumber; 416 } 417 418 419 420 /** 421 * {@inheritDoc} 422 */ 423 @Override() 424 public String getControlName() 425 { 426 return INFO_CONTROL_NAME_ENTRY_CHANGE_NOTIFICATION.get(); 427 } 428 429 430 431 /** 432 * {@inheritDoc} 433 */ 434 @Override() 435 public void toString(final StringBuilder buffer) 436 { 437 buffer.append("EntryChangeNotificationControl(changeType="); 438 buffer.append(changeType.getName()); 439 440 if (previousDN != null) 441 { 442 buffer.append(", previousDN='"); 443 buffer.append(previousDN); 444 buffer.append('\''); 445 } 446 447 if (changeNumber > 0) 448 { 449 buffer.append(", changeNumber="); 450 buffer.append(changeNumber); 451 } 452 453 buffer.append(", isCritical="); 454 buffer.append(isCritical()); 455 buffer.append(')'); 456 } 457}