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