directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r985778 - in /directory/shared/branches/shared-codec-merge/ldap/src: main/java/org/apache/directory/shared/ldap/message/ test/java/org/apache/directory/shared/ldap/codec/add/
Date Sun, 15 Aug 2010 23:29:17 GMT
Author: elecharny
Date: Sun Aug 15 23:29:16 2010
New Revision: 985778

URL: http://svn.apache.org/viewvc?rev=985778&view=rev
Log:
o Merged all the AddRequestXXX methods

Modified:
    directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/message/AddRequestImpl.java
    directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/message/AddResponseImpl.java
    directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/message/LdapProtocolEncoder.java
    directory/shared/branches/shared-codec-merge/ldap/src/test/java/org/apache/directory/shared/ldap/codec/add/AddRequestTest.java

Modified: directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/message/AddRequestImpl.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/message/AddRequestImpl.java?rev=985778&r1=985777&r2=985778&view=diff
==============================================================================
--- directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/message/AddRequestImpl.java
(original)
+++ directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/message/AddRequestImpl.java
Sun Aug 15 23:29:16 2010
@@ -20,14 +20,16 @@
 package org.apache.directory.shared.ldap.message;
 
 
+import java.util.List;
+
 import org.apache.directory.shared.ldap.codec.MessageTypeEnum;
 import org.apache.directory.shared.ldap.entry.DefaultEntry;
 import org.apache.directory.shared.ldap.entry.DefaultEntryAttribute;
 import org.apache.directory.shared.ldap.entry.Entry;
 import org.apache.directory.shared.ldap.entry.EntryAttribute;
 import org.apache.directory.shared.ldap.exception.LdapException;
-import org.apache.directory.shared.ldap.message.internal.InternalAddRequest;
 import org.apache.directory.shared.ldap.message.internal.AddResponse;
+import org.apache.directory.shared.ldap.message.internal.InternalAddRequest;
 import org.apache.directory.shared.ldap.message.internal.ResultResponse;
 import org.apache.directory.shared.ldap.name.DN;
 
@@ -49,6 +51,18 @@ public class AddRequestImpl extends Abst
     /** The current attribute being decoded */
     private EntryAttribute currentAttribute;
 
+    /** The add request length */
+    private int addRequestLength;
+
+    /** The Entry length */
+    private int entryLength;
+
+    /** The list of all attributes length */
+    private List<Integer> attributesLength;
+
+    /** The list of all vals length */
+    private List<Integer> valuesLength;
+
 
     // ------------------------------------------------------------------------
     // Constructors
