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 [1/2] - /directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/

Author: elecharny
Date: Fri Jan 28 19:58:29 2011
New Revision: 1064846

URL: http://svn.apache.org/viewvc?rev=1064846&view=rev
Log:
Refactored the decorators :
o the Decorator interface has been created
o the computeLength() and encode() methods have been added to each message

Added:
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/Decorator.java
Modified:
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/AbandonRequestDecorator.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/AbandonableRequestDecorator.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/AbandonableResultResponseRequestDecorator.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/AddRequestDecorator.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/AddResponseDecorator.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/BindRequestDecorator.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/BindResponseDecorator.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/CompareRequestDecorator.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/CompareResponseDecorator.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/DeleteRequestDecorator.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/DeleteResponseDecorator.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/ExtendedRequestDecorator.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/ExtendedResponseDecorator.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/IntermediateResponseDecorator.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/LdapResultDecorator.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/MessageDecorator.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/ModifyDnRequestDecorator.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/ModifyDnResponseDecorator.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/ModifyRequestDecorator.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/ModifyResponseDecorator.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/RequestDecorator.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/ResultResponseRequestDecorator.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/SearchRequestDecorator.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/SearchResultDoneDecorator.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/SearchResultEntryDecorator.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/SearchResultReferenceDecorator.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/SingleReplyRequestDecorator.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/UnbindRequestDecorator.java

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/AbandonRequestDecorator.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/AbandonRequestDecorator.java?rev=1064846&r1=1064845&r2=1064846&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/AbandonRequestDecorator.java (original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/AbandonRequestDecorator.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.Value;
+import org.apache.directory.shared.i18n.I18n;
+import org.apache.directory.shared.ldap.codec.LdapConstants;
 import org.apache.directory.shared.ldap.model.message.AbandonRequest;
 
 
@@ -51,10 +58,8 @@ public class AbandonRequestDecorator ext
 
 
     //-------------------------------------------------------------------------
-    // The SearchResultReference methods
+    // The AbandonRequest methods
     //-------------------------------------------------------------------------
-    
-    
     /**
      * {@inheritDoc}
      */
@@ -71,4 +76,50 @@ public class AbandonRequestDecorator ext
     {
         getAbandonRequest().setAbandoned( requestId );
     }
+
+    
+    //-------------------------------------------------------------------------
+    // The Decorator methods
+    //-------------------------------------------------------------------------
+    /**
+     * Encode the Abandon protocolOp part
+     */
+    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
+    {
+        try
+        {
+            // The tag
+            buffer.put( LdapConstants.ABANDON_REQUEST_TAG );
+
+            // The length. It has to be evaluated depending on
+            // the abandoned messageId value.
+            buffer.put( ( byte ) Value.getNbBytes( getAbandoned() ) );
+
+            // The abandoned messageId
+            buffer.put( Value.getBytes( getAbandoned() ) );
+        }
+        catch ( BufferOverflowException boe )
+        {
+            String msg = I18n.err( I18n.ERR_04005 );
+            throw new EncoderException( msg );
+        }
+        
+        return buffer;
+    }
+
+
+    /**
+     * Compute the AbandonRequest length 
+     * 
+     * AbandonRequest : 
+     * 0x50 0x0(1..4) abandoned MessageId 
+     * 
+     * Length(AbandonRequest) = Length(0x50) + 1 + Length(abandoned MessageId)
+     */
+    public int computeLength()
+    {
+        int length = 1 + 1 + Value.getNbBytes( getAbandoned() );
+
+        return length;
+    }
 }

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/AbandonableRequestDecorator.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/AbandonableRequestDecorator.java?rev=1064846&r1=1064845&r2=1064846&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/AbandonableRequestDecorator.java (original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/AbandonableRequestDecorator.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 AbandonableRequestDecorator extends RequestDecorator implements AbandonableRequest
+public abstract class AbandonableRequestDecorator extends RequestDecorator implements AbandonableRequest
 {
     /**
      * Makes Request a MessageDecorator.

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/AbandonableResultResponseRequestDecorator.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/AbandonableResultResponseRequestDecorator.java?rev=1064846&r1=1064845&r2=1064846&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/AbandonableResultResponseRequestDecorator.java (original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/AbandonableResultResponseRequestDecorator.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 AbandonableResultResponseRequestDecorator extends AbandonableRequestDecorator implements ResultResponseRequest
+public abstract class AbandonableResultResponseRequestDecorator extends AbandonableRequestDecorator implements ResultResponseRequest
 {
     /**
      * Makes Request a MessageDecorator.

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/AddRequestDecorator.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/AddRequestDecorator.java?rev=1064846&r1=1064845&r2=1064846&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/AddRequestDecorator.java (original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/AddRequestDecorator.java Fri Jan 28 19:58:29 2011
@@ -20,8 +20,16 @@
 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.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;
@@ -251,4 +259,202 @@ public class AddRequestDecorator extends
     {
         currentAttribute.add( value );
     }
+    
+    
+    //-------------------------------------------------------------------------
+    // The Decorator methods
+    //-------------------------------------------------------------------------
+    /**
+     * Compute the AddRequest length
+     * 
+     * AddRequest :
+     * 
+     * 0x68 L1
+     *  |
+     *  +--> 0x04 L2 entry
+     *  +--> 0x30 L3 (attributes)
+     *        |
+     *        +--> 0x30 L4-1 (attribute)
+     *        |     |
+     *        |     +--> 0x04 L5-1 type
+     *        |     +--> 0x31 L6-1 (values)
+     *        |           |
+     *        |           +--> 0x04 L7-1-1 value
+     *        |           +--> ...
+     *        |           +--> 0x04 L7-1-n value
+     *        |
+     *        +--> 0x30 L4-2 (attribute)
+     *        |     |
+     *        |     +--> 0x04 L5-2 type
+     *        |     +--> 0x31 L6-2 (values)
+     *        |           |
+     *        |           +--> 0x04 L7-2-1 value
+     *        |           +--> ...
+     *        |           +--> 0x04 L7-2-n value
+     *        |
+     *        +--> ...
+     *        |
+     *        +--> 0x30 L4-m (attribute)
+     *              |
+     *              +--> 0x04 L5-m type
+     *              +--> 0x31 L6-m (values)
+     *                    |
+     *                    +--> 0x04 L7-m-1 value
+     *                    +--> ...
+     *                    +--> 0x04 L7-m-n value
+     */
+    public int computeLength()
+    {
+        AddRequest addRequest = getAddRequest();
+        Entry entry = addRequest.getEntry();
+
+        if ( entry == null )
+        {
+            throw new IllegalArgumentException( I18n.err( I18n.ERR_04481_ENTRY_NULL_VALUE ) );
+        }
+
+        // The entry Dn
+        int addRequestLength = 1 + TLV.getNbBytes( Dn.getNbBytes(entry.getDn()) ) + Dn.getNbBytes(entry.getDn());
+
+        // The attributes sequence
+        int entryLength = 0;
+
+        if ( entry.size() != 0 )
+        {
+            List<Integer> attributesLength = new LinkedList<Integer>();
+            List<Integer> valuesLength = new LinkedList<Integer>();
+
+            // 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;
+
+                // The values
+                if ( attribute.size() != 0 )
+                {
+                    localValuesLength = 0;
+
+                    for ( org.apache.directory.shared.ldap.model.entry.Value<?> value : attribute )
+                    {
+                        int valueLength = value.getBytes().length;
+                        localValuesLength += 1 + TLV.getNbBytes( valueLength ) + valueLength;
+                    }
+
+                    localAttributeLength += 1 + TLV.getNbBytes( localValuesLength ) + localValuesLength;
+                }
+
+                // add the attribute length to the attributes length
+                entryLength += 1 + TLV.getNbBytes( localAttributeLength ) + localAttributeLength;
+
+                attributesLength.add( localAttributeLength );
+                valuesLength.add( localValuesLength );
+            }
+
+            setAttributesLength( attributesLength );
+            setValuesLength( valuesLength );
+            setEntryLength( entryLength );
+        }
+
+        addRequestLength += 1 + TLV.getNbBytes( entryLength ) + entryLength;
+        setAddRequestLength( addRequestLength );
+
+        // Return the result.
+        return 1 + TLV.getNbBytes( addRequestLength ) + addRequestLength;
+    }
+
+
+    /**
+     * Encode the AddRequest message to a PDU. 
+     * 
+     * AddRequest :
+     * 
+     * 0x68 LL
+     *   0x04 LL entry
+     *   0x30 LL attributesList
+     *     0x30 LL attributeList
+     *       0x04 LL attributeDescription
+     *       0x31 LL attributeValues
+     *         0x04 LL attributeValue
+     *         ... 
+     *         0x04 LL attributeValue
+     *     ... 
+     *     0x30 LL attributeList
+     *       0x04 LL attributeDescription
+     *       0x31 LL attributeValue
+     *         0x04 LL attributeValue
+     *         ... 
+     *         0x04 LL attributeValue 
+     * 
+     * @param buffer The buffer where to put the PDU
+     */
+    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
+    {
+        try
+        {
+            // The AddRequest Tag
+            buffer.put( LdapConstants.ADD_REQUEST_TAG );
+            buffer.put( TLV.getBytes( getAddRequestLength() ) );
+
+            // The entry
+            org.apache.directory.shared.asn1.ber.tlv.Value.encode( buffer, Dn.getBytes( getEntryDn() ) );
+
+            // The attributes sequence
+            buffer.put( UniversalTag.SEQUENCE.getValue() );
+            buffer.put( TLV.getBytes( getEntryLength() ) );
+
+            // The partial attribute list
+            Entry entry = getEntry();
+
+            if ( entry.size() != 0 )
+            {
+                int attributeNumber = 0;
+
+                // Compute the attributes length
+                for ( EntryAttribute attribute : entry )
+                {
+                    // The attributes list sequence
+                    buffer.put( UniversalTag.SEQUENCE.getValue() );
+                    int localAttributeLength = getAttributesLength().get( attributeNumber );
+                    buffer.put( TLV.getBytes( localAttributeLength ) );
+
+                    // The attribute type
+                    org.apache.directory.shared.asn1.ber.tlv.Value.encode( buffer, attribute.getId() );
+
+                    // The values
+                    buffer.put( UniversalTag.SET.getValue() );
+                    int localValuesLength = getValuesLength().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() )
+                            {
+                                org.apache.directory.shared.asn1.ber.tlv.Value.encode( buffer, value.getBytes() );
+                            }
+                            else
+                            {
+                                org.apache.directory.shared.asn1.ber.tlv.Value.encode( buffer, value.getString() );
+                            }
+                        }
+                    }
+
+                    // Go to the next attribute number;
+                    attributeNumber++;
+                }
+            }
+            
+            return buffer;
+        }
+        catch ( BufferOverflowException boe )
+        {
+            throw new EncoderException( "The PDU buffer size is too small !" );
+        }
+    }
 }

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/AddResponseDecorator.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/AddResponseDecorator.java?rev=1064846&r1=1064845&r2=1064846&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/AddResponseDecorator.java (original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/AddResponseDecorator.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.AddResponse;
 
 
@@ -71,4 +78,58 @@ public class AddResponseDecorator extend
     {
         return addResponseLength;
     }
+
+    
+    //-------------------------------------------------------------------------
+    // The Decorator methods
+    //-------------------------------------------------------------------------
+    /**
+     * Compute the AddResponse length 
+     * 
+     * AddResponse : 
+     * 
+     * 0x69 L1
+     *  |
+     *  +--> LdapResult
+     * 
+     * L1 = Length(LdapResult)
+     * 
+     * Length(AddResponse) = Length(0x69) + Length(L1) + L1
+     */
+    public int computeLength()
+    {
+        AddResponse addResponse = getAddResponse();
+        setLdapResult( new LdapResultDecorator( addResponse.getLdapResult() ) );
+        int addResponseLength = ((LdapResultDecorator)getLdapResult()).computeLength();
+
+        setAddResponseLength( addResponseLength );
+
+        return 1 + TLV.getNbBytes( addResponseLength ) + addResponseLength;
+    }
+
+
+    /**
+     * Encode the AddResponse message to a PDU.
+     * 
+     * @param buffer The buffer where to put the PDU
+     * @param addResponseDecorator the AddResponse decorator
+     */
+    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
+    {
+        try
+        {
+            // The AddResponse Tag
+            buffer.put( LdapConstants.ADD_RESPONSE_TAG );
+            buffer.put( TLV.getBytes( getAddResponseLength() ) );
+
+            // The LdapResult
+            ((LdapResultDecorator)getLdapResult()).encode( buffer );
+            
+            return buffer;
+        }
+        catch ( BufferOverflowException boe )
+        {
+            throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
+        }
+    }
 }

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/BindRequestDecorator.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/BindRequestDecorator.java?rev=1064846&r1=1064845&r2=1064846&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/BindRequestDecorator.java (original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/BindRequestDecorator.java Fri Jan 28 19:58:29 2011
@@ -20,8 +20,17 @@
 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.model.message.BindRequest;
 import org.apache.directory.shared.ldap.model.name.Dn;
+import org.apache.directory.shared.util.Strings;
 
 
 /**
@@ -238,4 +247,172 @@ public class BindRequestDecorator extend
     {
         getBindRequest().setSaslMechanism( saslMechanism );
     }
+
+
+    //-------------------------------------------------------------------------
+    // The Decorator methods
+    //-------------------------------------------------------------------------
+    /**
+     * Compute the BindRequest length 
+     * 
+     * BindRequest : 
+     * <pre>
+     * 0x60 L1 
+     *   | 
+     *   +--> 0x02 0x01 (1..127) version 
+     *   +--> 0x04 L2 name 
+     *   +--> authentication 
+     *   
+     * L2 = Length(name)
+     * L3/4 = Length(authentication) 
+     * Length(BindRequest) = Length(0x60) + Length(L1) + L1 + Length(0x02) + 1 + 1 + 
+     *      Length(0x04) + Length(L2) + L2 + Length(authentication)
+     * </pre>
+     */
+    public int computeLength()
+    {
+        int bindRequestLength = 1 + 1 + 1; // Initialized with version
+
+        // The name
+        bindRequestLength += 1 + TLV.getNbBytes( Dn.getNbBytes( getName() ) )
+            + Dn.getNbBytes( getName() );
+
+        byte[] credentials = getCredentials();
+
+        // The authentication
+        if ( isSimple() )
+        {
+            // Compute a SimpleBind operation
+            if ( credentials != null )
+            {
+                bindRequestLength += 1 + TLV.getNbBytes( credentials.length ) + credentials.length;
+            }
+            else
+            {
+                bindRequestLength += 1 + 1;
+            }
+        }
+        else
+        {
+            byte[] mechanismBytes = Strings.getBytesUtf8( getSaslMechanism() );
+            int saslMechanismLength = 1 + TLV.getNbBytes( mechanismBytes.length ) + mechanismBytes.length;
+            int saslCredentialsLength = 0;
+
+            if ( credentials != null )
+            {
+                saslCredentialsLength = 1 + TLV.getNbBytes( credentials.length ) + credentials.length;
+            }
+
+            int saslLength = 1 + TLV.getNbBytes( saslMechanismLength + saslCredentialsLength ) + saslMechanismLength
+                + saslCredentialsLength;
+
+            bindRequestLength += saslLength;
+
+            // Store the mechanism and credentials lengths
+            setSaslMechanismLength( saslMechanismLength );
+            setSaslCredentialsLength( saslCredentialsLength );
+        }
+
+        setBindRequestLength( bindRequestLength );
+
+        // Return the result.
+        return 1 + TLV.getNbBytes( bindRequestLength ) + bindRequestLength;
+    }
+
+
+    /**
+     * Encode the BindRequest message to a PDU. 
+     * 
+     * BindRequest : 
+     * <pre>
+     * 0x60 LL 
+     *   0x02 LL version         0x80 LL simple 
+     *   0x04 LL name           /   
+     *   authentication.encode() 
+     *                          \ 0x83 LL mechanism [0x04 LL credential]
+     * </pre>
+     * 
+     * @param buffer The buffer where to put the PDU
+     * @return The PDU.
+     */
+    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
+    {
+        try
+        {
+            // The BindRequest Tag
+            buffer.put( LdapConstants.BIND_REQUEST_TAG );
+            buffer.put( TLV.getBytes( getBindRequestLength() ) );
+
+        }
+        catch ( BufferOverflowException boe )
+        {
+            throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
+        }
+
+        // The version (LDAP V3 only)
+        Value.encode( buffer, 3 );
+
+        // The name
+        Value.encode( buffer, Dn.getBytes( getName() ) );
+
+        byte[] credentials = getCredentials();
+
+        // The authentication
+        if ( isSimple() )
+        {
+            // Simple authentication
+            try
+            {
+                // The simpleAuthentication Tag
+                buffer.put( ( byte ) LdapConstants.BIND_REQUEST_SIMPLE_TAG );
+
+                if ( credentials != null )
+                {
+                    buffer.put( TLV.getBytes( credentials.length ) );
+
+                    if ( credentials.length != 0 )
+                    {
+                        buffer.put( credentials );
+                    }
+                }
+                else
+                {
+                    buffer.put( ( byte ) 0 );
+                }
+            }
+            catch ( BufferOverflowException boe )
+            {
+                String msg = I18n.err( I18n.ERR_04005 );
+                throw new EncoderException( msg );
+            }
+        }
+        else
+        {
+            // SASL Bind
+            try
+            {
+                // The saslAuthentication Tag
+                buffer.put( ( byte ) LdapConstants.BIND_REQUEST_SASL_TAG );
+
+                byte[] mechanismBytes = Strings.getBytesUtf8( getSaslMechanism() );
+
+                buffer.put( TLV
+                    .getBytes( getSaslMechanismLength() + getSaslCredentialsLength() ) );
+
+                Value.encode( buffer, mechanismBytes );
+
+                if ( credentials != null )
+                {
+                    Value.encode( buffer, credentials );
+                }
+            }
+            catch ( BufferOverflowException boe )
+            {
+                String msg = I18n.err( I18n.ERR_04005 );
+                throw new EncoderException( msg );
+            }
+        }
+        
+        return buffer;
+    }
 }

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/BindResponseDecorator.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/BindResponseDecorator.java?rev=1064846&r1=1064845&r2=1064846&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/BindResponseDecorator.java (original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/BindResponseDecorator.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.BindResponse;
 
 
@@ -94,4 +101,91 @@ public class BindResponseDecorator exten
     {
         getBindResponse().setServerSaslCreds( serverSaslCreds );
     }
+    
+    
+    //-------------------------------------------------------------------------
+    // The Decorator methods
+    //-------------------------------------------------------------------------
+    /**
+     * Compute the BindResponse length 
+     * 
+     * BindResponse : 
+     * <pre>
+     * 0x61 L1 
+     *   | 
+     *   +--> LdapResult
+     *   +--> [serverSaslCreds] 
+     *   
+     * L1 = Length(LdapResult) [ + Length(serverSaslCreds) ] 
+     * Length(BindResponse) = Length(0x61) + Length(L1) + L1
+     * </pre>
+     */
+    public int computeLength()
+    {
+        BindResponse bindResponse = getBindResponse();
+        int ldapResultLength = ((LdapResultDecorator)getLdapResult()).computeLength();
+
+        int bindResponseLength = ldapResultLength;
+
+        byte[] serverSaslCreds = bindResponse.getServerSaslCreds();
+
+        if ( serverSaslCreds != null )
+        {
+            bindResponseLength += 1 + TLV.getNbBytes( serverSaslCreds.length ) + serverSaslCreds.length;
+        }
+
+        setBindResponseLength( bindResponseLength );
+
+        return 1 + TLV.getNbBytes( bindResponseLength ) + bindResponseLength;
+    }
+
+
+    /**
+     * Encode the BindResponse message to a PDU.
+     * 
+     * BindResponse :
+     * <pre>
+     * LdapResult.encode 
+     * [0x87 LL serverSaslCreds]
+     * </pre>
+     * 
+     * @param bb The buffer where to put the PDU
+     * @param bindResponseDecorator The decorated BindResponse to encode
+     * @throws EncoderException when encoding operations fail
+     */
+    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
+    {
+        BindResponse bindResponse = getBindResponse();
+
+        try
+        {
+            // The BindResponse Tag
+            buffer.put( LdapConstants.BIND_RESPONSE_TAG );
+            buffer.put( TLV.getBytes( getBindResponseLength() ) );
+
+            // The LdapResult
+            ((LdapResultDecorator)getLdapResult()).encode( buffer );
+
+            // The serverSaslCredential, if any
+            byte[] serverSaslCreds = bindResponse.getServerSaslCreds();
+
+            if ( serverSaslCreds != null )
+            {
+                buffer.put( ( byte ) LdapConstants.SERVER_SASL_CREDENTIAL_TAG );
+
+                buffer.put( TLV.getBytes( serverSaslCreds.length ) );
+
+                if ( serverSaslCreds.length != 0 )
+                {
+                    buffer.put( serverSaslCreds );
+                }
+            }
+        }
+        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/CompareRequestDecorator.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/CompareRequestDecorator.java?rev=1064846&r1=1064845&r2=1064846&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/CompareRequestDecorator.java (original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/CompareRequestDecorator.java Fri Jan 28 19:58:29 2011
@@ -20,9 +20,19 @@
 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.UniversalTag;
+import org.apache.directory.shared.i18n.I18n;
+import org.apache.directory.shared.ldap.codec.LdapConstants;
+import org.apache.directory.shared.ldap.model.entry.BinaryValue;
 import org.apache.directory.shared.ldap.model.entry.Value;
 import org.apache.directory.shared.ldap.model.message.CompareRequest;
 import org.apache.directory.shared.ldap.model.name.Dn;
+import org.apache.directory.shared.util.Strings;
 
 
 /**
@@ -213,4 +223,102 @@ public class CompareRequestDecorator ext
     {
         getCompareRequest().setAttributeId( attrId );
     }
-}
+
+    
+    //-------------------------------------------------------------------------
+    // The Decorator methods
+    //-------------------------------------------------------------------------
+    /**
+     * Compute the CompareRequest length 
+     * 
+     * CompareRequest : 
+     * 0x6E L1 
+     *   | 
+     *   +--> 0x04 L2 entry 
+     *   +--> 0x30 L3 (ava) 
+     *         | 
+     *         +--> 0x04 L4 attributeDesc 
+     *         +--> 0x04 L5 assertionValue 
+     *         
+     * L3 = Length(0x04) + Length(L4) + L4 + Length(0x04) +
+     *      Length(L5) + L5 
+     * Length(CompareRequest) = Length(0x6E) + Length(L1) + L1 +
+     *      Length(0x04) + Length(L2) + L2 + Length(0x30) + Length(L3) + L3
+     * 
+     * @return The CompareRequest PDU's length
+     */
+    public int computeLength()
+    {
+        // The entry Dn
+        Dn entry = getName();
+        int compareRequestLength = 1 + TLV.getNbBytes( Dn.getNbBytes( entry ) ) + Dn.getNbBytes( entry );
+
+        // The attribute value assertion
+        byte[] attributeIdBytes = Strings.getBytesUtf8( getAttributeId() );
+        int avaLength = 1 + TLV.getNbBytes( attributeIdBytes.length ) + attributeIdBytes.length;
+        setAttrIdBytes( attributeIdBytes );
+
+        org.apache.directory.shared.ldap.model.entry.Value<?> assertionValue = getAssertionValue();
+
+        if ( assertionValue instanceof BinaryValue )
+        {
+            byte[] value = getAssertionValue().getBytes();
+            avaLength += 1 + TLV.getNbBytes( value.length ) + value.length;
+            setAttrValBytes( value );
+        }
+        else
+        {
+            byte[] value = Strings.getBytesUtf8( getAssertionValue().getString() );
+            avaLength += 1 + TLV.getNbBytes( value.length ) + value.length;
+            setAttrValBytes( value );
+        }
+
+        setAvaLength( avaLength );
+        compareRequestLength += 1 + TLV.getNbBytes( avaLength ) + avaLength;
+        setCompareRequestLength( compareRequestLength );
+
+        return 1 + TLV.getNbBytes( compareRequestLength ) + compareRequestLength;
+    }
+
+
+    /**
+     * Encode the CompareRequest message to a PDU. 
+     * 
+     * CompareRequest : 
+     *   0x6E LL 
+     *     0x04 LL entry 
+     *     0x30 LL attributeValueAssertion 
+     *       0x04 LL attributeDesc 
+     *       0x04 LL assertionValue
+     * 
+     * @param buffer The buffer where to put the PDU
+     */
+    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
+    {
+        try
+        {
+            // The CompareRequest Tag
+            buffer.put( LdapConstants.COMPARE_REQUEST_TAG );
+            buffer.put( TLV.getBytes( getCompareRequestLength() ) );
+
+            // The entry
+            org.apache.directory.shared.asn1.ber.tlv.Value.encode( buffer, Dn.getBytes( getName() ) );
+
+            // The attributeValueAssertion sequence Tag
+            buffer.put( UniversalTag.SEQUENCE.getValue() );
+            buffer.put( TLV.getBytes( getAvaLength() ) );
+        }
+        catch ( BufferOverflowException boe )
+        {
+            throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
+        }
+
+        // The attributeDesc
+        org.apache.directory.shared.asn1.ber.tlv.Value.encode( buffer, getAttrIdBytes() );
+
+        // The assertionValue
+        org.apache.directory.shared.asn1.ber.tlv.Value.encode( buffer, ( byte[] ) getAttrValBytes() );
+        
+        return buffer;
+    }
+ }

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/CompareResponseDecorator.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/CompareResponseDecorator.java?rev=1064846&r1=1064845&r2=1064846&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/CompareResponseDecorator.java (original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/CompareResponseDecorator.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.CompareResponse;
 
 
@@ -76,8 +83,6 @@ public class CompareResponseDecorator ex
     //-------------------------------------------------------------------------
     // The CompareResponse methods
     //-------------------------------------------------------------------------
-    
-    
     /**
      * {@inheritDoc}
      */
@@ -85,4 +90,56 @@ public class CompareResponseDecorator ex
     {
         return getCompareResponse().isTrue();
     }
+
+    
+    //-------------------------------------------------------------------------
+    // The Decorator methods
+    //-------------------------------------------------------------------------
+    /**
+     * Compute the CompareResponse length 
+     * 
+     * CompareResponse :
+     * 
+     * 0x6F L1
+     *  |
+     *  +--> LdapResult
+     * 
+     * L1 = Length(LdapResult)
+     * 
+     * Length(CompareResponse) = Length(0x6F) + Length(L1) + L1
+     */
+    public int computeLength()
+    {
+        int compareResponseLength = ((LdapResultDecorator)getLdapResult()).computeLength();
+
+        setCompareResponseLength( compareResponseLength );
+
+        return 1 + TLV.getNbBytes( compareResponseLength ) + compareResponseLength;
+    }
+
+
+    /**
+     * Encode the CompareResponse message to a PDU.
+     * 
+     * @param buffer The buffer where to put the PDU
+     */
+    public ByteBuffer encode( ByteBuffer buffer )
+        throws EncoderException
+    {
+        try
+        {
+            // The CompareResponse Tag
+            buffer.put( LdapConstants.COMPARE_RESPONSE_TAG );
+            buffer.put( TLV.getBytes( getCompareResponseLength() ) );
+
+            // The LdapResult
+            ((LdapResultDecorator)getLdapResult()).encode( buffer );
+        }
+        catch ( BufferOverflowException boe )
+        {
+            throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
+        }
+        
+        return buffer;
+    }
 }

Added: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/Decorator.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/Decorator.java?rev=1064846&view=auto
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/Decorator.java (added)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/Decorator.java Fri Jan 28 19:58:29 2011
@@ -0,0 +1,52 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.directory.shared.ldap.codec.decorators;
+
+import java.nio.ByteBuffer;
+
+import org.apache.directory.shared.asn1.EncoderException;
+
+
+
+
+/**
+ * A decorator interface exposing the ComputeLength and encode methods.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public interface Decorator
+{
+    /**
+     * Compute the object length, which is the sum of all inner length.
+     * 
+     * @return The object's computed length
+     */
+    int computeLength();
+
+
+    /**
+     * Encode the object to a PDU.
+     * 
+     * @param buffer The buffer where to put the PDU
+     * @return The PDU.
+     * @throws EncoderException if the buffer can't be encoded
+     */
+    ByteBuffer encode( ByteBuffer buffer ) throws EncoderException;
+}

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/DeleteRequestDecorator.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/DeleteRequestDecorator.java?rev=1064846&r1=1064845&r2=1064846&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/DeleteRequestDecorator.java (original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/DeleteRequestDecorator.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.DeleteRequest;
 import org.apache.directory.shared.ldap.model.name.Dn;
 
@@ -54,8 +61,6 @@ public class DeleteRequestDecorator exte
     //-------------------------------------------------------------------------
     // The DeleteRequest methods
     //-------------------------------------------------------------------------
-
-    
     /**
      * {@inheritDoc}
      */
@@ -72,4 +77,51 @@ public class DeleteRequestDecorator exte
     {
         getDeleteRequest().setName( name );
     }
+
+    
+    //-------------------------------------------------------------------------
+    // The Decorator methods
+    //-------------------------------------------------------------------------
+    /**
+     * Compute the DelRequest length 
+     * 
+     * DelRequest : 
+     * 0x4A L1 entry 
+     * 
+     * L1 = Length(entry) 
+     * Length(DelRequest) = Length(0x4A) + Length(L1) + L1
+     */
+    public int computeLength()
+    {
+        // The entry
+        return 1 + TLV.getNbBytes( Dn.getNbBytes( getName() ) ) + Dn.getNbBytes( getName() );
+    }
+
+
+    /**
+     * Encode the DelRequest message to a PDU. 
+     * 
+     * DelRequest : 
+     * 0x4A LL entry
+     * 
+     * @param buffer The buffer where to put the PDU
+     */
+    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
+    {
+        try
+        {
+            // The DelRequest Tag
+            buffer.put( LdapConstants.DEL_REQUEST_TAG );
+
+            // The entry
+            buffer.put( TLV.getBytes( Dn.getNbBytes( getName() ) ) );
+            buffer.put( Dn.getBytes( getName() ) );
+        }
+        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/DeleteResponseDecorator.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/DeleteResponseDecorator.java?rev=1064846&r1=1064845&r2=1064846&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/DeleteResponseDecorator.java (original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/DeleteResponseDecorator.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.DeleteResponse;
 
 
@@ -71,4 +78,55 @@ public class DeleteResponseDecorator ext
     {
         return deleteResponseLength;
     }
+
+    
+    //-------------------------------------------------------------------------
+    // The Decorator methods
+    //-------------------------------------------------------------------------
+    /**
+     * Compute the DelResponse length 
+     * 
+     * DelResponse :
+     * 
+     * 0x6B L1
+     *  |
+     *  +--> LdapResult
+     * 
+     * L1 = Length(LdapResult)
+     * 
+     * Length(DelResponse) = Length(0x6B) + Length(L1) + L1
+     */
+    public int computeLength()
+    {
+        int deleteResponseLength = ((LdapResultDecorator)getLdapResult()).computeLength();
+
+        setDeleteResponseLength( deleteResponseLength );
+
+        return 1 + TLV.getNbBytes( deleteResponseLength ) + deleteResponseLength;
+    }
+
+
+    /**
+     * Encode the DelResponse message to a PDU.
+     * 
+     * @param buffer The buffer where to put the PDU
+     */
+    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
+    {
+        try
+        {
+            // The DelResponse Tag
+            buffer.put( LdapConstants.DEL_RESPONSE_TAG );
+            buffer.put( TLV.getBytes( getDeleteResponseLength() ) );
+
+            // 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/ExtendedRequestDecorator.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/ExtendedRequestDecorator.java?rev=1064846&r1=1064845&r2=1064846&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/ExtendedRequestDecorator.java (original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/ExtendedRequestDecorator.java Fri Jan 28 19:58:29 2011
@@ -20,7 +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.i18n.I18n;
+import org.apache.directory.shared.ldap.codec.LdapConstants;
 import org.apache.directory.shared.ldap.model.message.ExtendedRequest;
+import org.apache.directory.shared.util.Strings;
 
 
 /**
@@ -138,4 +146,97 @@ public class ExtendedRequestDecorator ex
     {
         getExtendedRequest().setRequestValue( requestValue );
     }
+
+    
+    //-------------------------------------------------------------------------
+    // The Decorator methods
+    //-------------------------------------------------------------------------
+    /**
+     * Compute the ExtendedRequest length
+     * 
+     * ExtendedRequest :
+     * 
+     * 0x77 L1
+     *  |
+     *  +--> 0x80 L2 name
+     *  [+--> 0x81 L3 value]
+     * 
+     * L1 = Length(0x80) + Length(L2) + L2
+     *      [+ Length(0x81) + Length(L3) + L3]
+     * 
+     * Length(ExtendedRequest) = Length(0x77) + Length(L1) + L1
+     */
+    public int computeLength()
+    {
+        byte[] requestNameBytes = Strings.getBytesUtf8( getRequestName() );
+
+        setRequestNameBytes( requestNameBytes );
+
+        int extendedRequestLength = 1 + TLV.getNbBytes( requestNameBytes.length ) + requestNameBytes.length;
+
+        if ( getRequestValue() != null )
+        {
+            extendedRequestLength += 1 + TLV.getNbBytes( getRequestValue().length )
+                + getRequestValue().length;
+        }
+
+        setExtendedRequestLength( extendedRequestLength );
+
+        return 1 + TLV.getNbBytes( extendedRequestLength ) + extendedRequestLength;
+    }
+
+
+    /**
+     * Encode the ExtendedRequest message to a PDU. 
+     * 
+     * ExtendedRequest :
+     * 
+     * 0x80 LL resquest name
+     * [0x81 LL request value]
+     * 
+     * @param buffer The buffer where to put the PDU
+     * @return The PDU.
+     */
+    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
+    {
+        try
+        {
+            // The BindResponse Tag
+            buffer.put( LdapConstants.EXTENDED_REQUEST_TAG );
+            buffer.put( TLV.getBytes( getExtendedRequestLength() ) );
+
+            // The requestName, if any
+            if ( getRequestNameBytes() == null )
+            {
+                throw new EncoderException( I18n.err( I18n.ERR_04043 ) );
+            }
+
+            buffer.put( ( byte ) LdapConstants.EXTENDED_REQUEST_NAME_TAG );
+            buffer.put( TLV.getBytes( getRequestNameBytes().length ) );
+
+            if ( getRequestNameBytes().length != 0 )
+            {
+                buffer.put( getRequestNameBytes() );
+            }
+
+            // The requestValue, if any
+            if ( getRequestValue() != null )
+            {
+                buffer.put( ( byte ) LdapConstants.EXTENDED_REQUEST_VALUE_TAG );
+
+                buffer.put( TLV.getBytes( getRequestValue().length ) );
+
+                if ( getRequestValue().length != 0 )
+                {
+                    buffer.put( getRequestValue() );
+                }
+            }
+        }
+        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/ExtendedResponseDecorator.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/ExtendedResponseDecorator.java?rev=1064846&r1=1064845&r2=1064846&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/ExtendedResponseDecorator.java (original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/ExtendedResponseDecorator.java Fri Jan 28 19:58:29 2011
@@ -20,7 +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.i18n.I18n;
+import org.apache.directory.shared.ldap.codec.LdapConstants;
 import org.apache.directory.shared.ldap.model.message.ExtendedResponse;
+import org.apache.directory.shared.util.Strings;
 
 
 /**
@@ -158,4 +166,114 @@ public class ExtendedResponseDecorator e
     {
         getExtendedResponse().setResponseValue( responseValue );
     }
+
+    
+    //-------------------------------------------------------------------------
+    // The Decorator methods
+    //-------------------------------------------------------------------------
+    /**
+     * Compute the ExtendedResponse length
+     * 
+     * ExtendedResponse :
+     * 
+     * 0x78 L1
+     *  |
+     *  +--> LdapResult
+     * [+--> 0x8A L2 name
+     * [+--> 0x8B L3 response]]
+     * 
+     * L1 = Length(LdapResult)
+     *      [ + Length(0x8A) + Length(L2) + L2
+     *       [ + Length(0x8B) + Length(L3) + L3]]
+     * 
+     * Length(ExtendedResponse) = Length(0x78) + Length(L1) + L1
+     * 
+     * @return The ExtendedResponse length
+     */
+    public int computeLength()
+    {
+        int ldapResultLength = ((LdapResultDecorator)getLdapResult()).computeLength();
+
+        int extendedResponseLength = ldapResultLength;
+
+        String id = getResponseName();
+
+        if ( !Strings.isEmpty(id) )
+        {
+            byte[] idBytes = Strings.getBytesUtf8(id);
+            setResponseNameBytes( idBytes );
+            int idLength = idBytes.length;
+            extendedResponseLength += 1 + TLV.getNbBytes( idLength ) + idLength;
+        }
+
+        byte[] encodedValue = getResponseValue();
+
+        if ( encodedValue != null )
+        {
+            extendedResponseLength += 1 + TLV.getNbBytes( encodedValue.length ) + encodedValue.length;
+        }
+
+        setExtendedResponseLength( extendedResponseLength );
+
+        return 1 + TLV.getNbBytes( extendedResponseLength ) + extendedResponseLength;
+    }
+
+
+    /**
+     * Encode the ExtendedResponse message to a PDU. 
+     * ExtendedResponse :
+     * LdapResult.encode()
+     * [0x8A LL response name]
+     * [0x8B LL response]
+     * 
+     * @param buffer The buffer where to put the PDU
+     * @return The PDU.
+     */
+    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
+    {
+        try
+        {
+            // The ExtendedResponse Tag
+            buffer.put( LdapConstants.EXTENDED_RESPONSE_TAG );
+            buffer.put( TLV.getBytes( getExtendedResponseLength() ) );
+
+            // The LdapResult
+            ((LdapResultDecorator)getLdapResult()).encode( buffer );
+
+            // The ID, if any
+            byte[] idBytes = getResponseNameBytes();
+
+            if ( idBytes != null )
+            {
+                buffer.put( ( byte ) LdapConstants.EXTENDED_RESPONSE_RESPONSE_NAME_TAG );
+                buffer.put( TLV.getBytes( idBytes.length ) );
+
+                if ( idBytes.length != 0 )
+                {
+                    buffer.put( idBytes );
+                }
+            }
+
+            // The encodedValue, if any
+            byte[] encodedValue = getResponseValue();
+
+            if ( encodedValue != null )
+            {
+                buffer.put( ( byte ) LdapConstants.EXTENDED_RESPONSE_RESPONSE_TAG );
+
+                buffer.put( TLV.getBytes( encodedValue.length ) );
+
+                if ( encodedValue.length != 0 )
+                {
+                    buffer.put( encodedValue );
+                }
+            }
+        }
+        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/IntermediateResponseDecorator.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/IntermediateResponseDecorator.java?rev=1064846&r1=1064845&r2=1064846&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/IntermediateResponseDecorator.java (original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/IntermediateResponseDecorator.java Fri Jan 28 19:58:29 2011
@@ -20,7 +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.i18n.I18n;
+import org.apache.directory.shared.ldap.codec.LdapConstants;
 import org.apache.directory.shared.ldap.model.message.IntermediateResponse;
+import org.apache.directory.shared.util.Strings;
 
 
 /**
@@ -138,4 +146,101 @@ public class IntermediateResponseDecorat
     {
         getIntermediateResponse().setResponseValue( value );
     }
+    
+    
+    //-------------------------------------------------------------------------
+    // The Decorator methods
+    //-------------------------------------------------------------------------
+    /**
+     * Compute the intermediateResponse length
+     * 
+     * intermediateResponse :
+     * 
+     * 0x79 L1
+     *  |
+     * [+--> 0x80 L2 name
+     * [+--> 0x81 L3 response]]
+     * 
+     * L1 = [ + Length(0x80) + Length(L2) + L2
+     *      [ + Length(0x81) + Length(L3) + L3]]
+     * 
+     * Length(IntermediateResponse) = Length(0x79) + Length(L1) + L1
+     * 
+     * @return The IntermediateResponse length
+     */
+    public int computeLength()
+    {
+        int intermediateResponseLength = 0;
+
+        if ( !Strings.isEmpty( getResponseName() ) )
+        {
+            byte[] responseNameBytes = Strings.getBytesUtf8( getResponseName() );
+
+            int responseNameLength = responseNameBytes.length;
+            intermediateResponseLength += 1 + TLV.getNbBytes( responseNameLength ) + responseNameLength;
+            setResponseNameBytes( responseNameBytes );
+        }
+
+        byte[] encodedValue = getResponseValue();
+
+        if ( encodedValue != null )
+        {
+            intermediateResponseLength += 1 + TLV.getNbBytes( encodedValue.length ) + encodedValue.length;
+        }
+
+        setIntermediateResponseLength( intermediateResponseLength );
+
+        return 1 + TLV.getNbBytes( intermediateResponseLength ) + intermediateResponseLength;
+    }
+
+
+    /**
+     * Encode the IntermediateResponse message to a PDU. 
+     * IntermediateResponse :
+     *   0x79 LL
+     *     [0x80 LL response name]
+     *     [0x81 LL responseValue]
+     * 
+     * @param buffer The buffer where to put the PDU
+     */
+    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
+    {
+        try
+        {
+            // The ExtendedResponse Tag
+            buffer.put( LdapConstants.INTERMEDIATE_RESPONSE_TAG );
+            buffer.put( TLV.getBytes( getIntermediateResponseLength() ) );
+
+            // The responseName, if any
+            byte[] responseNameBytes = getResponseNameBytes();
+
+            if ( ( responseNameBytes != null ) && ( responseNameBytes.length != 0 ) )
+            {
+                buffer.put( ( byte ) LdapConstants.INTERMEDIATE_RESPONSE_NAME_TAG );
+                buffer.put( TLV.getBytes( responseNameBytes.length ) );
+                buffer.put( responseNameBytes );
+            }
+
+            // The encodedValue, if any
+            byte[] encodedValue = getResponseValue();
+
+            if ( encodedValue != null )
+            {
+                buffer.put( ( byte ) LdapConstants.INTERMEDIATE_RESPONSE_VALUE_TAG );
+
+                buffer.put( TLV.getBytes( encodedValue.length ) );
+
+                if ( encodedValue.length != 0 )
+                {
+                    buffer.put( encodedValue );
+                }
+            }
+        }
+        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/LdapResultDecorator.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/LdapResultDecorator.java?rev=1064846&r1=1064845&r2=1064846&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/LdapResultDecorator.java (original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/LdapResultDecorator.java Fri Jan 28 19:58:29 2011
@@ -20,10 +20,20 @@
 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.UniversalTag;
+import org.apache.directory.shared.asn1.ber.tlv.Value;
+import org.apache.directory.shared.i18n.I18n;
+import org.apache.directory.shared.ldap.codec.LdapEncoder;
 import org.apache.directory.shared.ldap.model.message.LdapResult;
 import org.apache.directory.shared.ldap.model.message.Referral;
 import org.apache.directory.shared.ldap.model.message.ResultCodeEnum;
 import org.apache.directory.shared.ldap.model.name.Dn;
+import org.apache.directory.shared.util.Strings;
 
 
 /**
@@ -31,7 +41,7 @@ import org.apache.directory.shared.ldap.
  *
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  */
-public class LdapResultDecorator implements LdapResult
+public class LdapResultDecorator implements LdapResult, Decorator
 {
     /** The decorated LdapResult */
     private final LdapResult decoratedLdapResult;
@@ -195,4 +205,110 @@ public class LdapResultDecorator impleme
     {
         return decoratedLdapResult.toString();
     }
+    
+    
+    //-------------------------------------------------------------------------
+    // The Decorator methods
+    //-------------------------------------------------------------------------
+    /**
+     * Compute the LdapResult length 
+     * 
+     * LdapResult : 
+     * 0x0A 01 resultCode (0..80)
+     *   0x04 L1 matchedDN (L1 = Length(matchedDN)) 
+     *   0x04 L2 errorMessage (L2 = Length(errorMessage)) 
+     *   [0x83 L3] referrals 
+     *     | 
+     *     +--> 0x04 L4 referral 
+     *     +--> 0x04 L5 referral 
+     *     +--> ... 
+     *     +--> 0x04 Li referral 
+     *     +--> ... 
+     *     +--> 0x04 Ln referral 
+     *     
+     * L1 = Length(matchedDN) 
+     * L2 = Length(errorMessage) 
+     * L3 = n*Length(0x04) + sum(Length(L4) .. Length(Ln)) + sum(L4..Ln) 
+     * L4..n = Length(0x04) + Length(Li) + Li 
+     * Length(LdapResult) = Length(0x0x0A) +
+     *      Length(0x01) + 1 + Length(0x04) + Length(L1) + L1 + Length(0x04) +
+     *      Length(L2) + L2 + Length(0x83) + Length(L3) + L3
+     */
+    public int computeLength()
+    {
+        int ldapResultLength = 0;
+
+        // The result code : always 3 bytes
+        ldapResultLength = 1 + 1 + 1;
+
+        // The matchedDN length
+        if ( getMatchedDn() == null )
+        {
+            ldapResultLength += 1 + 1;
+        }
+        else
+        {
+            byte[] matchedDNBytes = Strings.getBytesUtf8( Strings .trimLeft( getMatchedDn().getName() ) );
+            ldapResultLength += 1 + TLV.getNbBytes( matchedDNBytes.length ) + matchedDNBytes.length;
+            setMatchedDnBytes( matchedDNBytes );
+        }
+
+        // The errorMessage length
+        byte[] errorMessageBytes = Strings.getBytesUtf8( getErrorMessage() );
+        ldapResultLength += 1 + TLV.getNbBytes( errorMessageBytes.length ) + errorMessageBytes.length;
+        setErrorMessageBytes( errorMessageBytes );
+
+        int referralLength = LdapEncoder.computeReferralLength( getReferral() );
+
+        if ( referralLength != 0 )
+        {
+            // The referrals
+            ldapResultLength += 1 + TLV.getNbBytes( referralLength ) + referralLength;
+        }
+
+        return ldapResultLength;
+    }
+
+
+    /**
+     * Encode the LdapResult message to a PDU.
+     * 
+     * @param buffer The buffer where to put the PDU
+     * @return The PDU.
+     */
+    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
+    {
+        if ( buffer == null )
+        {
+            throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
+        }
+
+        try
+        {
+            // The result code
+            buffer.put( UniversalTag.ENUMERATED.getValue() );
+            buffer.put( ( byte ) 1 );
+            buffer.put( ( byte ) getResultCode().getValue() );
+        }
+        catch ( BufferOverflowException boe )
+        {
+            throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
+        }
+
+        // The matchedDN
+        Value.encode( buffer, getMatchedDnBytes() );
+
+        // The error message
+        Value.encode( buffer, getErrorMessageBytes() );
+
+        // The referrals, if any
+        Referral referral = getReferral();
+
+        if ( referral != null )
+        {
+            LdapEncoder.encodeReferral( buffer, referral );
+        }
+
+        return buffer;
+    }
 }

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/MessageDecorator.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/MessageDecorator.java?rev=1064846&r1=1064845&r2=1064846&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/MessageDecorator.java (original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/MessageDecorator.java Fri Jan 28 19:58:29 2011
@@ -20,8 +20,10 @@
 package org.apache.directory.shared.ldap.codec.decorators;
 
 
+import java.util.HashMap;
 import java.util.Map;
 
+import org.apache.directory.shared.ldap.codec.controls.ControlDecorator;
 import org.apache.directory.shared.ldap.model.exception.MessageException;
 import org.apache.directory.shared.ldap.model.message.AbandonRequest;
 import org.apache.directory.shared.ldap.model.message.AddRequest;
@@ -55,11 +57,14 @@ import org.apache.directory.shared.ldap.
  * @TODO make this class abstract, after finishing switch and all types and make default blow an EncoderException
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  */
-public class MessageDecorator implements Message
+public abstract class MessageDecorator implements Message, Decorator
 {
     /** The decorated Control */
     private final Message decoratedMessage;
 
+    /** Map of message controls using OID Strings for keys and Control values */
+    private final Map<String, Control> controls;
+
     /** The encoded Message length */
     protected int messageLength;
 
@@ -72,85 +77,126 @@ public class MessageDecorator implements
     
     public static MessageDecorator getDecorator( Message decoratedMessage )
     {
+        if ( decoratedMessage instanceof MessageDecorator )
+        {
+            return (MessageDecorator)decoratedMessage;
+        }
+        
+        MessageDecorator decorator = null;
+        
         switch ( decoratedMessage.getType() )
         {
             case ABANDON_REQUEST:
-                return new AbandonRequestDecorator( ( AbandonRequest ) decoratedMessage );
+                decorator = new AbandonRequestDecorator( ( AbandonRequest ) decoratedMessage );
+                break;
 
             case ADD_REQUEST:
-                return new AddRequestDecorator( ( AddRequest ) decoratedMessage );
+                decorator = new AddRequestDecorator( ( AddRequest ) decoratedMessage );
+                break;
                 
             case ADD_RESPONSE:
-                return new AddResponseDecorator( ( AddResponse ) decoratedMessage );
+                decorator = new AddResponseDecorator( ( AddResponse ) decoratedMessage );
+                break;
                 
             case BIND_REQUEST:
-                return new BindRequestDecorator( ( BindRequest ) decoratedMessage );
+                decorator = new BindRequestDecorator( ( BindRequest ) decoratedMessage );
+                break;
                 
             case BIND_RESPONSE:
-                return new BindResponseDecorator( ( BindResponse ) decoratedMessage );
+                decorator = new BindResponseDecorator( ( BindResponse ) decoratedMessage );
+                break;
                 
             case COMPARE_REQUEST:
-                return new CompareRequestDecorator( ( CompareRequest ) decoratedMessage );
+                decorator = new CompareRequestDecorator( ( CompareRequest ) decoratedMessage );
+                break;
                 
             case COMPARE_RESPONSE:
-                return new CompareResponseDecorator( ( CompareResponse ) decoratedMessage );
+                decorator = new CompareResponseDecorator( ( CompareResponse ) decoratedMessage );
+                break;
                 
             case DEL_REQUEST:
-                return new DeleteRequestDecorator( ( DeleteRequest ) decoratedMessage );
+                decorator = new DeleteRequestDecorator( ( DeleteRequest ) decoratedMessage );
+                break;
 
             case DEL_RESPONSE:
-                return new DeleteResponseDecorator( ( DeleteResponse ) decoratedMessage );
+                decorator = new DeleteResponseDecorator( ( DeleteResponse ) decoratedMessage );
+                break;
                 
             case EXTENDED_REQUEST:
-                return new ExtendedRequestDecorator( ( ExtendedRequest ) decoratedMessage );
+                decorator = new ExtendedRequestDecorator( ( ExtendedRequest ) decoratedMessage );
+                break;
                 
             case EXTENDED_RESPONSE:
-                return new ExtendedResponseDecorator( ( ExtendedResponse ) decoratedMessage );
+                decorator = new ExtendedResponseDecorator( ( ExtendedResponse ) decoratedMessage );
+                break;
                 
             case INTERMEDIATE_RESPONSE:
-                return new IntermediateResponseDecorator( ( IntermediateResponse ) decoratedMessage );
+                decorator = new IntermediateResponseDecorator( ( IntermediateResponse ) decoratedMessage );
+                break;
                 
             case MODIFY_REQUEST:
-                return new ModifyRequestDecorator( ( ModifyRequest ) decoratedMessage );
+                decorator = new ModifyRequestDecorator( ( ModifyRequest ) decoratedMessage );
+                break;
                 
             case MODIFY_RESPONSE:
-                return new ModifyResponseDecorator( ( ModifyResponse ) decoratedMessage );
+                decorator = new ModifyResponseDecorator( ( ModifyResponse ) decoratedMessage );
+                break;
                 
             case MODIFYDN_REQUEST:
-                return new ModifyDnRequestDecorator( ( ModifyDnRequest ) decoratedMessage );
+                decorator = new ModifyDnRequestDecorator( ( ModifyDnRequest ) decoratedMessage );
+                break;
                 
             case MODIFYDN_RESPONSE:
-                return new ModifyDnResponseDecorator( ( ModifyDnResponse ) decoratedMessage );
+                decorator = new ModifyDnResponseDecorator( ( ModifyDnResponse ) decoratedMessage );
+                break;
                 
             case SEARCH_REQUEST:
-                return new SearchRequestDecorator( ( SearchRequest ) decoratedMessage );
+                decorator = new SearchRequestDecorator( ( SearchRequest ) decoratedMessage );
+                break;
                 
             case SEARCH_RESULT_DONE:
-                return new SearchResultDoneDecorator( ( SearchResultDone ) decoratedMessage );
+                decorator = new SearchResultDoneDecorator( ( SearchResultDone ) decoratedMessage );
+                break;
                 
             case SEARCH_RESULT_ENTRY:
-                return new SearchResultEntryDecorator( ( SearchResultEntry ) decoratedMessage );
+                decorator = new SearchResultEntryDecorator( ( SearchResultEntry ) decoratedMessage );
+                break;
                 
             case SEARCH_RESULT_REFERENCE:
-                return new SearchResultReferenceDecorator( ( SearchResultReference ) decoratedMessage );
+                decorator = new SearchResultReferenceDecorator( ( SearchResultReference ) decoratedMessage );
+                break;
             
             case UNBIND_REQUEST:
-                return new UnbindRequestDecorator( ( UnbindRequest ) decoratedMessage );
+                decorator = new UnbindRequestDecorator( ( UnbindRequest ) decoratedMessage );
+                break;
+                
+            default :
+                return null;
+        }
+        
+        Map<String, Control> controls = decoratedMessage.getControls();
+        
+        if ( controls != null )
+        {
+            for ( Control control : controls.values() )
+            {
+                Control controlDecorator = ControlDecorator.getDecorator( control );
                 
-            default:
-                return new MessageDecorator( decoratedMessage );
+                decorator.addControl( controlDecorator );
+            }
         }
+        
+        return decorator;
     }
 
 
     /**
-     * Makes a Message an Encodeable object.
-     *
-     * @TODO make me protected after making this class abstract
+     * Makes a Message an Decorator object.
      */
-    public MessageDecorator( Message decoratedMessage )
+    protected MessageDecorator( Message decoratedMessage )
     {
         this.decoratedMessage = decoratedMessage;
+        controls = new HashMap<String, Control>();
     }
 
 
@@ -213,8 +259,6 @@ public class MessageDecorator implements
     //-------------------------------------------------------------------------
     // The Message methods
     //-------------------------------------------------------------------------
-    
-    
     /**
      * {@inheritDoc}
      */
@@ -229,7 +273,7 @@ public class MessageDecorator implements
      */
     public Map<String, Control> getControls()
     {
-        return decoratedMessage.getControls();
+        return controls;
     }
 
 
@@ -238,7 +282,7 @@ public class MessageDecorator implements
      */
     public Control getControl( String oid )
     {
-        return decoratedMessage.getControl( oid );
+        return controls.get( oid );
     }
 
 
@@ -247,7 +291,7 @@ public class MessageDecorator implements
      */
     public boolean hasControl( String oid )
     {
-        return decoratedMessage.hasControl( oid );
+        return controls.containsKey( oid );
     }
 
 
@@ -256,7 +300,10 @@ public class MessageDecorator implements
      */
     public void addControl( Control control ) throws MessageException
     {
-        decoratedMessage.addControl( control );
+        ControlDecorator controlDecorator = (ControlDecorator)control;
+        Control decoratedControl = controlDecorator.getDecorated();
+        decoratedMessage.addControl( decoratedControl );
+        controls.put( control.getOid(), control );
         currentControl = control;
     }
 
@@ -266,7 +313,11 @@ public class MessageDecorator implements
      */
     public void addAllControls( Control[] controls ) throws MessageException
     {
-        decoratedMessage.addAllControls( controls );
+        for ( Control control : controls )
+        {
+            decoratedMessage.addControl( ((ControlDecorator)control).getDecorated() );
+            this.controls.put( control.getOid(), control );
+        }
     }
 
 
@@ -276,6 +327,7 @@ public class MessageDecorator implements
     public void removeControl( Control control ) throws MessageException
     {
         decoratedMessage.removeControl( control );
+        controls.remove( control.getOid() );
     }
 
 

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/ModifyDnRequestDecorator.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/ModifyDnRequestDecorator.java?rev=1064846&r1=1064845&r2=1064846&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/ModifyDnRequestDecorator.java (original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/ModifyDnRequestDecorator.java Fri Jan 28 19:58:29 2011
@@ -20,9 +20,18 @@
 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.model.message.ModifyDnRequest;
 import org.apache.directory.shared.ldap.model.name.Dn;
 import org.apache.directory.shared.ldap.model.name.Rdn;
+import org.apache.directory.shared.util.Strings;
 
 
 /**
@@ -159,4 +168,106 @@ public class ModifyDnRequestDecorator ex
     {
         return getModifyDnRequest().isMove();
     }
+
+    
+    //-------------------------------------------------------------------------
+    // The Decorator methods
+    //-------------------------------------------------------------------------
+    /**
+     * Compute the ModifyDNRequest length
+     * 
+     * ModifyDNRequest :
+     * <pre>
+     * 0x6C L1
+     *  |
+     *  +--> 0x04 L2 entry
+     *  +--> 0x04 L3 newRDN
+     *  +--> 0x01 0x01 (true/false) deleteOldRDN (3 bytes)
+     * [+--> 0x80 L4 newSuperior ] 
+     * 
+     * L2 = Length(0x04) + Length(Length(entry)) + Length(entry) 
+     * L3 = Length(0x04) + Length(Length(newRDN)) + Length(newRDN) 
+     * L4 = Length(0x80) + Length(Length(newSuperior)) + Length(newSuperior)
+     * L1 = L2 + L3 + 3 [+ L4] 
+     * 
+     * Length(ModifyDNRequest) = Length(0x6C) + Length(L1) + L1
+     * </pre>
+     * 
+     * @return The PDU's length of a ModifyDN Request
+     */
+    public int computeLength()
+    {
+        int newRdnlength = Strings.getBytesUtf8( getNewRdn().getName() ).length;
+
+        int modifyDNRequestLength = 1 + TLV.getNbBytes( Dn.getNbBytes( getName() ) )
+            + Dn.getNbBytes( getName() ) + 1 + TLV.getNbBytes( newRdnlength ) + newRdnlength + 1 + 1
+            + 1; // deleteOldRDN
+
+        if ( getNewSuperior() != null )
+        {
+            modifyDNRequestLength += 1 + TLV.getNbBytes( Dn.getNbBytes( getNewSuperior() ) )
+                + Dn.getNbBytes( getNewSuperior() );
+        }
+
+        setModifyDnRequestLength( modifyDNRequestLength );
+
+        return 1 + TLV.getNbBytes( modifyDNRequestLength ) + modifyDNRequestLength;
+    }
+
+
+    /**
+     * Encode the ModifyDNRequest message to a PDU. 
+     * 
+     * ModifyDNRequest :
+     * <pre>
+     * 0x6C LL
+     *   0x04 LL entry
+     *   0x04 LL newRDN
+     *   0x01 0x01 deleteOldRDN
+     *   [0x80 LL newSuperior]
+     * </pre>
+     * @param buffer The buffer where to put the PDU
+     * @return The PDU.
+     */
+    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
+    {
+        try
+        {
+            // The ModifyDNRequest Tag
+            buffer.put( LdapConstants.MODIFY_DN_REQUEST_TAG );
+            buffer.put( TLV.getBytes( getModifyDnResponseLength() ) );
+
+            // The entry
+
+            Value.encode( buffer, Dn.getBytes( getName() ) );
+
+            // The newRDN
+            Value.encode( buffer, getNewRdn().getName() );
+
+            // The flag deleteOldRdn
+            Value.encode( buffer, getDeleteOldRdn() );
+
+            // The new superior, if any
+            if ( getNewSuperior() != null )
+            {
+                // Encode the reference
+                buffer.put( ( byte ) LdapConstants.MODIFY_DN_REQUEST_NEW_SUPERIOR_TAG );
+
+                int newSuperiorLength = Dn.getNbBytes( getNewSuperior() );
+
+                buffer.put( TLV.getBytes( newSuperiorLength ) );
+
+                if ( newSuperiorLength != 0 )
+                {
+                    buffer.put( Dn.getBytes( getNewSuperior() ) );
+                }
+            }
+        }
+        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/ModifyDnResponseDecorator.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/ModifyDnResponseDecorator.java?rev=1064846&r1=1064845&r2=1064846&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/ModifyDnResponseDecorator.java (original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/decorators/ModifyDnResponseDecorator.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.ModifyDnResponse;
 
 
@@ -71,4 +78,55 @@ public class ModifyDnResponseDecorator e
     {
         return modifyDnResponseLength;
     }
+
+
+    //-------------------------------------------------------------------------
+    // The Decorator methods
+    //-------------------------------------------------------------------------
+    /**
+     * Compute the ModifyDNResponse length 
+     * 
+     * ModifyDNResponse : 
+     * <pre>
+     * 0x6D L1 
+     *   | 
+     *   +--> LdapResult 
+     *   
+     * L1 = Length(LdapResult) 
+     * Length(ModifyDNResponse) = Length(0x6D) + Length(L1) + L1
+     * </pre>
+     */
+    public int computeLength()
+    {
+        int modifyDnResponseLength = ((LdapResultDecorator)getLdapResult()).computeLength();
+
+        setModifyDnResponseLength( modifyDnResponseLength );
+
+        return 1 + TLV.getNbBytes( modifyDnResponseLength ) + modifyDnResponseLength;
+    }
+    
+    
+    /**
+     * Encode the ModifyDnResponse 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_DN_RESPONSE_TAG );
+            buffer.put( TLV.getBytes( getModifyDnResponseLength() ) );
+
+            // The LdapResult
+            ((LdapResultDecorator)getLdapResult()).encode( buffer );
+        }
+        catch ( BufferOverflowException boe )
+        {
+            throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
+        }
+        
+        return buffer;
+    }
 }