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