@@ -56,9 +70,18 @@ public class AddRequestImpl extends Abst
 
     /**
      * Creates an AddRequest implementation to create a new entry.
+     */
+    public AddRequestImpl()
+    {
+        super( -1, TYPE );
+        entry = new DefaultEntry();
+    }
+
+
+    /**
+     * Creates an AddRequest implementation to create a new entry.
      * 
-     * @param id
-     *            the sequence identifier of the AddRequest message.
+     * @param id the sequence identifier of the AddRequest message.
      */
     public AddRequestImpl( final int id )
     {
@@ -210,6 +233,82 @@ public class AddRequestImpl extends Abst
 
 
     /**
+     * Stores the encoded length for the AddRequest
+     * @param addRequestLength The encoded length
+     */
+    /* No qualifier*/void setAddRequestLength( int addRequestLength )
+    {
+        this.addRequestLength = addRequestLength;
+    }
+
+
+    /**
+     * @return The encoded AddRequest's length
+     */
+    /* No qualifier */int getAddRequestLength()
+    {
+        return addRequestLength;
+    }
+
+
+    /**
+     * Stores the encoded length for the Entry
+     * @param entryLength The encoded length
+     */
+    /* No qualifier*/void setEntryLength( int entryLength )
+    {
+        this.entryLength = entryLength;
+    }
+
+
+    /**
+     * @return The encoded Entry's length
+     */
+    /* No qualifier */int getEntryLength()
+    {
+        return entryLength;
+    }
+
+
+    /**
+     * Stores the encoded length for the attributes
+     * @param atributesLength The encoded length
+     */
+    /* No qualifier*/void setAttributesLength( List<Integer> attributesLength )
+    {
+        this.attributesLength = attributesLength;
+    }
+
+
+    /**
+     * @return The encoded values length
+     */
+    /* No qualifier */List<Integer> getAttributesLength()
+    {
+        return attributesLength;
+    }
+
+
+    /**
+     * Stores the encoded length for the values
+     * @param valuesLength The encoded length
+     */
+    /* No qualifier*/void setValuesLength( List<Integer> valuesLength )
+    {
+        this.valuesLength = valuesLength;
+    }
+
+
+    /**
+     * @return The encoded values length
+     */
+    /* No qualifier */List<Integer> getValuesLength()
+    {
+        return valuesLength;
+    }
+
+
+    /**
      * Checks to see if an object is equivalent to this AddRequest. First
      * there's a quick test to see if the obj is the same object as this one -
      * if so true is returned. Next if the super method fails false is returned.
@@ -218,8 +317,7 @@ public class AddRequestImpl extends Abst
      * not the same false is returned otherwise the method exists returning
      * true.
      * 
-     * @param obj
-     *            the object to test for equality to this
+     * @param obj the object to test for equality to this
      * @return true if the obj is equal to this AddRequest, false otherwise
      */
     public boolean equals( Object obj )

Modified: directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/message/AddResponseImpl.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/message/AddResponseImpl.java?rev=985778&r1=985777&r2=985778&view=diff
==============================================================================
--- directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/message/AddResponseImpl.java
(original)
+++ directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/message/AddResponseImpl.java
Sun Aug 15 23:29:16 2010
@@ -20,8 +20,8 @@
 package org.apache.directory.shared.ldap.message;
 
 
-import org.apache.directory.shared.ldap.message.internal.InternalAbstractResultResponse;
 import org.apache.directory.shared.ldap.message.internal.AddResponse;
+import org.apache.directory.shared.ldap.message.internal.InternalAbstractResultResponse;
 
 
 /**
@@ -78,7 +78,6 @@ public class AddResponseImpl extends Int
      */
     public String toString()
     {
-
         StringBuilder sb = new StringBuilder();
 
         sb.append( "    Add Response\n" );

Modified: directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/message/LdapProtocolEncoder.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/message/LdapProtocolEncoder.java?rev=985778&r1=985777&r2=985778&view=diff
==============================================================================
--- directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/message/LdapProtocolEncoder.java
(original)
+++ directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/message/LdapProtocolEncoder.java
Sun Aug 15 23:29:16 2010
@@ -47,6 +47,7 @@ import org.apache.directory.shared.ldap.
 import org.apache.directory.shared.ldap.message.internal.DeleteResponse;
 import org.apache.directory.shared.ldap.message.internal.ExtendedResponse;
 import org.apache.directory.shared.ldap.message.internal.InternalAbandonRequest;
+import org.apache.directory.shared.ldap.message.internal.InternalAddRequest;
 import org.apache.directory.shared.ldap.message.internal.InternalBindRequest;
 import org.apache.directory.shared.ldap.message.internal.InternalDeleteRequest;
 import org.apache.directory.shared.ldap.message.internal.InternalIntermediateResponse;
@@ -127,7 +128,7 @@ public class LdapProtocolEncoder extends
             || ( message instanceof SearchResultDone ) || ( message instanceof SearchResultEntry
)
             || ( message instanceof SearchResultReference ) || ( message instanceof InternalAbandonRequest
)
             || ( message instanceof InternalDeleteRequest ) || ( message instanceof InternalUnbindRequest
)
-            || ( message instanceof InternalBindRequest ) )
+            || ( message instanceof InternalBindRequest ) || ( message instanceof InternalAddRequest
) )
         {
             try
             {
@@ -410,6 +411,109 @@ public class LdapProtocolEncoder extends
 
 
     /**
+     * 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
+     */
+    private int computeAddRequestLength( AddRequestImpl addRequest )
+    {
+        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.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 );
+            }
+
+            addRequest.setAttributesLength( attributesLength );
+            addRequest.setValuesLength( valuesLength );
+            addRequest.setEntryLength( entryLength );
+        }
+
+        addRequestLength += 1 + TLV.getNbBytes( entryLength ) + entryLength;
+        addRequest.setAddRequestLength( addRequestLength );
+
+        // Return the result.
+        return 1 + TLV.getNbBytes( addRequestLength ) + addRequestLength;
+    }
+
+
+    /**
      * Compute the AddResponse length 
      * 
      * AddResponse : 
@@ -996,6 +1100,95 @@ public class LdapProtocolEncoder extends
 
 
     /**
+     * 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
+     */
+    protected void encodeAddRequest( ByteBuffer buffer, AddRequestImpl addRequest ) throws
EncoderException
+    {
+        try
+        {
+            // The AddRequest Tag
+            buffer.put( LdapConstants.ADD_REQUEST_TAG );
+            buffer.put( TLV.getBytes( addRequest.getAddRequestLength() ) );
+
+            // The entry
+            Value.encode( buffer, DN.getBytes( addRequest.getEntryDn() ) );
+
+            // The attributes sequence
+            buffer.put( UniversalTag.SEQUENCE_TAG );
+            buffer.put( TLV.getBytes( addRequest.getEntryLength() ) );
+
+            // The partial attribute list
+            Entry entry = addRequest.getEntry();
+
+            if ( entry.size() != 0 )
+            {
+                int attributeNumber = 0;
+
+                // Compute the attributes length
+                for ( EntryAttribute attribute : entry )
+                {
+                    // The attributes list sequence
+                    buffer.put( UniversalTag.SEQUENCE_TAG );
+                    int localAttributeLength = addRequest.getAttributesLength().get( attributeNumber
);
+                    buffer.put( TLV.getBytes( localAttributeLength ) );
+
+                    // The attribute type
+                    Value.encode( buffer, attribute.getId() );
+
+                    // The values
+                    buffer.put( UniversalTag.SET_TAG );
+                    int localValuesLength = addRequest.getValuesLength().get( attributeNumber
);
+                    buffer.put( TLV.getBytes( localValuesLength ) );
+
+                    if ( attribute.size() != 0 )
+                    {
+                        for ( org.apache.directory.shared.ldap.entry.Value<?> value
: attribute )
+                        {
+                            if ( value.isBinary() )
+                            {
+                                Value.encode( buffer, value.getBytes() );
+                            }
+                            else
+                            {
+                                Value.encode( buffer, value.getString() );
+                            }
+                        }
+                    }
+
+                    // Go to the next attribute number;
+                    attributeNumber++;
+                }
+            }
+        }
+        catch ( BufferOverflowException boe )
+        {
+            throw new EncoderException( "The PDU buffer size is too small !" );
+        }
+    }
+
+
+    /**
      * Encode the AddResponse message to a PDU.
      * 
      * @param buffer The buffer where to put the PDU
@@ -1573,6 +1766,9 @@ public class LdapProtocolEncoder extends
             case ABANDON_REQUEST:
                 return computeAbandonRequestLength( ( AbandonRequestImpl ) message );
 
+            case ADD_REQUEST:
+                return computeAddRequestLength( ( AddRequestImpl ) message );
+
             case ADD_RESPONSE:
                 return computeAddResponseLength( ( AddResponseImpl ) message );
 
@@ -1629,6 +1825,10 @@ public class LdapProtocolEncoder extends
                 encodeAbandonRequest( bb, ( AbandonRequestImpl ) message );
                 break;
 
+            case ADD_REQUEST:
+                encodeAddRequest( bb, ( AddRequestImpl ) message );
+                break;
+
             case ADD_RESPONSE:
                 encodeAddResponse( bb, ( AddResponseImpl ) message );
                 break;

Modified: directory/shared/branches/shared-codec-merge/ldap/src/test/java/org/apache/directory/shared/ldap/codec/add/AddRequestTest.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-codec-merge/ldap/src/test/java/org/apache/directory/shared/ldap/codec/add/AddRequestTest.java?rev=985778&r1=985777&r2=985778&view=diff
==============================================================================
--- directory/shared/branches/shared-codec-merge/ldap/src/test/java/org/apache/directory/shared/ldap/codec/add/AddRequestTest.java
(original)
+++ directory/shared/branches/shared-codec-merge/ldap/src/test/java/org/apache/directory/shared/ldap/codec/add/AddRequestTest.java
Sun Aug 15 23:29:16 2010
@@ -38,13 +38,13 @@ import org.apache.directory.shared.asn1.
 import org.apache.directory.shared.asn1.ber.IAsn1Container;
 import org.apache.directory.shared.asn1.codec.DecoderException;
 import org.apache.directory.shared.asn1.codec.EncoderException;
-import org.apache.directory.shared.ldap.codec.LdapMessageCodec;
 import org.apache.directory.shared.ldap.codec.LdapMessageContainer;
 import org.apache.directory.shared.ldap.codec.ResponseCarryingException;
 import org.apache.directory.shared.ldap.entry.Entry;
 import org.apache.directory.shared.ldap.entry.EntryAttribute;
 import org.apache.directory.shared.ldap.entry.Value;
 import org.apache.directory.shared.ldap.message.AddResponseImpl;
+import org.apache.directory.shared.ldap.message.LdapProtocolEncoder;
 import org.apache.directory.shared.ldap.message.ResultCodeEnum;
 import org.apache.directory.shared.ldap.message.control.Control;
 import org.apache.directory.shared.ldap.message.internal.InternalAddRequest;
@@ -63,6 +63,10 @@ import org.junit.runner.RunWith;
 @Concurrent()
 public class AddRequestTest
 {
+    /** The encoder instance */
+    LdapProtocolEncoder encoder = new LdapProtocolEncoder();
+
+
     /**
      * Test the decoding of a AddRequest
      */
