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 2005/08/01 08:35:39 UTC

svn commit: r226754 - in /directory/sandbox/trunk/asn1-new-codec/src: java/org/apache/asn1/ldap/pojo/ModifyRequest.java test/org/apache/asn1/ldap/codec/ModifyRequestTest.java

Author: elecharny
Date: Sun Jul 31 23:35:31 2005
New Revision: 226754

URL: http://svn.apache.org/viewcvs?rev=226754&view=rev
Log:
Added the endoding of the modifyRequest message

Modified:
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/ModifyRequest.java
    directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/codec/ModifyRequestTest.java

Modified: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/ModifyRequest.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/ModifyRequest.java?rev=226754&r1=226753&r2=226754&view=diff
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/ModifyRequest.java (original)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/ModifyRequest.java Sun Jul 31 23:35:31 2005
@@ -17,15 +17,23 @@
 package org.apache.asn1.ldap.pojo;
 
 import org.apache.asn1.Asn1Object;
+import org.apache.asn1.EncoderException;
 import org.apache.asn1.ber.tlv.Length;
+import org.apache.asn1.ber.tlv.UniversalTag;
+import org.apache.asn1.ber.tlv.Value;
+import org.apache.asn1.ldap.codec.LdapConstants;
 import org.apache.asn1.ldap.codec.primitives.LdapDN;
 import org.apache.asn1.ldap.codec.primitives.LdapString;
 import org.apache.asn1.primitives.OctetString;
 
 import org.apache.log4j.Logger;
 
+import java.nio.BufferOverflowException;
+import java.nio.ByteBuffer;
 import java.util.ArrayList;
 import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
 
 import javax.naming.NamingEnumeration;
 import javax.naming.NamingException;
@@ -78,6 +86,21 @@
     /** A local storage for the operation */
     private transient int currentOperation;
 
