001/*
002 * Copyright 2020-2022 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2020-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) 2020-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.util.ArrayList;
041import java.util.List;
042
043import com.unboundid.asn1.ASN1Element;
044import com.unboundid.asn1.ASN1Integer;
045import com.unboundid.asn1.ASN1Sequence;
046import com.unboundid.ldap.sdk.LDAPException;
047import com.unboundid.ldap.sdk.ResultCode;
048import com.unboundid.util.Debug;
049import com.unboundid.util.NotMutable;
050import com.unboundid.util.NotNull;
051import com.unboundid.util.Nullable;
052import com.unboundid.util.StaticUtils;
053import com.unboundid.util.ThreadSafety;
054import com.unboundid.util.ThreadSafetyLevel;
055import com.unboundid.util.Validator;
056
057import static com.unboundid.ldap.sdk.unboundidds.extensions.ExtOpMessages.*;
058
059
060
061/**
062 * This class provides a collect support data log capture window implementation
063 * that indicates that the tool should capture a specified amount of data (in
064 * kilobytes) from the beginning and end of each log file when processing a
065 * {@link CollectSupportDataExtendedRequest}.
066 * <BR>
067 * <BLOCKQUOTE>
068 *   <B>NOTE:</B>  This class, and other classes within the
069 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
070 *   supported for use against Ping Identity, UnboundID, and
071 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
072 *   for proprietary functionality or for external specifications that are not
073 *   considered stable or mature enough to be guaranteed to work in an
074 *   interoperable way with other types of LDAP servers.
075 * </BLOCKQUOTE>
076 *
077 * @see  CollectSupportDataExtendedRequest
078 */
079@NotMutable()
080@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
081public final class HeadAndTailSizeCollectSupportDataLogCaptureWindow
082       extends CollectSupportDataLogCaptureWindow
083{
084  /**
085   * The BER type for the element used to specify the amount of data in
086   * kilobytes to capture from the beginning of each log file.
087   */
088  private static final byte TYPE_HEAD_SIZE_KB = (byte) 0x80;
089
090
091
092  /**
093   * The BER type for the element used to specify the amount of data in
094   * kilobytes to capture from the end of each log file.
095   */
096  private static final byte TYPE_TAIL_SIZE_KB = (byte) 0x81;
097
098
099
100  /**
101   * The serial version UID for this serializable class.
102   */
103  private static final long serialVersionUID = 6810565494534462677L;
104
105
106
107  // An ASN.1 element that provides an encoded representation of this head and
108  // tail size collect support data log capture window.
109  @NotNull private final ASN1Element encodedWindow;
110
111  // The amount of data in kilobytes to capture from the beginning of each log
112  // file.
113  @Nullable private final Integer headSizeKB;
114
115  // The amount of data in kilobytes to capture from the end of each log file.
116  @Nullable private final Integer tailSizeKB;
117
118
119
120  /**
121   * Creates a new instance of this collect support data log capture window
122   * object that will capture the specified amount of data from the beginning
123   * and end of each log file.
124   *
125   * @param  headSizeKB  The amount of data in kilobytes to capture from the
126   *                     beginning of each log file.  This may be {@code null}
127   *                     if the server should select an appropriate value.
128   * @param  tailSizeKB  The amount of data in kilobytes to capture from the end
129   *                     of each log file.  This may be {@code null} if the
130   *                     server should select an appropriate value.
131   */
132  public HeadAndTailSizeCollectSupportDataLogCaptureWindow(
133              @Nullable final Integer headSizeKB,
134              @Nullable final Integer tailSizeKB)
135  {
136    if (headSizeKB != null)
137    {
138      Validator.ensureTrue((headSizeKB >= 0),
139           "If HeadAndTailSizeCollectSupportDataLogCaptureWindow.headSizeKB " +
140                "is non-null, then it must also be non-negative.");
141    }
142
143    if (tailSizeKB != null)
144    {
145      Validator.ensureTrue((tailSizeKB >= 0),
146           "If HeadAndTailSizeCollectSupportDataLogCaptureWindow.tailSizeKB " +
147                "is non-null, then it must also be non-negative.");
148    }
149
150    this.headSizeKB = headSizeKB;
151    this.tailSizeKB = tailSizeKB;
152
153    final List<ASN1Element> elements = new ArrayList<>(2);
154    if (headSizeKB != null)
155    {
156      elements.add(new ASN1Integer(TYPE_HEAD_SIZE_KB, headSizeKB));
157    }
158
159    if (tailSizeKB != null)
160    {
161      elements.add(new ASN1Integer(TYPE_TAIL_SIZE_KB, tailSizeKB));
162    }
163
164    encodedWindow = new ASN1Sequence(TYPE_HEAD_AND_TAIL_SIZE, elements);
165  }
166
167
168
169  /**
170   * Retrieves the amount of data in kilobytes to capture from the beginning of
171   * each log file, if specified.
172   *
173   * @return  The amount of data in kilobytes to capture from the beginning of
174   *          each log file, or {@code null} if the server should select an
175   *          appropriate value.
176   */
177  @Nullable()
178  public Integer getHeadSizeKB()
179  {
180    return headSizeKB;
181  }
182
183
184
185  /**
186   * Retrieves the amount of data in kilobytes to capture from the end of each
187   * log file, if specified.
188   *
189   * @return  The amount of data in kilobytes to capture from the end of each
190   *          log file, or {@code null} if the server should select an
191   *          appropriate value.
192   */
193  @Nullable()
194  public Integer getTailSizeKB()
195  {
196    return tailSizeKB;
197  }
198
199
200
201  /**
202   * Decodes the provided ASN.1 element as a head and tail size collect support
203   * data log capture window object.
204   *
205   * @param  e  The ASN.1 element to be decoded.  It must not be {@code null}.
206   *
207   * @return  The head and tail size collect support data log capture window
208   *          object that was decoded.
209   *
210   * @throws  LDAPException  If the provided ASN.1 element cannot be decoded as
211   *                         a valid head and tail size collect support data log
212   *                         capture window object.
213   */
214  @NotNull()
215  static HeadAndTailSizeCollectSupportDataLogCaptureWindow decodeInternal(
216              @NotNull final ASN1Element e)
217         throws LDAPException
218  {
219    try
220    {
221      Integer headSizeKB = null;
222      Integer tailSizeKB = null;
223      for (final ASN1Element element :
224           ASN1Sequence.decodeAsSequence(e).elements())
225      {
226        switch (element.getType())
227        {
228          case TYPE_HEAD_SIZE_KB:
229            headSizeKB = ASN1Integer.decodeAsInteger(element).intValue();
230            if (headSizeKB < 0)
231            {
232              throw new LDAPException(ResultCode.DECODING_ERROR,
233                   ERR_HT_SIZE_CSD_LOG_CAPTURE_WINDOW_INVALID_HEAD_SIZE.get(
234                        headSizeKB));
235            }
236            break;
237          case TYPE_TAIL_SIZE_KB:
238            tailSizeKB = ASN1Integer.decodeAsInteger(element).intValue();
239            if (tailSizeKB < 0)
240            {
241              throw new LDAPException(ResultCode.DECODING_ERROR,
242                   ERR_HT_SIZE_CSD_LOG_CAPTURE_WINDOW_INVALID_TAIL_SIZE.get(
243                        tailSizeKB));
244            }
245            break;
246          default:
247            throw new LDAPException(ResultCode.DECODING_ERROR,
248                 ERR_HT_SIZE_CSD_LOG_CAPTURE_WINDOW_INVALID_ELEMENT_TYPE.get(
249                      StaticUtils.toHex(element.getType())));
250        }
251      }
252
253      return new HeadAndTailSizeCollectSupportDataLogCaptureWindow(headSizeKB,
254           tailSizeKB);
255    }
256    catch (final LDAPException le)
257    {
258      Debug.debugException(le);
259      throw le;
260    }
261    catch (final Exception ex)
262    {
263      Debug.debugException(ex);
264      throw new LDAPException(ResultCode.DECODING_ERROR,
265           ERR_HT_SIZE_CSD_LOG_WINDOW_CANNOT_DECODE.get(
266                StaticUtils.getExceptionMessage(ex)),
267           ex);
268    }
269  }
270
271
272
273  /**
274   * {@inheritDoc}
275   */
276  @Override()
277  @NotNull()
278  public ASN1Element encode()
279  {
280    return encodedWindow;
281  }
282
283
284
285  /**
286   * {@inheritDoc}
287   */
288  @Override()
289  public void toString(@NotNull final StringBuilder buffer)
290  {
291    buffer.append("HeadAndTailSizeCollectSupportDataLogCaptureWindow(");
292
293    if (headSizeKB != null)
294    {
295      buffer.append("headSizeKB=");
296      buffer.append(headSizeKB);
297    }
298
299    if (tailSizeKB != null)
300    {
301      if (headSizeKB != null)
302      {
303        buffer.append(", ");
304      }
305
306      buffer.append("tailSizeKB=");
307      buffer.append(tailSizeKB);
308    }
309
310    buffer.append(')');
311  }
312}