001/*
002 * Copyright 2012-2020 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2012-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.NotMutable;
055import com.unboundid.util.StaticUtils;
056import com.unboundid.util.ThreadSafety;
057import com.unboundid.util.ThreadSafetyLevel;
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 provide information about the notification subscriptions defined in the
066 * target server.
067 * <BR>
068 * <BLOCKQUOTE>
069 *   <B>NOTE:</B>  This class, and other classes within the
070 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
071 *   supported for use against Ping Identity, UnboundID, and
072 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
073 *   for proprietary functionality or for external specifications that are not
074 *   considered stable or mature enough to be guaranteed to work in an
075 *   interoperable way with other types of LDAP servers.
076 * </BLOCKQUOTE>
077 * <BR>
078 * The OID for this result is 1.3.6.1.4.1.30221.2.6.41, and the value (if
079 * present) should have the following encoding:
080 * <BR><BR>
081 * <PRE>
082 *   ListNotificationSubscriptionsResponse ::= SEQUENCE OF SEQUENCE {
083 *        notificationDestinationID     OCTET STRING,
084 *        destinationDetails            SEQUENCE OF OCTET STRING,
085 *        subscriptions                 SEQUENCE OF SEQUENCE {
086 *             subscriptionID          OCTET STRING,
087 *             subscriptionDetails     SEQUENCE OF OCTET STRING } }
088 * </PRE>
089 */
090@NotMutable()
091@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
092public final class ListNotificationSubscriptionsExtendedResult
093       extends ExtendedResult
094{
095  /**
096   * The OID (1.3.6.1.4.1.30221.2.6.41) for the list notification subscriptions
097   * extended result.
098   */
099  public static final String LIST_NOTIFICATION_SUBSCRIPTIONS_RESULT_OID =
100       "1.3.6.1.4.1.30221.2.6.41";
101
102
103
104  /**
105   * The serial version UID for this serializable class.
106   */
107  private static final long serialVersionUID = 8876370324325619149L;
108
109
110
111  // The notification destination details for this result.
112  private final List<NotificationDestinationDetails> destinations;
113
114
115
116  /**
117   * Creates a new list notification subscriptions extended result from the
118   * provided extended result.
119   *
120   * @param  extendedResult  The extended result to be decoded as a list
121   *                         notification subscriptions extended result.
122   *
123   * @throws LDAPException  If a problem is encountered while attempting to
124   *                         decode the provided extended result as a
125   *                         multi-update result.
126   */
127  public ListNotificationSubscriptionsExtendedResult(
128              final ExtendedResult extendedResult)
129         throws LDAPException
130  {
131    super(extendedResult);
132
133    final ASN1OctetString value = extendedResult.getValue();
134    if (value == null)
135    {
136      destinations = Collections.emptyList();
137      return;
138    }
139
140    try
141    {
142      final ASN1Element[] destsElements =
143           ASN1Sequence.decodeAsSequence(value.getValue()).elements();
144      final ArrayList<NotificationDestinationDetails> destList =
145           new ArrayList<>(destsElements.length);
146      for (final ASN1Element destElement : destsElements)
147      {
148        final ASN1Element[] destElements =
149             ASN1Sequence.decodeAsSequence(destElement).elements();
150        final String destID =
151             ASN1OctetString.decodeAsOctetString(destElements[0]).stringValue();
152
153        final ASN1Element[] destDetailsElements =
154             ASN1Sequence.decodeAsSequence(destElements[1]).elements();
155        final ArrayList<ASN1OctetString> destDetailsList =
156             new ArrayList<>(destDetailsElements.length);
157        for (final ASN1Element e : destDetailsElements)
158        {
159          destDetailsList.add(ASN1OctetString.decodeAsOctetString(e));
160        }
161
162        final ASN1Element[] subElements =
163             ASN1Sequence.decodeAsSequence(destElements[2]).elements();
164        final ArrayList<NotificationSubscriptionDetails> subscriptions =
165             new ArrayList<>(subElements.length);
166        for (final ASN1Element e : subElements)
167        {
168          final ASN1Element[] sElements =
169               ASN1Sequence.decodeAsSequence(e).elements();
170          final String subID =
171               ASN1OctetString.decodeAsOctetString(sElements[0]).stringValue();
172
173          final ASN1Element[] subDetailsElements =
174               ASN1Sequence.decodeAsSequence(sElements[1]).elements();
175          final ArrayList<ASN1OctetString> subDetails =
176               new ArrayList<>(subDetailsElements.length);
177          for (final ASN1Element sde : subDetailsElements)
178          {
179            subDetails.add(ASN1OctetString.decodeAsOctetString(sde));
180          }
181          subscriptions.add(
182               new NotificationSubscriptionDetails(subID, subDetails));
183        }
184
185        destList.add(new NotificationDestinationDetails(destID, destDetailsList,
186             subscriptions));
187      }
188
189      destinations = Collections.unmodifiableList(destList);
190    }
191    catch (final Exception e)
192    {
193      Debug.debugException(e);
194      throw new LDAPException(ResultCode.DECODING_ERROR,
195           ERR_LIST_NOTIFICATION_SUBS_RESULT_CANNOT_DECODE_VALUE.get(
196                StaticUtils.getExceptionMessage(e)),
197           e);
198    }
199  }
200
201
202
203  /**
204   * Creates a new list notification subscriptions extended request with the
205   * provided information.
206   *
207   * @param  messageID          The message ID for this extended result.
208   * @param  resultCode         The result code for this result.  It must not be
209   *                            {@code null}.
210   * @param  diagnosticMessage  The diagnostic message to include in the result.
211   *                            It may be {@code null} if no diagnostic message
212   *                            should be included.
213   * @param  matchedDN          The matched DN to include in the result.  It may
214   *                            be {@code null} if no matched DN should be
215   *                            included.
216   * @param  referralURLs       The set of referral URLs to include in the
217   *                            result.  It may be {@code null} or empty if no
218   *                            referral URLs should be included.
219   * @param  destinations       The notification destination details for this
220   *                            result.  It may be {@code null} or empty for a
221   *                            non-success result.
222   * @param  controls           The set of controls to include in the
223   *                            multi-update result.  It may be {@code null} or
224   *                            empty if no controls should be included.
225   *
226   * @throws  LDAPException  If any of the results are for an inappropriate
227   *                         operation type.
228   */
229  public ListNotificationSubscriptionsExtendedResult(final int messageID,
230              final ResultCode resultCode, final String diagnosticMessage,
231              final String matchedDN, final String[] referralURLs,
232              final Collection<NotificationDestinationDetails> destinations,
233              final Control... controls)
234         throws LDAPException
235  {
236    super(messageID, resultCode, diagnosticMessage, matchedDN, referralURLs,
237         LIST_NOTIFICATION_SUBSCRIPTIONS_RESULT_OID, encodeValue(destinations),
238         controls);
239
240    if (destinations == null)
241    {
242      this.destinations = Collections.emptyList();
243    }
244    else
245    {
246      this.destinations =
247           Collections.unmodifiableList(new ArrayList<>(destinations));
248    }
249  }
250
251
252
253  /**
254   * Encodes the information from the provided set of results into a form
255   * suitable for use as the value of the extended result.
256   *
257   * @param  destinations  The notification destination details for the result.
258   *                       It may be {@code null} or empty for a non-success
259   *                       result.
260   *
261   * @return  An ASN.1 element suitable for use as the value of the extended
262   *          result.
263   */
264  private static ASN1OctetString encodeValue(
265               final Collection<NotificationDestinationDetails> destinations)
266  {
267    if ((destinations == null) || destinations.isEmpty())
268    {
269      return null;
270    }
271
272    final ArrayList<ASN1Element> elements =
273         new ArrayList<>(destinations.size());
274    for (final NotificationDestinationDetails destDetails : destinations)
275    {
276      final ArrayList<ASN1Element> destElements = new ArrayList<>(3);
277      destElements.add(new ASN1OctetString(destDetails.getID()));
278      destElements.add(new ASN1Sequence(destDetails.getDetails()));
279
280      final ArrayList<ASN1Element> subElements =
281           new ArrayList<>(destDetails.getSubscriptions().size());
282      for (final NotificationSubscriptionDetails subDetails :
283           destDetails.getSubscriptions())
284      {
285        subElements.add(new ASN1Sequence(
286             new ASN1OctetString(subDetails.getID()),
287             new ASN1Sequence(subDetails.getDetails())));
288      }
289      destElements.add(new ASN1Sequence(subElements));
290      elements.add(new ASN1Sequence(destElements));
291    }
292
293    return new ASN1OctetString(new ASN1Sequence(elements).encode());
294  }
295
296
297
298  /**
299   * Retrieves a list of the defined notification destinations and their
300   * associated subscriptions.
301   *
302   * @return  A list of the defined notification destinations and their
303   *          associated subscriptions.
304   */
305  public List<NotificationDestinationDetails> getDestinations()
306  {
307    return destinations;
308  }
309
310
311
312  /**
313   * {@inheritDoc}
314   */
315  @Override()
316  public String getExtendedResultName()
317  {
318    return INFO_EXTENDED_RESULT_NAME_LIST_NOTIFICATION_SUBS.get();
319  }
320
321
322
323  /**
324   * Appends a string representation of this extended result to the provided
325   * buffer.
326   *
327   * @param  buffer  The buffer to which a string representation of this
328   *                 extended result will be appended.
329   */
330  @Override()
331  public void toString(final StringBuilder buffer)
332  {
333    buffer.append("ListNotificationSubscriptionsExtendedResult(resultCode=");
334    buffer.append(getResultCode());
335
336    final int messageID = getMessageID();
337    if (messageID >= 0)
338    {
339      buffer.append(", messageID=");
340      buffer.append(messageID);
341    }
342
343    buffer.append(", notificationDestinations={");
344    final Iterator<NotificationDestinationDetails> destIterator =
345         destinations.iterator();
346    while (destIterator.hasNext())
347    {
348      destIterator.next().toString(buffer);
349      if (destIterator.hasNext())
350      {
351        buffer.append(", ");
352      }
353    }
354    buffer.append('}');
355
356    final String diagnosticMessage = getDiagnosticMessage();
357    if (diagnosticMessage != null)
358    {
359      buffer.append(", diagnosticMessage='");
360      buffer.append(diagnosticMessage);
361      buffer.append('\'');
362    }
363
364    final String matchedDN = getMatchedDN();
365    if (matchedDN != null)
366    {
367      buffer.append(", matchedDN='");
368      buffer.append(matchedDN);
369      buffer.append('\'');
370    }
371
372    final String[] referralURLs = getReferralURLs();
373    if (referralURLs.length > 0)
374    {
375      buffer.append(", referralURLs={");
376      for (int i=0; i < referralURLs.length; i++)
377      {
378        if (i > 0)
379        {
380          buffer.append(", ");
381        }
382
383        buffer.append('\'');
384        buffer.append(referralURLs[i]);
385        buffer.append('\'');
386      }
387      buffer.append('}');
388    }
389
390    final Control[] responseControls = getResponseControls();
391    if (responseControls.length > 0)
392    {
393      buffer.append(", responseControls={");
394      for (int i=0; i < responseControls.length; i++)
395      {
396        if (i > 0)
397        {
398          buffer.append(", ");
399        }
400
401        buffer.append(responseControls[i]);
402      }
403      buffer.append('}');
404    }
405
406    buffer.append(')');
407  }
408}