001/*
002 * Copyright 2012-2022 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2012-2022 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) 2012-2022 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.controls;
037
038
039
040import java.util.ArrayList;
041import java.util.Arrays;
042import java.util.Collection;
043import java.util.Collections;
044import java.util.EnumSet;
045import java.util.Iterator;
046import java.util.Set;
047
048import com.unboundid.asn1.ASN1Element;
049import com.unboundid.asn1.ASN1Enumerated;
050import com.unboundid.asn1.ASN1OctetString;
051import com.unboundid.asn1.ASN1Sequence;
052import com.unboundid.ldap.sdk.Control;
053import com.unboundid.ldap.sdk.LDAPException;
054import com.unboundid.ldap.sdk.ResultCode;
055import com.unboundid.util.Debug;
056import com.unboundid.util.NotMutable;
057import com.unboundid.util.NotNull;
058import com.unboundid.util.StaticUtils;
059import com.unboundid.util.ThreadSafety;
060import com.unboundid.util.ThreadSafetyLevel;
061import com.unboundid.util.Validator;
062
063import static com.unboundid.ldap.sdk.unboundidds.controls.ControlMessages.*;
064
065
066
067/**
068 * This class provides an implementation of a control that can be used to
069 * indicate that the server should suppress the update to one or more
070 * operational attributes for the associated request.
071 * <BR>
072 * <BLOCKQUOTE>
073 *   <B>NOTE:</B>  This class, and other classes within the
074 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
075 *   supported for use against Ping Identity, UnboundID, and
076 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
077 *   for proprietary functionality or for external specifications that are not
078 *   considered stable or mature enough to be guaranteed to work in an
079 *   interoperable way with other types of LDAP servers.
080 * </BLOCKQUOTE>
081 * <BR>
082 * The request control has an OID of 1.3.6.1.4.1.30221.2.5.27, and the
083 * criticality may be either {@code true} or {@code false}.  The control must
084 * have a value with the following encoding:
085 * <PRE>
086 *   SuppressOperationalAttributeUpdateRequestValue ::= SEQUENCE {
087 *        suppressTypes     [0] SEQUENCE OF ENUMERATED {
088 *             last-access-time     (0),
089 *             last-login-time      (1),
090 *             last-login-ip        (2),
091 *             lastmod              (3),
092 *             ... },
093 *        ... }
094 * </PRE>
095 */
096@NotMutable()
097@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
098public final class SuppressOperationalAttributeUpdateRequestControl
099       extends Control
100{
101  /**
102   * The OID (1.3.6.1.4.1.30221.2.5.27) for the suppress operational attribute
103   * update request control.
104   */
105  @NotNull public static final String SUPPRESS_OP_ATTR_UPDATE_REQUEST_OID =
106       "1.3.6.1.4.1.30221.2.5.27";
107
108
109
110  /**
111   * The BER type to use for the set of suppress types.
112   */
113  private static final byte TYPE_SUPPRESS_TYPES = (byte) 0x80;
114
115
116  /**
117   * The serial version UID for this serializable class.
118   */
119  private static final long serialVersionUID = 4603958484615351672L;
120
121
122
123  // The set of suppress types to include in the control.
124  @NotNull private final Set<SuppressType> suppressTypes;
125
126
127
128  /**
129   * Creates a new instance of this control that will suppress updates to the
130   * specified kinds of operational attributes.  It will not be critical.
131   *
132   * @param  suppressTypes  The set of suppress types to include in the control.
133   *                        It must not be {@code null} or empty.
134   */
135  public SuppressOperationalAttributeUpdateRequestControl(
136              @NotNull final SuppressType... suppressTypes)
137  {
138    this(false, suppressTypes);
139  }
140
141
142
143  /**
144   * Creates a new instance of this control that will suppress updates to the
145   * specified kinds of operational attributes.  It will not be critical.
146   *
147   * @param  suppressTypes  The set of suppress types to include in the control.
148   *                        It must not be {@code null} or empty.
149   */
150  public SuppressOperationalAttributeUpdateRequestControl(
151              @NotNull final Collection<SuppressType> suppressTypes)
152  {
153    this(false, suppressTypes);
154  }
155
156
157
158  /**
159   * Creates a new instance of this control that will suppress updates to the
160   * specified kinds of operational attributes.
161   *
162   * @param  isCritical     Indicates whether the control should be considered
163   *                        critical.
164   * @param  suppressTypes  The set of suppress types to include in the control.
165   *                        It must not be {@code null} or empty.
166   */
167  public SuppressOperationalAttributeUpdateRequestControl(
168              final boolean isCritical,
169              @NotNull final SuppressType... suppressTypes)
170  {
171    this(isCritical, Arrays.asList(suppressTypes));
172  }
173
174
175
176  /**
177   * Creates a new instance of this control that will suppress updates to the
178   * specified kinds of operational attributes.
179   *
180   * @param  isCritical     Indicates whether the control should be considered
181   *                        critical.
182   * @param  suppressTypes  The set of suppress types to include in the control.
183   *                        It must not be {@code null} or empty.
184   */
185  public SuppressOperationalAttributeUpdateRequestControl(
186              final boolean isCritical,
187              @NotNull final Collection<SuppressType> suppressTypes)
188  {
189    super(SUPPRESS_OP_ATTR_UPDATE_REQUEST_OID, isCritical,
190         encodeValue(suppressTypes));
191
192    Validator.ensureFalse(suppressTypes.isEmpty());
193
194    final EnumSet<SuppressType> s = EnumSet.noneOf(SuppressType.class);
195    for (final SuppressType t : suppressTypes)
196    {
197      s.add(t);
198    }
199
200    this.suppressTypes = Collections.unmodifiableSet(s);
201  }
202
203
204
205  /**
206   * Decodes the provided generic control as a suppress operational attribute
207   * update request control.
208   *
209   * @param  control  The generic control to be decoded as a suppress
210   *                  operational attribute update request control.
211   *
212   * @throws  LDAPException  If a problem is encountered while attempting to
213   *                         decode the provided control.
214   */
215  public SuppressOperationalAttributeUpdateRequestControl(
216              @NotNull final Control control)
217         throws LDAPException
218  {
219    super(control);
220
221    final ASN1OctetString value = control.getValue();
222    if (value == null)
223    {
224      throw new LDAPException(ResultCode.DECODING_ERROR,
225           ERR_SUPPRESS_OP_ATTR_UPDATE_REQUEST_MISSING_VALUE.get());
226    }
227
228    try
229    {
230      final ASN1Sequence valueSequence =
231           ASN1Sequence.decodeAsSequence(value.getValue());
232      final ASN1Sequence suppressTypesSequence =
233           ASN1Sequence.decodeAsSequence(valueSequence.elements()[0]);
234
235      final EnumSet<SuppressType> s = EnumSet.noneOf(SuppressType.class);
236      for (final ASN1Element e : suppressTypesSequence.elements())
237      {
238        final ASN1Enumerated ae = ASN1Enumerated.decodeAsEnumerated(e);
239        final SuppressType t = SuppressType.valueOf(ae.intValue());
240        if (t == null)
241        {
242          throw new LDAPException(ResultCode.DECODING_ERROR,
243               ERR_SUPPRESS_OP_ATTR_UNRECOGNIZED_SUPPRESS_TYPE.get(
244                    ae.intValue()));
245        }
246        else
247        {
248          s.add(t);
249        }
250      }
251
252      suppressTypes = Collections.unmodifiableSet(s);
253    }
254    catch (final LDAPException le)
255    {
256      Debug.debugException(le);
257      throw le;
258    }
259    catch (final Exception e)
260    {
261      Debug.debugException(e);
262      throw new LDAPException(ResultCode.DECODING_ERROR,
263           ERR_SUPPRESS_OP_ATTR_UPDATE_REQUEST_CANNOT_DECODE.get(
264                StaticUtils.getExceptionMessage(e)),
265           e);
266    }
267  }
268
269
270
271  /**
272   * Encodes the provided information into an octet string suitable for use as
273   * the value of this control.
274   *
275   * @param  suppressTypes  The set of suppress types to include in the control.
276   *                        It must not be {@code null} or empty.
277   *
278   * @return  The ASN.1 octet string containing the encoded value.
279   */
280  @NotNull()
281  private static ASN1OctetString encodeValue(
282                      @NotNull final Collection<SuppressType> suppressTypes)
283  {
284    final ArrayList<ASN1Element> suppressTypeElements =
285         new ArrayList<>(suppressTypes.size());
286    for (final SuppressType t : suppressTypes)
287    {
288      suppressTypeElements.add(new ASN1Enumerated(t.intValue()));
289    }
290
291    final ASN1Sequence valueSequence = new ASN1Sequence(
292         new ASN1Sequence(TYPE_SUPPRESS_TYPES, suppressTypeElements));
293    return new ASN1OctetString(valueSequence.encode());
294  }
295
296
297
298  /**
299   * Retrieves the set of suppress types for this control.
300   *
301   * @return  The set of suppress types for this control.
302   */
303  @NotNull()
304  public Set<SuppressType> getSuppressTypes()
305  {
306    return suppressTypes;
307  }
308
309
310
311  /**
312   * {@inheritDoc}
313   */
314  @Override()
315  @NotNull()
316  public String getControlName()
317  {
318    return INFO_CONTROL_NAME_SUPPRESS_OP_ATTR_UPDATE_REQUEST.get();
319  }
320
321
322
323  /**
324   * {@inheritDoc}
325   */
326  @Override()
327  public void toString(@NotNull final StringBuilder buffer)
328  {
329    buffer.append("SuppressOperationalAttributeUpdateRequestControl(" +
330         "isCritical=");
331    buffer.append(isCritical());
332    buffer.append(", suppressTypes={");
333
334    final Iterator<SuppressType> iterator = suppressTypes.iterator();
335    while (iterator.hasNext())
336    {
337      buffer.append(iterator.next().name());
338      if (iterator.hasNext())
339      {
340        buffer.append(',');
341      }
342    }
343
344    buffer.append("})");
345  }
346}