001/* 002 * Copyright 2008-2020 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2008-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.unboundidds.controls; 037 038 039 040import java.util.ArrayList; 041 042import com.unboundid.asn1.ASN1Boolean; 043import com.unboundid.asn1.ASN1Element; 044import com.unboundid.asn1.ASN1OctetString; 045import com.unboundid.asn1.ASN1Sequence; 046import com.unboundid.ldap.sdk.Control; 047import com.unboundid.ldap.sdk.LDAPException; 048import com.unboundid.ldap.sdk.ResultCode; 049import com.unboundid.ldap.sdk.unboundidds.extensions. 050 StartInteractiveTransactionExtendedRequest; 051import com.unboundid.ldap.sdk.unboundidds.extensions. 052 StartInteractiveTransactionExtendedResult; 053import com.unboundid.util.NotMutable; 054import com.unboundid.util.NotNull; 055import com.unboundid.util.StaticUtils; 056import com.unboundid.util.ThreadSafety; 057import com.unboundid.util.ThreadSafetyLevel; 058import com.unboundid.util.Validator; 059 060import static com.unboundid.ldap.sdk.unboundidds.controls.ControlMessages.*; 061 062 063 064/** 065 * This class provides an implementation of the interactive transaction 066 * specification request control, which may be used to indicate that the 067 * associated operation is part of an interactive transaction. It may be used 068 * in conjunction with add, compare, delete, modify, modify DN, and search 069 * requests, as well as some types of extended requests. The transaction should 070 * be created with the start interactive transaction extended request, and the 071 * end interactive transaction extended request may be used to commit or abort 072 * the associated transaction. 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 elements of the interactive transaction specification request control may 085 * include: 086 * <UL> 087 * <LI><CODE>txnID</CODE> -- The transaction ID for the transaction, which was 088 * obtained from a previous 089 * {@link StartInteractiveTransactionExtendedResult}.</LI> 090 * <LI><CODE>abortOnFailure</CODE> -- Indicates whether the transaction should 091 * be aborted if the request associated with this control does not 092 * complete successfully.</LI> 093 * <LI><CODE>writeLock</CODE> -- Indicates whether the target entry may be 094 * altered by this or a subsequent operation which is part of the 095 * transaction. It should generally be {@code false} only for read 096 * operations in which it is known that the target entry will not be 097 * altered by a subsequent operation.</LI> 098 * </UL> 099 * See the documentation for the 100 * {@link StartInteractiveTransactionExtendedRequest} class for an example of 101 * processing an interactive transaction. 102 */ 103@NotMutable() 104@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 105public final class InteractiveTransactionSpecificationRequestControl 106 extends Control 107{ 108 /** 109 * The OID (1.3.6.1.4.1.30221.2.5.4) for the interactive transaction 110 * specification request control. 111 */ 112 @NotNull public static final String 113 INTERACTIVE_TRANSACTION_SPECIFICATION_REQUEST_OID = 114 "1.3.6.1.4.1.30221.2.5.4"; 115 116 117 118 /** 119 * The BER type for the {@code txnID} element of the control value. 120 */ 121 private static final byte TYPE_TXN_ID = (byte) 0x80; 122 123 124 125 /** 126 * The BER type for the {@code abortOnFailure} element of the control value. 127 */ 128 private static final byte TYPE_ABORT_ON_FAILURE = (byte) 0x81; 129 130 131 132 /** 133 * The BER type for the {@code writeLock} element of the control value. 134 */ 135 private static final byte TYPE_WRITE_LOCK = (byte) 0x82; 136 137 138 139 /** 140 * The serial version UID for this serializable class. 141 */ 142 private static final long serialVersionUID = -6473934815135786621L; 143 144 145 146 // The transaction ID for the associated transaction. 147 @NotNull private final ASN1OctetString transactionID; 148 149 // Indicates whether the transaction should be aborted if the associated 150 // operation does not complete successfully. 151 private final boolean abortOnFailure; 152 153 // Indicates whether the server should attempt to obtain a write lock on the 154 // target entry if the associated operation is a read operation. 155 private final boolean writeLock; 156 157 158 159 /** 160 * Creates a new interactive transaction specification request control with 161 * the provided transaction ID. The server will attempt to keep the 162 * transaction active in the event of a failure and will obtain write locks on 163 * targeted entries. 164 * 165 * @param transactionID The transaction ID for the associated transaction, 166 * as obtained from the start interactive transaction 167 * extended operation. It must not be {@code null}. 168 */ 169 public InteractiveTransactionSpecificationRequestControl( 170 @NotNull final ASN1OctetString transactionID) 171 { 172 this(transactionID, false, true); 173 } 174 175 176 177 /** 178 * Creates a new interactive transaction specification request control with 179 * the provided information. 180 * 181 * @param transactionID The transaction ID for the associated transaction, 182 * as obtained from the start interactive transaction 183 * extended operation. It must not be {@code null}. 184 * @param abortOnFailure Indicates whether the transaction should be aborted 185 * if the associated operation does not complete 186 * successfully. 187 * @param writeLock Indicates whether the server should attempt to 188 * obtain a write lock on the target entry. This 189 * should only be {@code false} if the associated 190 * operation is a search or compare and it is known 191 * that the target entry will not be updated later in 192 * the transaction. 193 */ 194 public InteractiveTransactionSpecificationRequestControl( 195 @NotNull final ASN1OctetString transactionID, 196 final boolean abortOnFailure, 197 final boolean writeLock) 198 { 199 super(INTERACTIVE_TRANSACTION_SPECIFICATION_REQUEST_OID, true, 200 encodeValue(transactionID, abortOnFailure, writeLock)); 201 202 this.transactionID = transactionID; 203 this.abortOnFailure = abortOnFailure; 204 this.writeLock = writeLock; 205 } 206 207 208 209 /** 210 * Creates a new interactive transaction specification request control which 211 * is decoded from the provided generic control. 212 * 213 * @param control The generic control to be decoded as an interactive 214 * transaction specification request control. 215 * 216 * @throws LDAPException If the provided control cannot be decoded as an 217 * interactive transaction specification request 218 * control. 219 */ 220 public InteractiveTransactionSpecificationRequestControl( 221 @NotNull final Control control) 222 throws LDAPException 223 { 224 super(control); 225 226 if (! control.hasValue()) 227 { 228 throw new LDAPException(ResultCode.DECODING_ERROR, 229 ERR_INT_TXN_REQUEST_NO_VALUE.get()); 230 } 231 232 final ASN1Element[] elements; 233 try 234 { 235 final ASN1Element e = ASN1Element.decode(control.getValue().getValue()); 236 elements = ASN1Sequence.decodeAsSequence(e).elements(); 237 } 238 catch (final Exception e) 239 { 240 throw new LDAPException(ResultCode.DECODING_ERROR, 241 ERR_INT_TXN_REQUEST_VALUE_NOT_SEQUENCE.get(e.getMessage()), e); 242 } 243 244 ASN1OctetString txnID = null; 245 boolean shouldAbortOnFailure = false; 246 boolean shouldWriteLock = true; 247 248 for (final ASN1Element element : elements) 249 { 250 switch (element.getType()) 251 { 252 case TYPE_TXN_ID: 253 txnID = ASN1OctetString.decodeAsOctetString(element); 254 break; 255 case TYPE_ABORT_ON_FAILURE: 256 try 257 { 258 shouldAbortOnFailure = 259 ASN1Boolean.decodeAsBoolean(element).booleanValue(); 260 } 261 catch (final Exception e) 262 { 263 throw new LDAPException(ResultCode.DECODING_ERROR, 264 ERR_INT_TXN_REQUEST_ABORT_ON_FAILURE_NOT_BOOLEAN.get( 265 e.getMessage()), e); 266 } 267 break; 268 case TYPE_WRITE_LOCK: 269 try 270 { 271 shouldWriteLock = 272 ASN1Boolean.decodeAsBoolean(element).booleanValue(); 273 } 274 catch (final Exception e) 275 { 276 throw new LDAPException(ResultCode.DECODING_ERROR, 277 ERR_INT_TXN_REQUEST_WRITE_LOCK_NOT_BOOLEAN.get(e.getMessage()), 278 e); 279 } 280 break; 281 default: 282 throw new LDAPException(ResultCode.DECODING_ERROR, 283 ERR_INT_TXN_REQUEST_INVALID_ELEMENT_TYPE.get( 284 StaticUtils.toHex(element.getType()))); 285 } 286 } 287 288 if (txnID == null) 289 { 290 throw new LDAPException(ResultCode.DECODING_ERROR, 291 ERR_INT_TXN_REQUEST_NO_TXN_ID.get()); 292 } 293 294 transactionID = txnID; 295 abortOnFailure = shouldAbortOnFailure; 296 writeLock = shouldWriteLock; 297 } 298 299 300 301 /** 302 * Encodes the provided information into an ASN.1 octet string suitable for 303 * use as the value of this control. 304 * 305 * @param transactionID The transaction ID for the associated transaction, 306 * as obtained from the start interactive transaction 307 * extended operation. It must not be {@code null}. 308 * @param abortOnFailure Indicates whether the transaction should be aborted 309 * if the associated operation does not complete 310 * successfully. 311 * @param writeLock Indicates whether the server should attempt to 312 * obtain a write lock on the target entry. This 313 * should only be {@code false} if the associated 314 * operation is a search or compare and it is known 315 * that the target entry will not be updated later in 316 * the transaction. 317 * 318 * @return The ASN.1 octet string containing the encoded value for this 319 * control. 320 */ 321 @NotNull() 322 private static ASN1OctetString encodeValue( 323 @NotNull final ASN1OctetString transactionID, 324 final boolean abortOnFailure, final boolean writeLock) 325 { 326 Validator.ensureNotNull(transactionID); 327 328 final ArrayList<ASN1Element> elements = new ArrayList<>(3); 329 elements.add(new ASN1OctetString(TYPE_TXN_ID, transactionID.getValue())); 330 331 if (abortOnFailure) 332 { 333 elements.add(new ASN1Boolean(TYPE_ABORT_ON_FAILURE, abortOnFailure)); 334 } 335 336 if (! writeLock) 337 { 338 elements.add(new ASN1Boolean(TYPE_WRITE_LOCK, writeLock)); 339 } 340 341 return new ASN1OctetString(new ASN1Sequence(elements).encode()); 342 } 343 344 345 346 /** 347 * Retrieves the transaction ID for the associated transaction. 348 * 349 * @return The transaction ID for the associated transaction. 350 */ 351 @NotNull() 352 public ASN1OctetString getTransactionID() 353 { 354 return transactionID; 355 } 356 357 358 359 /** 360 * Indicates whether the transaction should be aborted if the associated 361 * operation does not complete successfully. 362 * 363 * @return {@code true} if the transaction should be aborted if the 364 * associated operation does not complete successfully, or 365 * {@code false} if the server should attempt to keep the transaction 366 * active if the associated operation does not complete successfully. 367 */ 368 public boolean abortOnFailure() 369 { 370 return abortOnFailure; 371 } 372 373 374 375 /** 376 * Indicates whether the server should attempt to obtain a write lock on 377 * entries targeted by the associated operation. 378 * 379 * @return {@code true} if the server should attempt to obtain a write lock 380 * on entries targeted by the associated operation, or {@code false} 381 * if a read lock is acceptable as the entries are not expected to 382 * be altered later in the transaction. 383 */ 384 public boolean writeLock() 385 { 386 return writeLock; 387 } 388 389 390 391 /** 392 * {@inheritDoc} 393 */ 394 @Override() 395 @NotNull() 396 public String getControlName() 397 { 398 return INFO_CONTROL_NAME_INTERACTIVE_TXN_REQUEST.get(); 399 } 400 401 402 403 /** 404 * {@inheritDoc} 405 */ 406 @Override() 407 public void toString(@NotNull final StringBuilder buffer) 408 { 409 buffer.append("InteractiveTransactionSpecificationRequestControl(" + 410 "transactionID='"); 411 buffer.append(transactionID.stringValue()); 412 buffer.append("', abortOnFailure="); 413 buffer.append(abortOnFailure); 414 buffer.append(", writeLock="); 415 buffer.append(writeLock); 416 buffer.append(')'); 417 } 418}