001    /*
002     * Copyright 2016 UnboundID Corp.
003     * All Rights Reserved.
004     */
005    /*
006     * Copyright (C) 2016 UnboundID Corp.
007     *
008     * This program is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU General Public License (GPLv2 only)
010     * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
011     * as published by the Free Software Foundation.
012     *
013     * This program is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with this program; if not, see <http://www.gnu.org/licenses>.
020     */
021    package com.unboundid.ldap.sdk.experimental;
022    
023    
024    
025    import com.unboundid.asn1.ASN1Element;
026    import com.unboundid.asn1.ASN1OctetString;
027    import com.unboundid.asn1.ASN1Sequence;
028    import com.unboundid.ldap.sdk.CompareRequest;
029    import com.unboundid.ldap.sdk.Entry;
030    import com.unboundid.ldap.sdk.LDAPException;
031    import com.unboundid.ldap.sdk.OperationType;
032    import com.unboundid.ldap.sdk.ResultCode;
033    import com.unboundid.util.Debug;
034    import com.unboundid.util.NotMutable;
035    import com.unboundid.util.ThreadSafety;
036    import com.unboundid.util.ThreadSafetyLevel;
037    
038    import static com.unboundid.ldap.sdk.experimental.ExperimentalMessages.*;
039    
040    
041    
042    /**
043     * This class represents an entry that holds information about a compare
044     * operation processed by an LDAP server, as per the specification described in
045     * draft-chu-ldap-logschema-00.
046     */
047    @NotMutable()
048    @ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
049    public final class DraftChuLDAPLogSchema00CompareEntry
050           extends DraftChuLDAPLogSchema00Entry
051    {
052      /**
053       * The name of the attribute used to hold the encoded attribute value
054       * assertion.
055       */
056      public static final String ATTR_ENCODED_ASSERTION = "reqAssertion";
057    
058    
059    
060      /**
061       * The serial version UID for this serializable class.
062       */
063      private static final long serialVersionUID = 7968358177150902271L;
064    
065    
066    
067      // The assertion value for the compare operation.
068      private final ASN1OctetString assertionValue;
069    
070      // The attribute name for the compare operation.
071      private final String attributeName;
072    
073    
074    
075      /**
076       * Creates a new instance of this compare access log entry from the provided
077       * entry.
078       *
079       * @param  entry  The entry used to create this compare access log entry.
080       *
081       * @throws  LDAPException  If the provided entry cannot be decoded as a valid
082       *                         compare access log entry as per the specification
083       *                         contained in draft-chu-ldap-logschema-00.
084       */
085      public DraftChuLDAPLogSchema00CompareEntry(final Entry entry)
086             throws LDAPException
087      {
088        super(entry, OperationType.COMPARE);
089    
090    
091        // Decode the attribute value assertion.
092        final byte[] avaBytes =
093             entry.getAttributeValueBytes(ATTR_ENCODED_ASSERTION);
094        if (avaBytes == null)
095        {
096          throw new LDAPException(ResultCode.DECODING_ERROR,
097               ERR_LOGSCHEMA_DECODE_MISSING_REQUIRED_ATTR.get(entry.getDN(),
098                    ATTR_ENCODED_ASSERTION));
099        }
100        else
101        {
102          try
103          {
104            final ASN1Element[] elements =
105                 ASN1Sequence.decodeAsSequence(avaBytes).elements();
106            attributeName =
107                 ASN1OctetString.decodeAsOctetString(elements[0]).stringValue();
108            assertionValue = ASN1OctetString.decodeAsOctetString(elements[1]);
109          }
110          catch (final Exception e)
111          {
112            Debug.debugException(e);
113            throw new LDAPException(ResultCode.DECODING_ERROR,
114                 ERR_LOGSCHEMA_DECODE_COMPARE_AVA_ERROR.get(entry.getDN(),
115                      ATTR_ENCODED_ASSERTION),
116                 e);
117          }
118        }
119      }
120    
121    
122    
123      /**
124       * Retrieves the attribute name for the compare request described by this
125       * compare access log entry.
126       *
127       * @return  The attribute name for the compare request described by this
128       *          compare access log entry.
129       */
130      public String getAttributeName()
131      {
132        return attributeName;
133      }
134    
135    
136    
137      /**
138       * Retrieves the string representation of the assertion value for the compare
139       * request described by this compare access log entry.
140       *
141       * @return  The string representation of the assertion value for the compare
142       *          request described by this compare access log entry.
143       */
144      public String getAssertionValueString()
145      {
146        return assertionValue.stringValue();
147      }
148    
149    
150    
151      /**
152       * Retrieves the bytes that comprise the assertion value for the compare
153       * request described by this compare access log entry.
154       *
155       * @return  The bytes that comprise the assertion value for the compare
156       *          request described by this compare access log entry.
157       */
158      public byte[] getAssertionValueBytes()
159      {
160        return assertionValue.getValue();
161      }
162    
163    
164    
165      /**
166       * Retrieves a {@code CompareRequest} created from this compare access log
167       * entry.
168       *
169       * @return  The {@code CompareRequest} created from this compare access log
170       *          entry.
171       */
172      public CompareRequest toCompareRequest()
173      {
174        return new CompareRequest(getTargetEntryDN(), attributeName,
175             assertionValue.getValue(), getRequestControlArray());
176      }
177    }