* Create a BindRequest ready to be sent.
*/
private BindRequestCodec createBindMessage( BindRequest bindRequest ) throws LdapException
{
// Create a new codec BindRequest object
BindRequestCodec bindRequestCodec = new BindRequestCodec();
// clear the mappings if any (in case of a second call to bind() without calling unBind())
//clearMaps();
// Set the new messageId
int newId = messageId.incrementAndGet();
bindRequest.setMessageId( newId );
bindRequestCodec.setMessageId( newId );
// Set the version
bindRequestCodec.setVersion( LdapConnectionConfig.LDAP_V3 );
// Set the name
try
{
DN dn = new DN( bindRequest.getName() );
bindRequestCodec.setName( dn );
}
catch ( InvalidNameException ine )
{
String msg = "The given dn '" + bindRequest.getName() + "' is not valid";
LOG.error( msg );
LdapException ldapException = new LdapException( msg );
ldapException.initCause( ine );
throw ldapException;
}
// Set the credentials
LdapAuthentication authentication = null;
if ( bindRequest.isSimple() )
{
// Simple bind
authentication = new SimpleAuthentication();
( ( SimpleAuthentication ) authentication ).setSimple( bindRequest.getCredentials() );
}
else
{
// SASL bind
authentication = new SaslCredentials();
( ( SaslCredentials ) authentication ).setCredentials( bindRequest.getCredentials() );
( ( SaslCredentials ) authentication ).setMechanism( bindRequest.getSaslMechanism() );
}
// The authentication
bindRequestCodec.setAuthentication( authentication );
// Add the controls
setControls( bindRequest.getControls(), bindRequestCodec );
return bindRequestCodec;