001/*
002 * Copyright 2013-2020 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2013-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 java.util.ArrayList;
041import java.util.Collection;
042import java.util.Collections;
043import java.util.Iterator;
044import java.util.List;
045
046import com.unboundid.asn1.ASN1Element;
047import com.unboundid.asn1.ASN1OctetString;
048import com.unboundid.asn1.ASN1Sequence;
049import com.unboundid.ldap.sdk.Control;
050import com.unboundid.ldap.sdk.ExtendedResult;
051import com.unboundid.ldap.sdk.LDAPException;
052import com.unboundid.ldap.sdk.ResultCode;
053import com.unboundid.util.Debug;
054import com.unboundid.util.StaticUtils;
055import com.unboundid.util.ThreadSafety;
056import com.unboundid.util.ThreadSafetyLevel;
057import com.unboundid.util.Validator;
058
059import static com.unboundid.ldap.sdk.unboundidds.extensions.ExtOpMessages.*;
060
061
062
063/**
064 * This class provides an implementation of an extended result that can be used
065 * to retrieve backup compatibility data for a Directory Server backend.
066 * <BR>
067 * <BLOCKQUOTE>
068 *   <B>NOTE:</B>  This class, and other classes within the
069 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
070 *   supported for use against Ping Identity, UnboundID, and
071 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
072 *   for proprietary functionality or for external specifications that are not
073 *   considered stable or mature enough to be guaranteed to work in an
074 *   interoperable way with other types of LDAP servers.
075 * </BLOCKQUOTE>
076 * <BR>
077 * The OID for this extended result is 1.3.6.1.4.1.30221.2.6.31.  If the request
078 * was processed successfully, then the response will have a value with the
079 * following encoding:
080 * <PRE>
081 *   GetBackupCompatibilityDescriptorResult ::= SEQUENCE {
082 *        descriptor     [0] OCTET STRING,
083 *        properties     [1] SEQUENCE OF OCTET STRING OPTIONAL,
084 *        ... }
085 * </PRE>
086 *
087 * @see  GetBackupCompatibilityDescriptorExtendedRequest
088 * @see  IdentifyBackupCompatibilityProblemsExtendedRequest
089 */
090@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
091public final class GetBackupCompatibilityDescriptorExtendedResult
092       extends ExtendedResult
093{
094  /**
095   * The OID (1.3.6.1.4.1.30221.2.6.31) for the get backup compatibility
096   * descriptor extended result.
097   */
098  public static final String GET_BACKUP_COMPATIBILITY_DESCRIPTOR_RESULT_OID =
099       "1.3.6.1.4.1.30221.2.6.31";
100
101
102
103  /**
104   * The BER type for the descriptor element in the value sequence.
105   */
106  private static final byte TYPE_DESCRIPTOR = (byte) 0x80;
107
108
109
110  /**
111   * The BER type for the properties element in the value sequence.
112   */
113  private static final byte TYPE_PROPERTIES = (byte) 0xA1;
114
115
116
117  /**
118   * The serial version UID for this serializable class.
119   */
120  private static final long serialVersionUID = -2493658329210480765L;
121
122
123
124  // The backup compatibility descriptor token.
125  private final ASN1OctetString descriptor;
126
127  // A list of properties providing information about the backup compatibility
128  // descriptor.
129  private final List<String> properties;
130
131
132
133  /**
134   * Creates a new get backup compatibility descriptor extended result from the
135   * provided generic extended result.
136   *
137   * @param  result  The generic extended result to be decoded as a get backup
138   *                 compatibility descriptor extended result.
139   *
140   * @throws LDAPException  If the provided extended result cannot be parsed as
141   *                        a valid get backup compatibility descriptor
142   *                        extended result.
143   */
144  public GetBackupCompatibilityDescriptorExtendedResult(
145       final ExtendedResult result)
146       throws LDAPException
147  {
148    super(result);
149
150    final ASN1OctetString value = result.getValue();
151    if (value == null)
152    {
153      descriptor = null;
154      properties = Collections.emptyList();
155      return;
156    }
157
158    try
159    {
160      final ASN1Element[] elements =
161           ASN1Sequence.decodeAsSequence(value.getValue()).elements();
162      descriptor = elements[0].decodeAsOctetString();
163
164      if (elements.length > 1)
165      {
166        final ASN1Element[] propElements =
167             ASN1Sequence.decodeAsSequence(elements[1]).elements();
168        final ArrayList<String> propList = new ArrayList<>(propElements.length);
169        for (final ASN1Element e : propElements)
170        {
171          propList.add(ASN1OctetString.decodeAsOctetString(e).stringValue());
172        }
173        properties = Collections.unmodifiableList(propList);
174      }
175      else
176      {
177        properties = Collections.emptyList();
178      }
179    }
180    catch (final Exception e)
181    {
182      Debug.debugException(e);
183      throw new LDAPException(ResultCode.DECODING_ERROR,
184           ERR_GET_BACKUP_COMPAT_RESULT_ERROR_PARSING_VALUE.get(
185                StaticUtils.getExceptionMessage(e)),
186           e);
187    }
188  }
189
190
191
192  /**
193   * Creates a new get backup compatibility descriptor extended result with the
194   * provided information.
195   *
196   * @param  messageID          The message ID for the LDAP message that is
197   *                            associated with this LDAP result.
198   * @param  resultCode         The result code from the response.
199   * @param  diagnosticMessage  The diagnostic message from the response, if
200   *                            available.
201   * @param  matchedDN          The matched DN from the response, if available.
202   * @param  referralURLs       The set of referral URLs from the response, if
203   *                            available.
204   * @param  descriptor         The backup compatibility descriptor value.  It
205   *                            may be {@code null} for an unsuccessful result.
206   * @param  properties         A list of properties that provide information
207   *                            about the way the descriptor may be used.  It
208   *                            may be {@code null} or empty for an unsuccessful
209   *                            result, or if there are no properties.
210   * @param  responseControls   The set of controls from the response, if
211   *                            available.
212   */
213  public GetBackupCompatibilityDescriptorExtendedResult(final int messageID,
214              final ResultCode resultCode, final String diagnosticMessage,
215              final String matchedDN, final String[] referralURLs,
216              final ASN1OctetString descriptor,
217              final Collection<String> properties,
218              final Control... responseControls)
219  {
220    super(messageID, resultCode, diagnosticMessage, matchedDN, referralURLs,
221         ((descriptor == null) ? null :
222              GET_BACKUP_COMPATIBILITY_DESCRIPTOR_RESULT_OID),
223         encodeValue(descriptor, properties), responseControls);
224
225    if (descriptor == null)
226    {
227      this.descriptor = null;
228    }
229    else
230    {
231      this.descriptor =
232           new ASN1OctetString(TYPE_DESCRIPTOR, descriptor.getValue());
233    }
234
235    if (properties == null)
236    {
237      this.properties = Collections.emptyList();
238    }
239    else
240    {
241      this.properties =
242           Collections.unmodifiableList(new ArrayList<>(properties));
243    }
244  }
245
246
247
248  /**
249   * Creates an ASN.1 octet string containing an encoded representation of the
250   * value for a get backup compatibility descriptor extended result with the
251   * provided information.
252   *
253   * @param  descriptor  The backup compatibility descriptor value.  It may be
254   *                     {@code null} for an unsuccessful result.
255   * @param  properties  A list of properties that provide information about the
256   *                     way the descriptor may be used.  It may be {@code null}
257   *                     or empty for an unsuccessful result, or if there are no
258   *                     properties.
259   *
260   * @return  An ASN.1 octet string containing an encoded representation of the
261   *          value for a get backup compatibility descriptor extended result,
262   *          or {@code null} if a result with the provided information should
263   *          not have a value.
264   */
265  public static ASN1OctetString encodeValue(final ASN1OctetString descriptor,
266                                            final Collection<String> properties)
267  {
268    if (descriptor == null)
269    {
270      Validator.ensureTrue(((properties == null) || properties.isEmpty()),
271           "The properties must be null or empty if the descriptor is null.");
272      return null;
273    }
274
275    final ArrayList<ASN1Element> elements = new ArrayList<>(2);
276    elements.add(new ASN1OctetString(TYPE_DESCRIPTOR, descriptor.getValue()));
277
278    if ((properties != null) && (! properties.isEmpty()))
279    {
280      final ArrayList<ASN1Element> propElements =
281           new ArrayList<>(properties.size());
282      for (final String property : properties)
283      {
284        propElements.add(new ASN1OctetString(property));
285      }
286      elements.add(new ASN1Sequence(TYPE_PROPERTIES, propElements));
287    }
288
289    return new ASN1OctetString(new ASN1Sequence(elements).encode());
290  }
291
292
293
294  /**
295   * Retrieves the backup compatibility descriptor value, if available.
296   *
297   * @return  The backup compatibility descriptor value, or {@code null} if none
298   *          was provided.
299   */
300  public ASN1OctetString getDescriptor()
301  {
302    return descriptor;
303  }
304
305
306
307  /**
308   * Retrieves a list of properties that provide information about the way the
309   * descriptor may be used.
310   *
311   * @return  A list of properties that provide information about the way the
312   *          descriptor may be used, or an empty list if no properties were
313   *          provided.
314   */
315  public List<String> getProperties()
316  {
317    return properties;
318  }
319
320
321
322  /**
323   * {@inheritDoc}
324   */
325  @Override()
326  public String getExtendedResultName()
327  {
328    return INFO_EXTENDED_RESULT_NAME_GET_BACKUP_COMPAT.get();
329  }
330
331
332
333  /**
334   * {@inheritDoc}
335   */
336  @Override()
337  public void toString(final StringBuilder buffer)
338  {
339    buffer.append("GetBackupCompatibilityDescriptorExtendedResult(resultCode=");
340    buffer.append(getResultCode());
341
342    final int messageID = getMessageID();
343    if (messageID >= 0)
344    {
345      buffer.append(", messageID=");
346      buffer.append(messageID);
347    }
348
349    if (descriptor != null)
350    {
351      buffer.append(", descriptorLength=");
352      buffer.append(descriptor.getValueLength());
353    }
354
355    if (! properties.isEmpty())
356    {
357      buffer.append(", descriptorProperties={");
358
359      final Iterator<String> iterator = properties.iterator();
360      while (iterator.hasNext())
361      {
362        buffer.append('\'');
363        buffer.append(iterator.next());
364        buffer.append('\'');
365
366        if (iterator.hasNext())
367        {
368          buffer.append(',');
369        }
370      }
371
372      buffer.append('}');
373    }
374
375    final String diagnosticMessage = getDiagnosticMessage();
376    if (diagnosticMessage != null)
377    {
378      buffer.append(", diagnosticMessage='");
379      buffer.append(diagnosticMessage);
380      buffer.append('\'');
381    }
382
383    final String matchedDN = getMatchedDN();
384    if (matchedDN != null)
385    {
386      buffer.append(", matchedDN='");
387      buffer.append(matchedDN);
388      buffer.append('\'');
389    }
390
391    final String[] referralURLs = getReferralURLs();
392    if (referralURLs.length > 0)
393    {
394      buffer.append(", referralURLs={");
395      for (int i=0; i < referralURLs.length; i++)
396      {
397        if (i > 0)
398        {
399          buffer.append(", ");
400        }
401
402        buffer.append('\'');
403        buffer.append(referralURLs[i]);
404        buffer.append('\'');
405      }
406      buffer.append('}');
407    }
408
409    final Control[] responseControls = getResponseControls();
410    if (responseControls.length > 0)
411    {
412      buffer.append(", responseControls={");
413      for (int i=0; i < responseControls.length; i++)
414      {
415        if (i > 0)
416        {
417          buffer.append(", ");
418        }
419
420        buffer.append(responseControls[i]);
421      }
422      buffer.append('}');
423    }
424
425    buffer.append(')');
426  }
427}