001/* 002 * Copyright 2010-2024 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2010-2024 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) 2010-2024 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.unboundidds.extensions; 037 038 039 040import java.util.ArrayList; 041import java.util.Collection; 042 043import com.unboundid.asn1.ASN1Element; 044import com.unboundid.asn1.ASN1OctetString; 045import com.unboundid.asn1.ASN1Sequence; 046import com.unboundid.ldap.sdk.Attribute; 047import com.unboundid.ldap.sdk.ChangeLogEntry; 048import com.unboundid.ldap.sdk.Control; 049import com.unboundid.ldap.sdk.Entry; 050import com.unboundid.ldap.sdk.IntermediateResponse; 051import com.unboundid.ldap.sdk.LDAPException; 052import com.unboundid.ldap.sdk.LDAPRuntimeException; 053import com.unboundid.ldap.sdk.ResultCode; 054import com.unboundid.ldap.sdk.unboundidds.UnboundIDChangeLogEntry; 055import com.unboundid.util.Base64; 056import com.unboundid.util.Debug; 057import com.unboundid.util.NotMutable; 058import com.unboundid.util.NotNull; 059import com.unboundid.util.Nullable; 060import com.unboundid.util.StaticUtils; 061import com.unboundid.util.ThreadSafety; 062import com.unboundid.util.ThreadSafetyLevel; 063import com.unboundid.util.Validator; 064 065import static com.unboundid.ldap.sdk.unboundidds.extensions.ExtOpMessages.*; 066 067 068 069/** 070 * This class provides an implementation of an intermediate response which 071 * provides information about a changelog entry returned from a Directory 072 * Server. 073 * <BR> 074 * <BLOCKQUOTE> 075 * <B>NOTE:</B> This class, and other classes within the 076 * {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only 077 * supported for use against Ping Identity, UnboundID, and 078 * Nokia/Alcatel-Lucent 8661 server products. These classes provide support 079 * for proprietary functionality or for external specifications that are not 080 * considered stable or mature enough to be guaranteed to work in an 081 * interoperable way with other types of LDAP servers. 082 * </BLOCKQUOTE> 083 * <BR> 084 * The changelog entry intermediate response value is encoded as follows: 085 * <PRE> 086 * ChangelogEntryIntermediateResponse ::= SEQUENCE { 087 * resumeToken OCTET STRING, 088 * serverID OCTET STRING, 089 * changelogEntryDN LDAPDN, 090 * changelogEntryAttributes PartialAttributeList, 091 * ... } 092 * </PRE> 093 */ 094@NotMutable() 095@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 096public final class ChangelogEntryIntermediateResponse 097 extends IntermediateResponse 098{ 099 /** 100 * The OID (1.3.6.1.4.1.30221.2.6.11) for the get stream directory values 101 * intermediate response. 102 */ 103 @NotNull public static final String 104 CHANGELOG_ENTRY_INTERMEDIATE_RESPONSE_OID = "1.3.6.1.4.1.30221.2.6.11"; 105 106 107 108 /** 109 * The serial version UID for this serializable class. 110 */ 111 private static final long serialVersionUID = 5616371094806687752L; 112 113 114 115 // A token that may be used to start retrieving changelog entries 116 // immediately after this entry. 117 @NotNull private final ASN1OctetString resumeToken; 118 119 // The changelog entry included in this intermediate response. 120 @NotNull private final UnboundIDChangeLogEntry changeLogEntry; 121 122 // The server ID for the server from which the changelog entry was retrieved. 123 @NotNull private final String serverID; 124 125 126 127 /** 128 * Creates a new changelog entry intermediate response with the provided 129 * information. 130 * 131 * @param changeLogEntry The changelog entry included in this intermediate 132 * response. It must not be {@code null}. 133 * @param serverID The server ID for the server from which the 134 * changelog entry was received. It must not be 135 * {@code null}. 136 * @param resumeToken A token that may be used to resume the process of 137 * retrieving changes at the point immediately after 138 * this change. It must not be {@code null}. 139 * @param controls The set of controls to include in the response. It 140 * may be {@code null} or empty if no controls should 141 * be included. 142 */ 143 public ChangelogEntryIntermediateResponse( 144 @NotNull final ChangeLogEntry changeLogEntry, 145 @NotNull final String serverID, 146 @NotNull final ASN1OctetString resumeToken, 147 @Nullable final Control... controls) 148 { 149 super(CHANGELOG_ENTRY_INTERMEDIATE_RESPONSE_OID, 150 encodeValue(changeLogEntry, serverID, resumeToken), controls); 151 152 if (changeLogEntry instanceof UnboundIDChangeLogEntry) 153 { 154 this.changeLogEntry = (UnboundIDChangeLogEntry) changeLogEntry; 155 } 156 else 157 { 158 try 159 { 160 this.changeLogEntry = new UnboundIDChangeLogEntry(changeLogEntry); 161 } 162 catch (final LDAPException le) 163 { 164 // This should never happen. 165 Debug.debugException(le); 166 throw new LDAPRuntimeException(le); 167 } 168 } 169 170 this.serverID = serverID; 171 this.resumeToken = resumeToken; 172 } 173 174 175 176 /** 177 * Creates a new changelog entry intermediate response from the provided 178 * generic intermediate response. 179 * 180 * @param r The generic intermediate response to be decoded. 181 * 182 * @throws LDAPException If the provided intermediate response cannot be 183 * decoded as a changelog entry response. 184 */ 185 public ChangelogEntryIntermediateResponse( 186 @NotNull final IntermediateResponse r) 187 throws LDAPException 188 { 189 super(r); 190 191 final ASN1OctetString value = r.getValue(); 192 if (value == null) 193 { 194 throw new LDAPException(ResultCode.DECODING_ERROR, 195 ERR_CHANGELOG_ENTRY_IR_NO_VALUE.get()); 196 } 197 198 final ASN1Sequence valueSequence; 199 try 200 { 201 valueSequence = ASN1Sequence.decodeAsSequence(value.getValue()); 202 } 203 catch (final Exception e) 204 { 205 Debug.debugException(e); 206 throw new LDAPException(ResultCode.DECODING_ERROR, 207 ERR_CHANGELOG_ENTRY_IR_VALUE_NOT_SEQUENCE.get( 208 StaticUtils.getExceptionMessage(e)), e); 209 } 210 211 final ASN1Element[] valueElements = valueSequence.elements(); 212 if (valueElements.length != 4) 213 { 214 throw new LDAPException(ResultCode.DECODING_ERROR, 215 ERR_CHANGELOG_ENTRY_IR_INVALID_VALUE_COUNT.get( 216 valueElements.length)); 217 } 218 219 resumeToken = ASN1OctetString.decodeAsOctetString(valueElements[0]); 220 221 serverID = 222 ASN1OctetString.decodeAsOctetString(valueElements[1]).stringValue(); 223 224 final String dn = 225 ASN1OctetString.decodeAsOctetString(valueElements[2]).stringValue(); 226 227 try 228 { 229 final ASN1Element[] attrsElements = 230 ASN1Sequence.decodeAsSequence(valueElements[3]).elements(); 231 final ArrayList<Attribute> attributes = 232 new ArrayList<>(attrsElements.length); 233 for (final ASN1Element e : attrsElements) 234 { 235 attributes.add(Attribute.decode(ASN1Sequence.decodeAsSequence(e))); 236 } 237 238 changeLogEntry = new UnboundIDChangeLogEntry(new Entry(dn, attributes)); 239 } 240 catch (final Exception e) 241 { 242 Debug.debugException(e); 243 throw new LDAPException(ResultCode.DECODING_ERROR, 244 ERR_CHANGELOG_ENTRY_IR_ERROR_PARSING_VALUE.get( 245 StaticUtils.getExceptionMessage(e)), e); 246 } 247 } 248 249 250 251 /** 252 * Encodes the provided information in a form suitable for use as the value of 253 * this intermediate response. 254 * 255 * @param changeLogEntry The changelog entry included in this intermediate 256 * response. 257 * @param serverID The server ID for the server from which the 258 * changelog entry was received. 259 * @param resumeToken A token that may be used to resume the process of 260 * retrieving changes at the point immediately after 261 * this change. 262 * 263 * @return The encoded value. 264 */ 265 @NotNull() 266 private static ASN1OctetString encodeValue( 267 @NotNull final ChangeLogEntry changeLogEntry, 268 @NotNull final String serverID, 269 @NotNull final ASN1OctetString resumeToken) 270 { 271 Validator.ensureNotNull(changeLogEntry); 272 Validator.ensureNotNull(serverID); 273 Validator.ensureNotNull(resumeToken); 274 275 final Collection<Attribute> attrs = changeLogEntry.getAttributes(); 276 final ArrayList<ASN1Element> attrElements = 277 new ArrayList<>(attrs.size()); 278 for (final Attribute a : attrs) 279 { 280 attrElements.add(a.encode()); 281 } 282 283 final ASN1Sequence s = new ASN1Sequence( 284 resumeToken, 285 new ASN1OctetString(serverID), 286 new ASN1OctetString(changeLogEntry.getDN()), 287 new ASN1Sequence(attrElements)); 288 289 return new ASN1OctetString(s.encode()); 290 } 291 292 293 294 /** 295 * Retrieves the changelog entry contained in this intermediate response. 296 * 297 * @return The changelog entry contained in this intermediate response. 298 */ 299 @NotNull() 300 public UnboundIDChangeLogEntry getChangeLogEntry() 301 { 302 return changeLogEntry; 303 } 304 305 306 307 /** 308 * Retrieves the server ID for the server from which the changelog entry was 309 * retrieved. 310 * 311 * @return The server ID for the server from which the changelog entry was 312 * retrieved. 313 */ 314 @NotNull() 315 public String getServerID() 316 { 317 return serverID; 318 } 319 320 321 322 /** 323 * Retrieves a token that may be used to resume the process of retrieving 324 * changes at the point immediately after this change. 325 * 326 * @return A token that may be used to resume the process of retrieving 327 * changes at the point immediately after this change. 328 */ 329 @NotNull() 330 public ASN1OctetString getResumeToken() 331 { 332 return resumeToken; 333 } 334 335 336 337 /** 338 * {@inheritDoc} 339 */ 340 @Override() 341 @NotNull() 342 public String getIntermediateResponseName() 343 { 344 return INFO_CHANGELOG_ENTRY_IR_NAME.get(); 345 } 346 347 348 349 /** 350 * {@inheritDoc} 351 */ 352 @Override() 353 @NotNull() 354 public String valueToString() 355 { 356 final StringBuilder buffer = new StringBuilder(); 357 358 buffer.append("changeNumber='"); 359 buffer.append(changeLogEntry.getChangeNumber()); 360 buffer.append("' changeType='"); 361 buffer.append(changeLogEntry.getChangeType().getName()); 362 buffer.append("' targetDN='"); 363 buffer.append(changeLogEntry.getTargetDN()); 364 buffer.append("' serverID='"); 365 buffer.append(serverID); 366 buffer.append("' resumeToken='"); 367 Base64.encode(resumeToken.getValue(), buffer); 368 buffer.append('\''); 369 370 return buffer.toString(); 371 } 372 373 374 375 /** 376 * {@inheritDoc} 377 */ 378 @Override() 379 public void toString(@NotNull final StringBuilder buffer) 380 { 381 buffer.append("ChangelogEntryIntermediateResponse("); 382 383 final int messageID = getMessageID(); 384 if (messageID >= 0) 385 { 386 buffer.append("messageID="); 387 buffer.append(messageID); 388 buffer.append(", "); 389 } 390 391 buffer.append("changelogEntry="); 392 changeLogEntry.toString(buffer); 393 buffer.append(", serverID='"); 394 buffer.append(serverID); 395 buffer.append("', resumeToken='"); 396 Base64.encode(resumeToken.getValue(), buffer); 397 buffer.append('\''); 398 399 final Control[] controls = getControls(); 400 if (controls.length > 0) 401 { 402 buffer.append(", controls={"); 403 for (int i=0; i < controls.length; i++) 404 { 405 if (i > 0) 406 { 407 buffer.append(", "); 408 } 409 410 buffer.append(controls[i]); 411 } 412 buffer.append('}'); 413 } 414 415 buffer.append(')'); 416 } 417}