+    /** The modify request length */
+    private transient int modifyRequestLength;
+    
+    /** The modifications length */
+    private transient int modificationsLength;
+    
+    /** The modification sequence length */
+    private transient List modificationSequenceLength;
+    
+    /** The list of all modification length */
+    private transient List modificationLength;
+    
+    /** The list of all vals length */
+    private transient List valuesLength;
+
     //~ Constructors -------------------------------------------------------------------------------
 
     /**
@@ -137,15 +160,15 @@
         switch ( currentOperation )
         {
 
-            case 0 : // add
+            case LdapConstants.OPERATION_ADD : // add
                 operation = DirContext.ADD_ATTRIBUTE;
                 break;
 
-            case 1 : // delete
+            case LdapConstants.OPERATION_DELETE : // delete
                 operation = DirContext.REMOVE_ATTRIBUTE;
                 break;
 
-            case 2 : // replace
+            case LdapConstants.OPERATION_REPLACE : // replace
                 operation = DirContext.REPLACE_ATTRIBUTE;
                 break;
         }
@@ -249,29 +272,32 @@
     public int computeLength()
     {
         // Initialized with object
-        int modifyRequestLength = 1 + Length.getNbBytes( object.getLength() ) + object.getLength();
+        modifyRequestLength = 1 + Length.getNbBytes( object.getLength() ) + object.getLength();
         
         // Modifications
-        int modificationsLength = 0;
+        modificationsLength = 0;
         
         if ( ( modifications != null ) && ( modifications.size() != 0 ) )
         {
             Iterator modificationsIterator = modifications.iterator();
+            modificationSequenceLength = new LinkedList();
+            modificationLength = new LinkedList();
+            valuesLength = new LinkedList();
             
             while ( modificationsIterator.hasNext() )
             {
                 // Modification sequence length initialized with the operation
-                int modificationSequenceLength = 1 + 1 + 1;
+                int localModificationSequenceLength = 1 + 1 + 1;
+                int localValuesLength = 0;
                 
                 ModificationItem modification = (ModificationItem)modificationsIterator.next();
                 
                 // Modification length initialized with the type
                 int typeLength = modification.getAttribute().getID().length();
-                int modificationLength = 1 + Length.getNbBytes( typeLength ) + typeLength;
+                int localModificationLength = 1 + Length.getNbBytes( typeLength ) + typeLength;
                 
                 try
                 {
-                    int valuesLength = 0;
                     
                     NamingEnumeration values = modification.getAttribute().getAll();
 
@@ -282,11 +308,11 @@
                         {
                             OctetString value = (OctetString)values.next();
                             
-                            valuesLength += 1 + Length.getNbBytes( value.getLength() ) + value.getLength();
+                            localValuesLength += 1 + Length.getNbBytes( value.getLength() ) + value.getLength();
                         }
                     }
                     
-                    modificationLength += 1 + Length.getNbBytes( valuesLength ) + valuesLength;
+                    localModificationLength += 1 + Length.getNbBytes( localValuesLength ) + localValuesLength;
                 }
                 catch (NamingException ne)
                 {
@@ -294,10 +320,15 @@
                 }
                 
                 // Compute the modificationSequenceLength
-                modificationSequenceLength += 1 + Length.getNbBytes( modificationLength ) + modificationLength;
+                localModificationSequenceLength += 1 + Length.getNbBytes( localModificationLength ) + localModificationLength;
                 
                 // Add the tag and the length
-                modificationsLength += 1 + Length.getNbBytes( modificationSequenceLength ) + modificationSequenceLength;
+                modificationsLength += 1 + Length.getNbBytes( localModificationSequenceLength ) + localModificationSequenceLength;
+                
+                // Store the arrays of values
+                valuesLength.add( new Integer( localValuesLength ) );
+                modificationLength.add( new Integer( localModificationLength ) );
+                modificationSequenceLength.add( new Integer( localModificationSequenceLength ) );
             }
             
             // Add the modifications length to the modificationRequestLength
@@ -305,6 +336,138 @@
         }
 
         return 1 + Length.getNbBytes( modifyRequestLength ) + modifyRequestLength;
+    }
+
+    /**
+     * Encode the ModifyRequest message to a PDU.
+     * 
+     * AddRequest :
+     * 
+     * 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
+     * 
+     * @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( "Cannot put a PDU in a null buffer !" );
+        }
+
+        try 
+        {
+            // The AddRequest Tag
+            buffer.put( LdapConstants.MODIFY_REQUEST_TAG );
+            buffer.put( Length.getBytes( modifyRequestLength ) ) ;
+            
+            // The entry
+            Value.encode( buffer, object );
+            
+            // The modifications sequence
+            buffer.put( UniversalTag.SEQUENCE_TAG );
+            buffer.put( Length.getBytes( modificationsLength ) ) ;
+
+            // The modifications list
+            if ( ( modifications != null ) && ( modifications.size() != 0 ) )
+            {
+                Iterator modificationIterator = modifications.iterator();
+                int modificationNumber = 0;
+                
+                // Compute the modifications length
+                while ( modificationIterator.hasNext() )
+                {
+                    ModificationItem modification = (ModificationItem)modificationIterator.next();
+                    
+                    // The modification sequence
+                    buffer.put( UniversalTag.SEQUENCE_TAG );
+                    int localModificationSequenceLength = ( (Integer)modificationSequenceLength.get( modificationNumber ) ).intValue();
+                    buffer.put( Length.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_TAG );
+                    buffer.put( (byte)1 );
+                    
+                    switch ( modification.getModificationOp() )
+                    {
+
+                        case DirContext.ADD_ATTRIBUTE : // add
+                            buffer.put( (byte)LdapConstants.OPERATION_ADD );
+                            break;
+
+                        case DirContext.REMOVE_ATTRIBUTE : // delete
+                            buffer.put( (byte)LdapConstants.OPERATION_DELETE );
+                            break;
+
+                        case DirContext.REPLACE_ATTRIBUTE : // replace
+                            buffer.put( (byte)LdapConstants.OPERATION_REPLACE );
+                            break;
+                    }
+
+                    // The modification
+                    buffer.put( UniversalTag.SEQUENCE_TAG );
+                    int localModificationLength = ( (Integer)modificationLength.get( modificationNumber ) ).intValue();
+                    buffer.put( Length.getBytes( localModificationLength ) );
+                    
+                    // The modification type
+                    Value.encode( buffer, modification.getAttribute().getID() );
+                    
+                    // The values
+                    buffer.put( UniversalTag.SET_TAG );
+                    int localValuesLength = ( (Integer)valuesLength.get( modificationNumber ) ).intValue();
+                    buffer.put( Length.getBytes( localValuesLength ) );
+                    
+                    try
+                    {
+                        NamingEnumeration values = modification.getAttribute().getAll();
+                        
+                        if ( values.hasMoreElements() )
+                        {
+                            while ( values.hasMoreElements() )
+                            {
+                                OctetString value = (OctetString)values.next();
+                                
+                                Value.encode( buffer, value );
+                            }
+                        }
+                        
+                    }
+                    catch (NamingException ne)
+                    {
+                        throw new EncoderException("Cannot enumerate the values");
+                    }
+                    
+                    // Go to the next modification number;
+                    modificationNumber++;
+                }
+            }
+        }
+        catch ( BufferOverflowException boe )
+        {
+            throw new EncoderException("The PDU buffer size is too small !"); 
+        }
+
+        return buffer;
     }
 
     /**

Modified: directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/codec/ModifyRequestTest.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/codec/ModifyRequestTest.java?rev=226754&r1=226753&r2=226754&view=diff
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/codec/ModifyRequestTest.java (original)
+++ directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/codec/ModifyRequestTest.java Sun Jul 31 23:35:31 2005
@@ -27,11 +27,13 @@
 import javax.naming.directory.ModificationItem;
 
 import org.apache.asn1.DecoderException;
+import org.apache.asn1.EncoderException;
 import org.apache.asn1.ber.Asn1Decoder;
 import org.apache.asn1.ber.containers.IAsn1Container;
 import org.apache.asn1.ldap.pojo.LdapMessage;
 import org.apache.asn1.ldap.pojo.ModifyRequest;
 import org.apache.asn1.primitives.OctetString;
+import org.apache.asn1.util.StringUtils;
 import org.apache.log4j.Logger;
 import org.apache.log4j.PropertyConfigurator;
 
@@ -96,6 +98,7 @@
                 0x04, 0x04, 't', 'e', 's', 't'
             } );
 
+        String decodedPdu = StringUtils.dumpBytes( stream.array() );
         stream.flip();
 
         // Allocate a LdapMessage Container
@@ -157,5 +160,20 @@
 
         // Check the length
         Assert.assertEquals(0x56, message.computeLength());
+
+        // Check the encoding
+        try
+        {
+            ByteBuffer bb = message.encode( null );
+            
+            String encodedPdu = StringUtils.dumpBytes( bb.array() ); 
+            
+            Assert.assertEquals(encodedPdu, decodedPdu );
+        }
+        catch ( EncoderException ee )
+        {
+            ee.printStackTrace();
+            Assert.fail( ee.getMessage() );
+        }
     }
 }