001/*
002 * Copyright 2010-2020 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2010-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) 2010-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.extensions;
037
038
039
040import com.unboundid.asn1.ASN1Boolean;
041import com.unboundid.asn1.ASN1Element;
042import com.unboundid.asn1.ASN1OctetString;
043import com.unboundid.asn1.ASN1Sequence;
044import com.unboundid.ldap.sdk.Control;
045import com.unboundid.ldap.sdk.ExtendedRequest;
046import com.unboundid.ldap.sdk.ExtendedResult;
047import com.unboundid.ldap.sdk.LDAPConnection;
048import com.unboundid.ldap.sdk.LDAPException;
049import com.unboundid.ldap.sdk.ResultCode;
050import com.unboundid.util.Debug;
051import com.unboundid.util.NotMutable;
052import com.unboundid.util.ThreadSafety;
053import com.unboundid.util.ThreadSafetyLevel;
054import com.unboundid.util.Validator;
055
056import static com.unboundid.ldap.sdk.extensions.ExtOpMessages.*;
057
058
059
060/**
061 * This class provides an implementation of the end transaction extended
062 * request as defined in
063 * <A HREF="http://www.ietf.org/rfc/rfc5805.txt">RFC 5805</A>.  It may be used
064 * to either commit or abort a transaction that was created using the start
065 * transaction request.  See the documentation for the
066 * {@link StartTransactionExtendedRequest} class for an example of processing an
067 * LDAP transaction.
068 */
069@NotMutable()
070@ThreadSafety(level=ThreadSafetyLevel.NOT_THREADSAFE)
071public final class EndTransactionExtendedRequest
072       extends ExtendedRequest
073{
074  /**
075   * The OID (1.3.6.1.1.21.3) for the end transaction extended request.
076   */
077  public static final String END_TRANSACTION_REQUEST_OID = "1.3.6.1.1.21.3";
078
079
080
081  /**
082   * The serial version UID for this serializable class.
083   */
084  private static final long serialVersionUID = -7135468264026410702L;
085
086
087
088  // The transaction ID for the associated transaction.
089  private final ASN1OctetString transactionID;
090
091  // Indicates whether to commit or abort the associated transaction.
092  private final boolean commit;
093
094
095
096  /**
097   * Creates a new end transaction extended request with the provided
098   * information.
099   *
100   * @param  transactionID  The transaction ID for the transaction to commit or
101   *                        abort.  It must not be {@code null}.
102   * @param  commit         {@code true} if the transaction should be committed,
103   *                        or {@code false} if the transaction should be
104   *                        aborted.
105   * @param  controls       The set of controls to include in the request.
106   */
107  public EndTransactionExtendedRequest(final ASN1OctetString transactionID,
108                                       final boolean commit,
109                                       final Control... controls)
110  {
111    super(END_TRANSACTION_REQUEST_OID, encodeValue(transactionID, commit),
112          controls);
113
114    this.transactionID = transactionID;
115    this.commit        = commit;
116  }
117
118
119
120  /**
121   * Creates a new end transaction extended request from the provided generic
122   * extended request.
123   *
124   * @param  extendedRequest  The generic extended request to use to create this
125   *                          end transaction extended request.
126   *
127   * @throws  LDAPException  If a problem occurs while decoding the request.
128   */
129  public EndTransactionExtendedRequest(final ExtendedRequest extendedRequest)
130         throws LDAPException
131  {
132    super(extendedRequest);
133
134    final ASN1OctetString value = extendedRequest.getValue();
135    if (value == null)
136    {
137      throw new LDAPException(ResultCode.DECODING_ERROR,
138           ERR_END_TXN_REQUEST_NO_VALUE.get());
139    }
140
141    try
142    {
143      final ASN1Element valueElement = ASN1Element.decode(value.getValue());
144      final ASN1Element[] elements =
145           ASN1Sequence.decodeAsSequence(valueElement).elements();
146      if (elements.length == 1)
147      {
148        commit        = true;
149        transactionID = ASN1OctetString.decodeAsOctetString(elements[0]);
150      }
151      else
152      {
153        commit        = ASN1Boolean.decodeAsBoolean(elements[0]).booleanValue();
154        transactionID = ASN1OctetString.decodeAsOctetString(elements[1]);
155      }
156    }
157    catch (final Exception e)
158    {
159      Debug.debugException(e);
160      throw new LDAPException(ResultCode.DECODING_ERROR,
161           ERR_END_TXN_REQUEST_CANNOT_DECODE.get(e), e);
162    }
163  }
164
165
166
167  /**
168   * Generates the value to include in this extended request.
169   *
170   * @param  transactionID  The transaction ID for the transaction to commit or
171   *                        abort.  It must not be {@code null}.
172   * @param  commit         {@code true} if the transaction should be committed,
173   *                        or {@code false} if the transaction should be
174   *                        aborted.
175   *
176   * @return  The ASN.1 octet string containing the encoded request value.
177   */
178  private static ASN1OctetString
179       encodeValue(final ASN1OctetString transactionID,
180                   final boolean commit)
181  {
182    Validator.ensureNotNull(transactionID);
183
184    final ASN1Element[] valueElements;
185    if (commit)
186    {
187      valueElements = new ASN1Element[]
188      {
189        transactionID
190      };
191    }
192    else
193    {
194      valueElements = new ASN1Element[]
195      {
196        new ASN1Boolean(commit),
197        transactionID
198      };
199    }
200
201    return new ASN1OctetString(new ASN1Sequence(valueElements).encode());
202  }
203
204
205
206  /**
207   * Retrieves the transaction ID for the transaction to commit or abort.
208   *
209   * @return  The transaction ID for the transaction to commit or abort.
210   */
211  public ASN1OctetString getTransactionID()
212  {
213    return transactionID;
214  }
215
216
217
218  /**
219   * Indicates whether the transaction should be committed or aborted.
220   *
221   * @return  {@code true} if the transaction should be committed, or
222   *          {@code false} if it should be aborted.
223   */
224  public boolean commit()
225  {
226    return commit;
227  }
228
229
230
231  /**
232   * {@inheritDoc}
233   */
234  @Override()
235  public EndTransactionExtendedResult process(final LDAPConnection connection,
236                                              final int depth)
237         throws LDAPException
238  {
239    final ExtendedResult extendedResponse = super.process(connection, depth);
240    return new EndTransactionExtendedResult(extendedResponse);
241  }
242
243
244
245  /**
246   * {@inheritDoc}
247   */
248  @Override()
249  public EndTransactionExtendedRequest duplicate()
250  {
251    return duplicate(getControls());
252  }
253
254
255
256  /**
257   * {@inheritDoc}
258   */
259  @Override()
260  public EndTransactionExtendedRequest duplicate(final Control[] controls)
261  {
262    final EndTransactionExtendedRequest r =
263         new EndTransactionExtendedRequest(transactionID, commit, controls);
264    r.setResponseTimeoutMillis(getResponseTimeoutMillis(null));
265    return r;
266  }
267
268
269
270  /**
271   * {@inheritDoc}
272   */
273  @Override()
274  public String getExtendedRequestName()
275  {
276    return INFO_EXTENDED_REQUEST_NAME_END_TXN.get();
277  }
278
279
280
281  /**
282   * {@inheritDoc}
283   */
284  @Override()
285  public void toString(final StringBuilder buffer)
286  {
287    buffer.append("EndTransactionExtendedRequest(transactionID='");
288    buffer.append(transactionID.stringValue());
289    buffer.append("', commit=");
290    buffer.append(commit);
291
292    final Control[] controls = getControls();
293    if (controls.length > 0)
294    {
295      buffer.append("controls={");
296      for (int i=0; i < controls.length; i++)
297      {
298        if (i > 0)
299        {
300          buffer.append(", ");
301        }
302
303        buffer.append(controls[i]);
304      }
305      buffer.append('}');
306    }
307
308    buffer.append(')');
309  }
310}