001/*
002 * Copyright 2010-2018 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2010-2018 Ping Identity Corporation
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU General Public License (GPLv2 only)
010 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
011 * as published by the Free Software Foundation.
012 *
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program; if not, see <http://www.gnu.org/licenses>.
020 */
021package com.unboundid.ldap.sdk.extensions;
022
023
024
025import com.unboundid.ldap.sdk.Control;
026import com.unboundid.ldap.sdk.ExtendedRequest;
027import com.unboundid.ldap.sdk.ExtendedResult;
028import com.unboundid.ldap.sdk.LDAPConnection;
029import com.unboundid.ldap.sdk.LDAPException;
030import com.unboundid.ldap.sdk.ResultCode;
031import com.unboundid.ldap.sdk.controls.TransactionSpecificationRequestControl;
032import com.unboundid.util.NotMutable;
033import com.unboundid.util.ThreadSafety;
034import com.unboundid.util.ThreadSafetyLevel;
035
036import static com.unboundid.ldap.sdk.extensions.ExtOpMessages.*;
037
038
039
040/**
041 * This class provides an implementation of the start transaction extended
042 * request as defined in
043 * <A HREF="http://www.ietf.org/rfc/rfc5805.txt">RFC 5805</A>.  It may be used
044 * to begin a transaction that allows multiple write operations to be processed
045 * as a single atomic unit.  The {@link StartTransactionExtendedResult} that is
046 * returned will include a transaction ID.  For each operation that is performed
047 * as part of the transaction, this transaction ID should be included in the
048 * corresponding request through the
049 * {@link TransactionSpecificationRequestControl}.  Finally, after all requests
050 * for the transaction have been submitted to the server, the
051 * {@link EndTransactionExtendedRequest} should be used to commit that
052 * transaction, or it may also be used to abort the transaction if it is decided
053 * that it is no longer needed.
054 * <BR><BR>
055 * <H2>Example</H2>
056 * The following example demonstrates the process for using LDAP  transactions.
057 * It will modify two different entries as a single atomic unit.
058 * <PRE>
059 * // Use the start transaction extended operation to begin a transaction.
060 * StartTransactionExtendedResult startTxnResult;
061 * try
062 * {
063 *   startTxnResult = (StartTransactionExtendedResult)
064 *        connection.processExtendedOperation(
065 *             new StartTransactionExtendedRequest());
066 *   // This doesn't necessarily mean that the operation was successful, since
067 *   // some kinds of extended operations return non-success results under
068 *   // normal conditions.
069 * }
070 * catch (LDAPException le)
071 * {
072 *   // For an extended operation, this generally means that a problem was
073 *   // encountered while trying to send the request or read the result.
074 *   startTxnResult = new StartTransactionExtendedResult(
075 *        new ExtendedResult(le));
076 * }
077 * LDAPTestUtils.assertResultCodeEquals(startTxnResult, ResultCode.SUCCESS);
078 * ASN1OctetString txnID = startTxnResult.getTransactionID();
079 *
080 *
081 * // At this point, we have a transaction available for use.  If any problem
082 * // arises, we want to ensure that the transaction is aborted, so create a
083 * // try block to process the operations and a finally block to commit or
084 * // abort the transaction.
085 * boolean commit = false;
086 * try
087 * {
088 *   // Create and process a modify operation to update a first entry as part
089 *   // of the transaction.  Make sure to include the transaction specification
090 *   // control in the request to indicate that it should be part of the
091 *   // transaction.
092 *   ModifyRequest firstModifyRequest = new ModifyRequest(
093 *        "cn=first,dc=example,dc=com",
094 *        new Modification(ModificationType.REPLACE, "description", "first"));
095 *   firstModifyRequest.addControl(
096 *        new TransactionSpecificationRequestControl(txnID));
097 *   LDAPResult firstModifyResult;
098 *   try
099 *   {
100 *     firstModifyResult = connection.modify(firstModifyRequest);
101 *   }
102 *   catch (LDAPException le)
103 *   {
104 *     firstModifyResult = le.toLDAPResult();
105 *   }
106 *   LDAPTestUtils.assertResultCodeEquals(firstModifyResult,
107 *        ResultCode.SUCCESS);
108 *
109 *   // Perform a second modify operation as part of the transaction.
110 *   ModifyRequest secondModifyRequest = new ModifyRequest(
111 *        "cn=second,dc=example,dc=com",
112 *        new Modification(ModificationType.REPLACE, "description", "second"));
113 *   secondModifyRequest.addControl(
114 *        new TransactionSpecificationRequestControl(txnID));
115 *   LDAPResult secondModifyResult;
116 *   try
117 *   {
118 *     secondModifyResult = connection.modify(secondModifyRequest);
119 *   }
120 *   catch (LDAPException le)
121 *   {
122 *     secondModifyResult = le.toLDAPResult();
123 *   }
124 *   LDAPTestUtils.assertResultCodeEquals(secondModifyResult,
125 *        ResultCode.SUCCESS);
126 *
127 *   // If we've gotten here, then all writes have been processed successfully
128 *   // and we can indicate that the transaction should be committed rather
129 *   // than aborted.
130 *   commit = true;
131 * }
132 * finally
133 * {
134 *   // Commit or abort the transaction.
135 *   EndTransactionExtendedResult endTxnResult;
136 *   try
137 *   {
138 *     endTxnResult = (EndTransactionExtendedResult)
139 *          connection.processExtendedOperation(
140 *               new EndTransactionExtendedRequest(txnID, commit));
141 *   }
142 *   catch (LDAPException le)
143 *   {
144 *     endTxnResult = new EndTransactionExtendedResult(new ExtendedResult(le));
145 *   }
146 *   LDAPTestUtils.assertResultCodeEquals(endTxnResult, ResultCode.SUCCESS);
147 * }
148 * </PRE>
149 */
150@NotMutable()
151@ThreadSafety(level=ThreadSafetyLevel.NOT_THREADSAFE)
152public final class StartTransactionExtendedRequest
153       extends ExtendedRequest
154{
155  /**
156   * The OID (1.3.6.1.1.21.1) for the start transaction extended request.
157   */
158  public static final String START_TRANSACTION_REQUEST_OID = "1.3.6.1.1.21.1";
159
160
161  /**
162   * The serial version UID for this serializable class.
163   */
164  private static final long serialVersionUID = 7382735226826929629L;
165
166
167
168  /**
169   * Creates a new start transaction extended request.
170   */
171  public StartTransactionExtendedRequest()
172  {
173    super(START_TRANSACTION_REQUEST_OID);
174  }
175
176
177
178  /**
179   * Creates a new start transaction extended request.
180   *
181   * @param  controls  The set of controls to include in the request.
182   */
183  public StartTransactionExtendedRequest(final Control[] controls)
184  {
185    super(START_TRANSACTION_REQUEST_OID, controls);
186  }
187
188
189
190  /**
191   * Creates a new start transaction extended request from the provided generic
192   * extended request.
193   *
194   * @param  extendedRequest  The generic extended request to use to create this
195   *                          start transaction extended request.
196   *
197   * @throws  LDAPException  If a problem occurs while decoding the request.
198   */
199  public StartTransactionExtendedRequest(final ExtendedRequest extendedRequest)
200         throws LDAPException
201  {
202    super(extendedRequest);
203
204    if (extendedRequest.hasValue())
205    {
206      throw new LDAPException(ResultCode.DECODING_ERROR,
207           ERR_START_TXN_REQUEST_HAS_VALUE.get());
208    }
209  }
210
211
212
213  /**
214   * {@inheritDoc}
215   */
216  @Override()
217  public StartTransactionExtendedResult process(
218              final LDAPConnection connection, final int depth)
219         throws LDAPException
220  {
221    final ExtendedResult extendedResponse = super.process(connection, depth);
222    return new StartTransactionExtendedResult(extendedResponse);
223  }
224
225
226
227  /**
228   * {@inheritDoc}
229   */
230  @Override()
231  public StartTransactionExtendedRequest duplicate()
232  {
233    return duplicate(getControls());
234  }
235
236
237
238  /**
239   * {@inheritDoc}
240   */
241  @Override()
242  public StartTransactionExtendedRequest duplicate(final Control[] controls)
243  {
244    final StartTransactionExtendedRequest r =
245         new StartTransactionExtendedRequest(controls);
246    r.setResponseTimeoutMillis(getResponseTimeoutMillis(null));
247    return r;
248  }
249
250
251
252  /**
253   * {@inheritDoc}
254   */
255  @Override()
256  public String getExtendedRequestName()
257  {
258    return INFO_EXTENDED_REQUEST_NAME_START_TXN.get();
259  }
260
261
262
263  /**
264   * {@inheritDoc}
265   */
266  @Override()
267  public void toString(final StringBuilder buffer)
268  {
269    buffer.append("StartTransactionExtendedRequest(");
270
271    final Control[] controls = getControls();
272    if (controls.length > 0)
273    {
274      buffer.append("controls={");
275      for (int i=0; i < controls.length; i++)
276      {
277        if (i > 0)
278        {
279          buffer.append(", ");
280        }
281
282        buffer.append(controls[i]);
283      }
284      buffer.append('}');
285    }
286
287    buffer.append(')');
288  }
289}