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.DecodeableControl;
043import com.unboundid.ldap.sdk.LDAPException;
044import com.unboundid.ldap.sdk.LDAPResult;
045import com.unboundid.ldap.sdk.ResultCode;
046import com.unboundid.util.NotMutable;
047import com.unboundid.util.NotNull;
048import com.unboundid.util.Nullable;
049import com.unboundid.util.ThreadSafety;
050import com.unboundid.util.ThreadSafetyLevel;
051import com.unboundid.util.Validator;
052
053import static com.unboundid.ldap.sdk.forgerockds.controls.ControlMessages.*;
054
055
056
057/**
058 * This class provides an implementation of a control that may be used to
059 * provide the replication change sequence number (CSN) in the response to an
060 * add, delete, modify, or modify DN request that included the change sequence
061 * number request control.
062 * <BR>
063 * This response control has an OID of 1.3.6.1.4.1.42.2.27.9.5.9, and the value
064 * is the string representation of the change sequence number.  As with all
065 * response controls, the criticality should be false.
066 *
067 * @see  ChangeSequenceNumberRequestControl
068 */
069@NotMutable()
070@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
071public final class ChangeSequenceNumberResponseControl
072       extends Control
073       implements DecodeableControl
074{
075  /**
076   * The OID (1.3.6.1.4.1.42.2.27.9.5.9) for the change sequence number response
077   * control.
078   */
079  @NotNull public static final String CHANGE_SEQUENCE_NUMBER_RESPONSE_OID =
080       "1.3.6.1.4.1.42.2.27.9.5.9";
081
082
083
084  /**
085   * The serial version UID for this serializable class.
086   */
087  private static final long serialVersionUID = 1107845623167919411L;
088
089
090
091  // The change sequence number returned by the server.
092  @NotNull private final String csn;
093
094
095
096  /**
097   * Creates a new empty control instance that is intended to be used only for
098   * decoding controls via the {@code DecodeableControl} interface.
099   */
100  ChangeSequenceNumberResponseControl()
101  {
102    csn = null;
103  }
104
105
106
107  /**
108   * Creates a new change sequence number response control with the provided
109   * CSN.
110   *
111   * @param  csn  The change sequence number returned by the server.  It must
112   *              not be {@code null}.
113   */
114  public ChangeSequenceNumberResponseControl(@NotNull final String csn)
115  {
116    super(CHANGE_SEQUENCE_NUMBER_RESPONSE_OID, false,
117          new ASN1OctetString(csn));
118
119    Validator.ensureNotNull(csn);
120
121    this.csn = csn;
122  }
123
124
125
126  /**
127   * Creates a new change sequence number response control with the provided
128   * information.
129   *
130   * @param  oid         The OID for the control.
131   * @param  isCritical  Indicates whether the control should be marked
132   *                     critical.
133   * @param  value       The encoded value for the control.  This may be
134   *                     {@code null} if no value was provided.
135   *
136   * @throws  LDAPException  If the provided control cannot be decoded as a
137   *                         change sequence number response control.
138   */
139  public ChangeSequenceNumberResponseControl(@NotNull final String oid,
140              final boolean isCritical,
141              @Nullable final ASN1OctetString value)
142         throws LDAPException
143  {
144    super(oid, isCritical, value);
145
146    if (value == null)
147    {
148      throw new LDAPException(ResultCode.DECODING_ERROR,
149           ERR_CSN_RESPONSE_NO_VALUE.get());
150    }
151    else
152    {
153      csn = value.stringValue();
154    }
155  }
156
157
158
159  /**
160   * {@inheritDoc}
161   */
162  @Override()
163  @NotNull()
164  public ChangeSequenceNumberResponseControl
165              decodeControl(@NotNull final String oid, final boolean isCritical,
166                            @Nullable final ASN1OctetString value)
167         throws LDAPException
168  {
169    return new ChangeSequenceNumberResponseControl(oid, isCritical, value);
170  }
171
172
173
174  /**
175   * Extracts a change sequence number response control from the provided
176   * result.
177   *
178   * @param  result  The result from which to retrieve the change sequence
179   *                 number response control.
180   *
181   * @return  The change sequence number response control contained in the
182   *          provided result, or {@code null} if the result did not contain a
183   *          change sequence number response control.
184   *
185   * @throws  LDAPException  If a problem is encountered while attempting to
186   *                         decode the change sequence number response control
187   *                         contained in the provided result.
188   */
189  @Nullable()
190  public static ChangeSequenceNumberResponseControl get(
191              @NotNull final LDAPResult result)
192         throws LDAPException
193  {
194    final Control c =
195         result.getResponseControl(CHANGE_SEQUENCE_NUMBER_RESPONSE_OID);
196    if (c == null)
197    {
198      return null;
199    }
200
201    if (c instanceof ChangeSequenceNumberResponseControl)
202    {
203      return (ChangeSequenceNumberResponseControl) c;
204    }
205    else
206    {
207      return new ChangeSequenceNumberResponseControl(c.getOID(),
208           c.isCritical(), c.getValue());
209    }
210  }
211
212
213
214  /**
215   * Retrieves the CSN returned by the server.
216   *
217   * @return  The CSN returned by the server.
218   */
219  @NotNull()
220  public String getCSN()
221  {
222    return csn;
223  }
224
225
226
227  /**
228   * {@inheritDoc}
229   */
230  @Override()
231  @NotNull()
232  public String getControlName()
233  {
234    return INFO_CONTROL_NAME_CSN_RESPONSE.get();
235  }
236
237
238
239  /**
240   * {@inheritDoc}
241   */
242  @Override()
243  public void toString(@NotNull final StringBuilder buffer)
244  {
245    buffer.append("ChangeSequenceNumberResponseControl(csn='");
246    buffer.append(csn);
247    buffer.append("')");
248  }
249}