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