001/*
002 * Copyright 2009-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2009-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) 2009-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.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.ASN1Enumerated;
048import com.unboundid.asn1.ASN1OctetString;
049import com.unboundid.asn1.ASN1Sequence;
050import com.unboundid.asn1.ASN1Set;
051import com.unboundid.ldap.sdk.Control;
052import com.unboundid.ldap.sdk.IntermediateResponse;
053import com.unboundid.ldap.sdk.LDAPException;
054import com.unboundid.ldap.sdk.ResultCode;
055import com.unboundid.util.Debug;
056import com.unboundid.util.NotMutable;
057import com.unboundid.util.NotNull;
058import com.unboundid.util.Nullable;
059import com.unboundid.util.StaticUtils;
060import com.unboundid.util.ThreadSafety;
061import com.unboundid.util.ThreadSafetyLevel;
062
063import static com.unboundid.ldap.sdk.unboundidds.extensions.ExtOpMessages.*;
064
065
066
067/**
068 * This class provides an implementation of the stream directory values
069 * intermediate response, which may be used to provide a partial or complete
070 * list of the values for a specified attribute, or DNs of entries contained in
071 * a specified portion of the server DIT.
072 * <BR>
073 * <BLOCKQUOTE>
074 *   <B>NOTE:</B>  This class, and other classes within the
075 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
076 *   supported for use against Ping Identity, UnboundID, and
077 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
078 *   for proprietary functionality or for external specifications that are not
079 *   considered stable or mature enough to be guaranteed to work in an
080 *   interoperable way with other types of LDAP servers.
081 * </BLOCKQUOTE>
082 * <BR>
083 * This intermediate response has an OID
084 * of "1.3.6.1.4.1.30221.2.6.7" and the value is encoded as follows:
085 * <PRE>
086 *   StreamDirectoryValuesIntermediateResponse ::= SEQUENCE {
087 *        attributeName         [0] LDAPString OPTIONAL,
088 *        result                [1] ENUMERATED {
089 *             allValuesReturned       (0),
090 *             moreValuesToReturn      (1),
091 *             attributeNotIndexed     (2),
092 *             processingError         (3),
093 *             ... },
094 *        diagnosticMessage     [2] OCTET STRING OPTIONAL,
095 *        values                [3] SET OF OCTET STRING OPTIONAL,
096 *        ... }
097 * </PRE>
098 */
099@NotMutable()
100@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
101public final class StreamDirectoryValuesIntermediateResponse
102       extends IntermediateResponse
103{
104  /**
105   * The OID (1.3.6.1.4.1.30221.2.6.7) for the get stream directory values
106   * intermediate response.
107   */
108  @NotNull public static final String
109       STREAM_DIRECTORY_VALUES_INTERMEDIATE_RESPONSE_OID =
110            "1.3.6.1.4.1.30221.2.6.7";
111
112
113
114  /**
115   * The integer value for the "all values returned" result.
116   */
117  public static final int RESULT_ALL_VALUES_RETURNED = 0;
118
119
120
121  /**
122   * The integer value for the "more values to return" result.
123   */
124  public static final int RESULT_MORE_VALUES_TO_RETURN = 1;
125
126
127
128  /**
129   * The integer value for the "attribute not indexed" result.
130   */
131  public static final int RESULT_ATTRIBUTE_NOT_INDEXED = 2;
132
133
134
135  /**
136   * The integer value for the "processing error" result.
137   */
138  public static final int RESULT_PROCESSING_ERROR = 3;
139
140
141
142  /**
143   * The BER type for the attribute name element.
144   */
145  private static final byte TYPE_ATTRIBUTE_NAME = (byte) 0x80;
146
147
148
149  /**
150   * The BER type for the result element.
151   */
152  private static final byte TYPE_RESULT = (byte) 0x81;
153
154
155
156  /**
157   * The BER type for the diagnostic message element.
158   */
159  private static final byte TYPE_DIAGNOSTIC_MESSAGE = (byte) 0x82;
160
161
162
163  /**
164   * The BER type for the values element.
165   */
166  private static final byte TYPE_VALUES = (byte) 0xA3;
167
168
169
170  /**
171   * The serial version UID for this serializable class.
172   */
173  private static final long serialVersionUID = -1756020236490168006L;
174
175
176
177  // The result code for this stream directory values intermediate response.
178  private final int result;
179
180  // The list of values for this stream directory values intermediate response.
181  @NotNull private final List<ASN1OctetString> values;
182
183  // The attribute name for this stream directory values intermediate response,
184  // if any.
185  @Nullable private final String attributeName;
186
187  // The diagnostic message for this stream directory values intermediate
188  // response, if any.
189  @Nullable private final String diagnosticMessage;
190
191
192
193  /**
194   * Creates a new stream directory values intermediate response with the
195   * provided information.
196   *
197   * @param  attributeName      The name of the attribute with which the
198   *                            included values are associated.  This may be
199   *                            {@code null} if the provided values are DNs.
200   * @param  result             The integer value that provides information
201   *                            about the state of the stream directory values
202   *                            response.
203   * @param  diagnosticMessage  The diagnostic message that provides more
204   *                            information about the result, or {@code null} if
205   *                            none is required.
206   * @param  values             The set of values included in this stream
207   *                            directory values intermediate response.  It may
208   *                            be {@code null} or empty if this is an error
209   *                            result, or there are no values of the specified
210   *                            type in the server.
211   * @param  controls           The set of controls to include in this
212   *                            intermediate response.  It may be {@code null}
213   *                            or empty if there should not be any controls.
214   */
215  public StreamDirectoryValuesIntermediateResponse(
216              @Nullable final String attributeName,
217              final int result,
218              @Nullable final String diagnosticMessage,
219              @Nullable final Collection<ASN1OctetString> values,
220              @Nullable final Control... controls)
221  {
222    super(STREAM_DIRECTORY_VALUES_INTERMEDIATE_RESPONSE_OID,
223          encodeValue(attributeName, result, diagnosticMessage, values),
224          controls);
225
226    this.attributeName     = attributeName;
227    this.result            = result;
228    this.diagnosticMessage = diagnosticMessage;
229
230    if ((values == null) || values.isEmpty())
231    {
232      this.values = Collections.emptyList();
233    }
234    else
235    {
236      this.values = Collections.unmodifiableList(new ArrayList<>(values));
237    }
238  }
239
240
241
242  /**
243   * Creates a new stream directory values intermediate response with
244   * information from the provided generic intermediate response.
245   *
246   * @param  intermediateResponse  The generic intermediate response that should
247   *                               be used to create this new intermediate
248   *                               response.
249   *
250   * @throws  LDAPException  If the provided intermediate response cannot be
251   *                         parsed as a stream directory values intermediate
252   *                         response.
253   */
254  public StreamDirectoryValuesIntermediateResponse(
255                 @NotNull final IntermediateResponse intermediateResponse)
256         throws LDAPException
257  {
258    super(intermediateResponse);
259
260    final ASN1OctetString value = intermediateResponse.getValue();
261    if (value == null)
262    {
263      throw new LDAPException(ResultCode.DECODING_ERROR,
264           ERR_STREAM_DIRECTORY_VALUES_RESPONSE_NO_VALUE.get());
265    }
266
267    int    tmpResult  = -1;
268    String tmpAttr    = null;
269    String tmpMessage = null;
270    final ArrayList<ASN1OctetString> tmpValues = new ArrayList<>(100);
271
272    try
273    {
274      final ASN1Element[] elements =
275           ASN1Element.decode(value.getValue()).decodeAsSequence().elements();
276      for (final ASN1Element e : elements)
277      {
278        switch (e.getType())
279        {
280          case TYPE_ATTRIBUTE_NAME:
281            tmpAttr = e.decodeAsOctetString().stringValue();
282            break;
283          case TYPE_RESULT:
284            tmpResult = e.decodeAsEnumerated().intValue();
285            if (tmpResult < 0)
286            {
287              throw new LDAPException(ResultCode.DECODING_ERROR,
288                   ERR_STREAM_DIRECTORY_VALUES_RESPONSE_INVALID_RESULT.get(
289                        tmpResult));
290            }
291            break;
292          case TYPE_DIAGNOSTIC_MESSAGE:
293            tmpMessage = e.decodeAsOctetString().stringValue();
294            break;
295          case TYPE_VALUES:
296            final ASN1Element[] valueElements = e.decodeAsSet().elements();
297            for (final ASN1Element ve : valueElements)
298            {
299              tmpValues.add(ve.decodeAsOctetString());
300            }
301            break;
302          default:
303            throw new LDAPException(ResultCode.DECODING_ERROR,
304                 ERR_STREAM_DIRECTORY_VALUES_RESPONSE_INVALID_SEQUENCE_TYPE.get(
305                      StaticUtils.toHex(e.getType())));
306        }
307      }
308    }
309    catch (final LDAPException le)
310    {
311      throw le;
312    }
313    catch (final Exception e)
314    {
315      Debug.debugException(e);
316      throw new LDAPException(ResultCode.DECODING_ERROR,
317           ERR_STREAM_DIRECTORY_VALUES_RESPONSE_CANNOT_DECODE.get(
318                StaticUtils.getExceptionMessage(e)), e);
319    }
320
321    if (tmpResult < 0)
322    {
323      throw new LDAPException(ResultCode.DECODING_ERROR,
324           ERR_STREAM_DIRECTORY_VALUES_RESPONSE_NO_RESULT.get());
325    }
326
327    attributeName     = tmpAttr;
328    result            = tmpResult;
329    diagnosticMessage = tmpMessage;
330    values            = Collections.unmodifiableList(tmpValues);
331  }
332
333
334
335  /**
336   * Encodes the provided information in a form suitable for use as the value of
337   * this intermediate response.
338   *
339   * @param  attributeName      The name of the attribute with which the
340   *                            included values are associated.  This may be
341   *                            {@code null} if the provided values are DNs.
342   * @param  result             The integer value that provides information
343   *                            about the state of the stream directory values
344   *                            response.
345   * @param  diagnosticMessage  The diagnostic message that provides more
346   *                            information about the result, or {@code null} if
347   *                            none is required.
348   * @param  values             The set of values included in this stream
349   *                            directory values intermediate response.  It may
350   *                            be {@code null} or empty if this is an error
351   *                            result, or there are no values of the specified
352   *                            type in the server.
353   *
354   * @return  An ASN.1 octet string containing the encoded value to use for this
355   *          intermediate response.
356   */
357  @NotNull()
358  private static ASN1OctetString encodeValue(
359               @Nullable final String attributeName,
360               final int result,
361               @Nullable final String diagnosticMessage,
362               @Nullable final Collection<ASN1OctetString> values)
363  {
364    final ArrayList<ASN1Element> elements = new ArrayList<>(4);
365
366    if (attributeName != null)
367    {
368      elements.add(new ASN1OctetString(TYPE_ATTRIBUTE_NAME, attributeName));
369    }
370
371    elements.add(new ASN1Enumerated(TYPE_RESULT, result));
372
373    if (diagnosticMessage != null)
374    {
375      elements.add(new ASN1OctetString(TYPE_DIAGNOSTIC_MESSAGE,
376                                       diagnosticMessage));
377    }
378
379    if ((values != null) && (! values.isEmpty()))
380    {
381      elements.add(new ASN1Set(TYPE_VALUES, values));
382    }
383
384    return new ASN1OctetString(new ASN1Sequence(elements).encode());
385  }
386
387
388
389  /**
390   * Retrieves the name of the attribute with which this stream directory values
391   * intermediate response is associated.
392   *
393   * @return  The name of the attribute with which this stream directory values
394   *          intermediate response is associated, or {@code null} if the values
395   *          are entry DNs rather than attribute values.
396   */
397  @Nullable()
398  public String getAttributeName()
399  {
400    return attributeName;
401  }
402
403
404
405  /**
406   * Retrieves the integer value of the result for this stream directory values
407   * intermediate response.
408   *
409   * @return  The integer value of the result for this stream directory values
410   *          intermediate response.
411   */
412  public int getResult()
413  {
414    return result;
415  }
416
417
418
419  /**
420   * Retrieves the diagnostic message for this stream directory values
421   * intermediate response.
422   *
423   * @return  The diagnostic message for this stream directory values
424   *          intermediate response, or {@code null} if there is none.
425   */
426  @Nullable()
427  public String getDiagnosticMessage()
428  {
429    return diagnosticMessage;
430  }
431
432
433
434  /**
435   * Retrieves the list of values for this stream directory values intermediate
436   * response.
437   *
438   * @return  The list of values for this stream directory values intermediate
439   *          response, or an empty list if there are no values.
440   */
441  @NotNull()
442  public List<ASN1OctetString> getValues()
443  {
444    return values;
445  }
446
447
448
449  /**
450   * {@inheritDoc}
451   */
452  @Override()
453  @NotNull()
454  public String getIntermediateResponseName()
455  {
456    return INFO_INTERMEDIATE_RESPONSE_NAME_STREAM_DIRECTORY_VALUES.get();
457  }
458
459
460
461  /**
462   * {@inheritDoc}
463   */
464  @Override()
465  @NotNull()
466  public String valueToString()
467  {
468    final StringBuilder buffer = new StringBuilder();
469
470    if (attributeName != null)
471    {
472      buffer.append("attributeName='");
473      buffer.append(attributeName);
474      buffer.append("' ");
475    }
476
477    buffer.append("result='");
478    switch (result)
479    {
480      case RESULT_ALL_VALUES_RETURNED:
481        buffer.append("all values returned");
482        break;
483      case RESULT_ATTRIBUTE_NOT_INDEXED:
484        buffer.append("attribute not indexed");
485        break;
486      case RESULT_MORE_VALUES_TO_RETURN:
487        buffer.append("more values to return");
488        break;
489      case RESULT_PROCESSING_ERROR:
490        buffer.append("processing error");
491        break;
492      default:
493        buffer.append(result);
494        break;
495    }
496    buffer.append('\'');
497
498    if (diagnosticMessage != null)
499    {
500      buffer.append(" diagnosticMessage='");
501      buffer.append(diagnosticMessage);
502      buffer.append('\'');
503    }
504
505    buffer.append(" valueCount='");
506    buffer.append(values.size());
507    buffer.append('\'');
508
509    return buffer.toString();
510  }
511
512
513
514  /**
515   * {@inheritDoc}
516   */
517  @Override()
518  public void toString(@NotNull final StringBuilder buffer)
519  {
520    buffer.append("StreamDirectoryValuesIntermediateResponse(");
521
522    final int messageID = getMessageID();
523    if (messageID >= 0)
524    {
525      buffer.append("messageID=");
526      buffer.append(messageID);
527      buffer.append(", ");
528    }
529
530    if (attributeName != null)
531    {
532      buffer.append("attributeName='");
533      buffer.append(attributeName);
534      buffer.append("', ");
535    }
536
537    buffer.append("result=");
538    buffer.append(result);
539
540    if (diagnosticMessage != null)
541    {
542      buffer.append(", diagnosticMessage='");
543      buffer.append(diagnosticMessage);
544      buffer.append('\'');
545    }
546
547    buffer.append(", values={");
548
549    final Iterator<ASN1OctetString> iterator = values.iterator();
550    while (iterator.hasNext())
551    {
552      buffer.append('\'');
553      buffer.append(iterator.next().stringValue());
554      buffer.append('\'');
555      if (iterator.hasNext())
556      {
557        buffer.append(", ");
558      }
559    }
560
561    final Control[] controls = getControls();
562    if (controls.length > 0)
563    {
564      buffer.append(", controls={");
565      for (int i=0; i < controls.length; i++)
566      {
567        if (i > 0)
568        {
569          buffer.append(", ");
570        }
571
572        buffer.append(controls[i]);
573      }
574      buffer.append('}');
575    }
576
577    buffer.append("})");
578  }
579}