directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r984358 - in /directory/shared/branches/shared-codec-merge/ldap/src: main/java/org/apache/directory/shared/ldap/codec/ main/java/org/apache/directory/shared/ldap/message/ main/java/org/apache/directory/shared/ldap/message/internal/ test/jav...
Date Wed, 11 Aug 2010 10:11:29 GMT
Author: elecharny
Date: Wed Aug 11 10:11:29 2010
New Revision: 984358

URL: http://svn.apache.org/viewvc?rev=984358&view=rev
Log:
Added some missing parts in the Ldap encoder : mainly the LdapUrl encoding, plus some minor
bugs fixed

Modified:
    directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/codec/LdapProtocolEncoder.java
    directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/message/LdapResultImpl.java
    directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/message/ReferralImpl.java
    directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/message/internal/InternalLdapResult.java
    directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/message/internal/InternalReferral.java
    directory/shared/branches/shared-codec-merge/ldap/src/test/java/org/apache/directory/shared/ldap/message/LdapResultImplTest.java
    directory/shared/branches/shared-codec-merge/ldap/src/test/java/org/apache/directory/shared/ldap/message/ReferralImplTest.java

Modified: directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/codec/LdapProtocolEncoder.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/codec/LdapProtocolEncoder.java?rev=984358&r1=984357&r2=984358&view=diff
==============================================================================
--- directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/codec/LdapProtocolEncoder.java
(original)
+++ directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/codec/LdapProtocolEncoder.java
Wed Aug 11 10:11:29 2010
@@ -81,27 +81,45 @@ public class LdapProtocolEncoder extends
     }
 
 
+    /**
+     * Generate the PDU which contains the encoded object. 
+     * 
+     * The generation is done in two phases : 
+     * - first, we compute the length of each part and the
+     * global PDU length 
+     * - second, we produce the PDU. 
+     * 
+     * <pre>
+     * 0x30 L1 
+     *   | 
+     *   +--> 0x02 L2 MessageId  
+     *   +--> ProtocolOp 
+     *   +--> Controls 
+     *   
+     * L2 = Length(MessageId)
+     * L1 = Length(0x02) + Length(L2) + L2 + Length(ProtocolOp) + Length(Controls)
+     * LdapMessageLength = Length(0x30) + Length(L1) + L1
+     * </pre>
+     * 
+     * @param message The message to encode
+     * @return A ByteBuffer that contains the PDU
+     * @throws EncoderException If anything goes wrong.
+     */
     private ByteBuffer encodeMessage( InternalMessage message ) throws EncoderException
     {
-        int length = computeLength( message );
-
-        return encodeMessage( length, message );
-    }
-
+        int length = computeMessageLength( message );
 
