directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r984184 - in /directory: apacheds/branches/apacheds-codec-merge/protocol-ldap/src/main/java/org/apache/directory/server/ldap/ apacheds/branches/apacheds-codec-merge/server-integ/src/test/java/org/apache/directory/server/operations/bind/ sha...
Date Tue, 10 Aug 2010 19:42:22 GMT
Author: elecharny
Date: Tue Aug 10 19:42:21 2010
New Revision: 984184

URL: http://svn.apache.org/viewvc?rev=984184&view=rev
Log:
o Removed the Asn1CodecEncoder class
o Removed the DecoderCallbackImpl inner class in the Asn1CodecDecoder class
o Added a getCallback() method into the AbstractStatefullDecoder class
o Added the mina dependency in the shared-ldap project
o Added the LdapProtocolEncoder class in the server
o Removed the MessageEncoder class
o Added some missing Javadoc

Added:
    directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/codec/LdapProtocolEncoder.java
Removed:
    directory/shared/branches/shared-codec-merge/asn1-codec/src/main/java/org/apache/directory/shared/asn1/codec/Asn1CodecEncoder.java
    directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/message/MessageEncoder.java
Modified:
    directory/apacheds/branches/apacheds-codec-merge/protocol-ldap/src/main/java/org/apache/directory/server/ldap/LdapProtocolCodecFactory.java
    directory/apacheds/branches/apacheds-codec-merge/server-integ/src/test/java/org/apache/directory/server/operations/bind/SaslBindIT.java
    directory/shared/branches/shared-codec-merge/asn1-codec/src/main/java/org/apache/directory/shared/asn1/codec/Asn1CodecDecoder.java
    directory/shared/branches/shared-codec-merge/asn1/src/main/java/org/apache/directory/shared/asn1/codec/stateful/AbstractStatefulDecoder.java
    directory/shared/branches/shared-codec-merge/asn1/src/main/java/org/apache/directory/shared/asn1/codec/stateful/StatefulDecoder.java
    directory/shared/branches/shared-codec-merge/ldap/pom.xml
    directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/codec/LdapDecoder.java
    directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/message/MessageDecoder.java

Modified: directory/apacheds/branches/apacheds-codec-merge/protocol-ldap/src/main/java/org/apache/directory/server/ldap/LdapProtocolCodecFactory.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-codec-merge/protocol-ldap/src/main/java/org/apache/directory/server/ldap/LdapProtocolCodecFactory.java?rev=984184&r1=984183&r2=984184&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-codec-merge/protocol-ldap/src/main/java/org/apache/directory/server/ldap/LdapProtocolCodecFactory.java (original)
+++ directory/apacheds/branches/apacheds-codec-merge/protocol-ldap/src/main/java/org/apache/directory/server/ldap/LdapProtocolCodecFactory.java Tue Aug 10 19:42:21 2010
@@ -22,9 +22,8 @@ package org.apache.directory.server.ldap
 
 import org.apache.directory.server.core.DirectoryService;
 import org.apache.directory.shared.asn1.codec.Asn1CodecDecoder;
-import org.apache.directory.shared.asn1.codec.Asn1CodecEncoder;
+import org.apache.directory.shared.ldap.codec.LdapProtocolEncoder;
 import org.apache.directory.shared.ldap.message.MessageDecoder;
-import org.apache.directory.shared.ldap.message.MessageEncoder;
 import org.apache.directory.shared.ldap.message.spi.BinaryAttributeDetector;
 import org.apache.directory.shared.ldap.schema.AttributeType;
 import org.apache.directory.shared.ldap.schema.SchemaManager;
@@ -44,9 +43,9 @@ final class LdapProtocolCodecFactory imp
 {
     /** the directory service for which this factor generates codecs */
     final private DirectoryService directoryService;
-    
+
     /** The tag stored into the session if we want to set a max PDU size */
-    public final static String MAX_PDU_SIZE = "MAX_PDU_SIZE"; 
+    public final static String MAX_PDU_SIZE = "MAX_PDU_SIZE";
 
 
     /**
@@ -61,19 +60,17 @@ final class LdapProtocolCodecFactory imp
     }
 
 
-    /*
-     * (non-Javadoc)
-     * @see org.apache.mina.filter.codec.ProtocolCodecFactory#getEncoder()
+    /**
+     * {@inheritDoc}
      */
     public ProtocolEncoder getEncoder( IoSession session )
     {
-        return new Asn1CodecEncoder( new MessageEncoder() );
+        return new LdapProtocolEncoder();
     }
 
 
-    /*
-     * (non-Javadoc)
-     * @see org.apache.mina.filter.codec.ProtocolCodecFactory#getDecoder()
+    /**
+     * {@inheritDoc}
      */
     public ProtocolDecoder getDecoder( IoSession session )
     {
@@ -82,11 +79,11 @@ final class LdapProtocolCodecFactory imp
             public boolean isBinary( String id )
             {
                 SchemaManager schemaManager = directoryService.getSchemaManager();
-                
+
                 try
                 {
                     AttributeType type = schemaManager.lookupAttributeTypeRegistry( id );
-                    return ! type.getSyntax().isHumanReadable();
+                    return !type.getSyntax().isHumanReadable();
                 }
                 catch ( Exception e )
                 {
@@ -94,11 +91,10 @@ final class LdapProtocolCodecFactory imp
                     {
                         return false;
                     }
-                    
+
                     return id.endsWith( ";binary" );
                 }
             }
-        },
-        directoryService.getMaxPDUSize() ) );
+        }, directoryService.getMaxPDUSize() ) );
     }
 }
\ No newline at end of file

Modified: directory/apacheds/branches/apacheds-codec-merge/server-integ/src/test/java/org/apache/directory/server/operations/bind/SaslBindIT.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-codec-merge/server-integ/src/test/java/org/apache/directory/server/operations/bind/SaslBindIT.java?rev=984184&r1=984183&r2=984184&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-codec-merge/server-integ/src/test/java/org/apache/directory/server/operations/bind/SaslBindIT.java (original)
+++ directory/apacheds/branches/apacheds-codec-merge/server-integ/src/test/java/org/apache/directory/server/operations/bind/SaslBindIT.java Tue Aug 10 19:42:21 2010
@@ -25,6 +25,7 @@ import static org.junit.Assert.assertTru
 import static org.junit.Assert.fail;
 
 import java.lang.reflect.Field;
+import java.nio.ByteBuffer;
 import java.util.Hashtable;
 
 import javax.naming.AuthenticationNotSupportedException;
@@ -54,12 +55,14 @@ import org.apache.directory.server.ldap.
 import org.apache.directory.server.ldap.handlers.bind.ntlm.NtlmMechanismHandler;
 import org.apache.directory.server.ldap.handlers.bind.plain.PlainMechanismHandler;
 import org.apache.directory.server.ldap.handlers.extended.StoredProcedureExtendedOperationHandler;
+import org.apache.directory.shared.ldap.codec.LdapMessageCodec;
+import org.apache.directory.shared.ldap.codec.LdapTransformer;
 import org.apache.directory.shared.ldap.constants.SupportedSaslMechanisms;
 import org.apache.directory.shared.ldap.message.BindRequestImpl;
 import org.apache.directory.shared.ldap.message.MessageDecoder;
-import org.apache.directory.shared.ldap.message.MessageEncoder;
 import org.apache.directory.shared.ldap.message.ResultCodeEnum;
 import org.apache.directory.shared.ldap.message.internal.InternalBindResponse;
+import org.apache.directory.shared.ldap.message.internal.InternalMessage;
 import org.apache.directory.shared.ldap.message.spi.BinaryAttributeDetector;
 import org.apache.directory.shared.ldap.name.DN;
 import org.junit.Test;
