001/*
002 * Copyright 2016-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2016-2024 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) 2016-2024 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.experimental;
037
038
039
040import com.unboundid.asn1.ASN1Element;
041import com.unboundid.asn1.ASN1OctetString;
042import com.unboundid.asn1.ASN1Sequence;
043import com.unboundid.ldap.sdk.CompareRequest;
044import com.unboundid.ldap.sdk.Entry;
045import com.unboundid.ldap.sdk.LDAPException;
046import com.unboundid.ldap.sdk.OperationType;
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.ThreadSafety;
052import com.unboundid.util.ThreadSafetyLevel;
053
054import static com.unboundid.ldap.sdk.experimental.ExperimentalMessages.*;
055
056
057
058/**
059 * This class represents an entry that holds information about a compare
060 * operation processed by an LDAP server, as per the specification described in
061 * draft-chu-ldap-logschema-00.
062 */
063@NotMutable()
064@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
065public final class DraftChuLDAPLogSchema00CompareEntry
066       extends DraftChuLDAPLogSchema00Entry
067{
068  /**
069   * The name of the attribute used to hold the encoded attribute value
070   * assertion.
071   */
072  @NotNull public static final String ATTR_ENCODED_ASSERTION = "reqAssertion";
073
074
075
076  /**
077   * The serial version UID for this serializable class.
078   */
079  private static final long serialVersionUID = 7968358177150902271L;
080
081
082
083  // The assertion value for the compare operation.
084  @NotNull private final ASN1OctetString assertionValue;
085
086  // The attribute name for the compare operation.
087  @NotNull private final String attributeName;
088
089
090
091  /**
092   * Creates a new instance of this compare access log entry from the provided
093   * entry.
094   *
095   * @param  entry  The entry used to create this compare access log entry.
096   *
097   * @throws  LDAPException  If the provided entry cannot be decoded as a valid
098   *                         compare access log entry as per the specification
099   *                         contained in draft-chu-ldap-logschema-00.
100   */
101  public DraftChuLDAPLogSchema00CompareEntry(@NotNull final Entry entry)
102         throws LDAPException
103  {
104    super(entry, OperationType.COMPARE);
105
106
107    // Decode the attribute value assertion.
108    final byte[] avaBytes =
109         entry.getAttributeValueBytes(ATTR_ENCODED_ASSERTION);
110    if (avaBytes == null)
111    {
112      throw new LDAPException(ResultCode.DECODING_ERROR,
113           ERR_LOGSCHEMA_DECODE_MISSING_REQUIRED_ATTR.get(entry.getDN(),
114                ATTR_ENCODED_ASSERTION));
115    }
116    else
117    {
118      try
119      {
120        final ASN1Element[] elements =
121             ASN1Sequence.decodeAsSequence(avaBytes).elements();
122        attributeName =
123             ASN1OctetString.decodeAsOctetString(elements[0]).stringValue();
124        assertionValue = ASN1OctetString.decodeAsOctetString(elements[1]);
125      }
126      catch (final Exception e)
127      {
128        Debug.debugException(e);
129        throw new LDAPException(ResultCode.DECODING_ERROR,
130             ERR_LOGSCHEMA_DECODE_COMPARE_AVA_ERROR.get(entry.getDN(),
131                  ATTR_ENCODED_ASSERTION),
132             e);
133      }
134    }
135  }
136
137
138
139  /**
140   * Retrieves the attribute name for the compare request described by this
141   * compare access log entry.
142   *
143   * @return  The attribute name for the compare request described by this
144   *          compare access log entry.
145   */
146  @NotNull()
147  public String getAttributeName()
148  {
149    return attributeName;
150  }
151
152
153
154  /**
155   * Retrieves the string representation of the assertion value for the compare
156   * request described by this compare access log entry.
157   *
158   * @return  The string representation of the assertion value for the compare
159   *          request described by this compare access log entry.
160   */
161  @NotNull()
162  public String getAssertionValueString()
163  {
164    return assertionValue.stringValue();
165  }
166
167
168
169  /**
170   * Retrieves the bytes that comprise the assertion value for the compare
171   * request described by this compare access log entry.
172   *
173   * @return  The bytes that comprise the assertion value for the compare
174   *          request described by this compare access log entry.
175   */
176  @NotNull()
177  public byte[] getAssertionValueBytes()
178  {
179    return assertionValue.getValue();
180  }
181
182
183
184  /**
185   * Retrieves a {@code CompareRequest} created from this compare access log
186   * entry.
187   *
188   * @return  The {@code CompareRequest} created from this compare access log
189   *          entry.
190   */
191  @NotNull()
192  public CompareRequest toCompareRequest()
193  {
194    return new CompareRequest(getTargetEntryDN(), attributeName,
195         assertionValue.getValue(), getRequestControlArray());
196  }
197}