directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r1179420 - in /directory/shared/trunk/ldap: codec/core/src/main/java/org/apache/directory/shared/ldap/codec/api/ codec/core/src/test/java/org/apache/directory/shared/ldap/codec/ extras/sp/src/main/java/org/apache/directory/shared/ldap/sp/ n...
Date Wed, 05 Oct 2011 20:27:17 GMT
Author: elecharny
Date: Wed Oct  5 20:27:16 2011
New Revision: 1179420

URL: http://svn.apache.org/viewvc?rev=1179420&view=rev
Log:
o Removed a layer in the LdapDecoder hierarchy
o Some code cleaning

Modified:
    directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/shared/ldap/codec/api/LdapDecoder.java
    directory/shared/trunk/ldap/codec/core/src/test/java/org/apache/directory/shared/ldap/codec/LdapDecoderTest.java
    directory/shared/trunk/ldap/extras/sp/src/main/java/org/apache/directory/shared/ldap/sp/JavaStoredProcUtils.java
    directory/shared/trunk/ldap/net/mina/src/main/java/org/apache/directory/shared/ldap/codec/protocol/mina/LdapProtocolDecoder.java

Modified: directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/shared/ldap/codec/api/LdapDecoder.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/shared/ldap/codec/api/LdapDecoder.java?rev=1179420&r1=1179419&r2=1179420&view=diff
==============================================================================
--- directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/shared/ldap/codec/api/LdapDecoder.java
(original)
+++ directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/shared/ldap/codec/api/LdapDecoder.java
Wed Oct  5 20:27:16 2011
@@ -22,15 +22,12 @@ package org.apache.directory.shared.ldap
 
 import java.io.InputStream;
 import java.nio.ByteBuffer;
-import java.util.List;
 
 import org.apache.directory.shared.asn1.DecoderException;
 import org.apache.directory.shared.asn1.ber.Asn1Decoder;
 import org.apache.directory.shared.asn1.ber.tlv.TLVStateEnum;
 import org.apache.directory.shared.i18n.I18n;
-import org.apache.directory.shared.ldap.model.exception.ResponseCarryingMessageException;
 import org.apache.directory.shared.ldap.model.message.Message;
-import org.apache.directory.shared.util.Strings;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -119,7 +116,7 @@ public class LdapDecoder
      * is stored into this container
      * @param decodedMessages The list of decoded messages
      * @throws Exception If the decoding failed
-     */
+     *
     public void decode( ByteBuffer buffer, LdapMessageContainer<MessageDecorator<?
extends Message>> messageContainer, List<Message> decodedMessages ) throws DecoderException
     {
         buffer.mark();
@@ -190,5 +187,5 @@ public class LdapDecoder
                 }
             }
         }
-    }
+    }*/
 }

Modified: directory/shared/trunk/ldap/codec/core/src/test/java/org/apache/directory/shared/ldap/codec/LdapDecoderTest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/codec/core/src/test/java/org/apache/directory/shared/ldap/codec/LdapDecoderTest.java?rev=1179420&r1=1179419&r2=1179420&view=diff
==============================================================================
--- directory/shared/trunk/ldap/codec/core/src/test/java/org/apache/directory/shared/ldap/codec/LdapDecoderTest.java
(original)
+++ directory/shared/trunk/ldap/codec/core/src/test/java/org/apache/directory/shared/ldap/codec/LdapDecoderTest.java
Wed Oct  5 20:27:16 2011
@@ -35,13 +35,16 @@ import org.apache.directory.shared.asn1.
 import org.apache.directory.shared.ldap.codec.api.LdapDecoder;
 import org.apache.directory.shared.ldap.codec.api.LdapMessageContainer;
 import org.apache.directory.shared.ldap.codec.api.MessageDecorator;
+import org.apache.directory.shared.ldap.codec.api.ResponseCarryingException;
 import org.apache.directory.shared.ldap.codec.decorators.BindRequestDecorator;
 import org.apache.directory.shared.ldap.codec.osgi.AbstractCodecServiceTest;
+import org.apache.directory.shared.ldap.model.exception.ResponseCarryingMessageException;
 import org.apache.directory.shared.ldap.model.message.BindRequest;
 import org.apache.directory.shared.ldap.model.message.Message;
 import org.apache.directory.shared.util.Strings;
 import org.apache.mina.core.session.DummySession;
 import org.apache.mina.core.session.IoSession;
