directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r226617 - in /directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo: AbandonRequest.java AddResponse.java LdapMessage.java LdapResponse.java LdapResult.java UnBindRequest.java
Date Sun, 31 Jul 2005 08:07:25 GMT
Author: elecharny
Date: Sun Jul 31 01:07:19 2005
New Revision: 226617

URL: http://svn.apache.org/viewcvs?rev=226617&view=rev
Log:
- Added a try/catch when encoding to avoid a ByteBuffer exception to be thrown
- Implemented LdapResult, LdapResponse and AddResponse encoding
- replaced the encoding of Strings and Integer by a call to Value.encode()

Modified:
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/AbandonRequest.java
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/AddResponse.java
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/LdapMessage.java
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/LdapResponse.java
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/LdapResult.java
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/UnBindRequest.java

Modified: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/AbandonRequest.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/AbandonRequest.java?rev=226617&r1=226616&r2=226617&view=diff
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/AbandonRequest.java
(original)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/AbandonRequest.java
Sun Jul 31 01:07:19 2005
@@ -16,6 +16,7 @@
  */
 package org.apache.asn1.ldap.pojo;
 
+import java.nio.BufferOverflowException;
 import java.nio.ByteBuffer;
 
 import org.apache.asn1.EncoderException;
@@ -96,15 +97,22 @@
             throw new EncoderException("Cannot put a PDU in a null buffer !");
         }
         
-        // The tag
-        buffer.put(LdapConstants.ABANDON_REQUEST_TAG);
-        
-        // The length. It has to be evaluated depending on 
-        // the messageId value.
-        buffer.put( (byte)Value.getNbBytes( messageId ) );
-        
-        // The messageId
-        buffer.put( Value.getBytes( messageId ) );
+        try 
+        {
+            // The tag
+            buffer.put(LdapConstants.ABANDON_REQUEST_TAG);
+            
+            // The length. It has to be evaluated depending on 
+            // the messageId value.
+            buffer.put( (byte)Value.getNbBytes( messageId ) );
+            
+            // The messageId
+            buffer.put( Value.getBytes( messageId ) );
+        }
+        catch ( BufferOverflowException boe )
+        {
+            throw new EncoderException("The PDU buffer size is too small !"); 
+        }
         
         return buffer;
     }

Modified: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/AddResponse.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/AddResponse.java?rev=226617&r1=226616&r2=226617&view=diff
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/AddResponse.java
(original)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/AddResponse.java
Sun Jul 31 01:07:19 2005
@@ -16,7 +16,12 @@
  */
 package org.apache.asn1.ldap.pojo;
 
+import java.nio.BufferOverflowException;
+import java.nio.ByteBuffer;
+
+import org.apache.asn1.EncoderException;
 import org.apache.asn1.ber.tlv.Length;
+import org.apache.asn1.ldap.codec.LdapConstants;
 
 /**
  * An AddResponse Message. Its syntax is :
@@ -56,6 +61,34 @@
         int ldapResponseLength = super.computeLength();
         
         return 1 + Length.getNbBytes( ldapResponseLength ) + ldapResponseLength;
+    }
+
+    /**
+     * Encode the AddResponse 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("Cannot put a PDU in a null buffer !");
+        }
+        
+        try
+        {
+            // The tag
+            buffer.put( LdapConstants.ADD_RESPONSE_TAG );
+            buffer.put( Length.getBytes( getLdapResponseLength() ) );
+        }
+        catch ( BufferOverflowException boe )
+        {
+            throw new EncoderException("The PDU buffer size is too small !"); 
+        }
+        
+        // The ldapResult
+        return super.encode( buffer);
     }
 
     /**

Modified: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/LdapMessage.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/LdapMessage.java?rev=226617&r1=226616&r2=226617&view=diff
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/LdapMessage.java
(original)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/LdapMessage.java
Sun Jul 31 01:07:19 2005
@@ -22,6 +22,7 @@
 import org.apache.asn1.ber.tlv.Value;
 import org.apache.asn1.ldap.codec.LdapConstants;
 
+import java.nio.BufferOverflowException;
 import java.nio.ByteBuffer;
 
 import java.util.ArrayList;
@@ -452,22 +453,23 @@
     {
         // Allocate the bytes buffer.
         ByteBuffer bb = ByteBuffer.allocate( computeLength() );
-        
-        // The LdapMessage Sequence
-        bb.put( LdapConstants.SEQUENCE_TAG );
-        
-        // The length has been calculated by the computeLength method
-        bb.put( Length.getBytes( ldapMessageLength ) );
-        
-        // The message Id
-        bb.put( LdapConstants.INTEGER_TAG );
 
-        // The length. It has to be evaluated depending on 
-        // the messageId value.
-        bb.put( (byte)Value.getNbBytes( messageId ) );
+        try 
+        {
+            // The LdapMessage Sequence
+            bb.put( LdapConstants.SEQUENCE_TAG );
+            
+            // The length has been calculated by the computeLength method
+            bb.put( Length.getBytes( ldapMessageLength ) );
+        }
+        catch ( BufferOverflowException boe )
+        {
+            throw new EncoderException("The PDU buffer size is too small !"); 
+        }
+            
         
-        // The messageId
-        bb.put( Value.getBytes( messageId ) );
+        // The message Id
+        Value.encode( bb, messageId);
         
         // Add the protocolOp part
         protocolOp.encode(bb);
@@ -476,7 +478,7 @@
         if (controls != null)
         {
             // Encode the control's sequence
-            bb.put( LdapConstants.CONTROLS_SEQUENCE_TAG );
+            bb.put( (byte)LdapConstants.CONTROLS_SEQUENCE_TAG );
             bb.put( Length.getBytes( controlsSequenceLength ) );
             
 

Modified: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/LdapResponse.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/LdapResponse.java?rev=226617&r1=226616&r2=226617&view=diff
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/LdapResponse.java
(original)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/LdapResponse.java
Sun Jul 31 01:07:19 2005
@@ -16,6 +16,10 @@
  */
 package org.apache.asn1.ldap.pojo;
 
