001/*
002 * Copyright 2014-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2014-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) 2014-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.LinkedHashSet;
045import java.util.Set;
046
047import com.unboundid.asn1.ASN1Element;
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.ExtendedRequest;
053import com.unboundid.ldap.sdk.ExtendedResult;
054import com.unboundid.ldap.sdk.LDAPConnection;
055import com.unboundid.ldap.sdk.LDAPException;
056import com.unboundid.ldap.sdk.ResultCode;
057import com.unboundid.util.Debug;
058import com.unboundid.util.NotMutable;
059import com.unboundid.util.NotNull;
060import com.unboundid.util.Nullable;
061import com.unboundid.util.StaticUtils;
062import com.unboundid.util.ThreadSafety;
063import com.unboundid.util.ThreadSafetyLevel;
064import com.unboundid.util.Validator;
065
066import static com.unboundid.ldap.sdk.unboundidds.extensions.ExtOpMessages.*;
067
068
069
070/**
071 * This class provides an extended request that may be used to retrieve a list
072 * of the subscriptions associated with a specified notification manager,
073 * optionally restricted to a specified set of destinations.
074 * <BR>
075 * <BLOCKQUOTE>
076 *   <B>NOTE:</B>  This class, and other classes within the
077 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
078 *   supported for use against Ping Identity, UnboundID, and
079 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
080 *   for proprietary functionality or for external specifications that are not
081 *   considered stable or mature enough to be guaranteed to work in an
082 *   interoperable way with other types of LDAP servers.
083 * </BLOCKQUOTE>
084 * <BR>
085 * The request has an OID of 1.3.6.1.4.1.30221.2.6.40 and a value with the
086 * following encoding: <BR><BR>
087 * <PRE>
088 *   ListNotificationSubscriptionsRequest ::= SEQUENCE {
089 *        notificationManagerID          OCTET STRING,
090 *        notificationDestinationIDs     SET OF OCTET STRING OPTIONAL }
091 * </PRE>
092 */
093@NotMutable()
094@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
095public final class ListNotificationSubscriptionsExtendedRequest
096       extends ExtendedRequest
097{
098  /**
099   * The OID (1.3.6.1.4.1.30221.2.6.40) for the list notification subscriptions
100   * extended request.
101   */
102  @NotNull public static final String
103       LIST_NOTIFICATION_SUBSCRIPTIONS_REQUEST_OID =
104            "1.3.6.1.4.1.30221.2.6.40";
105
106
107
108  /**
109   * The serial version UID for this serializable class.
110   */
111  private static final long serialVersionUID = -8124073083247944273L;
112
113
114
115  // The notification destination IDs.
116  @NotNull private final Set<String> destinationIDs;
117
118  // The notification manager ID.
119  @NotNull private final String managerID;
120
121
122
123  /**
124   * Creates a new list notification subscriptions extended request with the
125   * provided information.
126   *
127   * @param  managerID          The notification manager ID.  It must not be
128   *                            {@code null}.
129   * @param  destinationIDs     The set of notification destination IDs for
130   *                            which to retrieve the subscription information.
131   *                            It may be {@code null} or empty if subscription
132   *                            information for all destinations should be
133   *                            returned.
134   */
135  public ListNotificationSubscriptionsExtendedRequest(
136              @NotNull final String managerID,
137              @Nullable final String... destinationIDs)
138  {
139    this(managerID, StaticUtils.toList(destinationIDs));
140  }
141
142
143
144  /**
145   * Creates a new list notification subscriptions extended request with the
146   * provided information.
147   *
148   * @param  managerID          The notification manager ID.  It must not be
149   *                            {@code null}.
150   * @param  destinationIDs     The set of notification destination IDs for
151   *                            which to retrieve the subscription information.
152   *                            It may be {@code null} or empty if subscription
153   *                            information for all destinations should be
154   *                            returned.
155   * @param  controls           The set of controls to include in the request.
156   *                            It may be {@code null} or empty if no controls
157   *                            are needed.
158   */
159  public ListNotificationSubscriptionsExtendedRequest(
160              @NotNull final String managerID,
161              @Nullable final Collection<String> destinationIDs,
162              @Nullable final Control... controls)
163  {
164    super(LIST_NOTIFICATION_SUBSCRIPTIONS_REQUEST_OID,
165         encodeValue(managerID, destinationIDs), controls);
166
167    this.managerID = managerID;
168
169    if (destinationIDs == null)
170    {
171      this.destinationIDs = Collections.emptySet();
172    }
173    else
174    {
175      this.destinationIDs =
176           Collections.unmodifiableSet(new LinkedHashSet<>(destinationIDs));
177    }
178  }
179
180
181
182  /**
183   * Creates a new list notification subscriptions extended request from the
184   * provided generic extended request.
185   *
186   * @param  extendedRequest  The generic extended request to use to create this
187   *                          list notification subscriptions extended request.
188   *
189   * @throws  LDAPException  If a problem occurs while decoding the request.
190   */
191  public ListNotificationSubscriptionsExtendedRequest(
192              @NotNull final ExtendedRequest extendedRequest)
193         throws LDAPException
194  {
195    super(extendedRequest);
196
197    final ASN1OctetString value = extendedRequest.getValue();
198    if (value == null)
199    {
200      throw new LDAPException(ResultCode.DECODING_ERROR,
201           ERR_LIST_NOTIFICATION_SUBS_REQ_DECODE_NO_VALUE.get());
202    }
203
204    try
205    {
206      final ASN1Element[] elements =
207           ASN1Sequence.decodeAsSequence(value.getValue()).elements();
208      managerID =
209           ASN1OctetString.decodeAsOctetString(elements[0]).stringValue();
210
211      if (elements.length > 1)
212      {
213        final ASN1Element[] destIDElements =
214             ASN1Sequence.decodeAsSequence(elements[1]).elements();
215
216        final LinkedHashSet<String> destIDs = new LinkedHashSet<>(
217             StaticUtils.computeMapCapacity(destIDElements.length));
218        for (final ASN1Element e : destIDElements)
219        {
220          destIDs.add(ASN1OctetString.decodeAsOctetString(e).stringValue());
221        }
222        destinationIDs = Collections.unmodifiableSet(destIDs);
223      }
224      else
225      {
226        destinationIDs = Collections.emptySet();
227      }
228    }
229    catch (final Exception e)
230    {
231      Debug.debugException(e);
232      throw new LDAPException(ResultCode.DECODING_ERROR,
233           ERR_LIST_NOTIFICATION_SUBS_REQ_ERROR_DECODING_VALUE.get(
234                StaticUtils.getExceptionMessage(e)),
235           e);
236    }
237  }
238
239
240
241  /**
242   * Encodes the provided information into an ASN.1 octet string suitable for
243   * use as the value of this extended request.
244   *
245   * @param  managerID          The notification manager ID.  It must not be
246   *                            {@code null}.
247   * @param  destinationIDs     The set of notification destination IDs for
248   *                            which to retrieve the subscription information.
249   *                            It may be {@code null} or empty if subscription
250   *                            information for all destinations should be
251   *                            returned.
252   *
253   * @return  The ASN.1 octet string containing the encoded value.
254   */
255  @NotNull()
256  private static ASN1OctetString encodeValue(@NotNull final String managerID,
257                      @Nullable final Collection<String> destinationIDs)
258  {
259    Validator.ensureNotNull(managerID);
260
261    final ArrayList<ASN1Element> elements = new ArrayList<>(2);
262    elements.add(new ASN1OctetString(managerID));
263
264    if ((destinationIDs != null) && (! destinationIDs.isEmpty()))
265    {
266      final LinkedHashSet<ASN1Element> destIDElements = new LinkedHashSet<>(
267           StaticUtils.computeMapCapacity(destinationIDs.size()));
268      for (final String destinationID : destinationIDs)
269      {
270        destIDElements.add(new ASN1OctetString(destinationID));
271      }
272      elements.add(new ASN1Set(destIDElements));
273    }
274
275    return new ASN1OctetString(new ASN1Sequence(elements).encode());
276  }
277
278
279
280  /**
281   * {@inheritDoc}
282   */
283  @Override()
284  @NotNull()
285  public ListNotificationSubscriptionsExtendedResult process(
286              @NotNull final LDAPConnection connection, final int depth)
287         throws LDAPException
288  {
289    final ExtendedResult extendedResponse = super.process(connection, depth);
290    return new ListNotificationSubscriptionsExtendedResult(extendedResponse);
291  }
292
293
294
295  /**
296   * Retrieves the notification manager ID.
297   *
298   * @return  The notification manager ID.
299   */
300  @NotNull()
301  public String getManagerID()
302  {
303    return managerID;
304  }
305
306
307
308  /**
309   * Retrieves the notification destination IDs, if any were provided.
310   *
311   * @return  The notification destination IDs, or an empty set if none were
312   *          provided.
313   */
314  @NotNull()
315  public Set<String> getDestinationIDs()
316  {
317    return destinationIDs;
318  }
319
320
321
322  /**
323   * {@inheritDoc}
324   */
325  @Override()
326  @NotNull()
327  public ListNotificationSubscriptionsExtendedRequest duplicate()
328  {
329    return duplicate(getControls());
330  }
331
332
333
334  /**
335   * {@inheritDoc}
336   */
337  @Override()
338  @NotNull()
339  public ListNotificationSubscriptionsExtendedRequest duplicate(
340              @Nullable final Control[] controls)
341  {
342    final ListNotificationSubscriptionsExtendedRequest r =
343         new ListNotificationSubscriptionsExtendedRequest(managerID,
344              destinationIDs, controls);
345    r.setResponseTimeoutMillis(getResponseTimeoutMillis(null));
346    r.setIntermediateResponseListener(getIntermediateResponseListener());
347    r.setReferralDepth(getReferralDepth());
348    r.setReferralConnector(getReferralConnectorInternal());
349    return r;
350  }
351
352
353
354  /**
355   * {@inheritDoc}
356   */
357  @Override()
358  @NotNull()
359  public String getExtendedRequestName()
360  {
361    return INFO_EXTENDED_REQUEST_NAME_LIST_NOTIFICATION_SUBS.get();
362  }
363
364
365
366  /**
367   * {@inheritDoc}
368   */
369  @Override()
370  public void toString(@NotNull final StringBuilder buffer)
371  {
372    buffer.append("ListNotificationSubscriptionsExtendedRequest(managerID='");
373    buffer.append(managerID);
374    buffer.append('\'');
375
376    if (! destinationIDs.isEmpty())
377    {
378      buffer.append(", destinationIDs={");
379
380      final Iterator<String> iterator = destinationIDs.iterator();
381      while (iterator.hasNext())
382      {
383        buffer.append('\'');
384        buffer.append(iterator.next());
385        buffer.append('\'');
386
387        if (iterator.hasNext())
388        {
389          buffer.append(", ");
390        }
391      }
392
393      buffer.append('}');
394    }
395
396    final Control[] controls = getControls();
397    if (controls.length > 0)
398    {
399      buffer.append(", controls={");
400      for (int i=0; i < controls.length; i++)
401      {
402        if (i > 0)
403        {
404          buffer.append(", ");
405        }
406
407        buffer.append(controls[i]);
408      }
409      buffer.append('}');
410    }
411
412    buffer.append(')');
413  }
414}