@@ -73,523 +76,493 @@ import org.slf4j.LoggerFactory;
  * 
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  */
-@RunWith ( FrameworkRunner.class ) 
-@ApplyLdifs( {
-    // Entry # 1
-    "dn: ou=users,dc=example,dc=com", 
-    "objectClass: organizationalUnit", 
-    "objectClass: top", 
-    "ou: users\n",  
-    // Entry # 2
-    "dn: uid=hnelson,ou=users,dc=example,dc=com", 
-    "objectClass: inetOrgPerson", 
-    "objectClass: organizationalPerson", 
-    "objectClass: person", 
-    "objectClass: top", 
-    "uid: hnelson", 
-    "userPassword: secret", 
-    "cn: Horatio Nelson", 
-    "sn: Nelson" 
-    }
-)
-@CreateDS( allowAnonAccess=true, name="SaslBindIT-class",
-    partitions =
-        {
-            @CreatePartition(
-                name = "example",
-                suffix = "dc=example,dc=com",
-                contextEntry = @ContextEntry( 
-                    entryLdif =
-                        "dn: dc=example,dc=com\n" +
-                        "dc: example\n" +
-                        "objectClass: top\n" +
-                        "objectClass: domain\n\n" ),
-                indexes = 
-                {
-                    @CreateIndex( attribute = "objectClass" ),
-                    @CreateIndex( attribute = "dc" ),
-                    @CreateIndex( attribute = "ou" )
-                } )
-        })
-@CreateLdapServer ( 
-    transports = 
-    {
-        @CreateTransport( protocol = "LDAP" )
-    },
-    saslHost="localhost",
-    saslMechanisms = 
-    {
-        @SaslMechanism( name=SupportedSaslMechanisms.PLAIN, implClass=PlainMechanismHandler.class ),
-        @SaslMechanism( name=SupportedSaslMechanisms.CRAM_MD5, implClass=CramMd5MechanismHandler.class),
-        @SaslMechanism( name=SupportedSaslMechanisms.DIGEST_MD5, implClass=DigestMd5MechanismHandler.class),
-        @SaslMechanism( name=SupportedSaslMechanisms.GSSAPI, implClass=GssapiMechanismHandler.class),
-        @SaslMechanism( name=SupportedSaslMechanisms.NTLM, implClass=NtlmMechanismHandler.class),
-        @SaslMechanism( name=SupportedSaslMechanisms.GSS_SPNEGO, implClass=NtlmMechanismHandler.class)
-    },
-    extendedOpHandlers = 
-    {
-        StoredProcedureExtendedOperationHandler.class
-    },
-    ntlmProvider=BogusNtlmProvider.class
-    )
+@RunWith(FrameworkRunner.class)
+@ApplyLdifs(
+    {
+        // Entry # 1
+        "dn: ou=users,dc=example,dc=com", "objectClass: organizationalUnit", "objectClass: top",
+        "ou: users\n",
+        // Entry # 2
+        "dn: uid=hnelson,ou=users,dc=example,dc=com", "objectClass: inetOrgPerson",
+        "objectClass: organizationalPerson", "objectClass: person", "objectClass: top", "uid: hnelson",
+        "userPassword: secret", "cn: Horatio Nelson", "sn: Nelson" })
+@CreateDS(allowAnonAccess = true, name = "SaslBindIT-class", partitions =
+    { @CreatePartition(name = "example", suffix = "dc=example,dc=com", contextEntry = @ContextEntry(entryLdif = "dn: dc=example,dc=com\n"
+        + "dc: example\n" + "objectClass: top\n" + "objectClass: domain\n\n"), indexes =
+        { @CreateIndex(attribute = "objectClass"), @CreateIndex(attribute = "dc"), @CreateIndex(attribute = "ou") }) })
+@CreateLdapServer(transports =
+    { @CreateTransport(protocol = "LDAP") }, saslHost = "localhost", saslMechanisms =
+    { @SaslMechanism(name = SupportedSaslMechanisms.PLAIN, implClass = PlainMechanismHandler.class),
+        @SaslMechanism(name = SupportedSaslMechanisms.CRAM_MD5, implClass = CramMd5MechanismHandler.class),
+        @SaslMechanism(name = SupportedSaslMechanisms.DIGEST_MD5, implClass = DigestMd5MechanismHandler.class),
+        @SaslMechanism(name = SupportedSaslMechanisms.GSSAPI, implClass = GssapiMechanismHandler.class),
+        @SaslMechanism(name = SupportedSaslMechanisms.NTLM, implClass = NtlmMechanismHandler.class),
+        @SaslMechanism(name = SupportedSaslMechanisms.GSS_SPNEGO, implClass = NtlmMechanismHandler.class) }, extendedOpHandlers =
+    { StoredProcedureExtendedOperationHandler.class }, ntlmProvider = BogusNtlmProvider.class)
 public class SaslBindIT extends AbstractLdapTestUnit
 {
 
-    
-     /**
-      * Tests to make sure the server properly returns the supportedSASLMechanisms.
-      */
-     @Test
-     public void testSupportedSASLMechanisms()
-     {
-         try
-         {
-             // We have to tell the server that it should accept anonymous
-             // auth, because we are reading the rootDSE
-             ldapServer.setAllowAnonymousAccess( true );
-             ldapServer.getDirectoryService().setAllowAnonymousAccess( true );
-             
-             // Point on rootDSE
-             DirContext context = new InitialDirContext();
-
-             Attributes attrs = context.getAttributes( "ldap://localhost:" 
-                 + ldapServer.getPort(), new String[]
-                 { "supportedSASLMechanisms" } );
-
-//             Thread.sleep( 10 * 60 * 1000 );
-             NamingEnumeration<? extends Attribute> answer = attrs.getAll();
-             Attribute result = answer.next();
-             assertEquals( 6, result.size() );
-             assertTrue( result.contains( SupportedSaslMechanisms.GSSAPI ) );
-             assertTrue( result.contains( SupportedSaslMechanisms.DIGEST_MD5 ) );
-             assertTrue( result.contains( SupportedSaslMechanisms.CRAM_MD5 ) );
-             assertTrue( result.contains( SupportedSaslMechanisms.NTLM ) );
-             assertTrue( result.contains( SupportedSaslMechanisms.PLAIN ) );
-             assertTrue( result.contains( SupportedSaslMechanisms.GSS_SPNEGO ) );
-         }
-         catch ( Exception e )
-         {
-             fail( "Should not have caught exception." );
-         }
-     }
-     
-     
-     /**
-      * Tests to make sure PLAIN-binds works
-      */
-     @Test
-     public void testSaslBindPLAIN()
-     {
-         try
-         {
-             Hashtable<String, String> env = new Hashtable<String, String>();
-             env.put( Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory" );
-             env.put( Context.PROVIDER_URL, "ldap://localhost:" + ldapServer.getPort() );
-
-             env.put( Context.SECURITY_AUTHENTICATION, "PLAIN" );
-             env.put( Context.SECURITY_PRINCIPAL, "uid=hnelson,ou=users,dc=example,dc=com" );
-             env.put( Context.SECURITY_CREDENTIALS, "secret" );
-
-             DirContext context = new InitialDirContext( env );
-
-             String[] attrIDs =
-                 { "uid" };
-
-             Attributes attrs = context.getAttributes( "uid=hnelson,ou=users,dc=example,dc=com", attrIDs );
-             String uid = null;
-
-             if ( attrs.get( "uid" ) != null )
-             {
-                 uid = ( String ) attrs.get( "uid" ).get();
-             }
-
-             assertEquals( uid, "hnelson" );
-         }
-         catch ( NamingException e )
-         {
-             fail( "Should not have caught exception." );
-         }
-     }
-
-
-     /**
-      * Test a SASL bind with an empty mechanism 
-      */
-     @Test
-     public void testSaslBindNoMech()
-     {
-         try
-         {
-             Hashtable<String, String> env = new Hashtable<String, String>();
-             env.put( Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory" );
-             env.put( Context.PROVIDER_URL, "ldap://localhost:" + ldapServer.getPort() );
-
-             env.put( Context.SECURITY_AUTHENTICATION, "" );
-             env.put( Context.SECURITY_PRINCIPAL, "uid=hnelson,ou=users,dc=example,dc=com" );
-             env.put( Context.SECURITY_CREDENTIALS, "secret" );
-
-             new InitialDirContext( env );
-             fail( "Should not be there" );
-         }
-         catch ( AuthenticationNotSupportedException anse )
-         {
-             assertTrue( true );
-         }
-         catch ( NamingException ne )
-         {
-             fail( "Should not have caught exception." );
-         }
-     }
-
-
-     /**
-      * Tests to make sure CRAM-MD5 binds below the RootDSE work.
-      */
-     @Test
-     public void testSaslCramMd5Bind()
-     {
-         try
-         {
-             Hashtable<String, String> env = new Hashtable<String, String>();
-             env.put( Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory" );
-             env.put( Context.PROVIDER_URL, "ldap://localhost:" + ldapServer.getPort() );
-
-             env.put( Context.SECURITY_AUTHENTICATION, "CRAM-MD5" );
-             env.put( Context.SECURITY_PRINCIPAL, "hnelson" );
-             env.put( Context.SECURITY_CREDENTIALS, "secret" );
-
-             DirContext context = new InitialDirContext( env );
-
-             String[] attrIDs =
-                 { "uid" };
-
-             Attributes attrs = context.getAttributes( "uid=hnelson,ou=users,dc=example,dc=com", attrIDs );
-
-             String uid = null;
-
-             if ( attrs.get( "uid" ) != null )
-             {
-                 uid = ( String ) attrs.get( "uid" ).get();
-             }
-
-             assertEquals( uid, "hnelson" );
-         }
-         catch ( NamingException e )
-         {
-             fail( "Should not have caught exception." );
-         }
-     }
-     
-     
-     /**
-      * Tests to make sure CRAM-MD5 binds below the RootDSE fail if the password is bad.
-      */
-     @Test
-     public void testSaslCramMd5BindBadPassword()
-     {
-         try
-         {
-             Hashtable<String, String> env = new Hashtable<String, String>();
-             env.put( Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory" );
-             env.put( Context.PROVIDER_URL, "ldap://localhost:" + ldapServer.getPort() );
-
-             env.put( Context.SECURITY_AUTHENTICATION, "CRAM-MD5" );
-             env.put( Context.SECURITY_PRINCIPAL, "hnelson" );
-             env.put( Context.SECURITY_CREDENTIALS, "badsecret" );
-
-             DirContext context = new InitialDirContext( env );
-
-             String[] attrIDs =
-                 { "uid" };
-
-             context.getAttributes( "uid=hnelson,ou=users,dc=example,dc=com", attrIDs );
-
-             fail( "Should have thrown exception." );
-         }
-         catch ( NamingException e )
-         {
-             assertTrue( e.getMessage().contains( "Invalid response" ) );
-         }
-     }
-     
-     
-     /**
-      * Tests to make sure DIGEST-MD5 binds below the RootDSE work.
-      */
-     @Test
-     public void testSaslDigestMd5Bind() throws Exception
-     {
-         Hashtable<String, String> env = new Hashtable<String, String>();
-         env.put( Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory" );
-         env.put( Context.PROVIDER_URL, "ldap://localhost:" + ldapServer.getPort() );
-
-         env.put( Context.SECURITY_AUTHENTICATION, "DIGEST-MD5" );
-         env.put( Context.SECURITY_PRINCIPAL, "hnelson" );
-         env.put( Context.SECURITY_CREDENTIALS, "secret" );
-
-         // Specify realm
-         env.put( "java.naming.security.sasl.realm", "example.com" );
-
-         // Request privacy protection
-         env.put( "javax.security.sasl.qop", "auth-conf" );
-
-         DirContext context = new InitialDirContext( env );
-
-         String[] attrIDs =
-             { "uid" };
-
-         Attributes attrs = context.getAttributes( "uid=hnelson,ou=users,dc=example,dc=com", attrIDs );
-
-         String uid = null;
-
-         if ( attrs.get( "uid" ) != null )
-         {
-             uid = ( String ) attrs.get( "uid" ).get();
-         }
-
-         assertEquals( uid, "hnelson" );
-     }
-
-     
-     /**
-      * Tests to make sure DIGEST-MD5 binds below the RootDSE fail if the realm is bad.
-      */
-     @Test
-     public void testSaslDigestMd5BindBadRealm()
-     {
-         try
-         {
-             Hashtable<String, String> env = new Hashtable<String, String>();
-             env.put( Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory" );
-             env.put( Context.PROVIDER_URL, "ldap://localhost:" + ldapServer.getPort() );
-
-             env.put( Context.SECURITY_AUTHENTICATION, "DIGEST-MD5" );
-             env.put( Context.SECURITY_PRINCIPAL, "hnelson" );
-             env.put( Context.SECURITY_CREDENTIALS, "secret" );
-
-             // Bad realm
-             env.put( "java.naming.security.sasl.realm", "badrealm.com" );
-
-             // Request privacy protection
-             env.put( "javax.security.sasl.qop", "auth-conf" );
-
-             DirContext context = new InitialDirContext( env );
-
-             String[] attrIDs =
-                 { "uid" };
-
-             context.getAttributes( "uid=hnelson,ou=users,dc=example,dc=com", attrIDs );
-
-             fail( "Should have thrown exception." );
-         }
-         catch ( NamingException e )
-         {
-             assertTrue( e.getMessage().contains( "Nonexistent realm" ) );
-         }
-     }
-
-
-     /**
-      * Tests to make sure DIGEST-MD5 binds below the RootDSE fail if the password is bad.
-      */
-     @Test
-     public void testSaslDigestMd5BindBadPassword()
-     {
-         try
-         {
-             Hashtable<String, String> env = new Hashtable<String, String>();
-             env.put( Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory" );
-             env.put( Context.PROVIDER_URL, "ldap://localhost:" + ldapServer.getPort() );
-
-             env.put( Context.SECURITY_AUTHENTICATION, "DIGEST-MD5" );
-             env.put( Context.SECURITY_PRINCIPAL, "hnelson" );
-             env.put( Context.SECURITY_CREDENTIALS, "badsecret" );
-
-             DirContext context = new InitialDirContext( env );
-             String[] attrIDs = { "uid" };
-
-             context.getAttributes( "uid=hnelson,ou=users,dc=example,dc=com", attrIDs );
-             fail( "Should have thrown exception." );
-         }
-         catch ( NamingException e )
-         {
-             assertTrue( e.getMessage().contains( "digest response format violation" ) );
-         }
-     }
-
-
-     /**
-      * Tests that the plumbing for NTLM bind works.
-      */
-     @Test
-     public void testNtlmBind() throws Exception
-     {
-         BogusNtlmProvider provider = getNtlmProviderUsingReflection();
-         
-         NtlmSaslBindClient client = new NtlmSaslBindClient( SupportedSaslMechanisms.NTLM );
-         InternalBindResponse type2response = client.bindType1( "type1_test".getBytes() );
-         assertEquals( 1, type2response.getMessageId() );
-         assertEquals( ResultCodeEnum.SASL_BIND_IN_PROGRESS, type2response.getLdapResult().getResultCode() );
-         assertTrue( ArrayUtils.isEquals( "type1_test".getBytes(), provider.getType1Response() ) );
-         assertTrue( ArrayUtils.isEquals( "challenge".getBytes(), type2response.getServerSaslCreds() ) );
-         
-         InternalBindResponse finalResponse = client.bindType3( "type3_test".getBytes() );
-         assertEquals( 2, finalResponse.getMessageId() );
-         assertEquals( ResultCodeEnum.SUCCESS, finalResponse.getLdapResult().getResultCode() );
-         assertTrue( ArrayUtils.isEquals( "type3_test".getBytes(), provider.getType3Response() ) );
-     }
-
-
-     /**
-      * Tests that the plumbing for NTLM bind works.
-      */
-     @Test
-     public void testGssSpnegoBind() throws Exception
-     {
-         BogusNtlmProvider provider = new BogusNtlmProvider();
-
-         // the provider configured in @CreateLdapServer only sets for the NTLM mechanism
-         // but we use the same NtlmMechanismHandler class for GSS_SPNEGO too but this is a separate
-         // instance, so we need to set the provider in the NtlmMechanismHandler instance of GSS_SPNEGO mechanism
-         NtlmMechanismHandler ntlmHandler = ( NtlmMechanismHandler ) ldapServer.getSaslMechanismHandlers().get( SupportedSaslMechanisms.GSS_SPNEGO );
-         ntlmHandler.setNtlmProvider( provider );
-
-         NtlmSaslBindClient client = new NtlmSaslBindClient( SupportedSaslMechanisms.GSS_SPNEGO );
-         InternalBindResponse type2response = client.bindType1( "type1_test".getBytes() );
-         assertEquals( 1, type2response.getMessageId() );
-         assertEquals( ResultCodeEnum.SASL_BIND_IN_PROGRESS, type2response.getLdapResult().getResultCode() );
-         assertTrue( ArrayUtils.isEquals( "type1_test".getBytes(), provider.getType1Response() ) );
-         assertTrue( ArrayUtils.isEquals( "challenge".getBytes(), type2response.getServerSaslCreds() ) );
-         
-         InternalBindResponse finalResponse = client.bindType3( "type3_test".getBytes() );
-         assertEquals( 2, finalResponse.getMessageId() );
-         assertEquals( ResultCodeEnum.SUCCESS, finalResponse.getLdapResult().getResultCode() );
-         assertTrue( ArrayUtils.isEquals( "type3_test".getBytes(), provider.getType3Response() ) );
-     }
-
-
-     /**
-      * A NTLM client
-      */
-     class NtlmSaslBindClient extends SocketClient
-     {
-         private final Logger LOG = LoggerFactory.getLogger( NtlmSaslBindClient.class );
-         
-         private final String mechanism;
-         
-         
-         NtlmSaslBindClient( String mechanism ) throws Exception
-         {
-             this.mechanism = mechanism;
-             setDefaultPort( ldapServer.getPort() );
-             connect( "localhost", ldapServer.getPort() );
-             setTcpNoDelay( false );
-             
-             LOG.debug( "isConnected() = {}", isConnected() );
-             LOG.debug( "LocalPort     = {}", getLocalPort() );
-             LOG.debug( "LocalAddress  = {}", getLocalAddress() );
-             LOG.debug( "RemotePort    = {}", getRemotePort() );
-             LOG.debug( "RemoteAddress = {}", getRemoteAddress() );
-         }
-
-         
-         InternalBindResponse bindType1( byte[] type1response ) throws Exception
-         {
-             if ( ! isConnected() )
-             {
-                 throw new IllegalStateException( "Client is not connected." );
-             }
-             
-             // Setup the bind request
-             BindRequestImpl request = new BindRequestImpl( 1 ) ;
-             request.setName( new DN( "uid=admin,ou=system" ) ) ;
-             request.setSimple( false ) ;
-             request.setCredentials( type1response ) ;
-             request.setSaslMechanism( mechanism );
-             request.setVersion3( true ) ;
-             
-             // Setup the ASN1 Encoder and Decoder
-             MessageEncoder encoder = new MessageEncoder();
-             MessageDecoder decoder = new MessageDecoder( new BinaryAttributeDetector() {
-                 public boolean isBinary( String attributeId )
-                 {
-                     return false;
-                 }
-             } );
-      
-             // Send encoded request to server
-             encoder.encodeBlocking( null, _output_, request );
-             _output_.flush();
-             
-             while ( _input_.available() <= 0 )
-             {
-                 Thread.sleep( 100 );
-             }
-             
-             // Retrieve the response back from server to my last request.
-             return ( InternalBindResponse ) decoder.decode( null, _input_ );
-         }
-         
-         
-         InternalBindResponse bindType3( byte[] type3response ) throws Exception
-         {
-             if ( ! isConnected() )
-             {
-                 throw new IllegalStateException( "Client is not connected." );
-             }
-             
-             // Setup the bind request
-             BindRequestImpl request = new BindRequestImpl( 2 ) ;
-             request.setName( new DN( "uid=admin,ou=system" ) ) ;
-             request.setSimple( false ) ;
-             request.setCredentials( type3response ) ;
-             request.setSaslMechanism( mechanism );
-             request.setVersion3( true ) ;
-             
-             // Setup the ASN1 Enoder and Decoder
-             MessageEncoder encoder = new MessageEncoder();
-             MessageDecoder decoder = new MessageDecoder( new BinaryAttributeDetector() {
-                 public boolean isBinary( String attributeId )
-                 {
-                     return false;
-                 }
-             } );
-      
-             // Send encoded request to server
-             encoder.encodeBlocking( null, _output_, request );
-             
-             _output_.flush();
-             
-             while ( _input_.available() <= 0 )
-             {
-                 Thread.sleep( 100 );
-             }
-             
-             // Retrieve the response back from server to my last request.
-             return ( InternalBindResponse ) decoder.decode( null, _input_ );
-         }
-     }
-     
-     
-     private BogusNtlmProvider getNtlmProviderUsingReflection()
-     {
-         BogusNtlmProvider provider = null;
-         try
-         {
-             NtlmMechanismHandler ntlmHandler = ( NtlmMechanismHandler ) ldapServer.getSaslMechanismHandlers().get( SupportedSaslMechanisms.NTLM );
-             
-             // there is no getter for 'provider' field hence this hack
-             Field field = ntlmHandler.getClass().getDeclaredField( "provider" );
-             field.setAccessible( true );
-             provider = ( BogusNtlmProvider ) field.get( ntlmHandler );
-         }
-         catch( Exception e )
-         {
-             e.printStackTrace();
-         }
-         
-         return provider;
-     }
+    /**
+     * Tests to make sure the server properly returns the supportedSASLMechanisms.
+     */
+    @Test
+    public void testSupportedSASLMechanisms()
+    {
+        try
+        {
+            // We have to tell the server that it should accept anonymous
+            // auth, because we are reading the rootDSE
+            ldapServer.setAllowAnonymousAccess( true );
+            ldapServer.getDirectoryService().setAllowAnonymousAccess( true );
+
+            // Point on rootDSE
+            DirContext context = new InitialDirContext();
+
+            Attributes attrs = context.getAttributes( "ldap://localhost:" + ldapServer.getPort(), new String[]
+                { "supportedSASLMechanisms" } );
+
+            //             Thread.sleep( 10 * 60 * 1000 );
+            NamingEnumeration<? extends Attribute> answer = attrs.getAll();
+            Attribute result = answer.next();
+            assertEquals( 6, result.size() );
+            assertTrue( result.contains( SupportedSaslMechanisms.GSSAPI ) );
+            assertTrue( result.contains( SupportedSaslMechanisms.DIGEST_MD5 ) );
+            assertTrue( result.contains( SupportedSaslMechanisms.CRAM_MD5 ) );
+            assertTrue( result.contains( SupportedSaslMechanisms.NTLM ) );
+            assertTrue( result.contains( SupportedSaslMechanisms.PLAIN ) );
+            assertTrue( result.contains( SupportedSaslMechanisms.GSS_SPNEGO ) );
+        }
+        catch ( Exception e )
+        {
+            fail( "Should not have caught exception." );
+        }
+    }
+
 
-}
+    /**
+     * Tests to make sure PLAIN-binds works
+     */
+    @Test
+    public void testSaslBindPLAIN()
+    {
+        try
+        {
+            Hashtable<String, String> env = new Hashtable<String, String>();
+            env.put( Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory" );
+            env.put( Context.PROVIDER_URL, "ldap://localhost:" + ldapServer.getPort() );
+
+            env.put( Context.SECURITY_AUTHENTICATION, "PLAIN" );
+            env.put( Context.SECURITY_PRINCIPAL, "uid=hnelson,ou=users,dc=example,dc=com" );
+            env.put( Context.SECURITY_CREDENTIALS, "secret" );
+
+            DirContext context = new InitialDirContext( env );
+
+            String[] attrIDs =
+                { "uid" };
+
+            Attributes attrs = context.getAttributes( "uid=hnelson,ou=users,dc=example,dc=com", attrIDs );
+            String uid = null;
+
+            if ( attrs.get( "uid" ) != null )
+            {
+                uid = ( String ) attrs.get( "uid" ).get();
+            }
+
+            assertEquals( uid, "hnelson" );
+        }
+        catch ( NamingException e )
+        {
+            fail( "Should not have caught exception." );
+        }
+    }
+
+
+    /**
+     * Test a SASL bind with an empty mechanism 
+     */
+    @Test
+    public void testSaslBindNoMech()
+    {
+        try
+        {
+            Hashtable<String, String> env = new Hashtable<String, String>();
+            env.put( Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory" );
+            env.put( Context.PROVIDER_URL, "ldap://localhost:" + ldapServer.getPort() );
+
+            env.put( Context.SECURITY_AUTHENTICATION, "" );
+            env.put( Context.SECURITY_PRINCIPAL, "uid=hnelson,ou=users,dc=example,dc=com" );
+            env.put( Context.SECURITY_CREDENTIALS, "secret" );
+
+            new InitialDirContext( env );
+            fail( "Should not be there" );
+        }
+        catch ( AuthenticationNotSupportedException anse )
+        {
+            assertTrue( true );
+        }
+        catch ( NamingException ne )
+        {
+            fail( "Should not have caught exception." );
+        }
+    }
+
+
+    /**
+     * Tests to make sure CRAM-MD5 binds below the RootDSE work.
+     */
+    @Test
+    public void testSaslCramMd5Bind()
+    {
+        try
+        {
+            Hashtable<String, String> env = new Hashtable<String, String>();
+            env.put( Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory" );
+            env.put( Context.PROVIDER_URL, "ldap://localhost:" + ldapServer.getPort() );
+
+            env.put( Context.SECURITY_AUTHENTICATION, "CRAM-MD5" );
+            env.put( Context.SECURITY_PRINCIPAL, "hnelson" );
+            env.put( Context.SECURITY_CREDENTIALS, "secret" );
+
+            DirContext context = new InitialDirContext( env );
+
+            String[] attrIDs =
+                { "uid" };
+
+            Attributes attrs = context.getAttributes( "uid=hnelson,ou=users,dc=example,dc=com", attrIDs );
+
+            String uid = null;
+
+            if ( attrs.get( "uid" ) != null )
+            {
+                uid = ( String ) attrs.get( "uid" ).get();
+            }
+
+            assertEquals( uid, "hnelson" );
+        }
+        catch ( NamingException e )
+        {
+            fail( "Should not have caught exception." );
+        }
+    }
+
+
+    /**
+     * Tests to make sure CRAM-MD5 binds below the RootDSE fail if the password is bad.
+     */
+    @Test
+    public void testSaslCramMd5BindBadPassword()
+    {
+        try
+        {
+            Hashtable<String, String> env = new Hashtable<String, String>();
+            env.put( Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory" );
+            env.put( Context.PROVIDER_URL, "ldap://localhost:" + ldapServer.getPort() );
+
+            env.put( Context.SECURITY_AUTHENTICATION, "CRAM-MD5" );
+            env.put( Context.SECURITY_PRINCIPAL, "hnelson" );
+            env.put( Context.SECURITY_CREDENTIALS, "badsecret" );
+
+            DirContext context = new InitialDirContext( env );
+
+            String[] attrIDs =
+                { "uid" };
+
+            context.getAttributes( "uid=hnelson,ou=users,dc=example,dc=com", attrIDs );
+
+            fail( "Should have thrown exception." );
+        }
+        catch ( NamingException e )
+        {
+            assertTrue( e.getMessage().contains( "Invalid response" ) );
+        }
+    }
+
+
+    /**
+     * Tests to make sure DIGEST-MD5 binds below the RootDSE work.
+     */
+    @Test
+    public void testSaslDigestMd5Bind() throws Exception
+    {
+        Hashtable<String, String> env = new Hashtable<String, String>();
+        env.put( Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory" );
+        env.put( Context.PROVIDER_URL, "ldap://localhost:" + ldapServer.getPort() );
+
+        env.put( Context.SECURITY_AUTHENTICATION, "DIGEST-MD5" );
+        env.put( Context.SECURITY_PRINCIPAL, "hnelson" );
+        env.put( Context.SECURITY_CREDENTIALS, "secret" );
+
+        // Specify realm
+        env.put( "java.naming.security.sasl.realm", "example.com" );
+
+        // Request privacy protection
+        env.put( "javax.security.sasl.qop", "auth-conf" );
+
+        DirContext context = new InitialDirContext( env );
+
+        String[] attrIDs =
+            { "uid" };
+
+        Attributes attrs = context.getAttributes( "uid=hnelson,ou=users,dc=example,dc=com", attrIDs );
+
+        String uid = null;
+
+        if ( attrs.get( "uid" ) != null )
+        {
+            uid = ( String ) attrs.get( "uid" ).get();
+        }
+
+        assertEquals( uid, "hnelson" );
+    }
 
+
+    /**
+     * Tests to make sure DIGEST-MD5 binds below the RootDSE fail if the realm is bad.
+     */
+    @Test
+    public void testSaslDigestMd5BindBadRealm()
+    {
+        try
+        {
+            Hashtable<String, String> env = new Hashtable<String, String>();
+            env.put( Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory" );
+            env.put( Context.PROVIDER_URL, "ldap://localhost:" + ldapServer.getPort() );
+
+            env.put( Context.SECURITY_AUTHENTICATION, "DIGEST-MD5" );
+            env.put( Context.SECURITY_PRINCIPAL, "hnelson" );
+            env.put( Context.SECURITY_CREDENTIALS, "secret" );
+
+            // Bad realm
+            env.put( "java.naming.security.sasl.realm", "badrealm.com" );
+
+            // Request privacy protection
+            env.put( "javax.security.sasl.qop", "auth-conf" );
+
+            DirContext context = new InitialDirContext( env );
+
+            String[] attrIDs =
+                { "uid" };
+
+            context.getAttributes( "uid=hnelson,ou=users,dc=example,dc=com", attrIDs );
+
+            fail( "Should have thrown exception." );
+        }
+        catch ( NamingException e )
+        {
+            assertTrue( e.getMessage().contains( "Nonexistent realm" ) );
+        }
+    }
+
+
+    /**
+     * Tests to make sure DIGEST-MD5 binds below the RootDSE fail if the password is bad.
+     */
+    @Test
+    public void testSaslDigestMd5BindBadPassword()
+    {
+        try
+        {
+            Hashtable<String, String> env = new Hashtable<String, String>();
+            env.put( Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory" );
+            env.put( Context.PROVIDER_URL, "ldap://localhost:" + ldapServer.getPort() );
+
+            env.put( Context.SECURITY_AUTHENTICATION, "DIGEST-MD5" );
+            env.put( Context.SECURITY_PRINCIPAL, "hnelson" );
+            env.put( Context.SECURITY_CREDENTIALS, "badsecret" );
+
+            DirContext context = new InitialDirContext( env );
+            String[] attrIDs =
+                { "uid" };
+
+            context.getAttributes( "uid=hnelson,ou=users,dc=example,dc=com", attrIDs );
+            fail( "Should have thrown exception." );
+        }
+        catch ( NamingException e )
+        {
+            assertTrue( e.getMessage().contains( "digest response format violation" ) );
+        }
+    }
+
+
+    /**
+     * Tests that the plumbing for NTLM bind works.
+     */
+    @Test
+    public void testNtlmBind() throws Exception
+    {
+        BogusNtlmProvider provider = getNtlmProviderUsingReflection();
+
+        NtlmSaslBindClient client = new NtlmSaslBindClient( SupportedSaslMechanisms.NTLM );
+        InternalBindResponse type2response = client.bindType1( "type1_test".getBytes() );
+        assertEquals( 1, type2response.getMessageId() );
+        assertEquals( ResultCodeEnum.SASL_BIND_IN_PROGRESS, type2response.getLdapResult().getResultCode() );
+        assertTrue( ArrayUtils.isEquals( "type1_test".getBytes(), provider.getType1Response() ) );
+        assertTrue( ArrayUtils.isEquals( "challenge".getBytes(), type2response.getServerSaslCreds() ) );
+
+        InternalBindResponse finalResponse = client.bindType3( "type3_test".getBytes() );
+        assertEquals( 2, finalResponse.getMessageId() );
+        assertEquals( ResultCodeEnum.SUCCESS, finalResponse.getLdapResult().getResultCode() );
+        assertTrue( ArrayUtils.isEquals( "type3_test".getBytes(), provider.getType3Response() ) );
+    }
+
+
+    /**
+     * Tests that the plumbing for NTLM bind works.
+     */
+    @Test
+    public void testGssSpnegoBind() throws Exception
+    {
+        BogusNtlmProvider provider = new BogusNtlmProvider();
+
+        // the provider configured in @CreateLdapServer only sets for the NTLM mechanism
+        // but we use the same NtlmMechanismHandler class for GSS_SPNEGO too but this is a separate
+        // instance, so we need to set the provider in the NtlmMechanismHandler instance of GSS_SPNEGO mechanism
+        NtlmMechanismHandler ntlmHandler = ( NtlmMechanismHandler ) ldapServer.getSaslMechanismHandlers().get(
+            SupportedSaslMechanisms.GSS_SPNEGO );
+        ntlmHandler.setNtlmProvider( provider );
+
+        NtlmSaslBindClient client = new NtlmSaslBindClient( SupportedSaslMechanisms.GSS_SPNEGO );
+        InternalBindResponse type2response = client.bindType1( "type1_test".getBytes() );
+        assertEquals( 1, type2response.getMessageId() );
+        assertEquals( ResultCodeEnum.SASL_BIND_IN_PROGRESS, type2response.getLdapResult().getResultCode() );
+        assertTrue( ArrayUtils.isEquals( "type1_test".getBytes(), provider.getType1Response() ) );
+        assertTrue( ArrayUtils.isEquals( "challenge".getBytes(), type2response.getServerSaslCreds() ) );
+
+        InternalBindResponse finalResponse = client.bindType3( "type3_test".getBytes() );
+        assertEquals( 2, finalResponse.getMessageId() );
+        assertEquals( ResultCodeEnum.SUCCESS, finalResponse.getLdapResult().getResultCode() );
+        assertTrue( ArrayUtils.isEquals( "type3_test".getBytes(), provider.getType3Response() ) );
+    }
+
+    /**
+     * A NTLM client
+     */
+    class NtlmSaslBindClient extends SocketClient
+    {
+        private final Logger LOG = LoggerFactory.getLogger( NtlmSaslBindClient.class );
+
+        private final String mechanism;
+
+
+        NtlmSaslBindClient( String mechanism ) throws Exception
+        {
+            this.mechanism = mechanism;
+            setDefaultPort( ldapServer.getPort() );
+            connect( "localhost", ldapServer.getPort() );
+            setTcpNoDelay( false );
+
+            LOG.debug( "isConnected() = {}", isConnected() );
+            LOG.debug( "LocalPort     = {}", getLocalPort() );
+            LOG.debug( "LocalAddress  = {}", getLocalAddress() );
+            LOG.debug( "RemotePort    = {}", getRemotePort() );
+            LOG.debug( "RemoteAddress = {}", getRemoteAddress() );
+        }
+
+
+        InternalBindResponse bindType1( byte[] type1response ) throws Exception
+        {
+            if ( !isConnected() )
+            {
+                throw new IllegalStateException( "Client is not connected." );
+            }
+
+            // Setup the bind request
+            BindRequestImpl request = new BindRequestImpl( 1 );
+            request.setName( new DN( "uid=admin,ou=system" ) );
+            request.setSimple( false );
+            request.setCredentials( type1response );
+            request.setSaslMechanism( mechanism );
+            request.setVersion3( true );
+
+            // Setup the ASN1 Encoder and Decoder
+            MessageDecoder decoder = new MessageDecoder( new BinaryAttributeDetector()
+            {
+                public boolean isBinary( String attributeId )
+                {
+                    return false;
+                }
+            } );
+
+            // Send encoded request to server
+            LdapMessageCodec ldapRequest = ( LdapMessageCodec ) LdapTransformer.transform( ( InternalMessage ) request );
+            ByteBuffer bb = ldapRequest.encode();
+            bb.flip();
+
+            _output_.write( bb.array() );
+            _output_.flush();
+
+            while ( _input_.available() <= 0 )
+            {
+                Thread.sleep( 100 );
+            }
+
+            // Retrieve the response back from server to my last request.
+            return ( InternalBindResponse ) decoder.decode( null, _input_ );
+        }
+
+
+        InternalBindResponse bindType3( byte[] type3response ) throws Exception
+        {
+            if ( !isConnected() )
+            {
+                throw new IllegalStateException( "Client is not connected." );
+            }
+
+            // Setup the bind request
+            BindRequestImpl request = new BindRequestImpl( 2 );
+            request.setName( new DN( "uid=admin,ou=system" ) );
+            request.setSimple( false );
+            request.setCredentials( type3response );
+            request.setSaslMechanism( mechanism );
+            request.setVersion3( true );
+
+            // Setup the ASN1 Enoder and Decoder
+            MessageDecoder decoder = new MessageDecoder( new BinaryAttributeDetector()
+            {
+                public boolean isBinary( String attributeId )
+                {
+                    return false;
+                }
+            } );
+
+            // Send encoded request to server
+            LdapMessageCodec ldapRequest = ( LdapMessageCodec ) LdapTransformer.transform( ( InternalMessage ) request );
+            ByteBuffer bb = ldapRequest.encode();
+            bb.flip();
+
+            _output_.write( bb.array() );
+            _output_.flush();
+
+            while ( _input_.available() <= 0 )
+            {
+                Thread.sleep( 100 );
+            }
+
+            // Retrieve the response back from server to my last request.
+            return ( InternalBindResponse ) decoder.decode( null, _input_ );
+        }
+    }
+
+
+    private BogusNtlmProvider getNtlmProviderUsingReflection()
+    {
+        BogusNtlmProvider provider = null;
+        try
+        {
+            NtlmMechanismHandler ntlmHandler = ( NtlmMechanismHandler ) ldapServer.getSaslMechanismHandlers().get(
+                SupportedSaslMechanisms.NTLM );
+
+            // there is no getter for 'provider' field hence this hack
+            Field field = ntlmHandler.getClass().getDeclaredField( "provider" );
+            field.setAccessible( true );
+            provider = ( BogusNtlmProvider ) field.get( ntlmHandler );
+        }
+        catch ( Exception e )
+        {
+            e.printStackTrace();
+        }
+
+        return provider;
+    }
+
+}

Modified: directory/shared/branches/shared-codec-merge/asn1-codec/src/main/java/org/apache/directory/shared/asn1/codec/Asn1CodecDecoder.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-codec-merge/asn1-codec/src/main/java/org/apache/directory/shared/asn1/codec/Asn1CodecDecoder.java?rev=984184&r1=984183&r2=984184&view=diff
==============================================================================
--- directory/shared/branches/shared-codec-merge/asn1-codec/src/main/java/org/apache/directory/shared/asn1/codec/Asn1CodecDecoder.java (original)
+++ directory/shared/branches/shared-codec-merge/asn1-codec/src/main/java/org/apache/directory/shared/asn1/codec/Asn1CodecDecoder.java Tue Aug 10 19:42:21 2010
@@ -37,9 +37,9 @@ public class Asn1CodecDecoder extends Pr
 {
     /** The stateful decoder */
     private final StatefulDecoder decoder;
-    
-    /** The associated callback */
-    private final DecoderCallbackImpl callback = new DecoderCallbackImpl();
+
+    /** The Output queue */
+    private ProtocolDecoderOutput decOut;
 
 
     /**
@@ -50,7 +50,13 @@ public class Asn1CodecDecoder extends Pr
     public Asn1CodecDecoder( StatefulDecoder decoder )
     {
         this.decoder = decoder;
-        this.decoder.setCallback( callback );
+        this.decoder.setCallback( new DecoderCallback()
+        {
+            public void decodeOccurred( StatefulDecoder decoder, Object decoded )
+            {
+                decOut.write( decoded );
+            }
+        } );
     }
 
 
@@ -59,18 +65,7 @@ public class Asn1CodecDecoder extends Pr
      */
     public void decode( IoSession session, IoBuffer in, ProtocolDecoderOutput out ) throws DecoderException
     {
-        callback.decOut = out;
+        decOut = out;
         decoder.decode( in.buf() );
     }
-
-
-    private class DecoderCallbackImpl implements DecoderCallback
-    {
-        private ProtocolDecoderOutput decOut;
-
-        public void decodeOccurred( StatefulDecoder decoder, Object decoded )
-        {
-            decOut.write( decoded );
-        }
-    }
 }

Modified: directory/shared/branches/shared-codec-merge/asn1/src/main/java/org/apache/directory/shared/asn1/codec/stateful/AbstractStatefulDecoder.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-codec-merge/asn1/src/main/java/org/apache/directory/shared/asn1/codec/stateful/AbstractStatefulDecoder.java?rev=984184&r1=984183&r2=984184&view=diff
==============================================================================
--- directory/shared/branches/shared-codec-merge/asn1/src/main/java/org/apache/directory/shared/asn1/codec/stateful/AbstractStatefulDecoder.java (original)
+++ directory/shared/branches/shared-codec-merge/asn1/src/main/java/org/apache/directory/shared/asn1/codec/stateful/AbstractStatefulDecoder.java Tue Aug 10 19:42:21 2010
@@ -50,10 +50,9 @@ public abstract class AbstractStatefulDe
     /**
      * Creates a stateful decoder with a callback.
      * 
-     * @param cb
-     *            the callback to use for this decoder
+     * @param cb the callback to use for this decoder
      */
-    public AbstractStatefulDecoder(DecoderCallback cb)
+    public AbstractStatefulDecoder( DecoderCallback cb )
     {
         setCallback( cb );
     }
@@ -62,10 +61,9 @@ public abstract class AbstractStatefulDe
     /**
      * Creates a stateful decoder with a monitor but no callback.
      * 
-     * @param monitor
-     *            the monitor to use for this decoder
+     * @param monitor the monitor to use for this decoder
      */
-    public AbstractStatefulDecoder(DecoderMonitor monitor)
+    public AbstractStatefulDecoder( DecoderMonitor monitor )
     {
         this.monitor = monitor;
     }
@@ -74,12 +72,10 @@ public abstract class AbstractStatefulDe
     /**
      * Creates a stateful decoder.
      * 
-     * @param cb
-     *            the callback to use for this decoder
-     * @param monitor
-     *            the monitor to use for this decoder
+     * @param cb the callback to use for this decoder
+     * @param monitor the monitor to use for this decoder
      */
-    public AbstractStatefulDecoder(DecoderCallback cb, DecoderMonitor monitor)
+    public AbstractStatefulDecoder( DecoderCallback cb, DecoderMonitor monitor )
     {
         this.monitor = monitor;
         setCallback( cb );
@@ -89,12 +85,8 @@ public abstract class AbstractStatefulDe
     // ------------------------------------------------------------------------
     // StatefulDecoder methods
     // ------------------------------------------------------------------------
-
-    /*
-     * (non-Javadoc)
-     * 
-     * @see org.apache.asn1.codec.stateful.StatefulDecoder#setCallback(
-     *      org.apache.asn1.codec.stateful.DecoderCallback)
+    /**
+     * {@inheritDoc}
      */
     public void setCallback( DecoderCallback cb )
     {
@@ -108,11 +100,17 @@ public abstract class AbstractStatefulDe
     }
 
 
-    /*
-     * (non-Javadoc)
-     * 
-     * @see org.apache.asn1.codec.stateful.StatefulDecoder#setDecoderMonitor(
-     *      org.apache.asn1.codec.stateful.DecoderMonitor)
+    /**
+     * {@inheritDoc}
+     */
+    public DecoderCallback getCallback()
+    {
+        return cb;
+    }
+
+
+    /**
+     * {@inheritDoc}
      */
     public void setDecoderMonitor( DecoderMonitor monitor )
     {

Modified: directory/shared/branches/shared-codec-merge/asn1/src/main/java/org/apache/directory/shared/asn1/codec/stateful/StatefulDecoder.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-codec-merge/asn1/src/main/java/org/apache/directory/shared/asn1/codec/stateful/StatefulDecoder.java?rev=984184&r1=984183&r2=984184&view=diff
==============================================================================
--- directory/shared/branches/shared-codec-merge/asn1/src/main/java/org/apache/directory/shared/asn1/codec/stateful/StatefulDecoder.java (original)
+++ directory/shared/branches/shared-codec-merge/asn1/src/main/java/org/apache/directory/shared/asn1/codec/stateful/StatefulDecoder.java Tue Aug 10 19:42:21 2010
@@ -24,7 +24,7 @@ import org.apache.directory.shared.asn1.
 
 
 /**
- * A decoder which decodes encoded data as it arrives in peices while
+ * A decoder which decodes encoded data as it arrives in pieces while
  * maintaining the state of the decode operation between the arrival of encoded
  * chunks. As chunks of encoded data arrive the decoder processes each chunk of
  * encoded data and maintains decoding state in between arrivals: it is hence
@@ -39,7 +39,7 @@ public interface StatefulDecoder
 {
     /**
      * Decodes a piece of encoded data. The nature of this call, synchronous
-     * verses asynchonous, with respect to driving the actual decoding of the
+     * verses asyncrhonous, with respect to driving the actual decoding of the
      * encoded data argument is determined by an implementation. A return from
      * this method does not guarantee any callbacks: zero or more callbacks may
      * occur during this call.
@@ -53,17 +53,13 @@ public interface StatefulDecoder
     /**
      * Sets the callback for this StatefulDecoder.
      * 
-     * @param cb
-     *            the callback to inform of a complete decode operation
+     * @param cb the callback to inform of a complete decode operation
      */
     void setCallback( DecoderCallback cb );
 
 
     /**
-     * Monitors all kinds of events that occur during processing.
-     * 
-     * @param monitor
-     *            to set for this StatefulDecoder
+     * @return The decoder callback
      */
-    void setDecoderMonitor( DecoderMonitor monitor );
+    DecoderCallback getCallback();
 }

Modified: directory/shared/branches/shared-codec-merge/ldap/pom.xml
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-codec-merge/ldap/pom.xml?rev=984184&r1=984183&r2=984184&view=diff
==============================================================================
--- directory/shared/branches/shared-codec-merge/ldap/pom.xml (original)
+++ directory/shared/branches/shared-codec-merge/ldap/pom.xml Tue Aug 10 19:42:21 2010
@@ -66,6 +66,11 @@
       <groupId>commons-collections</groupId>
       <artifactId>commons-collections</artifactId>
     </dependency>
+
+    <dependency>
+      <groupId>org.apache.mina</groupId>
+      <artifactId>mina-core</artifactId>
+    </dependency>
   </dependencies>
 
   <build>

Modified: directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/codec/LdapDecoder.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/codec/LdapDecoder.java?rev=984184&r1=984183&r2=984184&view=diff
==============================================================================
--- directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/codec/LdapDecoder.java (original)
+++ directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/codec/LdapDecoder.java Tue Aug 10 19:42:21 2010
@@ -27,7 +27,6 @@ import org.apache.directory.shared.asn1.
 import org.apache.directory.shared.asn1.ber.tlv.TLVStateEnum;
 import org.apache.directory.shared.asn1.codec.DecoderException;
 import org.apache.directory.shared.asn1.codec.stateful.DecoderCallback;
-import org.apache.directory.shared.asn1.codec.stateful.DecoderMonitor;
 import org.apache.directory.shared.i18n.I18n;
 import org.apache.directory.shared.ldap.message.spi.BinaryAttributeDetector;
 import org.apache.directory.shared.ldap.message.spi.Provider;
@@ -290,22 +289,21 @@ public class LdapDecoder implements Prov
 
 
     /**
-     * Not used ...
+     * Set the callback to call when the PDU has been decoded
      * 
-     * @deprecated
+     * @param cb The callback
      */
-    public void setDecoderMonitor( DecoderMonitor monitor )
+    public void setCallback( DecoderCallback cb )
     {
+        decoderCallback = cb;
     }
 
 
     /**
-     * Set the callback to call when the PDU has been decoded
-     * 
-     * @param cb The callback
+     * {@inheritDoc}
      */
-    public void setCallback( DecoderCallback cb )
+    public DecoderCallback getCallback()
     {
-        decoderCallback = cb;
+        return decoderCallback;
     }
 }

Added: 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=984184&view=auto
==============================================================================
--- directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/codec/LdapProtocolEncoder.java (added)
+++ directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/codec/LdapProtocolEncoder.java Tue Aug 10 19:42:21 2010
@@ -0,0 +1,56 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.directory.shared.ldap.codec;
+
+
+import java.nio.ByteBuffer;
+
+import org.apache.directory.shared.ldap.message.internal.InternalMessage;
+import org.apache.mina.core.buffer.IoBuffer;
+import org.apache.mina.core.session.IoSession;
+import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
+import org.apache.mina.filter.codec.ProtocolEncoderOutput;
+
+
+/**
+ * LDAP BER encoder.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class LdapProtocolEncoder extends ProtocolEncoderAdapter
+{
+    /**
+     * Encode a Ldap request and write it to the remote server.
+     * 
+     * @param session The session containing the LdapMessageContainer
+     * @param request The LDAP message we have to encode to a Byte stream
+     * @param out The callback we have to invoke when the message has been encoded 
+     */
+    public void encode( IoSession session, Object request, ProtocolEncoderOutput out ) throws Exception
+    {
+        LdapMessageCodec ldapRequest = ( LdapMessageCodec ) LdapTransformer.transform( ( InternalMessage ) request );
+        ByteBuffer bb = ldapRequest.encode();
+        bb.flip();
+
+        IoBuffer buffer = IoBuffer.wrap( bb );
+
+        out.write( buffer );
+    }
+}

Modified: directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/message/MessageDecoder.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/message/MessageDecoder.java?rev=984184&r1=984183&r2=984184&view=diff
==============================================================================
--- directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/message/MessageDecoder.java (original)
+++ directory/shared/branches/shared-codec-merge/ldap/src/main/java/org/apache/directory/shared/ldap/message/MessageDecoder.java Tue Aug 10 19:42:21 2010
@@ -20,20 +20,19 @@
 package org.apache.directory.shared.ldap.message;
 
 
+import java.io.InputStream;
+import java.util.Hashtable;
+
 import org.apache.directory.shared.asn1.Asn1Object;
 import org.apache.directory.shared.asn1.codec.DecoderException;
 import org.apache.directory.shared.asn1.codec.stateful.DecoderCallback;
-import org.apache.directory.shared.asn1.codec.stateful.DecoderMonitor;
 import org.apache.directory.shared.asn1.codec.stateful.StatefulDecoder;
-import org.apache.directory.shared.ldap.codec.ResponseCarryingException;
 import org.apache.directory.shared.ldap.codec.LdapTransformer;
+import org.apache.directory.shared.ldap.codec.ResponseCarryingException;
 import org.apache.directory.shared.ldap.message.spi.BinaryAttributeDetector;
 import org.apache.directory.shared.ldap.message.spi.Provider;
 import org.apache.directory.shared.ldap.message.spi.ProviderDecoder;
 
-import java.io.InputStream;
-import java.util.Hashtable;
-
 
 /**
  * Decodes a BER encoded LDAPv3 message envelope from an input stream
@@ -64,8 +63,8 @@ public final class MessageDecoder implem
     {
         this( binaryAttributeDetector, Integer.MAX_VALUE );
     }
-    
-    
+
+
     /**
      * Creates a MessageDecoder using default properties for enabling a BER
      * library provider.
@@ -78,7 +77,7 @@ public final class MessageDecoder implem
     {
         // We need to get the encoder class name
         Hashtable<Object, Object> providerEnv = Provider.getEnvironment();
-        
+
         this.provider = Provider.getProvider( providerEnv );
         this.decoder = this.provider.getDecoder( binaryAttributeDetector, maxPDUSize );
         this.decoder.setCallback( new DecoderCallback()
@@ -102,13 +101,10 @@ public final class MessageDecoder implem
      * Reads and decodes a BER encoded LDAPv3 ASN.1 message envelope structure
      * from an input stream to build a fully populated Message object instance.
      * 
-     * @param lock
-     *            lock object used to exclusively read from the input stream
-     * @param in
-     *            the input stream to read PDU data from.
+     * @param lock lock object used to exclusively read from the input stream
+     * @param in the input stream to read PDU data from.
      * @return the populated Message representing the PDU envelope.
-     * @throws MessageException
-     *             if there is a problem decoding.
+     * @throws MessageException if there is a problem decoding.
      */
     public Object decode( final Object lock, final InputStream in ) throws MessageException
     {
@@ -119,7 +115,7 @@ public final class MessageDecoder implem
             if ( lock == null )
             {
                 // Complain here somehow first then do the following w/o synch!
-    
+
                 // Call provider decoder to demarshall PDU into berlib specific form
                 providerEnvelope = decoder.decode( lock, in );
             }
@@ -147,10 +143,8 @@ public final class MessageDecoder implem
      * Decodes a chunk of stream data returning any resultant decoded PDU via a
      * callback.
      * 
-     * @param chunk
-     *            the chunk to decode
-     * @throws MessageException
-     *             if there are failures while decoding the chunk
+     * @param chunk the chunk to decode
+     * @throws MessageException if there are failures while decoding the chunk
      */
     public void decode( Object chunk ) throws MessageException
     {
@@ -161,11 +155,11 @@ public final class MessageDecoder implem
         catch ( DecoderException e )
         {
             // transform the DecoderException message to a MessageException
-            if ( e instanceof ResponseCarryingException ) 
+            if ( e instanceof ResponseCarryingException )
             {
                 ResponseCarryingMessageException rcme = new ResponseCarryingMessageException( e.getMessage() );
-                rcme.setResponse( ((ResponseCarryingException)e).getResponse() );
-                
+                rcme.setResponse( ( ( ResponseCarryingException ) e ).getResponse() );
+
                 throw rcme;
             }
             else
@@ -180,8 +174,7 @@ public final class MessageDecoder implem
     /**
      * Sets the callback used to deliver completly decoded PDU's.
      * 
-     * @param cb
-     *            the callback to use for decoded PDU delivery
+     * @param cb the callback to use for decoded PDU delivery
      */
     public void setCallback( DecoderCallback cb )
     {
@@ -190,18 +183,17 @@ public final class MessageDecoder implem
 
 
     /**
-     * Sets the monitor for this MessageDecoder which receives callbacks for
-     * noteworthy events during decoding.
-     * 
-     * @param monitor
-     *            the monitor to receive notifications via callback events
+     * {@inheritDoc}
      */
-    public void setDecoderMonitor( DecoderMonitor monitor )
+    public DecoderCallback getCallback()
     {
-        decoder.setDecoderMonitor( monitor );
+        return cb;
     }
 
 
+    /**
+     * {@inheritDoc}
+     */
     public Provider getProvider()
     {
         return this.provider;



Mime
View raw message