+import java.nio.ByteBuffer;
+
+import org.apache.asn1.EncoderException;
+
 
 /**
  * A generic LdapResponse Object. It will contain the LdapResult.
@@ -28,6 +32,9 @@
 
     /** The LdapResult element */
     private LdapResult ldapResult;
+    
+    /** The response length */
+    private transient int ldapResponseLength;
 
     //~ Constructors -------------------------------------------------------------------------------
 
@@ -62,6 +69,14 @@
     }
     
     /**
+     * @return Returns the ldapResponseLength.
+     */
+    public int getLdapResponseLength()
+    {
+        return ldapResponseLength;
+    }
+    
+    /**
      * Compute the LdapResponse length
      * 
      * LdapResponse :
@@ -70,10 +85,32 @@
      */
     public int computeLength()
     {
-        return ldapResult.computeLength();
+        ldapResponseLength = ldapResult.computeLength();
+        
+        return ldapResponseLength;
     }
     
     /**
+     * Encode the AddResponse 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("Cannot put a PDU in a null buffer !");
+        }
+        
+        // The ldapResult
+        ldapResult.encode( buffer );
+        
+        // The ldapResult
+        return buffer;
+    }
+
+    /**
      * Get a String representation of an Response
      *
      * @return An Response String 
@@ -82,5 +119,4 @@
     {
         return ldapResult.toString();
     }
-    
 }

Modified: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/LdapResult.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/LdapResult.java?rev=226617&r1=226616&r2=226617&view=diff
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/LdapResult.java
(original)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/LdapResult.java
Sun Jul 31 01:07:19 2005
@@ -17,15 +17,19 @@
 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.ldap.codec.primitives.LdapURL;
 
+import java.nio.BufferOverflowException;
+import java.nio.ByteBuffer;
 import java.util.ArrayList;
 import java.util.Iterator;
 
-
 /**
  * A ldapObject to store the LdapResult
  * 
@@ -84,16 +88,19 @@
      *  other                        (80) },
      *  -- 81-90 reserved for APIs --
      */
-    private int resultCode;
+    private int           resultCode;
 
     /** The DN that is matched by the Bind */
-    private LdapDN matchedDN;
+    private LdapDN        matchedDN;
 
     /** The error message */
-    private LdapString errorMessage;
+    private LdapString    errorMessage;
 
     /** The referrals, if any. This is an optional element */
-    private ArrayList referrals;
+    private ArrayList     referrals;
+
+    /** The inner size of the referrals sequence */
+    private transient int referralsLength;
 
     //~ Constructors -------------------------------------------------------------------------------
 
