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