directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r226692 - in /directory/sandbox/trunk/asn1-new-codec/src: java/org/apache/asn1/ldap/codec/ java/org/apache/asn1/ldap/pojo/ test/org/apache/asn1/ldap/codec/
Date Sun, 31 Jul 2005 20:31:15 GMT
Author: elecharny
Date: Sun Jul 31 13:31:08 2005
New Revision: 226692

URL: http://svn.apache.org/viewcvs?rev=226692&view=rev
Log:
Added the encoding of the AddRequest message

Modified:
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/codec/LdapConstants.java
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/AddRequest.java
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/SearchResultEntry.java
    directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/codec/AddRequestTest.java

Modified: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/codec/LdapConstants.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/codec/LdapConstants.java?rev=226692&r1=226691&r2=226692&view=diff
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/codec/LdapConstants.java
(original)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/codec/LdapConstants.java
Sun Jul 31 13:31:08 2005
@@ -59,6 +59,7 @@
     public static final byte SEARCH_RESULT_ENTRY_TAG = 0x64;
     public static final byte SEARCH_RESULT_DONE_TAG = 0x65;
     public static final byte MODIFY_RESPONSE_TAG = 0x67;
+    public static final byte ADD_REQUEST_TAG = 0x68;
     public static final byte ADD_RESPONSE_TAG = 0x69;
     public static final byte DEL_RESPONSE_TAG = 0x6B;
     public static final byte MODIFY_DN_REQUEST_TAG = 0x6C;

Modified: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/AddRequest.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/AddRequest.java?rev=226692&r1=226691&r2=226692&view=diff
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/AddRequest.java
(original)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/AddRequest.java
Sun Jul 31 13:31:08 2005
@@ -17,15 +17,22 @@
 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.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;
@@ -68,6 +75,21 @@
     /** The current attribute being decoded */
     private transient Attribute currentAttribute;
 
+    /** The current attribute being decoded */
+    private transient Attribute currentAttributeValue;
+    
+    /** The add request length */
+    private transient int addRequestLength;
+    
+    /** The attributes length */
+    private transient int attributesLength;
+    
+    /** The list of all attributes length */
+    private transient List attributeLength;
+    
+    /** The list of all vals length */
+    private transient List valuesLength;
+
     //~ Constructors -------------------------------------------------------------------------------
 
     /**
@@ -181,27 +203,28 @@
      */
     public int computeLength()
     {
-        int addRequestLength = 0;
-        
         // The entry
-        addRequestLength += 1 + Length.getNbBytes( entry.getLength() ) + entry.getLength();
+        addRequestLength = 1 + Length.getNbBytes( entry.getLength() ) + entry.getLength();
         
         // The attributes sequence
-        int attributesLength = 0;
+        attributesLength = 0;
         
         if ( ( attributes != null ) && ( attributes.size() != 0 ) )
         {
             Iterator attributeIterator = attributes.iterator();
+            attributeLength = new LinkedList();
+            valuesLength = new LinkedList();
             
             // Compute the attributes length
             while ( attributeIterator.hasNext() )
             {
                 Attribute attribute = (Attribute)attributeIterator.next();
-                int attributeLength = 0;
+                int localAttributeLength = 0;
+                int localValuesLength = 0;
                 
                 // Get the type length
                 int idLength = attribute.getID().getBytes().length;
-                attributeLength = 1 + Length.getNbBytes( idLength ) + idLength;
+                localAttributeLength = 1 + Length.getNbBytes( idLength ) + idLength;
                 
                 // The values
                 try
@@ -210,16 +233,16 @@
 	                
 	                if ( values.hasMoreElements() )
 	                {
-	                    int valuesLength = 0;
+                        localValuesLength = 0;
 	                    
 		                while ( values.hasMoreElements() )
 		                {
 		                    OctetString value = (OctetString)values.next();
 		                    
-		                    valuesLength += 1 + Length.getNbBytes( value.getLength() ) + value.getLength();
+                            localValuesLength += 1 + Length.getNbBytes( value.getLength()
) + value.getLength();
 		                }
 
-	                    attributeLength += 1 + Length.getNbBytes( valuesLength ) + valuesLength;

+                        localAttributeLength += 1 + Length.getNbBytes( localValuesLength
) + localValuesLength; 
 	                }
 	                
                 }
@@ -229,7 +252,10 @@
                 }
                 
                 // add the attribute length to the attributes length
-                attributesLength += 1 + Length.getNbBytes( attributeLength ) + attributeLength;
+                attributesLength += 1 + Length.getNbBytes( localAttributeLength ) + localAttributeLength;
+                
+                attributeLength.add( new Integer( localAttributeLength ) );
+                valuesLength.add( new Integer( localValuesLength ) );
             }
         }
         
