001/*
002 * Copyright 2010-2023 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2010-2023 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-2023 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.extensions;
037
038
039
040import com.unboundid.asn1.ASN1Element;
041import com.unboundid.asn1.ASN1OctetString;
042import com.unboundid.asn1.ASN1Sequence;
043import com.unboundid.ldap.sdk.Control;
044import com.unboundid.ldap.sdk.IntermediateResponse;
045import com.unboundid.ldap.sdk.LDAPException;
046import com.unboundid.ldap.sdk.ResultCode;
047import com.unboundid.util.Debug;
048import com.unboundid.util.NotMutable;
049import com.unboundid.util.NotNull;
050import com.unboundid.util.Nullable;
051import com.unboundid.util.StaticUtils;
052import com.unboundid.util.ThreadSafety;
053import com.unboundid.util.ThreadSafetyLevel;
054
055import static com.unboundid.ldap.sdk.unboundidds.extensions.ExtOpMessages.*;
056
057
058
059/**
060 * This class provides an implementation of an intermediate response which
061 * indicates that the Directory Server may have already purged information about
062 * one or more changes.
063 * <BR>
064 * <BLOCKQUOTE>
065 *   <B>NOTE:</B>  This class, and other classes within the
066 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
067 *   supported for use against Ping Identity, UnboundID, and
068 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
069 *   for proprietary functionality or for external specifications that are not
070 *   considered stable or mature enough to be guaranteed to work in an
071 *   interoperable way with other types of LDAP servers.
072 * </BLOCKQUOTE>
073 * <BR>
074 * The missing changelog entries intermediate response value may be present, and
075 * if it is then it will have the following encoding:
076 * <PRE>
077 *   MissingEntriesIntermediateResponse ::= SEQUENCE {
078 *        message     [0] OCTET STRING OPTIONAL,
079 *        ... }
080 * </PRE>
081 */
082@NotMutable()
083@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
084public final class MissingChangelogEntriesIntermediateResponse
085       extends IntermediateResponse
086{
087  /**
088   * The OID (1.3.6.1.4.1.30221.2.6.12) for the get stream directory values
089   * intermediate response.
090   */
091  @NotNull public static final String
092       MISSING_CHANGELOG_ENTRIES_INTERMEDIATE_RESPONSE_OID =
093            "1.3.6.1.4.1.30221.2.6.12";
094
095
096
097  /**
098   * The BER type for the response message.
099   */
100  private static final byte TYPE_MESSAGE = (byte) 0x80;
101
102
103
104  /**
105   * The serial version UID for this serializable class.
106   */
107  private static final long serialVersionUID = -4961560327295588578L;
108
109
110
111  // A message which may provide additional information about the missing
112  // changes.
113  @Nullable private final String message;
114
115
116
117  /**
118   * Creates a new missing changelog entries intermediate response with the
119   * provided information.
120   *
121   * @param  message   A message which may provide additional information about
122   *                   the missing changes.  It may be {@code null} if no
123   *                   message is available.
124   * @param  controls  The set of controls to include in the intermediate
125   *                   response.  It may be {@code null} or empty if no controls
126   *                   should be included.
127   */
128  public MissingChangelogEntriesIntermediateResponse(
129              @Nullable final String message,
130              @Nullable final Control... controls)
131  {
132    super(MISSING_CHANGELOG_ENTRIES_INTERMEDIATE_RESPONSE_OID,
133          encodeValue(message), controls);
134
135    this.message = message;
136  }
137
138
139
140  /**
141   * Creates a new missing changelog entries intermediate response from the
142   * provided generic intermediate response.
143   *
144   * @param  r  The generic intermediate response to be decoded.
145   *
146   * @throws  LDAPException  If the provided intermediate response cannot be
147   *                         decoded as a missing changelog entries response.
148   */
149  public MissingChangelogEntriesIntermediateResponse(
150              @NotNull final IntermediateResponse r)
151         throws LDAPException
152  {
153    super(r);
154
155    final ASN1OctetString value = r.getValue();
156    if (value == null)
157    {
158      message = null;
159      return;
160    }
161
162    final ASN1Sequence valueSequence;
163    try
164    {
165      valueSequence = ASN1Sequence.decodeAsSequence(value.getValue());
166    }
167    catch (final Exception e)
168    {
169      Debug.debugException(e);
170      throw new LDAPException(ResultCode.DECODING_ERROR,
171           ERR_MISSING_CHANGELOG_ENTRIES_IR_VALUE_NOT_SEQUENCE.get(
172                StaticUtils.getExceptionMessage(e)), e);
173    }
174
175    String msg = null;
176    for (final ASN1Element e : valueSequence.elements())
177    {
178      final byte type = e.getType();
179      switch (type)
180      {
181        case TYPE_MESSAGE:
182          msg = ASN1OctetString.decodeAsOctetString(e).stringValue();
183          break;
184        default:
185          throw new LDAPException(ResultCode.DECODING_ERROR,
186               ERR_MISSING_CHANGELOG_ENTRIES_IR_UNEXPECTED_VALUE_TYPE.get(
187                    StaticUtils.toHex(type)));
188      }
189    }
190
191    message = msg;
192  }
193
194
195
196  /**
197   * Encodes the provided information in a form suitable for use as the value of
198   * this intermediate response.
199   *
200   * @param  message  A message which may provide additional information about
201   *                  the missing changes.  It may be {@code null} if no message
202   *                  is available.
203   *
204   * @return  The encoded value, or {@code null} if no value should be included
205   *          in the intermediate response.
206   */
207  @Nullable()
208  private static ASN1OctetString encodeValue(@Nullable final String message)
209  {
210    if (message == null)
211    {
212      return null;
213    }
214
215    final ASN1Sequence valueSequence = new ASN1Sequence(
216         new ASN1OctetString(TYPE_MESSAGE, message));
217    return new ASN1OctetString(valueSequence.encode());
218  }
219
220
221
222  /**
223   * Retrieves a message which may provide additional information about the
224   * missing changes.
225   *
226   * @return  A message which may provide additional information about the
227   *          missing changes, or {@code null} if none is available.
228   */
229  @Nullable()
230  public String getMessage()
231  {
232    return message;
233  }
234
235
236
237  /**
238   * {@inheritDoc}
239   */
240  @Override()
241  @NotNull()
242  public String getIntermediateResponseName()
243  {
244    return INFO_MISSING_CHANGELOG_ENTRIES_IR_NAME.get();
245  }
246
247
248
249  /**
250   * {@inheritDoc}
251   */
252  @Override()
253  @Nullable()
254  public String valueToString()
255  {
256    if (message == null)
257    {
258      return null;
259    }
260
261    final StringBuilder buffer = new StringBuilder();
262
263    buffer.append("message='");
264    buffer.append(message);
265    buffer.append('\'');
266
267    return buffer.toString();
268  }
269
270
271
272  /**
273   * {@inheritDoc}
274   */
275  @Override()
276  public void toString(@NotNull final StringBuilder buffer)
277  {
278    buffer.append("MissingChangelogEntriesIntermediateResponse(");
279
280    boolean appended = false;
281    final int messageID = getMessageID();
282    if (messageID >= 0)
283    {
284      buffer.append("messageID=");
285      buffer.append(messageID);
286      appended = true;
287    }
288
289    if (message != null)
290    {
291      if (appended)
292      {
293        buffer.append(", ");
294      }
295
296      buffer.append("message='");
297      buffer.append(message);
298      buffer.append('\'');
299      appended = true;
300    }
301
302    final Control[] controls = getControls();
303    if (controls.length > 0)
304    {
305      if (appended)
306      {
307        buffer.append(", ");
308      }
309
310      buffer.append("controls={");
311      for (int i=0; i < controls.length; i++)
312      {
313        if (i > 0)
314        {
315          buffer.append(", ");
316        }
317
318        buffer.append(controls[i]);
319      }
320      buffer.append('}');
321    }
322
323    buffer.append(')');
324  }
325}