001    /*
002     * Copyright 2010-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.io.Serializable;
026    import java.util.Date;
027    
028    import com.unboundid.asn1.ASN1Element;
029    import com.unboundid.asn1.ASN1OctetString;
030    import com.unboundid.ldap.sdk.LDAPException;
031    import com.unboundid.ldap.sdk.ResultCode;
032    import com.unboundid.util.Debug;
033    import com.unboundid.util.NotExtensible;
034    import com.unboundid.util.StaticUtils;
035    import com.unboundid.util.ThreadSafety;
036    import com.unboundid.util.ThreadSafetyLevel;
037    import com.unboundid.util.Validator;
038    
039    import static com.unboundid.ldap.sdk.unboundidds.extensions.ExtOpMessages.*;
040    
041    
042    
043    /**
044     * <BLOCKQUOTE>
045     *   <B>NOTE:</B>  This class is part of the Commercial Edition of the UnboundID
046     *   LDAP SDK for Java.  It is not available for use in applications that
047     *   include only the Standard Edition of the LDAP SDK, and is not supported for
048     *   use in conjunction with non-UnboundID products.
049     * </BLOCKQUOTE>
050     * This class defines the API that should be implemented by classes which may
051     * represent a way to identify the start of a batch of changes to retrieve using
052     * the {@link GetChangelogBatchExtendedRequest}.
053     */
054    @NotExtensible()
055    @ThreadSafety(level=ThreadSafetyLevel.INTERFACE_THREADSAFE)
056    public abstract class ChangelogBatchStartingPoint
057           implements Serializable
058    {
059      /**
060       * The serial version UID for this serializable class.
061       */
062      private static final long serialVersionUID = -1580168275337643812L;
063    
064    
065    
066      /**
067       * Encodes this starting point value to an ASN.1 element suitable for
068       * inclusion in a changelog batch extended request.
069       *
070       * @return  The encoded representation of this starting point value.
071       */
072      public abstract ASN1Element encode();
073    
074    
075    
076      /**
077       * Decodes the provided ASN.1 element as a changelog batch starting point.
078       *
079       * @param  element  The ASN.1 element to be decoded.  It must not be
080       *                  {@code null}.
081       *
082       * @return  The decoded changelog batch starting point.
083       *
084       * @throws  LDAPException  If the provided ASN.1 element cannot be decoded as
085       *                         a changelog batch starting point.
086       */
087      public static ChangelogBatchStartingPoint decode(final ASN1Element element)
088             throws LDAPException
089      {
090        Validator.ensureNotNull(element);
091    
092        switch (element.getType())
093        {
094          case ResumeWithTokenStartingPoint.TYPE:
095            return new ResumeWithTokenStartingPoint(
096                 ASN1OctetString.decodeAsOctetString(element));
097    
098          case ResumeWithCSNStartingPoint.TYPE:
099            return new ResumeWithCSNStartingPoint(
100                 ASN1OctetString.decodeAsOctetString(element).stringValue());
101    
102          case BeginningOfChangelogStartingPoint.TYPE:
103            if (element.getValueLength() != 0)
104            {
105              throw new LDAPException(ResultCode.DECODING_ERROR,
106                   ERR_BEGINNING_OF_CHANGELOG_STARTING_POINT_HAS_VALUE.get());
107            }
108            return new BeginningOfChangelogStartingPoint();
109    
110          case EndOfChangelogStartingPoint.TYPE:
111            if (element.getValueLength() != 0)
112            {
113              throw new LDAPException(ResultCode.DECODING_ERROR,
114                   ERR_END_OF_CHANGELOG_STARTING_POINT_HAS_VALUE.get());
115            }
116            return new EndOfChangelogStartingPoint();
117    
118          case ChangeTimeStartingPoint.TYPE:
119            final Date time;
120            try
121            {
122              time = StaticUtils.decodeGeneralizedTime(
123                   ASN1OctetString.decodeAsOctetString(element).stringValue());
124            }
125            catch (final Exception e)
126            {
127              Debug.debugException(e);
128              throw new LDAPException(ResultCode.DECODING_ERROR,
129                   ERR_CHANGE_TIME_STARTING_POINT_MALFORMED_VALUE.get(
130                        StaticUtils.getExceptionMessage(e)), e);
131            }
132            return new ChangeTimeStartingPoint(time.getTime());
133    
134          default:
135            throw new LDAPException(ResultCode.DECODING_ERROR,
136                 ERR_UNKNOWN_CHANGELOG_BATCH_STARTING_POINT_TYPE.get(
137                      StaticUtils.toHex(element.getType())));
138        }
139      }
140    
141    
142    
143      /**
144       * Retrieves a string representation of this changelog batch starting point.
145       *
146       * @return  A string representation of this changelog batch starting point.
147       */
148      @Override()
149      public final String toString()
150      {
151        final StringBuilder buffer = new StringBuilder();
152        toString(buffer);
153        return buffer.toString();
154      }
155    
156    
157    
158      /**
159       * Appends a string representation of this changelog batch starting point to
160       * the provided buffer.
161       *
162       * @param  buffer  The buffer to which the information should be appended.
163       */
164      public abstract void toString(final StringBuilder buffer);
165    }