@@ -239,6 +265,108 @@
         return 1 + Length.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
+     * @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.ADD_REQUEST_TAG );
+            buffer.put( Length.getBytes( addRequestLength ) ) ;
+            
+            // The entry
+            Value.encode( buffer, entry );
+            
+            // The attributes sequence
+            buffer.put( LdapConstants.SEQUENCE_TAG );
+            buffer.put( Length.getBytes( attributesLength ) ) ;
+
+            // The partial attribute list
+            if ( ( attributes != null ) && ( attributes.size() != 0 ) )
+            {
+                Iterator attributeIterator = attributes.iterator();
+                int attributeNumber = 0;
+                
+                // Compute the attributes length
+                while ( attributeIterator.hasNext() )
+                {
+                    Attribute attribute = (Attribute)attributeIterator.next();
+                    
+                    // The attributes list sequence
+                    buffer.put( LdapConstants.SEQUENCE_TAG );
+                    int localAttributeLength = ( (Integer)attributeLength.get( attributeNumber
) ).intValue();
+                    buffer.put( Length.getBytes( localAttributeLength ) );
+
+                    // The attribute type
+                    Value.encode( buffer, attribute.getID() );
+                    
+                    // The values
+                    buffer.put( LdapConstants.SET_TAG );
+                    int localValuesLength = ( (Integer)valuesLength.get( attributeNumber
) ).intValue();
+                    buffer.put( Length.getBytes( localValuesLength ) );
+                    
+                    try
+                    {
+                        NamingEnumeration values = attribute.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 attribute number;
+                    attributeNumber++;
+                }
+            }
+        }
+        catch ( BufferOverflowException boe )
+        {
+            throw new EncoderException("The PDU buffer size is too small !"); 
+        }
+
+        return buffer;
+    }
+
     /**
      * Return a String representing an AddRequest
      * 

Modified: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/SearchResultEntry.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/SearchResultEntry.java?rev=226692&r1=226691&r2=226692&view=diff
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/SearchResultEntry.java
(original)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/SearchResultEntry.java
Sun Jul 31 13:31:08 2005
@@ -224,8 +224,6 @@
      */
     public int computeLength()
     {
-        searchResultEntryLength = 0;
-        
         // The entry
         searchResultEntryLength = 1 + Length.getNbBytes( objectName.getLength() ) + objectName.getLength();
         

Modified: directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/codec/AddRequestTest.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/codec/AddRequestTest.java?rev=226692&r1=226691&r2=226692&view=diff
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/codec/AddRequestTest.java
(original)
+++ directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/codec/AddRequestTest.java
Sun Jul 31 13:31:08 2005
@@ -26,11 +26,13 @@
 import javax.naming.directory.BasicAttribute;
 
 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.AddRequest;
 import org.apache.asn1.ldap.pojo.LdapMessage;
 import org.apache.asn1.primitives.OctetString;
+import org.apache.asn1.util.StringUtils;
 import org.apache.log4j.Logger;
 import org.apache.log4j.PropertyConfigurator;
 
@@ -85,6 +87,7 @@
                 0x04, 0x05, 't', 'e', 's', 't', '3',
             } );
 
+        String decodedPdu = StringUtils.dumpBytes( stream.array() );
         stream.flip();
 
         // Allocate a LdapMessage Container
@@ -164,5 +167,19 @@
         // Check the length
         Assert.assertEquals(0x5B, 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() );
+        }
     }
 }



Mime
View raw message