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