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