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