001/*
002 * Copyright 2010-2022 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2010-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) 2010-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.extensions;
037
038
039
040import java.io.Serializable;
041import java.util.Date;
042
043import com.unboundid.asn1.ASN1Element;
044import com.unboundid.asn1.ASN1OctetString;
045import com.unboundid.ldap.sdk.LDAPException;
046import com.unboundid.ldap.sdk.ResultCode;
047import com.unboundid.util.Debug;
048import com.unboundid.util.NotExtensible;
049import com.unboundid.util.NotNull;
050import com.unboundid.util.StaticUtils;
051import com.unboundid.util.ThreadSafety;
052import com.unboundid.util.ThreadSafetyLevel;
053import com.unboundid.util.Validator;
054
055import static com.unboundid.ldap.sdk.unboundidds.extensions.ExtOpMessages.*;
056
057
058
059/**
060 * This class defines the API that should be implemented by classes which may
061 * represent a way to identify the start of a batch of changes to retrieve using
062 * the {@link GetChangelogBatchExtendedRequest}.
063 * <BR>
064 * <BLOCKQUOTE>
065 *   <B>NOTE:</B>  This class, and other classes within the
066 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
067 *   supported for use against Ping Identity, UnboundID, and
068 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
069 *   for proprietary functionality or for external specifications that are not
070 *   considered stable or mature enough to be guaranteed to work in an
071 *   interoperable way with other types of LDAP servers.
072 * </BLOCKQUOTE>
073 */
074@NotExtensible()
075@ThreadSafety(level=ThreadSafetyLevel.INTERFACE_THREADSAFE)
076public abstract class ChangelogBatchStartingPoint
077       implements Serializable
078{
079  /**
080   * The serial version UID for this serializable class.
081   */
082  private static final long serialVersionUID = -1580168275337643812L;
083
084
085
086  /**
087   * Encodes this starting point value to an ASN.1 element suitable for
088   * inclusion in a changelog batch extended request.
089   *
090   * @return  The encoded representation of this starting point value.
091   */
092  @NotNull()
093  public abstract ASN1Element encode();
094
095
096
097  /**
098   * Decodes the provided ASN.1 element as a changelog batch starting point.
099   *
100   * @param  element  The ASN.1 element to be decoded.  It must not be
101   *                  {@code null}.
102   *
103   * @return  The decoded changelog batch starting point.
104   *
105   * @throws  LDAPException  If the provided ASN.1 element cannot be decoded as
106   *                         a changelog batch starting point.
107   */
108  @NotNull()
109  public static ChangelogBatchStartingPoint decode(
110                     @NotNull final ASN1Element element)
111         throws LDAPException
112  {
113    Validator.ensureNotNull(element);
114
115    switch (element.getType())
116    {
117      case ResumeWithTokenStartingPoint.TYPE:
118        return new ResumeWithTokenStartingPoint(
119             ASN1OctetString.decodeAsOctetString(element));
120
121      case ResumeWithCSNStartingPoint.TYPE:
122        return new ResumeWithCSNStartingPoint(
123             ASN1OctetString.decodeAsOctetString(element).stringValue());
124
125      case BeginningOfChangelogStartingPoint.TYPE:
126        if (element.getValueLength() != 0)
127        {
128          throw new LDAPException(ResultCode.DECODING_ERROR,
129               ERR_BEGINNING_OF_CHANGELOG_STARTING_POINT_HAS_VALUE.get());
130        }
131        return new BeginningOfChangelogStartingPoint();
132
133      case EndOfChangelogStartingPoint.TYPE:
134        if (element.getValueLength() != 0)
135        {
136          throw new LDAPException(ResultCode.DECODING_ERROR,
137               ERR_END_OF_CHANGELOG_STARTING_POINT_HAS_VALUE.get());
138        }
139        return new EndOfChangelogStartingPoint();
140
141      case ChangeTimeStartingPoint.TYPE:
142        final Date time;
143        try
144        {
145          time = StaticUtils.decodeGeneralizedTime(
146               ASN1OctetString.decodeAsOctetString(element).stringValue());
147        }
148        catch (final Exception e)
149        {
150          Debug.debugException(e);
151          throw new LDAPException(ResultCode.DECODING_ERROR,
152               ERR_CHANGE_TIME_STARTING_POINT_MALFORMED_VALUE.get(
153                    StaticUtils.getExceptionMessage(e)), e);
154        }
155        return new ChangeTimeStartingPoint(time.getTime());
156
157      default:
158        throw new LDAPException(ResultCode.DECODING_ERROR,
159             ERR_UNKNOWN_CHANGELOG_BATCH_STARTING_POINT_TYPE.get(
160                  StaticUtils.toHex(element.getType())));
161    }
162  }
163
164
165
166  /**
167   * Retrieves a string representation of this changelog batch starting point.
168   *
169   * @return  A string representation of this changelog batch starting point.
170   */
171  @Override()
172  @NotNull()
173  public final String toString()
174  {
175    final StringBuilder buffer = new StringBuilder();
176    toString(buffer);
177    return buffer.toString();
178  }
179
180
181
182  /**
183   * Appends a string representation of this changelog batch starting point to
184   * the provided buffer.
185   *
186   * @param  buffer  The buffer to which the information should be appended.
187   */
188  public abstract void toString(@NotNull StringBuilder buffer);
189}