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