001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.shared.ldap.codec.compare;
021    
022    
023    import java.nio.BufferOverflowException;
024    import java.nio.ByteBuffer;
025    
026    import org.apache.directory.shared.asn1.ber.tlv.TLV;
027    import org.apache.directory.shared.asn1.ber.tlv.UniversalTag;
028    import org.apache.directory.shared.asn1.ber.tlv.Value;
029    import org.apache.directory.shared.asn1.codec.EncoderException;
030    import org.apache.directory.shared.ldap.codec.LdapConstants;
031    import org.apache.directory.shared.ldap.codec.LdapMessageCodec;
032    import org.apache.directory.shared.ldap.name.LdapDN;
033    import org.apache.directory.shared.ldap.util.StringTools;
034    
035    
036    /**
037     * A CompareRequest Message. Its syntax is :
038     * CompareRequest ::= [APPLICATION 14] SEQUENCE {
039     *              entry           LDAPDN,
040     *              ava             AttributeValueAssertion }
041     * 
042     * AttributeValueAssertion ::= SEQUENCE {
043     *              attributeDesc   AttributeDescription,
044     *              assertionValue  AssertionValue }
045     * 
046     * AttributeDescription ::= LDAPString
047     * 
048     * AssertionValue ::= OCTET STRING
049     * 
050     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
051     * @version $Rev: 764131 $, $Date: 2009-04-11 03:03:00 +0200 (Sam, 11 avr 2009) $, 
052     */
053    public class CompareRequestCodec extends LdapMessageCodec
054    {
055        // ~ Instance fields
056        // ----------------------------------------------------------------------------
057    
058        /** The entry to be compared */
059        private LdapDN entry;
060    
061        /** The attribute to be compared */
062        private String attributeDesc;
063    
064        /** The value to be compared */
065        private Object assertionValue;
066    
067        /** The compare request length */
068        private int compareRequestLength;
069    
070        /** The attribute value assertion length */
071        private int avaLength;
072    
073    
074        // ~ Constructors
075        // -------------------------------------------------------------------------------
076    
077        /**
078         * Creates a new CompareRequest object.
079         */
080        public CompareRequestCodec()
081        {
082            super();
083        }
084    
085    
086        // ~ Methods
087        // ------------------------------------------------------------------------------------
088    
089        /**
090         * Get the message type
091         * 
092         * @return Returns the type.
093         */
094        public int getMessageType()
095        {
096            return LdapConstants.COMPARE_REQUEST;
097        }
098    
099    
100        /**
101         * Get the entry to be compared
102         * 
103         * @return Returns the entry.
104         */
105        public LdapDN getEntry()
106        {
107            return entry;
108        }
109    
110    
111        /**
112         * Set the entry to be compared
113         * 
114         * @param entry The entry to set.
115         */
116        public void setEntry( LdapDN entry )
117        {
118            this.entry = entry;
119        }
120    
121    
122        /**
123         * Get the assertion value
124         * 
125         * @return Returns the assertionValue.
126         */
127        public Object getAssertionValue()
128        {
129            return assertionValue;
130        }
131    
132    
133        /**
134         * Set the assertion value
135         * 
136         * @param assertionValue The assertionValue to set.
137         */
138        public void setAssertionValue( Object assertionValue )
139        {
140            this.assertionValue = assertionValue;
141        }
142    
143    
144        /**
145         * Get the attribute description
146         * 
147         * @return Returns the attributeDesc.
148         */
149        public String getAttributeDesc()
150        {
151            return ( ( attributeDesc == null ) ? "" : attributeDesc );
152        }
153    
154    
155        /**
156         * Set the attribute description
157         * 
158         * @param attributeDesc The attributeDesc to set.
159         */
160        public void setAttributeDesc( String attributeDesc )
161        {
162            this.attributeDesc = attributeDesc;
163        }
164    
165    
166        /**
167         * Compute the CompareRequest length 
168         * 
169         * CompareRequest : 
170         * 0x6E L1 
171         *   | 
172         *   +--> 0x04 L2 entry 
173         *   +--> 0x30 L3 (ava) 
174         *         | 
175         *         +--> 0x04 L4 attributeDesc 
176         *         +--> 0x04 L5 assertionValue 
177         *         
178         * L3 = Length(0x04) + Length(L4) + L4 + Length(0x04) +
179         *      Length(L5) + L5 
180         * Length(CompareRequest) = Length(0x6E) + Length(L1) + L1 +
181         *      Length(0x04) + Length(L2) + L2 + Length(0x30) + Length(L3) + L3
182         * 
183         * @return The CompareRequest PDU's length
184         */
185        public int computeLength()
186        {
187    
188            // The entry
189            compareRequestLength = 1 + TLV.getNbBytes( LdapDN.getNbBytes( entry ) ) + LdapDN.getNbBytes( entry );
190    
191            // The attribute value assertion
192            int attributeDescLength = StringTools.getBytesUtf8( attributeDesc ).length;
193            avaLength = 1 + TLV.getNbBytes( attributeDescLength ) + attributeDescLength;
194    
195            if ( assertionValue instanceof String )
196            {
197                int assertionValueLength = StringTools.getBytesUtf8( ( String ) assertionValue ).length;
198                avaLength += 1 + TLV.getNbBytes( assertionValueLength ) + assertionValueLength;
199            }
200            else
201            {
202                avaLength += 1 + TLV.getNbBytes( ( ( byte[] ) assertionValue ).length )
203                    + ( ( byte[] ) assertionValue ).length;
204            }
205    
206            compareRequestLength += 1 + TLV.getNbBytes( avaLength ) + avaLength;
207    
208            return 1 + TLV.getNbBytes( compareRequestLength ) + compareRequestLength;
209        }
210    
211    
212        /**
213         * Encode the CompareRequest message to a PDU. 
214         * 
215         * CompareRequest : 
216         *   0x6E LL 
217         *     0x04 LL entry 
218         *     0x30 LL attributeValueAssertion 
219         *       0x04 LL attributeDesc 
220         *       0x04 LL assertionValue
221         * 
222         * @param buffer The buffer where to put the PDU
223         * @return The PDU.
224         */
225        public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
226        {
227            if ( buffer == null )
228            {
229                throw new EncoderException( "Cannot put a PDU in a null buffer !" );
230            }
231    
232            try
233            {
234                // The CompareRequest Tag
235                buffer.put( LdapConstants.COMPARE_REQUEST_TAG );
236                buffer.put( TLV.getBytes( compareRequestLength ) );
237    
238                // The entry
239                Value.encode( buffer, LdapDN.getBytes( entry ) );
240    
241                // The attributeValueAssertion sequence Tag
242                buffer.put( UniversalTag.SEQUENCE_TAG );
243                buffer.put( TLV.getBytes( avaLength ) );
244            }
245            catch ( BufferOverflowException boe )
246            {
247                throw new EncoderException( "The PDU buffer size is too small !" );
248            }
249    
250            // The attributeDesc
251            Value.encode( buffer, attributeDesc );
252    
253            // The assertionValue
254            if ( assertionValue instanceof String )
255            {
256                Value.encode( buffer, ( String ) assertionValue );
257            }
258            else
259            {
260                Value.encode( buffer, ( byte[] ) assertionValue );
261            }
262    
263            return buffer;
264        }
265    
266    
267        /**
268         * Get a String representation of a Compare Request
269         * 
270         * @return A Compare Request String
271         */
272        public String toString()
273        {
274    
275            StringBuffer sb = new StringBuffer();
276    
277            sb.append( "    Compare request\n" );
278            sb.append( "        Entry : '" ).append( entry ).append( "'\n" );
279            sb.append( "        Attribute description : '" ).append( attributeDesc ).append( "'\n" );
280            sb.append( "        Attribute value : '" ).append( StringTools.dumpObject( assertionValue )  ).append( "'\n" );
281    
282            return sb.toString();
283        }
284    }