001/*
002 * Copyright 2011-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2011-2024 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) 2011-2024 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.extensions;
037
038
039
040import java.util.ArrayList;
041import java.util.Collection;
042import java.util.Collections;
043import java.util.Iterator;
044import java.util.List;
045
046import com.unboundid.asn1.ASN1Boolean;
047import com.unboundid.asn1.ASN1Element;
048import com.unboundid.asn1.ASN1OctetString;
049import com.unboundid.asn1.ASN1Sequence;
050import com.unboundid.ldap.sdk.LDAPException;
051import com.unboundid.ldap.sdk.ResultCode;
052import com.unboundid.util.Debug;
053import com.unboundid.util.NotMutable;
054import com.unboundid.util.NotNull;
055import com.unboundid.util.Nullable;
056import com.unboundid.util.StaticUtils;
057import com.unboundid.util.ThreadSafety;
058import com.unboundid.util.ThreadSafetyLevel;
059import com.unboundid.util.Validator;
060
061import static com.unboundid.ldap.sdk.unboundidds.extensions.ExtOpMessages.*;
062
063
064
065/**
066 * This class provides an implementation of a get changelog batch change
067 * selection criteria value that indicates that the server should not return
068 * changes which target only the specified attributes.  This can be useful for
069 * ignoring changes to attributes which are changed frequently but not of
070 * interest to the client.  Note that changes returned may include changes to
071 * these attributes, but only if the change targets other attributes that should
072 * not be ignored.
073 * <BR>
074 * <BLOCKQUOTE>
075 *   <B>NOTE:</B>  This class, and other classes within the
076 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
077 *   supported for use against Ping Identity, UnboundID, and
078 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
079 *   for proprietary functionality or for external specifications that are not
080 *   considered stable or mature enough to be guaranteed to work in an
081 *   interoperable way with other types of LDAP servers.
082 * </BLOCKQUOTE>
083 */
084@NotMutable()
085@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
086public final class IgnoreAttributesChangeSelectionCriteria
087       extends ChangelogBatchChangeSelectionCriteria
088{
089  /**
090   * The inner BER type that should be used for encoded elements that represent
091   * an ignore attributes get changelog batch selection criteria value.
092   */
093  static final byte TYPE_SELECTION_CRITERIA_IGNORE_ATTRIBUTES = (byte) 0xA3;
094
095
096
097  // Indicates whether to automatically consider all operational attributes in
098  // the ignore list.
099  private final boolean ignoreOperationalAttributes;
100
101  // The names of the attributes to ignore.
102  @NotNull private final List<String> attributeNames;
103
104
105
106  /**
107   * Creates a new ignore attributes change selection criteria value with the
108   * provided information.
109   *
110   * @param  ignoreOperationalAttributes  Indicates whether to automatically
111   *                                      include all operational attributes in
112   *                                      the set of attributes to ignore.
113   * @param  attributeNames               The names of the attributes to ignore.
114   *                                      It may be {@code null} or empty only
115   *                                      if
116   *                                      {@code ignoreOperationalAttributes}
117   *                                      is {@code true} and no user attributes
118   *                                      changes should be ignored.
119   */
120  public IgnoreAttributesChangeSelectionCriteria(
121              final boolean ignoreOperationalAttributes,
122              @Nullable final String... attributeNames)
123  {
124    this(ignoreOperationalAttributes, StaticUtils.toList(attributeNames));
125  }
126
127
128
129  /**
130   * Creates a new ignore attributes change selection criteria value with the
131   * provided information.
132   *
133   * @param  ignoreOperationalAttributes  Indicates whether to automatically
134   *                                      include all operational attributes in
135   *                                      the set of attributes to ignore.
136   * @param  attributeNames               The names of the attributes to ignore.
137   *                                      It may be {@code null} or empty only
138   *                                      if
139   *                                      {@code ignoreOperationalAttributes}
140   *                                      is {@code true} and no user attributes
141   *                                      changes should be ignored.
142   */
143  public IgnoreAttributesChangeSelectionCriteria(
144              final boolean ignoreOperationalAttributes,
145              @Nullable final Collection<String> attributeNames)
146  {
147    if ((attributeNames == null) || attributeNames.isEmpty())
148    {
149      Validator.ensureTrue(ignoreOperationalAttributes);
150      this.attributeNames = Collections.emptyList();
151    }
152    else
153    {
154      this.attributeNames =
155           Collections.unmodifiableList(new ArrayList<>(attributeNames));
156    }
157
158    this.ignoreOperationalAttributes = ignoreOperationalAttributes;
159  }
160
161
162
163  /**
164   * Decodes the provided ASN.1 element, which is the inner element of a
165   * changelog batch change selection criteria element, as an all attributes
166   * change selection criteria value.
167   *
168   * @param  innerElement  The inner element of a changelog batch change
169   *                       selection criteria element to be decoded.
170   *
171   * @return  The decoded all attributes change selection criteria value.
172   *
173   * @throws  LDAPException  If a problem is encountered while trying to decode
174   *                         the provided element as the inner element of an all
175   *                         attributes change selection criteria value.
176   */
177  @NotNull()
178  static IgnoreAttributesChangeSelectionCriteria decodeInnerElement(
179              @NotNull final ASN1Element innerElement)
180         throws LDAPException
181  {
182    try
183    {
184      final ASN1Element[] elements =
185           ASN1Sequence.decodeAsSequence(innerElement).elements();
186      final ASN1Element[] attrElements =
187           ASN1Sequence.decodeAsSequence(elements[0]).elements();
188      final ArrayList<String> attrNames = new ArrayList<>(attrElements.length);
189      for (final ASN1Element e : attrElements)
190      {
191        attrNames.add(ASN1OctetString.decodeAsOctetString(e).stringValue());
192      }
193
194      return new IgnoreAttributesChangeSelectionCriteria(
195           ASN1Boolean.decodeAsBoolean(elements[1]).booleanValue(),
196           attrNames);
197    }
198    catch (final Exception e)
199    {
200      Debug.debugException(e);
201      throw new LDAPException(ResultCode.DECODING_ERROR,
202           ERR_IGNORE_ATTRS_CHANGE_SELECTION_CRITERIA_DECODE_ERROR.get(
203                StaticUtils.getExceptionMessage(e)),
204           e);
205    }
206  }
207
208
209
210  /**
211   * Indicates whether to automatically include all operational attributes in
212   * the set of attributes to ignore.
213   *
214   * @return  {@code true} if all operational attributes should automatically be
215   *          included in the set of attributes to ignore, or {@code false} if
216   *          only those operational attributes which are explicitly named
217   *          should be ignored.
218   */
219  public boolean ignoreOperationalAttributes()
220  {
221    return ignoreOperationalAttributes;
222  }
223
224
225
226  /**
227   * Retrieves the names of the target attributes for changes that should be
228   * retrieved.
229   *
230   * @return  The names of the target attributes for changes that should be
231   *          retrieved.
232   */
233  @NotNull()
234  public List<String> getAttributeNames()
235  {
236    return attributeNames;
237  }
238
239
240
241  /**
242   * {@inheritDoc}
243   */
244  @Override()
245  @NotNull()
246  public ASN1Element encodeInnerElement()
247  {
248    final ArrayList<ASN1Element> attrNameElements =
249         new ArrayList<>(attributeNames.size());
250    for (final String s : attributeNames)
251    {
252      attrNameElements.add(new ASN1OctetString(s));
253    }
254
255    return new ASN1Sequence(TYPE_SELECTION_CRITERIA_IGNORE_ATTRIBUTES,
256         new ASN1Sequence(attrNameElements),
257         new ASN1Boolean(ignoreOperationalAttributes));
258  }
259
260
261
262  /**
263   * {@inheritDoc}
264   */
265  @Override()
266  public void toString(@NotNull final StringBuilder buffer)
267  {
268    buffer.append("IgnoreAttributesChangeSelectionCriteria(attributeNames={");
269
270    final Iterator<String> iterator = attributeNames.iterator();
271    while (iterator.hasNext())
272    {
273      buffer.append(iterator.next());
274      if (iterator.hasNext())
275      {
276        buffer.append(',');
277      }
278    }
279
280    buffer.append("}, ignoreOperationalAttributes=");
281    buffer.append(ignoreOperationalAttributes);
282    buffer.append(')');
283  }
284}