001/*
002 * Copyright 2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 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) 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.forgerockds.controls;
037
038
039
040import com.unboundid.asn1.ASN1OctetString;
041import com.unboundid.ldap.sdk.Control;
042import com.unboundid.ldap.sdk.LDAPException;
043import com.unboundid.ldap.sdk.ResultCode;
044import com.unboundid.util.NotMutable;
045import com.unboundid.util.NotNull;
046import com.unboundid.util.ThreadSafety;
047import com.unboundid.util.ThreadSafetyLevel;
048
049import static com.unboundid.ldap.sdk.forgerockds.controls.ControlMessages.*;
050
051
052
053/**
054 * This class provides an implementation of a control that can be used to
055 * specify an external identifier for a request sent to a ForgeRock Directory
056 * Server that will appear in the access log message for the associated
057 * operation.
058 * <BR>
059 * This request control has an OID of 1.3.6.1.4.1.36733.2.1.5.1, and its value
060 * is the string representation of the desired transaction ID.  The control is
061 * typically not marked critical.
062 * <BR><BR>
063 * <H2>Example</H2>
064 * The following example demonstrates the use of the transaction ID request
065 * control:
066 * <PRE>
067 * ModifyRequest modifyRequest = new ModifyRequest("dc=example,dc=com",
068 *      new Modification(ModificationType.REPLACE, "attrName", "attrValue"));
069 * modifyRequest.addControl(new TransactionIDRequestControl("test-txn-id"));
070 * LDAPResult modifyResult = connection.modify(modifyRequest);
071 * </PRE>
072 */
073@NotMutable()
074@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
075public final class TransactionIDRequestControl
076       extends Control
077{
078  /**
079   * The OID (1.3.6.1.4.1.36733.2.1.5.1) for the transaction ID request control.
080   */
081  @NotNull public static final String TRANSACTION_ID_REQUEST_OID =
082       "1.3.6.1.4.1.36733.2.1.5.1";
083
084
085
086  /**
087   * The serial version UID for this serializable class.
088   */
089  private static final long serialVersionUID = 7792760251213801179L;
090
091
092
093  // The transaction ID to use for this control.
094  @NotNull private final String transactionID;
095
096
097
098  /**
099   * Creates a new transaction ID request control with the specified identifier.
100   * It will not be marked critical.
101   *
102   * @param  transactionID  The transaction ID to use for this control.  It must
103   *                        not be {@code null}.
104   */
105  public TransactionIDRequestControl(@NotNull final String transactionID)
106  {
107    this(false, transactionID);
108  }
109
110
111
112  /**
113   * Creates a new transaction ID request control with the specified identifier
114   * and criticality.
115   *
116   * @param  isCritical     Indicates whether the control should be marked
117   *                        critical.
118   * @param  transactionID  The transaction ID to use for this control.  It must
119   *                        not be {@code null}.
120   */
121  public TransactionIDRequestControl(final boolean isCritical,
122                                     @NotNull final String transactionID)
123  {
124    super(TRANSACTION_ID_REQUEST_OID, isCritical,
125         new ASN1OctetString(transactionID));
126
127    this.transactionID = transactionID;
128  }
129
130
131
132  /**
133   * Creates a new transaction ID request control which is decoded from
134   * the provided generic control.
135   *
136   * @param  control  The generic control to be decoded as a transaction ID
137   *                  request control.
138   *
139   * @throws  LDAPException  If the provided control cannot be decoded as a
140   *                         transaction ID request control.
141   */
142  public TransactionIDRequestControl(@NotNull final Control control)
143         throws LDAPException
144  {
145    super(control);
146
147    if (! control.hasValue())
148    {
149      throw new LDAPException(ResultCode.DECODING_ERROR,
150           ERR_TRANSACTION_ID_REQUEST_MISSING_VALUE.get());
151    }
152
153    transactionID = control.getValue().stringValue();
154  }
155
156
157
158  /**
159   * Retrieves the transaction ID to use for this control.
160   *
161   * @return  The transaction ID to use for this control.
162   */
163  @NotNull()
164  public String getTransactionID()
165  {
166    return transactionID;
167  }
168
169
170
171  /**
172   * {@inheritDoc}
173   */
174  @Override()
175  @NotNull()
176  public String getControlName()
177  {
178    return INFO_CONTROL_NAME_TRANSACTION_ID_REQUEST.get();
179  }
180
181
182
183  /**
184   * {@inheritDoc}
185   */
186  @Override()
187  public void toString(@NotNull final StringBuilder buffer)
188  {
189    buffer.append("TransactionIDRequestControl(id='");
190    buffer.append(transactionID);
191    buffer.append("')");
192  }
193}