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.i18n.I18n;
031    import org.apache.directory.shared.ldap.codec.LdapConstants;
032    import org.apache.directory.shared.ldap.codec.LdapMessageCodec;
033    import org.apache.directory.shared.ldap.codec.MessageTypeEnum;
034    import org.apache.directory.shared.ldap.name.DN;
035    import org.apache.directory.shared.ldap.util.StringTools;
036    
037    
038    /**
039     * A CompareRequest Message. Its syntax is :
040     * CompareRequest ::= [APPLICATION 14] SEQUENCE {
041     *              entry           LDAPDN,
042     *              ava             AttributeValueAssertion }
043     * 
044     * AttributeValueAssertion ::= SEQUENCE {
045     *              attributeDesc   AttributeDescription,
046     *              assertionValue  AssertionValue }
047     * 
048     * AttributeDescription ::= LDAPString
049     * 
050     * AssertionValue ::= OCTET STRING
051     * 
052     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
053     * @version $Rev: 918756 $, $Date: 2010-03-04 00:05:29 +0100 (Thu, 04 Mar 2010) $, 
054     */
055    public class CompareRequestCodec extends LdapMessageCodec
056    {
057        // ~ Instance fields
058        // ----------------------------------------------------------------------------
059    
060        /** The entry to be compared */
061        private DN entry;
062    
063        /** The attribute to be compared */
064        private String attributeDesc;
065    
066        /** The value to be compared */
067        private Object assertionValue;
068    
069        /** The compare request length */
070        private int compareRequestLength;
071    
072        /** The attribute value assertion length */
073        private int avaLength;
074    
075    
076        // ~ Constructors
077        // -------------------------------------------------------------------------------
078    
079        /**
080         * Creates a new CompareRequest object.
081         */
082        public CompareRequestCodec()
083        {
084            super();
085        }
086    
087    
088        // ~ Methods
089        // ------------------------------------------------------------------------------------
090    
091        /**
092         * Get the message type
093         * 
094         * @return Returns the type.
095         */
096        public MessageTypeEnum getMessageType()
097        {
098            return MessageTypeEnum.COMPARE_REQUEST;
099        }
100    
101        
102        /**
103         * {@inheritDoc}
104         */
105        public String getMessageTypeName()
106        {
107            return "COMPARE_REQUEST";
108        }
109    
110    
111        /**
112         * Get the entry to be compared
113         * 
114         * @return Returns the entry.
115         */
116        public DN getEntry()
117        {
118            return entry;
119        }
120    
121    
122        /**
123         * Set the entry to be compared
124         * 
125         * @param entry The entry to set.
126         */
127        public void setEntry( DN entry )
128        {
129            this.entry = entry;
130        }
131    
132    
133        /**
134         * Get the assertion value
135         * 
136         * @return Returns the assertionValue.
137         */
138        public Object getAssertionValue()
139        {
140            return assertionValue;
141        }
142    
143    
144        /**
145         * Set the assertion value
146         * 
147         * @param assertionValue The assertionValue to set.
148         */
149        public void setAssertionValue( Object assertionValue )
150        {
151            this.assertionValue = assertionValue;
152        }
153    
154    
155        /**
156         * Get the attribute description
157         * 
158         * @return Returns the attributeDesc.
159         */
160        public String getAttributeDesc()
161        {
162            return ( ( attributeDesc == null ) ? "" : attributeDesc );
163        }
164    
165    
166        /**
167         * Set the attribute description
168         * 
169         * @param attributeDesc The attributeDesc to set.
170         */
171        public void setAttributeDesc( String attributeDesc )
172        {
173            this.attributeDesc = attributeDesc;
174        }
175    
176    
177        /**
178         * Compute the CompareRequest length 
179         * 
180         * CompareRequest : 
181         * 0x6E L1 
182         *   | 
183         *   +--> 0x04 L2 entry 
184         *   +--> 0x30 L3 (ava) 
185         *         | 
186         *         +--> 0x04 L4 attributeDesc 
187         *         +--> 0x04 L5 assertionValue 
188         *         
189         * L3 = Length(0x04) + Length(L4) + L4 + Length(0x04) +
190         *      Length(L5) + L5 
191         * Length(CompareRequest) = Length(0x6E) + Length(L1) + L1 +
192         *      Length(0x04) + Length(L2) + L2 + Length(0x30) + Length(L3) + L3
193         * 
194         * @return The CompareRequest PDU's length
195         */
196        protected int computeLengthProtocolOp()
197        {
198            // The entry
199            compareRequestLength = 1 + TLV.getNbBytes( DN.getNbBytes( entry ) ) + DN.getNbBytes( entry );
200    
201            // The attribute value assertion
202            int attributeDescLength = StringTools.getBytesUtf8( attributeDesc ).length;
203            avaLength = 1 + TLV.getNbBytes( attributeDescLength ) + attributeDescLength;
204    
205            if ( assertionValue instanceof String )
206            {
207                int assertionValueLength = StringTools.getBytesUtf8( ( String ) assertionValue ).length;
208                avaLength += 1 + TLV.getNbBytes( assertionValueLength ) + assertionValueLength;
209            }
210            else
211            {
212                avaLength += 1 + TLV.getNbBytes( ( ( byte[] ) assertionValue ).length )
213                    + ( ( byte[] ) assertionValue ).length;
214            }
215    
216            compareRequestLength += 1 + TLV.getNbBytes( avaLength ) + avaLength;
217    
218            return 1 + TLV.getNbBytes( compareRequestLength ) + compareRequestLength;
219        }
220    
221    
222        /**
223         * Encode the CompareRequest message to a PDU. 
224         * 
225         * CompareRequest : 
226         *   0x6E LL 
227         *     0x04 LL entry 
228         *     0x30 LL attributeValueAssertion 
229         *       0x04 LL attributeDesc 
230         *       0x04 LL assertionValue
231         * 
232         * @param buffer The buffer where to put the PDU
233         * @return The PDU.
234         */
235        protected void encodeProtocolOp( ByteBuffer buffer ) throws EncoderException
236        {
237            try
238            {
239                // The CompareRequest Tag
240                buffer.put( LdapConstants.COMPARE_REQUEST_TAG );
241                buffer.put( TLV.getBytes( compareRequestLength ) );
242    
243                // The entry
244                Value.encode( buffer, DN.getBytes( entry ) );
245    
246                // The attributeValueAssertion sequence Tag
247                buffer.put( UniversalTag.SEQUENCE_TAG );
248                buffer.put( TLV.getBytes( avaLength ) );
249            }
250            catch ( BufferOverflowException boe )
251            {
252                throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
253            }
254    
255            // The attributeDesc
256            Value.encode( buffer, attributeDesc );
257    
258            // The assertionValue
259            if ( assertionValue instanceof String )
260            {
261                Value.encode( buffer, ( String ) assertionValue );
262            }
263            else
264            {
265                Value.encode( buffer, ( byte[] ) assertionValue );
266            }
267        }
268    
269    
270        /**
271         * Get a String representation of a Compare Request
272         * 
273         * @return A Compare Request String
274         */
275        public String toString()
276        {
277            StringBuilder sb = new StringBuilder();
278    
279            sb.append( "    Compare request\n" );
280            sb.append( "        Entry : '" ).append( entry ).append( "'\n" );
281            sb.append( "        Attribute description : '" ).append( attributeDesc ).append( "'\n" );
282            sb.append( "        Attribute value : '" ).append( StringTools.dumpObject( assertionValue ) ).append( '\'' );
283    
284            return toString( sb.toString() );
285        }
286    }