001    /*
002     * Copyright 2009-2012 UnboundID Corp.
003     * All Rights Reserved.
004     */
005    /*
006     * Copyright (C) 2009-2012 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.protocol;
022    
023    
024    
025    import com.unboundid.asn1.ASN1Buffer;
026    import com.unboundid.asn1.ASN1BufferSequence;
027    import com.unboundid.asn1.ASN1OctetString;
028    import com.unboundid.asn1.ASN1StreamReader;
029    import com.unboundid.ldap.sdk.LDAPException;
030    import com.unboundid.ldap.sdk.ResultCode;
031    import com.unboundid.util.NotMutable;
032    import com.unboundid.util.InternalUseOnly;
033    import com.unboundid.util.ThreadSafety;
034    import com.unboundid.util.ThreadSafetyLevel;
035    
036    import static com.unboundid.ldap.protocol.ProtocolMessages.*;
037    import static com.unboundid.util.Debug.*;
038    import static com.unboundid.util.StaticUtils.*;
039    import static com.unboundid.util.Validator.*;
040    
041    
042    
043    /**
044     * This class provides an implementation of an LDAP compare request protocol op.
045     */
046    @InternalUseOnly()
047    @NotMutable()
048    @ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
049    public final class CompareRequestProtocolOp
050           implements ProtocolOp
051    {
052      /**
053       * The serial version UID for this serializable class.
054       */
055      private static final long serialVersionUID = -562642367801440060L;
056    
057    
058    
059      // The assertion value for this compare request.
060      private final ASN1OctetString assertionValue;
061    
062      // The attribute name for this compare request.
063      private final String attributeName;
064    
065      // The entry DN for this compare request.
066      private final String dn;
067    
068    
069    
070      /**
071       * Creates a new compare request protocol op with the provided information.
072       *
073       * @param  dn              The DN for this compare request.
074       * @param  attributeName   The attribute name for this compare request.
075       * @param  assertionValue  The assertion value for this compare request.
076       */
077      public CompareRequestProtocolOp(final String dn, final String attributeName,
078                                      final ASN1OctetString assertionValue)
079      {
080        this.dn             = dn;
081        this.attributeName  = attributeName;
082        this.assertionValue = assertionValue;
083      }
084    
085    
086    
087      /**
088       * Creates a new compare request protocol op read from the provided ASN.1
089       * stream reader.
090       *
091       * @param  reader  The ASN.1 stream reader from which to read the compare
092       *                 request protocol op.
093       *
094       * @throws  LDAPException  If a problem occurs while reading or parsing the
095       *                         compare request.
096       */
097      CompareRequestProtocolOp(final ASN1StreamReader reader)
098           throws LDAPException
099      {
100        try
101        {
102          reader.beginSequence();
103          dn = reader.readString();
104    
105          reader.beginSequence();
106          attributeName = reader.readString();
107          assertionValue = new ASN1OctetString(reader.readBytes());
108          ensureNotNull(dn, attributeName, assertionValue);
109        }
110        catch (Exception e)
111        {
112          debugException(e);
113    
114          throw new LDAPException(ResultCode.DECODING_ERROR,
115               ERR_COMPARE_REQUEST_CANNOT_DECODE.get(getExceptionMessage(e)), e);
116        }
117      }
118    
119    
120    
121      /**
122       * Retrieves the DN for this compare request.
123       *
124       * @return  The DN for this compare request.
125       */
126      public String getDN()
127      {
128        return dn;
129      }
130    
131    
132    
133      /**
134       * Retrieves the attribute name for this compare request.
135       *
136       * @return  The attribute name for this compare request.
137       */
138      public String getAttributeName()
139      {
140        return attributeName;
141      }
142    
143    
144    
145      /**
146       * Retrieves the assertion value for this compare request.
147       *
148       * @return  The assertion value for this compare request.
149       */
150      public ASN1OctetString getAssertionValue()
151      {
152        return assertionValue;
153      }
154    
155    
156    
157      /**
158       * {@inheritDoc}
159       */
160      public byte getProtocolOpType()
161      {
162        return LDAPMessage.PROTOCOL_OP_TYPE_COMPARE_REQUEST;
163      }
164    
165    
166    
167      /**
168       * {@inheritDoc}
169       */
170      public void writeTo(final ASN1Buffer buffer)
171      {
172        final ASN1BufferSequence opSequence =
173             buffer.beginSequence(LDAPMessage.PROTOCOL_OP_TYPE_COMPARE_REQUEST);
174        buffer.addOctetString(dn);
175    
176        final ASN1BufferSequence avaSequence = buffer.beginSequence();
177        buffer.addOctetString(attributeName);
178        buffer.addElement(assertionValue);
179        avaSequence.end();
180        opSequence.end();
181      }
182    
183    
184    
185      /**
186       * Retrieves a string representation of this protocol op.
187       *
188       * @return  A string representation of this protocol op.
189       */
190      @Override()
191      public String toString()
192      {
193        final StringBuilder buffer = new StringBuilder();
194        toString(buffer);
195        return buffer.toString();
196      }
197    
198    
199    
200      /**
201       * {@inheritDoc}
202       */
203      public void toString(final StringBuilder buffer)
204      {
205        buffer.append("CompareRequestProtocolOp(dn='");
206        buffer.append(dn);
207        buffer.append("', attributeName='");
208        buffer.append(attributeName);
209        buffer.append("', assertionValue='");
210        buffer.append(assertionValue.stringValue());
211        buffer.append("')");
212      }
213    }