-    private ByteBuffer encodeMessage( int length, InternalMessage message ) throws EncoderException
-    {
         try
         {
-            ByteBuffer bb = ByteBuffer.allocate( length );
+            ByteBuffer buffer = ByteBuffer.allocate( length );
 
             try
             {
                 // The LdapMessage Sequence
-                bb.put( UniversalTag.SEQUENCE_TAG );
+                buffer.put( UniversalTag.SEQUENCE_TAG );
 
                 // The length has been calculated by the computeLength method
-                bb.put( TLV.getBytes( message.getMessageLength() ) );
+                buffer.put( TLV.getBytes( message.getMessageLength() ) );
             }
             catch ( BufferOverflowException boe )
             {
@@ -109,10 +127,10 @@ public class LdapProtocolEncoder extends
             }
 
             // The message Id
-            Value.encode( bb, message.getMessageId() );
+            Value.encode( buffer, message.getMessageId() );
 
             // Add the protocolOp part
-            encodeProtocolOp( bb, message );
+            encodeProtocolOp( buffer, message );
 
             // Do the same thing for Controls, if any.
             Map<String, Control> controls = message.getControls();
@@ -120,17 +138,17 @@ public class LdapProtocolEncoder extends
             if ( ( controls != null ) && ( controls.size() > 0 ) )
             {
                 // Encode the controls
-                bb.put( ( byte ) LdapConstants.CONTROLS_TAG );
-                bb.put( TLV.getBytes( message.getControlsLength() ) );
+                buffer.put( ( byte ) LdapConstants.CONTROLS_TAG );
+                buffer.put( TLV.getBytes( message.getControlsLength() ) );
 
                 // Encode each control
                 for ( Control control : controls.values() )
                 {
-                    ( ( CodecControl ) control ).encode( bb );
+                    ( ( CodecControl ) control ).encode( buffer );
                 }
             }
 
-            return bb;
+            return buffer;
         }
         catch ( EncoderException ee )
         {
@@ -153,7 +171,7 @@ public class LdapProtocolEncoder extends
      * L1 = length(ProtocolOp) 
      * LdapMessage length = Length(0x30) + Length(L1) + MessageId length + L1
      */
-    private int computeLength( InternalMessage message )
+    private int computeMessageLength( InternalMessage message )
     {
         // The length of the MessageId. It's the sum of
         // - the tag (0x02), 1 byte
@@ -162,7 +180,7 @@ public class LdapProtocolEncoder extends
         int ldapMessageLength = 1 + 1 + Value.getNbBytes( message.getMessageId() );
 
         // Get the protocolOp length
-        ldapMessageLength += computeLengthProtocolOp( message );
+        ldapMessageLength += computeProtocolOpLength( message );
         message.setMessageLength( ldapMessageLength );
 
         Map<String, Control> controls = message.getControls();
@@ -236,7 +254,7 @@ public class LdapProtocolEncoder extends
      *      Length(0x01) + 1 + Length(0x04) + Length(L1) + L1 + Length(0x04) +
      *      Length(L2) + L2 + Length(0x83) + Length(L3) + L3
      */
-    public int computeLength( InternalLdapResult ldapResult )
+    private int computeLdapResultLength( InternalLdapResult ldapResult )
     {
         int ldapResultLength = 0;
 
@@ -258,6 +276,7 @@ public class LdapProtocolEncoder extends
         // The errorMessage length
         byte[] errorMessageBytes = StringTools.getBytesUtf8( ldapResult.getErrorMessage()
);
         ldapResultLength += 1 + TLV.getNbBytes( errorMessageBytes.length ) + errorMessageBytes.length;
+        ldapResult.setErrorMessageBytes( errorMessageBytes );
 
         InternalReferral referral = ldapResult.getReferral();
 
@@ -274,6 +293,7 @@ public class LdapProtocolEncoder extends
                 {
                     byte[] ldapUrlBytes = StringTools.getBytesUtf8( ldapUrl );
                     referralsLength += 1 + TLV.getNbBytes( ldapUrlBytes.length ) + ldapUrlBytes.length;
+                    referral.addLdapUrlBytes( ldapUrlBytes );
                 }
 
                 // The referrals
@@ -286,28 +306,12 @@ public class LdapProtocolEncoder extends
 
 
     /**
-     * Compute the LdapResponse length
-     */
-    private int computeLdapResultLength( InternalLdapResult ldapResult )
-    {
-        int ldapResponseLength = 0;
-
-        if ( ldapResult != null )
-        {
-            ldapResponseLength = computeLength( ldapResult );
-        }
-
-        return ldapResponseLength;
-    }
-
-
-    /**
      * Encode the LdapResult message to a PDU.
      * 
      * @param buffer The buffer where to put the PDU
      * @return The PDU.
      */
-    public ByteBuffer encodeLdapResult( ByteBuffer buffer, InternalLdapResult ldapResult
) throws EncoderException
+    private ByteBuffer encodeLdapResult( ByteBuffer buffer, InternalLdapResult ldapResult
) throws EncoderException
     {
         if ( buffer == null )
         {
@@ -454,7 +458,7 @@ public class LdapProtocolEncoder extends
      *      Length(0x04) + Length(L2) + L2 + Length(authentication)
      * </pre>
      */
-    private int computeLengthProtocolOp( InternalMessage message )
+    private int computeProtocolOpLength( InternalMessage message )
     {
         switch ( message.getType() )
         {

Modified: directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/message/LdapResultImpl.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/message/LdapResultImpl.java?rev=984358&r1=984357&r2=984358&view=diff
==============================================================================
--- directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/message/LdapResultImpl.java
(original)
+++ directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/message/LdapResultImpl.java
Wed Aug 11 10:11:29 2010
@@ -95,7 +95,7 @@ public class LdapResultImpl implements I
     /**
      * {@inheritDoc}
      */
-    public void setErrorMessage( byte[] errorMessageBytes )
+    public void setErrorMessageBytes( byte[] errorMessageBytes )
     {
         this.errorMessageBytes = errorMessageBytes;
     }

Modified: directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/message/ReferralImpl.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/message/ReferralImpl.java?rev=984358&r1=984357&r2=984358&view=diff
==============================================================================
--- directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/message/ReferralImpl.java
(original)
+++ directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/message/ReferralImpl.java
Wed Aug 11 10:11:29 2010
@@ -43,6 +43,9 @@ public class ReferralImpl implements Int
     /** Sequence of LDAPUrls composing this Referral */
     private final List<String> urls = new ArrayList<String>();
 
+    /** The encoded LdapURL */
+    private final List<byte[]> urlsBytes = new ArrayList<byte[]>();
+
 
     // ------------------------------------------------------------------------
     // LdapResult Interface Method Implementations
@@ -60,10 +63,18 @@ public class ReferralImpl implements Int
 
 
     /**
+     * {@inheritDoc}
+     */
+    public Collection<byte[]> getLdapUrlsBytes()
+    {
+        return urlsBytes;
+    }
+
+
+    /**
      * Adds an LDAPv3 URL to this Referral.
      * 
-     * @param url
-     *            the LDAPv3 URL to add
+     * @param url the LDAPv3 URL to add
      */
     public void addLdapUrl( String url )
     {
@@ -72,6 +83,15 @@ public class ReferralImpl implements Int
 
 
     /**
+     * {@inheritDoc}
+     */
+    public void addLdapUrlBytes( byte[] urlBytes )
+    {
+        urlsBytes.add( urlBytes );
+    }
+
+
+    /**
      * Removes an LDAPv3 URL to this Referral.
      * 
      * @param url

Modified: directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/message/internal/InternalLdapResult.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/message/internal/InternalLdapResult.java?rev=984358&r1=984357&r2=984358&view=diff
==============================================================================
--- directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/message/internal/InternalLdapResult.java
(original)
+++ directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/message/internal/InternalLdapResult.java
Wed Aug 11 10:11:29 2010
@@ -130,7 +130,7 @@ public interface InternalLdapResult
      * Set the encoded message's bytes
      * @param errorMessageBytes The encoded bytes
      */
-    void setErrorMessage( byte[] errorMessageBytes );
+    void setErrorMessageBytes( byte[] errorMessageBytes );
 
 
     /**

Modified: directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/message/internal/InternalReferral.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/message/internal/InternalReferral.java?rev=984358&r1=984357&r2=984358&view=diff
==============================================================================
--- directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/message/internal/InternalReferral.java
(original)
+++ directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/message/internal/InternalReferral.java
Wed Aug 11 10:11:29 2010
@@ -96,6 +96,14 @@ public interface InternalReferral
 
 
     /**
+     * Gets an unmodifiable set of encoded referral urls.
+     * 
+     * @return the encoded url objects.
+     */
+    Collection<byte[]> getLdapUrlsBytes();
+
+
+    /**
      * Adds an LDAPv3 URL to this Referral.
      * 
      * @param url
@@ -105,6 +113,14 @@ public interface InternalReferral
 
 
     /**
+     * Adds an encoded LDAPv3 URL to this Referral.
+     * 
+     * @param urlBytes the encoded LDAPv3 URL to add
+     */
+    void addLdapUrlBytes( byte[] urlBytes );
+
+
+    /**
      * Removes an LDAPv3 URL to this Referral.
      * 
      * @param url

Modified: directory/shared/branches/shared-codec-merge/ldap/src/test/java/org/apache/directory/shared/ldap/message/LdapResultImplTest.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-codec-merge/ldap/src/test/java/org/apache/directory/shared/ldap/message/LdapResultImplTest.java?rev=984358&r1=984357&r2=984358&view=diff
==============================================================================
--- directory/shared/branches/shared-codec-merge/ldap/src/test/java/org/apache/directory/shared/ldap/message/LdapResultImplTest.java
(original)
+++ directory/shared/branches/shared-codec-merge/ldap/src/test/java/org/apache/directory/shared/ldap/message/LdapResultImplTest.java
Wed Aug 11 10:11:29 2010
@@ -160,7 +160,7 @@ public class LdapResultImplTest
             }
 
 
-            public void setErrorMessage( byte[] errorMessageBytes )
+            public void setErrorMessageBytes( byte[] errorMessageBytes )
             {
             }
         };

Modified: directory/shared/branches/shared-codec-merge/ldap/src/test/java/org/apache/directory/shared/ldap/message/ReferralImplTest.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-codec-merge/ldap/src/test/java/org/apache/directory/shared/ldap/message/ReferralImplTest.java?rev=984358&r1=984357&r2=984358&view=diff
==============================================================================
--- directory/shared/branches/shared-codec-merge/ldap/src/test/java/org/apache/directory/shared/ldap/message/ReferralImplTest.java
(original)
+++ directory/shared/branches/shared-codec-merge/ldap/src/test/java/org/apache/directory/shared/ldap/message/ReferralImplTest.java
Wed Aug 11 10:11:29 2010
@@ -20,19 +20,18 @@
 package org.apache.directory.shared.ldap.message;
 
 
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
 import java.util.Collection;
 import java.util.Collections;
 
 import org.apache.directory.junit.tools.Concurrent;
 import org.apache.directory.junit.tools.ConcurrentJunitRunner;
-import org.apache.directory.shared.ldap.message.ReferralImpl;
 import org.apache.directory.shared.ldap.message.internal.InternalReferral;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-
 
 /**
  * Tests the ReferralImpl class.
@@ -210,6 +209,17 @@ public class ReferralImplTest
             public void removeLdapUrl( String url )
             {
             }
+
+
+            public void addLdapUrlBytes( byte[] urlBytes )
+            {
+            }
+
+
+            public Collection<byte[]> getLdapUrlsBytes()
+            {
+                return null;
+            }
         };
 
         ReferralImpl refs1 = new ReferralImpl();



Mime
View raw message