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