@@ -217,38 +224,92 @@
     public int computeLength()
     {
         int ldapResultLength = 0;
-        
+
         // The result code : always 3 bytes
         ldapResultLength = 1 + 1 + 1;
-        
+
         // The matchedDN length
-        ldapResultLength += 1 + Length.getNbBytes(matchedDN.getLength()) + matchedDN.getLength();

-        
+        ldapResultLength += 1 + Length.getNbBytes( matchedDN.getLength() ) + matchedDN.getLength();
+
         // The errorMessage length
-        ldapResultLength += 1 + Length.getNbBytes(errorMessage.getLength()) + errorMessage.getLength();

+        ldapResultLength += 1 + Length.getNbBytes( errorMessage.getLength() ) + errorMessage.getLength();
 
         if ( ( referrals != null ) && ( referrals.size() != 0 ) )
         {
             Iterator referralIterator = referrals.iterator();
-            
-            int referralsLength = 0;
-            
+
+            referralsLength = 0;
+
             // Each referral
-            while (referralIterator.hasNext())
+            while ( referralIterator.hasNext() )
             {
-                LdapURL referral = (LdapURL)referralIterator.next();
-                
-                referralsLength += 1 + Length.getNbBytes(referral.getLength()) + referral.getLength();
+                LdapURL referral = ( LdapURL ) referralIterator.next();
+
+                referralsLength += 1 + Length.getNbBytes( referral.getLength() ) + referral.getLength();
             }
-            
+
             // The referrals
-            ldapResultLength += 1 + Length.getNbBytes(referralsLength) + referralsLength;
+            ldapResultLength += 1 + Length.getNbBytes( referralsLength ) + referralsLength;
         }
 
         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( "Cannot put a PDU in a null buffer !" );
+        }
+
+        try 
+        {
+            // The result code
+            buffer.put( LdapConstants.ENUMERATED_TAG );
+            buffer.put( ( byte ) 1 );
+            buffer.put( ( byte ) resultCode );
+        }
+        catch ( BufferOverflowException boe )
+        {
+            throw new EncoderException("The PDU buffer size is too small !"); 
+        }
+
+        // The matchedDN 
+        Value.encode( buffer, matchedDN);
+
+        // The error message
+        Value.encode( buffer, errorMessage);
+
+        // The referrals, if any
+        if ( ( referrals != null ) && ( referrals.size() != 0 ) )
+        {
+            // Encode the referrals sequence
+            // The referrals length MUST have been computed before !
+            buffer.put( (byte)LdapConstants.LDAP_RESULT_REFERRAL_SEQUENCE_TAG );
+            buffer.put( Length.getBytes( referralsLength ) );
+
+            // Each referral
+            Iterator referralIterator = referrals.iterator();
+
+            while ( referralIterator.hasNext() )
+            {
+                LdapURL referral = ( LdapURL ) referralIterator.next();
+
+                // Ecode the current referral
+                Value.encode( buffer, referral);
+            }
+        }
+
+        return buffer;
+    }
+
+    /**
      * Get a String representation of a LdapResult
      *
      * @return A LdapResult String 
@@ -501,8 +562,7 @@
         }
 
         sb.append( "            Matched DN : '" ).append( matchedDN.toString() ).append(
"'\n" );
-        sb.append( "            Error message : '" ).append( errorMessage.toString() ).append(
-            "'\n" );
+        sb.append( "            Error message : '" ).append( errorMessage.toString() ).append(
"'\n" );
 
         if ( referrals.size() != 0 )
         {
@@ -513,8 +573,8 @@
 
                 LdapURL referral = ( LdapURL ) referrals.get( i );
 
-                sb.append( "                Referral[" ).append( i ).append( "] :" )
-                  .append( referral.toString() ).append( '\n' );
+                sb.append( "                Referral[" ).append( i ).append( "] :" ).append(
referral.toString() )
+                        .append( '\n' );
             }
         }
 

Modified: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/UnBindRequest.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/UnBindRequest.java?rev=226617&r1=226616&r2=226617&view=diff
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/UnBindRequest.java
(original)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/UnBindRequest.java
Sun Jul 31 01:07:19 2005
@@ -16,6 +16,7 @@
  */
 package org.apache.asn1.ldap.pojo;
 
+import java.nio.BufferOverflowException;
 import java.nio.ByteBuffer;
 
 import org.apache.asn1.Asn1Object;
@@ -70,11 +71,18 @@
             throw new EncoderException("Cannot put a PDU in a null buffer !");
         }
         
-        // The tag
-        buffer.put(LdapConstants.UNBIND_REQUEST_TAG);
-        
-        // The length is always null.
-        buffer.put( (byte)0 );
+        try 
+        {
+            // The tag
+            buffer.put(LdapConstants.UNBIND_REQUEST_TAG);
+            
+            // The length is always null.
+            buffer.put( (byte)0 );
+        }
+        catch ( BufferOverflowException boe )
+        {
+            throw new EncoderException("The PDU buffer size is too small !"); 
+        }
         
         return buffer;
     }



Mime
View raw message