@@ -110,6 +114,7 @@ public class AddRequestTest
                 0x04, 0x05, 't', 'e', 's', 't', '1', 0x04, 0x05, 't', 'e', 's', 't', '2',
0x04, 0x05, 't', 'e', 's',
                 't', '3', } );
 
+        String decodedPdu = StringTools.dumpBytes( stream.array() );
         stream.flip();
 
         // Allocate a LdapMessage Container
@@ -179,12 +184,34 @@ public class AddRequestTest
             vals.remove( value.get() );
         }
 
-        // Check the length
-        AddRequestCodec addRequestCodec = new AddRequestCodec();
-        addRequestCodec.setEntry( entry );
-        addRequestCodec.setMessageId( addRequest.getMessageId() );
+        // Check the encoding
+        try
+        {
+            ByteBuffer bb = encoder.encodeMessage( addRequest );
 
-        assertEquals( 0x59, addRequestCodec.computeLength() );
+            // Check the length
+            assertEquals( 0x59, bb.limit() );
+
+            // We cannot compare the PDU, as the attributes order is not
+            // kept. Let's decode again and compare the resulting AddRequest
+            try
+            {
+                ldapDecoder.decode( bb, ldapMessageContainer );
+            }
+            catch ( DecoderException de )
+            {
+                de.printStackTrace();
+                fail( de.getMessage() );
+            }
+
+            InternalAddRequest addRequest2 = ( ( LdapMessageContainer ) ldapMessageContainer
).getInternalAddRequest();
+            assertEquals( addRequest, addRequest2 );
+        }
+        catch ( EncoderException ee )
+        {
+            ee.printStackTrace();
+            fail( ee.getMessage() );
+        }
     }
 
 