+import org.junit.BeforeClass;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
@@ -58,6 +61,70 @@ import com.mycila.junit.concurrent.Concu
 @Concurrency()
 public class LdapDecoderTest extends AbstractCodecServiceTest
 {
+    /** The ASN 1 decoder instance */
+    private static Asn1Decoder asn1Decoder;
+
+    
+    @BeforeClass
+    public static void init()
+    {
+        asn1Decoder = new Asn1Decoder();
+    }
+    
+
+    /**
+     * Decode an incoming buffer into LDAP messages. The result can be 0, 1 or many 
+     * LDAP messages, which will be stored into the array the caller has created.
+     * 
+     * @param buffer The incoming byte buffer
+     * @param messageContainer The LdapMessageContainer which will be used to store the
+     * message being decoded. If the message is not fully decoded, the ucrrent state
+     * is stored into this container
+     * @param decodedMessages The list of decoded messages
+     * @throws Exception If the decoding failed
+     */
+    private void decode( ByteBuffer buffer, LdapMessageContainer<MessageDecorator<?
extends Message>> messageContainer, List<Message> decodedMessages ) throws DecoderException
+    {
+        buffer.mark();
+
+        while ( buffer.hasRemaining() )
+        {
+            try
+            {
+                asn1Decoder.decode( buffer, messageContainer );
+
+                if ( messageContainer.getState() == TLVStateEnum.PDU_DECODED )
+                {
+                    Message message = messageContainer.getMessage();
+
+                    decodedMessages.add( message );
+
+                    messageContainer.clean();
+                }
+            }
+            catch ( DecoderException de )
+            {
+                buffer.clear();
+                messageContainer.clean();
+
+                if ( de instanceof ResponseCarryingException )
+                {
+                    // Transform the DecoderException message to a MessageException
+                    ResponseCarryingMessageException rcme = new ResponseCarryingMessageException(
de.getMessage() );
+                    rcme.setResponse( ( ( ResponseCarryingException ) de ).getResponse()
);
+
+                    throw rcme;
+                }
+                else
+                {
+                    // TODO : This is certainly not the way we should handle such an exception
!
+                    throw new ResponseCarryingException( de.getMessage() );
+                }
+            }
+        }
+    }
+
+    
     /**
      * Test the decoding of a full PDU
      */
@@ -118,7 +185,6 @@ public class LdapDecoderTest extends Abs
     @Test
     public void testDecode2Messages() throws Exception
     {
-        LdapDecoder ldapDecoder = new LdapDecoder();
         LdapMessageContainer<MessageDecorator<? extends Message>> container =

             new LdapMessageContainer<MessageDecorator<? extends Message>>( codec
);
 
@@ -163,7 +229,7 @@ public class LdapDecoderTest extends Abs
         // Decode a BindRequest PDU
         try
         {
-            ldapDecoder.decode( stream, container, result );
+            decode( stream, container, result );
         }
         catch ( DecoderException de )
         {

Modified: directory/shared/trunk/ldap/extras/sp/src/main/java/org/apache/directory/shared/ldap/sp/JavaStoredProcUtils.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/extras/sp/src/main/java/org/apache/directory/shared/ldap/sp/JavaStoredProcUtils.java?rev=1179420&r1=1179419&r2=1179420&view=diff
==============================================================================
--- directory/shared/trunk/ldap/extras/sp/src/main/java/org/apache/directory/shared/ldap/sp/JavaStoredProcUtils.java
(original)
+++ directory/shared/trunk/ldap/extras/sp/src/main/java/org/apache/directory/shared/ldap/sp/JavaStoredProcUtils.java
Wed Oct  5 20:27:16 2011
@@ -90,7 +90,7 @@ public final class JavaStoredProcUtils
         }
         finally
         {
-        	IOUtils.closeQuietly( in );
+            IOUtils.closeQuietly( in );
         }
 
         return buf;

Modified: directory/shared/trunk/ldap/net/mina/src/main/java/org/apache/directory/shared/ldap/codec/protocol/mina/LdapProtocolDecoder.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/net/mina/src/main/java/org/apache/directory/shared/ldap/codec/protocol/mina/LdapProtocolDecoder.java?rev=1179420&r1=1179419&r2=1179420&view=diff
==============================================================================
--- directory/shared/trunk/ldap/net/mina/src/main/java/org/apache/directory/shared/ldap/codec/protocol/mina/LdapProtocolDecoder.java
(original)
+++ directory/shared/trunk/ldap/net/mina/src/main/java/org/apache/directory/shared/ldap/codec/protocol/mina/LdapProtocolDecoder.java
Wed Oct  5 20:27:16 2011
@@ -20,18 +20,27 @@
 package org.apache.directory.shared.ldap.codec.protocol.mina;
 
 
+import java.io.InputStream;
 import java.nio.ByteBuffer;
 import java.util.ArrayList;
 import java.util.List;
 
+import org.apache.directory.shared.asn1.DecoderException;
+import org.apache.directory.shared.asn1.ber.Asn1Decoder;
+import org.apache.directory.shared.asn1.ber.tlv.TLVStateEnum;
 import org.apache.directory.shared.ldap.codec.api.LdapDecoder;
 import org.apache.directory.shared.ldap.codec.api.LdapMessageContainer;
 import org.apache.directory.shared.ldap.codec.api.MessageDecorator;
+import org.apache.directory.shared.ldap.codec.api.ResponseCarryingException;
+import org.apache.directory.shared.ldap.model.exception.ResponseCarryingMessageException;
 import org.apache.directory.shared.ldap.model.message.Message;
+import org.apache.directory.shared.util.Strings;
 import org.apache.mina.core.buffer.IoBuffer;
 import org.apache.mina.core.session.IoSession;
 import org.apache.mina.filter.codec.ProtocolDecoder;
 import org.apache.mina.filter.codec.ProtocolDecoderOutput;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 
 /**
@@ -41,9 +50,15 @@ import org.apache.mina.filter.codec.Prot
  */
 public class LdapProtocolDecoder implements ProtocolDecoder
 {
-    /** The stateful decoder */
-    private LdapDecoder decoder;
-    
+    /** The logger */
+    private static Logger LOG = LoggerFactory.getLogger( LdapDecoder.class );
+
+    /** A speedup for logger */
+    private static final boolean IS_DEBUG = LOG.isDebugEnabled();
+
+    /** The ASN 1 decoder instance */
+    private Asn1Decoder asn1Decoder;
+
     
     /**
      * Creates a new instance of LdapProtocolEncoder.
@@ -52,7 +67,7 @@ public class LdapProtocolDecoder impleme
      */
     public LdapProtocolDecoder()
     {
-        this.decoder = new LdapDecoder();
+        asn1Decoder = new Asn1Decoder();
     }
 
 
@@ -76,7 +91,7 @@ public class LdapProtocolDecoder impleme
         List<Message> decodedMessages = new ArrayList<Message>();
         ByteBuffer buf = in.buf();
 
-        decoder.decode( buf, messageContainer, decodedMessages );
+        decode( buf, messageContainer, decodedMessages );
         
         for ( Message message : decodedMessages )
         {
@@ -86,6 +101,90 @@ public class LdapProtocolDecoder impleme
 
 
     /**
+     * Decode an incoming buffer into LDAP messages. The result can be 0, 1 or many 
+     * LDAP messages, which will be stored into the array the caller has created.
+     * 
+     * @param buffer The incoming byte buffer
+     * @param messageContainer The LdapMessageContainer which will be used to store the
+     * message being decoded. If the message is not fully decoded, the ucrrent state
+     * is stored into this container
+     * @param decodedMessages The list of decoded messages
+     * @throws Exception If the decoding failed
+     */
+    private void decode( ByteBuffer buffer, LdapMessageContainer<MessageDecorator<?
extends Message>> messageContainer, List<Message> decodedMessages ) throws DecoderException
+    {
+        buffer.mark();
+
+        while ( buffer.hasRemaining() )
+        {
+            try
+            {
+                if ( IS_DEBUG )
+                {
+                    LOG.debug( "Decoding the PDU : " );
+
+                    int size = buffer.limit();
+                    int position = buffer.position();
+                    int pduLength = size - position;
+
+                    byte[] array = new byte[pduLength];
+
+                    System.arraycopy(buffer.array(), position, array, 0, pduLength);
+
+                    buffer.position( size );
+
+                    if ( array.length == 0 )
+                    {
+                        LOG.debug( "NULL buffer, what the HELL ???" );
+                    }
+                    else
+                    {
+                        LOG.debug( Strings.dumpBytes(array) );
+                    }
+
+                    buffer.reset();
+                }
+
+                asn1Decoder.decode( buffer, messageContainer );
+
+                if ( messageContainer.getState() == TLVStateEnum.PDU_DECODED )
+                {
+                    if ( IS_DEBUG )
+                    {
+                        LOG.debug( "Decoded LdapMessage : " + messageContainer.getMessage()
);
+                    }
+
+                    Message message = messageContainer.getMessage();
+
+                    decodedMessages.add( message );
+
+                    messageContainer.clean();
+                }
+            }
+            catch ( DecoderException de )
+            {
+                buffer.clear();
+                messageContainer.clean();
+
+                if ( de instanceof ResponseCarryingException )
+                {
+                    // Transform the DecoderException message to a MessageException
+                    ResponseCarryingMessageException rcme = new ResponseCarryingMessageException(
de.getMessage() );
+                    rcme.setResponse( ( ( ResponseCarryingException ) de ).getResponse()
);
+
+                    throw rcme;
+                }
+                else
+                {
+                    // TODO : This is certainly not the way we should handle such an exception
!
+                    throw new ResponseCarryingException( de.getMessage() );
+                }
+            }
+        }
+    }
+
+    
+    /**
      * {@inheritDoc}
      */
     public void finishDecode( IoSession session, ProtocolDecoderOutput out ) throws Exception



Mime
View raw message