001    /*
002     * Copyright 2011-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.List;
030    
031    import com.unboundid.asn1.ASN1Element;
032    import com.unboundid.asn1.ASN1OctetString;
033    import com.unboundid.asn1.ASN1Sequence;
034    import com.unboundid.ldap.sdk.LDAPException;
035    import com.unboundid.ldap.sdk.ResultCode;
036    import com.unboundid.util.Debug;
037    import com.unboundid.util.NotMutable;
038    import com.unboundid.util.StaticUtils;
039    import com.unboundid.util.ThreadSafety;
040    import com.unboundid.util.ThreadSafetyLevel;
041    import com.unboundid.util.Validator;
042    
043    import static com.unboundid.ldap.sdk.unboundidds.extensions.ExtOpMessages.*;
044    
045    
046    
047    /**
048     * <BLOCKQUOTE>
049     *   <B>NOTE:</B>  This class is part of the Commercial Edition of the UnboundID
050     *   LDAP SDK for Java.  It is not available for use in applications that
051     *   include only the Standard Edition of the LDAP SDK, and is not supported for
052     *   use in conjunction with non-UnboundID products.
053     * </BLOCKQUOTE>
054     * This class provides an implementation of a get changelog batch change
055     * selection criteria value that indicates that the server should only return
056     * changes which target one or more of the specified attributes.  The changes
057     * may target other attributes as well, but at least one of the associated
058     * attributes must be included in the change.
059     */
060    @NotMutable()
061    @ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
062    public final class AnyAttributesChangeSelectionCriteria
063           extends ChangelogBatchChangeSelectionCriteria
064    {
065      /**
066       * The inner BER type that should be used for encoded elements that represent
067       * an any attributes get changelog batch selection criteria value.
068       */
069      static final byte TYPE_SELECTION_CRITERIA_ANY_ATTRIBUTES = (byte) 0xA1;
070    
071    
072    
073      // The names of the target attributes.
074      private final List<String> attributeNames;
075    
076    
077    
078      /**
079       * Creates a new any attributes change selection criteria value with the
080       * provided set of attribute names.
081       *
082       * @param  attributeNames  The names of the target attributes for changes that
083       *                         should be retrieved.  It must not be {@code null}
084       *                         or empty.
085       */
086      public AnyAttributesChangeSelectionCriteria(final String... attributeNames)
087      {
088        this(StaticUtils.toList(attributeNames));
089      }
090    
091    
092    
093      /**
094       * Creates a new any attributes change selection criteria value with the
095       * provided set of attribute names.
096       *
097       * @param  attributeNames  The names of the target attributes for changes that
098       *                         should be retrieved.  It must not be {@code null}
099       *                         or empty.
100       */
101      public AnyAttributesChangeSelectionCriteria(
102                  final Collection<String> attributeNames)
103      {
104        Validator.ensureNotNull(attributeNames);
105        Validator.ensureFalse(attributeNames.isEmpty());
106    
107        this.attributeNames =
108             Collections.unmodifiableList(new ArrayList<String>(attributeNames));
109      }
110    
111    
112    
113      /**
114       * Decodes the provided ASN.1 element, which is the inner element of a
115       * changelog batch change selection criteria element, as an any attributes
116       * change selection criteria value.
117       *
118       * @param  innerElement  The inner element of a changelog batch change
119       *                       selection criteria element to be decoded.
120       *
121       * @return  The decoded any attributes change selection criteria value.
122       *
123       * @throws  LDAPException  If a problem is encountered while trying to decode
124       *                         the provided element as the inner element of an any
125       *                         attributes change selection criteria value.
126       */
127      static AnyAttributesChangeSelectionCriteria decodeInnerElement(
128                  final ASN1Element innerElement)
129             throws LDAPException
130      {
131        try
132        {
133          final ASN1Element[] attrElements =
134               ASN1Sequence.decodeAsSequence(innerElement).elements();
135          final ArrayList<String> attrNames =
136               new ArrayList<String>(attrElements.length);
137          for (final ASN1Element e : attrElements)
138          {
139            attrNames.add(ASN1OctetString.decodeAsOctetString(e).stringValue());
140          }
141    
142          return new AnyAttributesChangeSelectionCriteria(attrNames);
143        }
144        catch (final Exception e)
145        {
146          Debug.debugException(e);
147          throw new LDAPException(ResultCode.DECODING_ERROR,
148               ERR_ANY_ATTRS_CHANGE_SELECTION_CRITERIA_DECODE_ERROR.get(
149                    StaticUtils.getExceptionMessage(e)),
150               e);
151        }
152      }
153    
154    
155    
156      /**
157       * Retrieves the names of the target attributes for changes that should be
158       * retrieved.
159       *
160       * @return  The names of the target attributes for changes that should be
161       *          retrieved.
162       */
163      public List<String> getAttributeNames()
164      {
165        return attributeNames;
166      }
167    
168    
169    
170      /**
171       * {@inheritDoc}
172       */
173      @Override()
174      public ASN1Element encodeInnerElement()
175      {
176        final ArrayList<ASN1Element> elements =
177             new ArrayList<ASN1Element>(attributeNames.size());
178        for (final String s : attributeNames)
179        {
180          elements.add(new ASN1OctetString(s));
181        }
182    
183        return new ASN1Sequence(TYPE_SELECTION_CRITERIA_ANY_ATTRIBUTES, elements);
184      }
185    
186    
187    
188      /**
189       * {@inheritDoc}
190       */
191      @Override()
192      public void toString(final StringBuilder buffer)
193      {
194        buffer.append("AnyAttributesChangeSelectionCriteria(attributeNames={");
195    
196        final Iterator<String> iterator = attributeNames.iterator();
197        while (iterator.hasNext())
198        {
199          buffer.append(iterator.next());
200          if (iterator.hasNext())
201          {
202            buffer.append(',');
203          }
204        }
205    
206        buffer.append("})");
207      }
208    }