001/*
002 * Copyright 2014-2018 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2015-2018 Ping Identity Corporation
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU General Public License (GPLv2 only)
010 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
011 * as published by the Free Software Foundation.
012 *
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program; if not, see <http://www.gnu.org/licenses>.
020 */
021package com.unboundid.ldap.sdk.unboundidds.extensions;
022
023
024
025import java.util.ArrayList;
026import java.util.Collection;
027import java.util.Collections;
028import java.util.List;
029
030import com.unboundid.asn1.ASN1Element;
031import com.unboundid.asn1.ASN1OctetString;
032import com.unboundid.asn1.ASN1Sequence;
033import com.unboundid.ldap.sdk.Control;
034import com.unboundid.ldap.sdk.ExtendedRequest;
035import com.unboundid.ldap.sdk.LDAPException;
036import com.unboundid.ldap.sdk.ResultCode;
037import com.unboundid.util.Debug;
038import com.unboundid.util.NotMutable;
039import com.unboundid.util.StaticUtils;
040import com.unboundid.util.ThreadSafety;
041import com.unboundid.util.ThreadSafetyLevel;
042import com.unboundid.util.Validator;
043
044import static com.unboundid.ldap.sdk.unboundidds.extensions.ExtOpMessages.*;
045
046
047
048/**
049 * This class provides an extended request that may be used to create or update
050 * a notification subscription.
051 * <BR>
052 * <BLOCKQUOTE>
053 *   <B>NOTE:</B>  This class, and other classes within the
054 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
055 *   supported for use against Ping Identity, UnboundID, and Alcatel-Lucent 8661
056 *   server products.  These classes provide support for proprietary
057 *   functionality or for external specifications that are not considered stable
058 *   or mature enough to be guaranteed to work in an interoperable way with
059 *   other types of LDAP servers.
060 * </BLOCKQUOTE>
061 * <BR>
062 * The request has an OID of 1.3.6.1.4.1.30221.2.6.38 and a value with the
063 * following encoding:
064 * <BR><BR>
065 * <PRE>
066 *   SetNotificationSubscriptionRequest ::= SEQUENCE {
067 *        notificationManagerID          OCTET STRING,
068 *        notificationDestinationID      OCTET STRING,
069 *        notificationSubscriptionID     OCTET STRING,
070 *        subscriptionDetails            SEQUENCE OF OCTET STRING }
071 * </PRE>
072 */
073@NotMutable()
074@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
075public final class SetNotificationSubscriptionExtendedRequest
076       extends ExtendedRequest
077{
078  /**
079   * The OID (1.3.6.1.4.1.30221.2.6.38) for the set notification subscription
080   * extended request.
081   */
082  public static final String SET_NOTIFICATION_SUBSCRIPTION_REQUEST_OID =
083       "1.3.6.1.4.1.30221.2.6.38";
084
085
086
087  /**
088   * The serial version UID for this serializable class.
089   */
090  private static final long serialVersionUID = -5822283773149091097L;
091
092
093
094  // The implementation-specific details for the notification subscription.
095  private final List<ASN1OctetString> subscriptionDetails;
096
097  // The notification destination ID.
098  private final String destinationID;
099
100  // The notification manager ID.
101  private final String managerID;
102
103  // The notification subscription ID.
104  private final String subscriptionID;
105
106
107
108  /**
109   * Creates a new set notification subscription extended request with the
110   * provided information.
111   *
112   * @param  managerID            The notification manager ID.  It must not be
113   *                              {@code null}.
114   * @param  destinationID        The notification destination ID.  It must not
115   *                              be {@code null}.
116   * @param  subscriptionID       The notification subscription ID.  It must not
117   *                              be {@code null}.
118   * @param  subscriptionDetails  The implementation-specific details for the
119   *                              notification subscription.  At least one
120   *                              detail value must be provided.
121   */
122  public SetNotificationSubscriptionExtendedRequest(final String managerID,
123              final String destinationID, final String subscriptionID,
124              final ASN1OctetString... subscriptionDetails)
125  {
126    this(managerID, destinationID, subscriptionID,
127         StaticUtils.toList(subscriptionDetails));
128  }
129
130
131
132  /**
133   * Creates a new set notification subscription extended request with the
134   * provided information.
135   *
136   * Creates a new set notification subscription extended request with the
137   * provided information.
138   *
139   * @param  managerID            The notification manager ID.  It must not be
140   *                              {@code null}.
141   * @param  destinationID        The notification destination ID.  It must not
142   *                              be {@code null}.
143   * @param  subscriptionID       The notification subscription ID.  It must not
144   *                              be {@code null}.
145   * @param  subscriptionDetails  The implementation-specific details for the
146   *                              notification subscription.  At least one
147   *                              detail value must be provided.
148   * @param  controls             The set of controls to include in the request.
149   *                              It may be {@code null} or empty if no controls
150   *                              are needed.
151   */
152  public SetNotificationSubscriptionExtendedRequest(final String managerID,
153              final String destinationID, final String subscriptionID,
154              final Collection<ASN1OctetString> subscriptionDetails,
155              final Control... controls)
156  {
157    super(SET_NOTIFICATION_SUBSCRIPTION_REQUEST_OID,
158         encodeValue(managerID, destinationID, subscriptionID,
159              subscriptionDetails),
160         controls);
161
162    this.managerID = managerID;
163    this.destinationID = destinationID;
164    this.subscriptionID = subscriptionID;
165    this.subscriptionDetails = Collections.unmodifiableList(
166         new ArrayList<ASN1OctetString>(subscriptionDetails));
167  }
168
169
170
171  /**
172   * Creates a new set notification subscription extended request from the
173   * provided generic extended request.
174   *
175   * @param  extendedRequest  The generic extended request to use to create this
176   *                          set notification subscription extended request.
177   *
178   * @throws  LDAPException  If a problem occurs while decoding the request.
179   */
180  public SetNotificationSubscriptionExtendedRequest(
181              final ExtendedRequest extendedRequest)
182         throws LDAPException
183  {
184    super(extendedRequest);
185
186    final ASN1OctetString value = extendedRequest.getValue();
187    if (value == null)
188    {
189      throw new LDAPException(ResultCode.DECODING_ERROR,
190           ERR_SET_NOTIFICATION_SUB_REQ_DECODE_NO_VALUE.get());
191    }
192
193    try
194    {
195      final ASN1Element[] elements =
196           ASN1Sequence.decodeAsSequence(value.getValue()).elements();
197      managerID =
198           ASN1OctetString.decodeAsOctetString(elements[0]).stringValue();
199      destinationID =
200           ASN1OctetString.decodeAsOctetString(elements[1]).stringValue();
201      subscriptionID =
202           ASN1OctetString.decodeAsOctetString(elements[2]).stringValue();
203
204      final ASN1Element[] detailElements =
205           ASN1Sequence.decodeAsSequence(elements[3]).elements();
206      final ArrayList<ASN1OctetString> detailList =
207           new ArrayList<ASN1OctetString>(detailElements.length);
208      for (final ASN1Element e : detailElements)
209      {
210        detailList.add(ASN1OctetString.decodeAsOctetString(e));
211      }
212      subscriptionDetails = Collections.unmodifiableList(detailList);
213    }
214    catch (final Exception e)
215    {
216      Debug.debugException(e);
217      throw new LDAPException(ResultCode.DECODING_ERROR,
218           ERR_SET_NOTIFICATION_SUB_REQ_ERROR_DECODING_VALUE.get(
219                StaticUtils.getExceptionMessage(e)),
220           e);
221    }
222  }
223
224
225
226  /**
227   * Encodes the provided information into an ASN.1 octet string suitable for
228   * use as the value of this extended request.
229   *
230   * @param  managerID            The notification manager ID.  It must not be
231   *                              {@code null}.
232   * @param  destinationID        The notification destination ID.  It must not
233   *                              be {@code null}.
234   * @param  subscriptionID       The notification subscription ID.  It must not
235   *                              be {@code null}.
236   * @param  subscriptionDetails  The implementation-specific details for the
237   *                              notification subscription.  At least one
238   *                              detail value must be provided.
239   *
240   * @return  The ASN.1 octet string containing the encoded value.
241   */
242  private static ASN1OctetString encodeValue(final String managerID,
243                      final String destinationID, final String subscriptionID,
244                      final Collection<ASN1OctetString> subscriptionDetails)
245  {
246    Validator.ensureNotNull(managerID);
247    Validator.ensureNotNull(destinationID);
248    Validator.ensureNotNull(subscriptionID);
249    Validator.ensureNotNull(subscriptionDetails);
250    Validator.ensureFalse(subscriptionDetails.isEmpty());
251
252    final ASN1Sequence valueSequence = new ASN1Sequence(
253         new ASN1OctetString(managerID),
254         new ASN1OctetString(destinationID),
255         new ASN1OctetString(subscriptionID),
256         new ASN1Sequence(new ArrayList<ASN1Element>(subscriptionDetails)));
257    return new ASN1OctetString(valueSequence.encode());
258  }
259
260
261
262  /**
263   * Retrieves the notification manager ID.
264   *
265   * @return  The notification manager ID.
266   */
267  public String getManagerID()
268  {
269    return managerID;
270  }
271
272
273
274  /**
275   * Retrieves the notification destination ID.
276   *
277   * @return  The notification destination ID.
278   */
279  public String getDestinationID()
280  {
281    return destinationID;
282  }
283
284
285
286  /**
287   * Retrieves the notification subscription ID.
288   *
289   * @return  The notification subscription ID.
290   */
291  public String getSubscriptionID()
292  {
293    return subscriptionID;
294  }
295
296
297
298  /**
299   * Retrieves the implementation-specific details for the notification
300   * subscription.
301   *
302   * @return  The implementation-specific details for the notification
303   *          subscription.
304   */
305  public List<ASN1OctetString> getSubscriptionDetails()
306  {
307    return subscriptionDetails;
308  }
309
310
311
312  /**
313   * {@inheritDoc}
314   */
315  @Override()
316  public SetNotificationSubscriptionExtendedRequest duplicate()
317  {
318    return duplicate(getControls());
319  }
320
321
322
323  /**
324   * {@inheritDoc}
325   */
326  @Override()
327  public SetNotificationSubscriptionExtendedRequest
328              duplicate(final Control[] controls)
329  {
330    final SetNotificationSubscriptionExtendedRequest r =
331         new SetNotificationSubscriptionExtendedRequest(managerID,
332              destinationID, subscriptionID, subscriptionDetails, controls);
333    r.setResponseTimeoutMillis(getResponseTimeoutMillis(null));
334    return r;
335  }
336
337
338
339  /**
340   * {@inheritDoc}
341   */
342  @Override()
343  public String getExtendedRequestName()
344  {
345    return INFO_EXTENDED_REQUEST_NAME_SET_NOTIFICATION_SUB.get();
346  }
347
348
349
350  /**
351   * {@inheritDoc}
352   */
353  @Override()
354  public void toString(final StringBuilder buffer)
355  {
356    buffer.append("SetNotificationSubscriptionExtendedRequest(managerID='");
357    buffer.append(managerID);
358    buffer.append("', destinationID='");
359    buffer.append(destinationID);
360    buffer.append("', subscriptionID='");
361    buffer.append(subscriptionID);
362    buffer.append("', subscriptionDetails=ASN1OctetString[");
363    buffer.append(subscriptionDetails.size());
364    buffer.append(']');
365
366    final Control[] controls = getControls();
367    if (controls.length > 0)
368    {
369      buffer.append(", controls={");
370      for (int i=0; i < controls.length; i++)
371      {
372        if (i > 0)
373        {
374          buffer.append(", ");
375        }
376
377        buffer.append(controls[i]);
378      }
379      buffer.append('}');
380    }
381
382    buffer.append(')');
383  }
384}