You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2011/01/28 20:58:31 UTC

svn commit: r1064846 [2/2] - /directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/ModifyRequestDecorator.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/ModifyRequestDecorator.java?rev=1064846&r1=1064845&r2=1064846&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/ModifyRequestDecorator.java (original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/ModifyRequestDecorator.java Fri Jan 28 19:58:29 2011
@@ -20,10 +20,18 @@
 package org.apache.directory.shared.ldap.codec.decorators;
 
 
+import java.nio.BufferOverflowException;
+import java.nio.ByteBuffer;
 import java.util.Collection;
 import java.util.LinkedList;
 import java.util.List;
 
+import org.apache.directory.shared.asn1.EncoderException;
+import org.apache.directory.shared.asn1.ber.tlv.TLV;
+import org.apache.directory.shared.asn1.ber.tlv.UniversalTag;
+import org.apache.directory.shared.asn1.ber.tlv.Value;
+import org.apache.directory.shared.i18n.I18n;
+import org.apache.directory.shared.ldap.codec.LdapConstants;
 import org.apache.directory.shared.ldap.model.entry.DefaultEntryAttribute;
 import org.apache.directory.shared.ldap.model.entry.DefaultModification;
 import org.apache.directory.shared.ldap.model.entry.EntryAttribute;
@@ -376,4 +384,221 @@ public class ModifyRequestDecorator exte
     {
         getModifyRequest().replace( attr );
     }
+
+
+    //-------------------------------------------------------------------------
+    // The Decorator methods
+    //-------------------------------------------------------------------------
+    /**
+     * Compute the ModifyRequest length 
+     * 
+     * ModifyRequest :
+     * 
+     * 0x66 L1
+     *  |
+     *  +--> 0x04 L2 object
+     *  +--> 0x30 L3 modifications
+     *        |
+     *        +--> 0x30 L4-1 modification sequence
+     *        |     |
+     *        |     +--> 0x0A 0x01 (0..2) operation
+     *        |     +--> 0x30 L5-1 modification
+     *        |           |
+     *        |           +--> 0x04 L6-1 type
+     *        |           +--> 0x31 L7-1 vals
+     *        |                 |
+     *        |                 +--> 0x04 L8-1-1 attributeValue
+     *        |                 +--> 0x04 L8-1-2 attributeValue
+     *        |                 +--> ...
+     *        |                 +--> 0x04 L8-1-i attributeValue
+     *        |                 +--> ...
+     *        |                 +--> 0x04 L8-1-n attributeValue
+     *        |
+     *        +--> 0x30 L4-2 modification sequence
+     *        .     |
+     *        .     +--> 0x0A 0x01 (0..2) operation
+     *        .     +--> 0x30 L5-2 modification
+     *                    |
+     *                    +--> 0x04 L6-2 type
+     *                    +--> 0x31 L7-2 vals
+     *                          |
+     *                          +--> 0x04 L8-2-1 attributeValue
+     *                          +--> 0x04 L8-2-2 attributeValue
+     *                          +--> ...
+     *                          +--> 0x04 L8-2-i attributeValue
+     *                          +--> ...
+     *                          +--> 0x04 L8-2-n attributeValue
+     */
+    public int computeLength()
+    {
+        // Initialized with name
+        int modifyRequestLength = 1 + TLV.getNbBytes( Dn.getNbBytes( getName() ) )
+            + Dn.getNbBytes( getName() );
+
+        // All the changes length
+        int changesLength = 0;
+
+        Collection<Modification> modifications = getModifications();
+
+        if ( ( modifications != null ) && ( modifications.size() != 0 ) )
+        {
+            List<Integer> changeLength = new LinkedList<Integer>();
+            List<Integer> modificationLength = new LinkedList<Integer>();
+            List<Integer> valuesLength = new LinkedList<Integer>();
+
+            for ( Modification modification : modifications )
+            {
+                // Modification sequence length initialized with the operation
+                int localModificationSequenceLength = 1 + 1 + 1;
+                int localValuesLength = 0;
+
+                // Modification length initialized with the type
+                int typeLength = modification.getAttribute().getId().length();
+                int localModificationLength = 1 + TLV.getNbBytes( typeLength ) + typeLength;
+
+                // Get all the values
+                if ( modification.getAttribute().size() != 0 )
+                {
+                    for ( org.apache.directory.shared.ldap.model.entry.Value<?> value : modification.getAttribute() )
+                    {
+                        localValuesLength += 1 + TLV.getNbBytes( value.getBytes().length ) + value.getBytes().length;
+                    }
+                }
+
+                localModificationLength += 1 + TLV.getNbBytes( localValuesLength ) + localValuesLength;
+
+                // Compute the modificationSequenceLength
+                localModificationSequenceLength += 1 + TLV.getNbBytes( localModificationLength )
+                    + localModificationLength;
+
+                // Add the tag and the length
+                changesLength += 1 + TLV.getNbBytes( localModificationSequenceLength )
+                    + localModificationSequenceLength;
+
+                // Store the arrays of values
+                valuesLength.add( localValuesLength );
+                modificationLength.add( localModificationLength );
+                changeLength.add( localModificationSequenceLength );
+            }
+
+            // Add the modifications length to the modificationRequestLength
+            modifyRequestLength += 1 + TLV.getNbBytes( changesLength ) + changesLength;
+            setChangeLength( changeLength );
+            setModificationLength( modificationLength );
+            setValuesLength( valuesLength );
+        }
+
+        setChangesLength( changesLength );
+        setModifyRequestLength( modifyRequestLength );
+
+        return 1 + TLV.getNbBytes( modifyRequestLength ) + modifyRequestLength;
+    }
+
+
+    /**
+     * Encode the ModifyRequest message to a PDU. 
+     * 
+     * ModifyRequest : 
+     * <pre>
+     * 0x66 LL
+     *   0x04 LL object
+     *   0x30 LL modifiations
+     *     0x30 LL modification sequence
+     *       0x0A 0x01 operation
+     *       0x30 LL modification
+     *         0x04 LL type
+     *         0x31 LL vals
+     *           0x04 LL attributeValue
+     *           ... 
+     *           0x04 LL attributeValue
+     *     ... 
+     *     0x30 LL modification sequence
+     *       0x0A 0x01 operation
+     *       0x30 LL modification
+     *         0x04 LL type
+     *         0x31 LL vals
+     *           0x04 LL attributeValue
+     *           ... 
+     *           0x04 LL attributeValue
+     * </pre>
+     * 
+     * @param buffer The buffer where to put the PDU
+     * @return The PDU.
+     */
+    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
+    {
+        try
+        {
+            // The AddRequest Tag
+            buffer.put( LdapConstants.MODIFY_REQUEST_TAG );
+            buffer.put( TLV.getBytes( getModifyRequestLength() ) );
+
+            // The entry
+            Value.encode( buffer, Dn.getBytes( getName() ) );
+
+            // The modifications sequence
+            buffer.put( UniversalTag.SEQUENCE.getValue() );
+            buffer.put( TLV.getBytes( getChangesLength() ) );
+
+            // The modifications list
+            Collection<Modification> modifications = getModifications();
+
+            if ( ( modifications != null ) && ( modifications.size() != 0 ) )
+            {
+                int modificationNumber = 0;
+
+                // Compute the modifications length
+                for ( Modification modification : modifications )
+                {
+                    // The modification sequence
+                    buffer.put( UniversalTag.SEQUENCE.getValue() );
+                    int localModificationSequenceLength = getChangeLength().get( modificationNumber );
+                    buffer.put( TLV.getBytes( localModificationSequenceLength ) );
+
+                    // The operation. The value has to be changed, it's not
+                    // the same value in DirContext and in RFC 2251.
+                    buffer.put( UniversalTag.ENUMERATED.getValue() );
+                    buffer.put( ( byte ) 1 );
+                    buffer.put( ( byte ) modification.getOperation().getValue() );
+
+                    // The modification
+                    buffer.put( UniversalTag.SEQUENCE.getValue() );
+                    int localModificationLength = getModificationLength().get( modificationNumber );
+                    buffer.put( TLV.getBytes( localModificationLength ) );
+
+                    // The modification type
+                    Value.encode( buffer, modification.getAttribute().getId() );
+
+                    // The values
+                    buffer.put( UniversalTag.SET.getValue() );
+                    int localValuesLength = getValuesLength().get( modificationNumber );
+                    buffer.put( TLV.getBytes( localValuesLength ) );
+
+                    if ( modification.getAttribute().size() != 0 )
+                    {
+                        for ( org.apache.directory.shared.ldap.model.entry.Value<?> value : modification.getAttribute() )
+                        {
+                            if ( !value.isBinary() )
+                            {
+                                Value.encode( buffer, value.getString() );
+                            }
+                            else
+                            {
+                                Value.encode( buffer, value.getBytes() );
+                            }
+                        }
+                    }
+
+                    // Go to the next modification number;
+                    modificationNumber++;
+                }
+            }
+        }
+        catch ( BufferOverflowException boe )
+        {
+            throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
+        }
+        
+        return buffer;
+    }
 }

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/ModifyResponseDecorator.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/ModifyResponseDecorator.java?rev=1064846&r1=1064845&r2=1064846&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/ModifyResponseDecorator.java (original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/ModifyResponseDecorator.java Fri Jan 28 19:58:29 2011
@@ -20,6 +20,13 @@
 package org.apache.directory.shared.ldap.codec.decorators;
 
 
+import java.nio.BufferOverflowException;
+import java.nio.ByteBuffer;
+
+import org.apache.directory.shared.asn1.EncoderException;
+import org.apache.directory.shared.asn1.ber.tlv.TLV;
+import org.apache.directory.shared.i18n.I18n;
+import org.apache.directory.shared.ldap.codec.LdapConstants;
 import org.apache.directory.shared.ldap.model.message.ModifyResponse;
 
 
@@ -71,4 +78,55 @@ public class ModifyResponseDecorator ext
     {
         return modifyResponseLength;
     }
+
+    
+    //-------------------------------------------------------------------------
+    // The Decorator methods
+    //-------------------------------------------------------------------------
+    /**
+     * Compute the ModifyResponse length 
+     * 
+     * ModifyResponse : 
+     * <pre>
+     * 0x67 L1 
+     *   | 
+     *   +--> LdapResult 
+     *   
+     * L1 = Length(LdapResult) 
+     * Length(ModifyResponse) = Length(0x67) + Length(L1) + L1
+     * </pre>
+     */
+    public int computeLength()
+    {
+        int modifyResponseLength = ((LdapResultDecorator)getLdapResult()).computeLength();
+
+        setModifyResponseLength( modifyResponseLength );
+
+        return 1 + TLV.getNbBytes( modifyResponseLength ) + modifyResponseLength;
+    }
+
+
+    /**
+     * Encode the ModifyResponse message to a PDU.
+     * 
+     * @param buffer The buffer where to put the PDU
+     */
+    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
+    {
+        try
+        {
+            // The ModifyResponse Tag
+            buffer.put( LdapConstants.MODIFY_RESPONSE_TAG );
+            buffer.put( TLV.getBytes( getModifyResponseLength() ) );
+
+            // The LdapResult
+            ((LdapResultDecorator)getLdapResult()).encode( buffer );
+        }
+        catch ( BufferOverflowException boe )
+        {
+            throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
+        }
+        
+        return buffer;
+    }
 }

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/RequestDecorator.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/RequestDecorator.java?rev=1064846&r1=1064845&r2=1064846&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/RequestDecorator.java (original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/RequestDecorator.java Fri Jan 28 19:58:29 2011
@@ -29,7 +29,7 @@ import org.apache.directory.shared.ldap.
  *
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  */
-public class RequestDecorator extends MessageDecorator implements Request
+public abstract class RequestDecorator extends MessageDecorator implements Request
 {
     /**
      * Makes Request a MessageDecorator.

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/ResultResponseRequestDecorator.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/ResultResponseRequestDecorator.java?rev=1064846&r1=1064845&r2=1064846&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/ResultResponseRequestDecorator.java (original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/ResultResponseRequestDecorator.java Fri Jan 28 19:58:29 2011
@@ -31,7 +31,7 @@ import org.apache.directory.shared.ldap.
  *
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  */
-public class ResultResponseRequestDecorator extends RequestDecorator implements ResultResponseRequest
+public abstract class ResultResponseRequestDecorator extends RequestDecorator implements ResultResponseRequest
 {
     /**
      * Makes Request a MessageDecorator.

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/SearchRequestDecorator.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/SearchRequestDecorator.java?rev=1064846&r1=1064845&r2=1064846&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/SearchRequestDecorator.java (original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/SearchRequestDecorator.java Fri Jan 28 19:58:29 2011
@@ -20,12 +20,17 @@
 package org.apache.directory.shared.ldap.codec.decorators;
 
 
+import java.nio.BufferOverflowException;
+import java.nio.ByteBuffer;
 import java.util.ArrayList;
 import java.util.List;
 
 import org.apache.directory.shared.asn1.DecoderException;
+import org.apache.directory.shared.asn1.EncoderException;
 import org.apache.directory.shared.asn1.ber.Asn1Container;
 import org.apache.directory.shared.asn1.ber.tlv.TLV;
+import org.apache.directory.shared.asn1.ber.tlv.UniversalTag;
+import org.apache.directory.shared.i18n.I18n;
 import org.apache.directory.shared.ldap.codec.AttributeValueAssertion;
 import org.apache.directory.shared.ldap.codec.LdapConstants;
 import org.apache.directory.shared.ldap.codec.LdapMessageContainer;
@@ -60,6 +65,7 @@ import org.apache.directory.shared.ldap.
 import org.apache.directory.shared.ldap.model.message.MessageTypeEnum;
 import org.apache.directory.shared.ldap.model.message.SearchRequest;
 import org.apache.directory.shared.ldap.model.name.Dn;
+import org.apache.directory.shared.util.Strings;
 
 
 /**
@@ -1034,4 +1040,154 @@ public class SearchRequestDecorator exte
     {
         getSearchRequest().removeAttribute( attribute );
     }
+
+
+    //-------------------------------------------------------------------------
+    // The Decorator methods
+    //-------------------------------------------------------------------------
+    /**
+     * Compute the SearchRequest length
+     * 
+     * SearchRequest :
+     * <pre>
+     * 0x63 L1
+     *  |
+     *  +--> 0x04 L2 baseObject
+     *  +--> 0x0A 0x01 scope
+     *  +--> 0x0A 0x01 derefAliases
+     *  +--> 0x02 0x0(1..4) sizeLimit
+     *  +--> 0x02 0x0(1..4) timeLimit
+     *  +--> 0x01 0x01 typesOnly
+     *  +--> filter.computeLength()
+     *  +--> 0x30 L3 (Attribute description list)
+     *        |
+     *        +--> 0x04 L4-1 Attribute description 
+     *        +--> 0x04 L4-2 Attribute description 
+     *        +--> ... 
+     *        +--> 0x04 L4-i Attribute description 
+     *        +--> ... 
+     *        +--> 0x04 L4-n Attribute description 
+     *        </pre>
+     */
+    public int computeLength()
+    {
+        int searchRequestLength = 0;
+
+        // The baseObject
+        searchRequestLength += 1 + TLV.getNbBytes( Dn.getNbBytes( getBase() ) ) + Dn.getNbBytes( getBase() );
+
+        // The scope
+        searchRequestLength += 1 + 1 + 1;
+
+        // The derefAliases
+        searchRequestLength += 1 + 1 + 1;
+
+        // The sizeLimit
+        searchRequestLength += 1 + 1 + org.apache.directory.shared.asn1.ber.tlv.Value.getNbBytes( getSizeLimit() );
+
+        // The timeLimit
+        searchRequestLength += 1 + 1 + org.apache.directory.shared.asn1.ber.tlv.Value.getNbBytes( getTimeLimit() );
+
+        // The typesOnly
+        searchRequestLength += 1 + 1 + 1;
+
+        // The filter
+        setFilter( getFilter() );
+        searchRequestLength += 
+            getCodecFilter().computeLength();
+
+        // The attributes description list
+        int attributeDescriptionListLength = 0;
+
+        if ( ( getAttributes() != null ) && ( getAttributes().size() != 0 ) )
+        {
+            // Compute the attributes length
+            for ( String attribute : getAttributes() )
+            {
+                // add the attribute length to the attributes length
+                int idLength = Strings.getBytesUtf8(attribute).length;
+                attributeDescriptionListLength += 1 + TLV.getNbBytes( idLength ) + idLength;
+            }
+        }
+
+        setAttributeDescriptionListLength( attributeDescriptionListLength );
+
+        searchRequestLength += 1 + TLV.getNbBytes( attributeDescriptionListLength ) + attributeDescriptionListLength;
+
+        setSearchRequestLength( searchRequestLength );
+        // Return the result.
+        return 1 + TLV.getNbBytes( searchRequestLength ) + searchRequestLength;
+    }
+    
+    
+    /**
+     * Encode the SearchRequest message to a PDU.
+     * 
+     * SearchRequest :
+     * <pre>
+     * 0x63 LL
+     *   0x04 LL baseObject
+     *   0x0A 01 scope
+     *   0x0A 01 derefAliases
+     *   0x02 0N sizeLimit
+     *   0x02 0N timeLimit
+     *   0x01 0x01 typesOnly
+     *   filter.encode()
+     *   0x30 LL attributeDescriptionList
+     *     0x04 LL attributeDescription
+     *     ... 
+     *     0x04 LL attributeDescription
+     * </pre>
+     * @param buffer The buffer where to put the PDU
+     * @return The PDU.
+     */
+    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
+    {
+        try
+        {
+            // The SearchRequest Tag
+            buffer.put( LdapConstants.SEARCH_REQUEST_TAG );
+            buffer.put( TLV.getBytes( getSearchRequestLength() ) );
+
+            // The baseObject
+            org.apache.directory.shared.asn1.ber.tlv.Value.encode( buffer, Dn.getBytes( getBase()) );
+
+            // The scope
+            org.apache.directory.shared.asn1.ber.tlv.Value.encodeEnumerated( buffer, getScope().getScope() );
+
+            // The derefAliases
+            org.apache.directory.shared.asn1.ber.tlv.Value.encodeEnumerated( buffer, getDerefAliases().getValue() );
+
+            // The sizeLimit
+            org.apache.directory.shared.asn1.ber.tlv.Value.encode( buffer, getSizeLimit() );
+
+            // The timeLimit
+            org.apache.directory.shared.asn1.ber.tlv.Value.encode( buffer, getTimeLimit() );
+
+            // The typesOnly
+            org.apache.directory.shared.asn1.ber.tlv.Value.encode( buffer, getTypesOnly() );
+
+            // The filter
+            getCodecFilter().encode( buffer );
+
+            // The attributeDescriptionList
+            buffer.put( UniversalTag.SEQUENCE.getValue() );
+            buffer.put( TLV.getBytes( getAttributeDescriptionListLength() ) );
+
+            if ( ( getAttributes() != null ) && ( getAttributes().size() != 0 ) )
+            {
+                // encode each attribute
+                for ( String attribute : getAttributes() )
+                {
+                    org.apache.directory.shared.asn1.ber.tlv.Value.encode( buffer, attribute );
+                }
+            }
+        }
+        catch ( BufferOverflowException boe )
+        {
+            throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
+        }
+
+        return buffer;
+    }
 }

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/SearchResultDoneDecorator.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/SearchResultDoneDecorator.java?rev=1064846&r1=1064845&r2=1064846&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/SearchResultDoneDecorator.java (original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/SearchResultDoneDecorator.java Fri Jan 28 19:58:29 2011
@@ -20,6 +20,13 @@
 package org.apache.directory.shared.ldap.codec.decorators;
 
 
+import java.nio.BufferOverflowException;
+import java.nio.ByteBuffer;
+
+import org.apache.directory.shared.asn1.EncoderException;
+import org.apache.directory.shared.asn1.ber.tlv.TLV;
+import org.apache.directory.shared.i18n.I18n;
+import org.apache.directory.shared.ldap.codec.LdapConstants;
 import org.apache.directory.shared.ldap.model.message.SearchResultDone;
 
 
@@ -71,4 +78,56 @@ public class SearchResultDoneDecorator e
     {
         return searchResultDoneLength;
     }
+
+
+    //-------------------------------------------------------------------------
+    // The Decorator methods
+    //-------------------------------------------------------------------------
+    /**
+     * Compute the SearchResultDone length 
+     * 
+     * SearchResultDone : 
+     * <pre>
+     * 0x65 L1 
+     *   | 
+     *   +--> LdapResult 
+     *   
+     * L1 = Length(LdapResult) 
+     * Length(SearchResultDone) = Length(0x65) + Length(L1) + L1
+     * </pre>
+     */
+    public int computeLength()
+    {
+        int searchResultDoneLength = ((LdapResultDecorator)getLdapResult()).computeLength();
+
+        setSearchResultDoneLength( searchResultDoneLength );
+
+        return 1 + TLV.getNbBytes( searchResultDoneLength ) + searchResultDoneLength;
+    }
+
+
+    /**
+     * Encode the SearchResultDone message to a PDU.
+     * 
+     * @param buffer The buffer where to put the PDU
+     * @param searchResultDoneDecorator The SearchResultDone decorator
+     */
+    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
+    {
+        try
+        {
+            // The searchResultDone Tag
+            buffer.put( LdapConstants.SEARCH_RESULT_DONE_TAG );
+            buffer.put( TLV.getBytes( getSearchResultDoneLength() ) );
+
+            // The LdapResult
+            ((LdapResultDecorator)getLdapResult()).encode( buffer );
+        }
+        catch ( BufferOverflowException boe )
+        {
+            throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
+        }
+        
+        return buffer;
+    }
 }

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/SearchResultEntryDecorator.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/SearchResultEntryDecorator.java?rev=1064846&r1=1064845&r2=1064846&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/SearchResultEntryDecorator.java (original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/SearchResultEntryDecorator.java Fri Jan 28 19:58:29 2011
@@ -20,14 +20,25 @@
 package org.apache.directory.shared.ldap.codec.decorators;
 
 
+import java.nio.BufferOverflowException;
+import java.nio.ByteBuffer;
+import java.util.LinkedList;
 import java.util.List;
 
+import org.apache.directory.shared.asn1.EncoderException;
+import org.apache.directory.shared.asn1.ber.tlv.TLV;
+import org.apache.directory.shared.asn1.ber.tlv.UniversalTag;
+import org.apache.directory.shared.asn1.ber.tlv.Value;
+import org.apache.directory.shared.asn1.util.Asn1StringUtils;
+import org.apache.directory.shared.i18n.I18n;
+import org.apache.directory.shared.ldap.codec.LdapConstants;
 import org.apache.directory.shared.ldap.model.entry.DefaultEntryAttribute;
 import org.apache.directory.shared.ldap.model.entry.Entry;
 import org.apache.directory.shared.ldap.model.entry.EntryAttribute;
 import org.apache.directory.shared.ldap.model.exception.LdapException;
 import org.apache.directory.shared.ldap.model.message.SearchResultEntry;
 import org.apache.directory.shared.ldap.model.name.Dn;
+import org.apache.directory.shared.util.Strings;
 
 
 /**
@@ -249,4 +260,227 @@ public class SearchResultEntryDecorator 
     {
         getSearchResultEntry().setEntry( entry );
     }
+
+
+    //-------------------------------------------------------------------------
+    // The Decorator methods
+    //-------------------------------------------------------------------------
+    /**
+     * Compute the SearchResultEntry length
+     * 
+     * SearchResultEntry :
+     * <pre>
+     * 0x64 L1
+     *  |
+     *  +--> 0x04 L2 objectName
+     *  +--> 0x30 L3 (attributes)
+     *        |
+     *        +--> 0x30 L4-1 (partial attributes list)
+     *        |     |
+     *        |     +--> 0x04 L5-1 type
+     *        |     +--> 0x31 L6-1 (values)
+     *        |           |
+     *        |           +--> 0x04 L7-1-1 value
+     *        |           +--> ...
+     *        |           +--> 0x04 L7-1-n value
+     *        |
+     *        +--> 0x30 L4-2 (partial attributes list)
+     *        |     |
+     *        |     +--> 0x04 L5-2 type
+     *        |     +--> 0x31 L6-2 (values)
+     *        |           |
+     *        |           +--> 0x04 L7-2-1 value
+     *        |           +--> ...
+     *        |           +--> 0x04 L7-2-n value
+     *        |
+     *        +--> ...
+     *        |
+     *        +--> 0x30 L4-m (partial attributes list)
+     *              |
+     *              +--> 0x04 L5-m type
+     *              +--> 0x31 L6-m (values)
+     *                    |
+     *                    +--> 0x04 L7-m-1 value
+     *                    +--> ...
+     *                    +--> 0x04 L7-m-n value
+     * </pre>
+     */
+    public int computeLength()
+    {
+        Dn dn = getObjectName();
+
+        byte[] dnBytes = Strings.getBytesUtf8(dn.getName());
+
+        // The entry
+        int searchResultEntryLength = 1 + TLV.getNbBytes( dnBytes.length ) + dnBytes.length;
+        setObjectNameBytes( dnBytes );
+
+        // The attributes sequence
+        int attributesLength = 0;
+
+        Entry entry = getEntry();
+
+        if ( ( entry != null ) && ( entry.size() != 0 ) )
+        {
+            List<Integer> attributeLength = new LinkedList<Integer>();
+            List<Integer> valsLength = new LinkedList<Integer>();
+
+            // Store those lists in the object
+            setAttributeLength( attributeLength );
+            setValsLength( valsLength );
+
+            // Compute the attributes length
+            for ( EntryAttribute attribute : entry )
+            {
+                int localAttributeLength = 0;
+                int localValuesLength = 0;
+
+                // Get the type length
+                int idLength = attribute.getId().getBytes().length;
+                localAttributeLength = 1 + TLV.getNbBytes( idLength ) + idLength;
+
+                if ( attribute.size() != 0 )
+                {
+                    // The values
+                    if ( attribute.size() > 0 )
+                    {
+                        localValuesLength = 0;
+
+                        for ( org.apache.directory.shared.ldap.model.entry.Value<?> value : attribute )
+                        {
+                            byte[] binaryValue = value.getBytes();
+                            localValuesLength += 1 + TLV.getNbBytes( binaryValue.length ) + binaryValue.length;
+                        }
+
+                        localAttributeLength += 1 + TLV.getNbBytes( localValuesLength ) + localValuesLength;
+                    }
+                    else
+                    {
+                        // We have to deal with the special wase where
+                        // we don't have a value.
+                        // It will be encoded as an empty OCTETSTRING,
+                        // so it will be two byte slong (0x04 0x00)
+                        localAttributeLength += 1 + 1;
+                    }
+                }
+                else
+                {
+                    // We have no values. We will just have an empty SET OF :
+                    // 0x31 0x00
+                    localAttributeLength += 1 + 1;
+                }
+
+                // add the attribute length to the attributes length
+                attributesLength += 1 + TLV.getNbBytes( localAttributeLength ) + localAttributeLength;
+
+                // Store the lengths of the encoded attributes and values
+                attributeLength.add( localAttributeLength );
+                valsLength.add( localValuesLength );
+            }
+
+            // Store the lengths of the entry
+            setAttributesLength( attributesLength );
+        }
+
+        searchResultEntryLength += 1 + TLV.getNbBytes( attributesLength ) + attributesLength;
+
+        // Store the length of the response 
+        setSearchResultEntryLength( searchResultEntryLength );
+
+        // Return the result.
+        return 1 + TLV.getNbBytes( searchResultEntryLength ) + searchResultEntryLength;
+    }
+
+
+    /**
+     * Encode the SearchResultEntry message to a PDU.
+     * 
+     * SearchResultEntry :
+     * <pre>
+     * 0x64 LL
+     *   0x04 LL objectName
+     *   0x30 LL attributes
+     *     0x30 LL partialAttributeList
+     *       0x04 LL type
+     *       0x31 LL vals
+     *         0x04 LL attributeValue
+     *         ... 
+     *         0x04 LL attributeValue
+     *     ... 
+     *     0x30 LL partialAttributeList
+     *       0x04 LL type
+     *       0x31 LL vals
+     *         0x04 LL attributeValue
+     *         ... 
+     *         0x04 LL attributeValue 
+     * </pre>
+     * @param buffer The buffer where to put the PDU
+     * @param searchResultEntryDecorator the SearchResultEntry decorator
+     * @return The PDU.
+     */
+    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
+    {
+        try
+        {
+            // The SearchResultEntry Tag
+            buffer.put( LdapConstants.SEARCH_RESULT_ENTRY_TAG );
+            buffer.put( TLV.getBytes( getSearchResultEntryLength() ) );
+
+            // The objectName
+            Value.encode( buffer, getObjectNameBytes() );
+
+            // The attributes sequence
+            buffer.put( UniversalTag.SEQUENCE.getValue() );
+            buffer.put( TLV.getBytes( getAttributesLength() ) );
+
+            // The partial attribute list
+            Entry entry = getEntry();
+
+            if ( ( entry != null ) && ( entry.size() != 0 ) )
+            {
+                int attributeNumber = 0;
+
+                // Compute the attributes length
+                for ( EntryAttribute attribute : entry )
+                {
+                    // The partial attribute list sequence
+                    buffer.put( UniversalTag.SEQUENCE.getValue() );
+                    int localAttributeLength = getAttributeLength().get( attributeNumber );
+                    buffer.put( TLV.getBytes( localAttributeLength ) );
+
+                    // The attribute type
+                    Value.encode( buffer, Asn1StringUtils.asciiStringToByte( attribute.getUpId() ) );
+
+                    // The values
+                    buffer.put( UniversalTag.SET.getValue() );
+                    int localValuesLength = getValsLength().get( attributeNumber );
+                    buffer.put( TLV.getBytes( localValuesLength ) );
+
+                    if ( attribute.size() > 0 )
+                    {
+                        for ( org.apache.directory.shared.ldap.model.entry.Value<?> value : attribute )
+                        {
+                            if ( !value.isBinary() )
+                            {
+                                Value.encode( buffer, value.getString() );
+                            }
+                            else
+                            {
+                                Value.encode( buffer, value.getBytes() );
+                            }
+                        }
+                    }
+
+                    // Go to the next attribute number;
+                    attributeNumber++;
+                }
+            }
+        }
+        catch ( BufferOverflowException boe )
+        {
+            throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
+        }
+        
+        return buffer;
+    }
 }

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/SearchResultReferenceDecorator.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/SearchResultReferenceDecorator.java?rev=1064846&r1=1064845&r2=1064846&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/SearchResultReferenceDecorator.java (original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/SearchResultReferenceDecorator.java Fri Jan 28 19:58:29 2011
@@ -20,6 +20,15 @@
 package org.apache.directory.shared.ldap.codec.decorators;
 
 
+import java.nio.BufferOverflowException;
+import java.nio.ByteBuffer;
+
+import org.apache.directory.shared.asn1.EncoderException;
+import org.apache.directory.shared.asn1.ber.tlv.TLV;
+import org.apache.directory.shared.asn1.ber.tlv.Value;
+import org.apache.directory.shared.i18n.I18n;
+import org.apache.directory.shared.ldap.codec.LdapConstants;
+import org.apache.directory.shared.ldap.codec.LdapEncoder;
 import org.apache.directory.shared.ldap.model.message.Referral;
 import org.apache.directory.shared.ldap.model.message.SearchResultReference;
 
@@ -117,4 +126,93 @@ public class SearchResultReferenceDecora
     {
         getSearchResultReference().setReferral( referral );        
     }
+
+
+    //-------------------------------------------------------------------------
+    // The Decorator methods
+    //-------------------------------------------------------------------------
+    /**
+     * Compute the SearchResultReference length
+     * 
+     * SearchResultReference :
+     * <pre>
+     * 0x73 L1
+     *  |
+     *  +--> 0x04 L2 reference
+     *  +--> 0x04 L3 reference
+     *  +--> ...
+     *  +--> 0x04 Li reference
+     *  +--> ...
+     *  +--> 0x04 Ln reference
+     * 
+     * L1 = n*Length(0x04) + sum(Length(Li)) + sum(Length(reference[i]))
+     * 
+     * Length(SearchResultReference) = Length(0x73 + Length(L1) + L1
+     * </pre>
+     */
+    public int computeLength()
+    {
+        int searchResultReferenceLength = 0;
+
+        // We may have more than one reference.
+        Referral referral = getReferral();
+
+        int referralLength = LdapEncoder.computeReferralLength( referral );
+
+        if ( referralLength != 0 )
+        {
+            setReferral( referral );
+
+            searchResultReferenceLength = referralLength;
+        }
+
+        // Store the length of the response 
+        setSearchResultReferenceLength( searchResultReferenceLength );
+
+        return 1 + TLV.getNbBytes( searchResultReferenceLength ) + searchResultReferenceLength;
+    }
+
+
+    /**
+     * Encode the SearchResultReference message to a PDU.
+     * 
+     * SearchResultReference :
+     * <pre>
+     * 0x73 LL
+     *   0x04 LL reference
+     *   [0x04 LL reference]*
+     * </pre>
+     * @param buffer The buffer where to put the PDU
+     * @param searchResultReferenceDecorator The SearchResultReference decorator
+     * @return The PDU.
+     */
+    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
+    {
+        SearchResultReference searchResultReference = getSearchResultReference();
+        try
+        {
+            // The SearchResultReference Tag
+            buffer.put( LdapConstants.SEARCH_RESULT_REFERENCE_TAG );
+            buffer.put( TLV.getBytes( getSearchResultReferenceLength() ) );
+
+            // The referrals, if any
+            Referral referral = searchResultReference.getReferral();
+
+            if ( referral != null )
+            {
+                // Each referral
+                for ( byte[] ldapUrlBytes : referral.getLdapUrlsBytes() )
+                {
+                    // Encode the current referral
+                    Value.encode( buffer, ldapUrlBytes );
+                }
+            }
+        }
+        catch ( BufferOverflowException boe )
+        {
+            throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
+        }
+        
+        return buffer;
+    }
 }

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/SingleReplyRequestDecorator.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/SingleReplyRequestDecorator.java?rev=1064846&r1=1064845&r2=1064846&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/SingleReplyRequestDecorator.java (original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/SingleReplyRequestDecorator.java Fri Jan 28 19:58:29 2011
@@ -30,7 +30,7 @@ import org.apache.directory.shared.ldap.
  *
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  */
-public class SingleReplyRequestDecorator extends AbandonableResultResponseRequestDecorator implements SingleReplyRequest
+public abstract class SingleReplyRequestDecorator extends AbandonableResultResponseRequestDecorator implements SingleReplyRequest
 {
     /**
      * Makes Request a MessageDecorator.

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/UnbindRequestDecorator.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/UnbindRequestDecorator.java?rev=1064846&r1=1064845&r2=1064846&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/UnbindRequestDecorator.java (original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/UnbindRequestDecorator.java Fri Jan 28 19:58:29 2011
@@ -20,6 +20,12 @@
 package org.apache.directory.shared.ldap.codec.decorators;
 
 
+import java.nio.BufferOverflowException;
+import java.nio.ByteBuffer;
+
+import org.apache.directory.shared.asn1.EncoderException;
+import org.apache.directory.shared.i18n.I18n;
+import org.apache.directory.shared.ldap.codec.LdapConstants;
 import org.apache.directory.shared.ldap.model.message.UnbindRequest;
 
 
@@ -50,4 +56,42 @@ public class UnbindRequestDecorator exte
     {
         return ( UnbindRequest ) getDecoratedMessage();
     }
+
+
+    //-------------------------------------------------------------------------
+    // The Decorator methods
+    //-------------------------------------------------------------------------
+    /**
+     * Compute the UnBindRequest length 
+     * 
+     * UnBindRequest : 
+     * 0x42 00
+     */
+    public int computeLength()
+    {
+        return 2; // Always 2
+    }
+    
+    
+    /**
+     * Encode the Unbind protocolOp part
+     */
+    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
+    {
+        try
+        {
+            // The tag
+            buffer.put( LdapConstants.UNBIND_REQUEST_TAG );
+
+            // The length is always null.
+            buffer.put( ( byte ) 0 );
+        }
+        catch ( BufferOverflowException boe )
+        {
+            String msg = I18n.err( I18n.ERR_04005 );
+            throw new EncoderException( msg );
+        }
+        
+        return buffer;
+    }
 }