@@ -662,17 +689,13 @@ public class AddRequestTest
             assertEquals( "", value.getString() );
         }
 
-        // Check the length
-        AddRequestCodec addRequestCodec = new AddRequestCodec();
-        addRequestCodec.setEntry( entry );
-        addRequestCodec.setMessageId( addRequest.getMessageId() );
-
-        assertEquals( 0x34, addRequestCodec.computeLength() );
-
         // Check the encoding
         try
         {
-            ByteBuffer bb = addRequestCodec.encode();
+            ByteBuffer bb = encoder.encodeMessage( addRequest );
+
+            // Check the length
+            assertEquals( 0x34, bb.limit() );
 
             String encodedPdu = StringTools.dumpBytes( bb.array() );
 
@@ -769,18 +792,13 @@ public class AddRequestTest
         assertEquals( "2.16.840.1.113730.3.4.2", control.getOid() );
         assertEquals( "", StringTools.dumpBytes( ( byte[] ) control.getValue() ) );
 
-        // Check the length
-        AddRequestCodec addRequestCodec = new AddRequestCodec();
-        addRequestCodec.setEntry( entry );
-        addRequestCodec.setMessageId( addRequest.getMessageId() );
-        ( ( LdapMessageCodec ) addRequestCodec ).addControl( control );
-
-        assertEquals( 0x51, addRequestCodec.computeLength() );
-
         // Check the encoding
         try
         {
-            ByteBuffer bb = addRequestCodec.encode();
+            ByteBuffer bb = encoder.encodeMessage( addRequest );
+
+            // Check the length
+            assertEquals( 0x51, bb.limit() );
 
             String encodedPdu = StringTools.dumpBytes( bb.array() );
 



Mime
View raw message