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