buffer.put( TLV.getBytes( getBindRequestLength() ) );
}
catch ( BufferOverflowException boe )
{
throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
}
// The version (LDAP V3 only)
Value.encode( buffer, 3 );
Dn dn = getDn();
if ( !Dn.isNullOrEmpty( dn ) )
{
// A DN has been provided
Value.encode( buffer, Dn.getBytes( dn ) );
}
else
{
// No DN has been provided, let's use the name as a string instead
String name = getName();
if ( Strings.isEmpty( name ) )
{
name = "";
}
Value.encode( buffer, name.getBytes() );
}
byte[] credentials = getCredentials();
// The authentication
if ( isSimple() )
{
// Simple authentication
try
{
// The simpleAuthentication Tag
buffer.put( ( byte ) LdapConstants.BIND_REQUEST_SIMPLE_TAG );
if ( credentials != null )
{
buffer.put( TLV.getBytes( credentials.length ) );
if ( credentials.length != 0 )
{
buffer.put( credentials );
}
}
else
{
buffer.put( ( byte ) 0 );
}
}
catch ( BufferOverflowException boe )
{
String msg = I18n.err( I18n.ERR_04005 );
throw new EncoderException( msg );
}
}
else
{
// SASL Bind
try
{
// The saslAuthentication Tag
buffer.put( ( byte ) LdapConstants.BIND_REQUEST_SASL_TAG );
byte[] mechanismBytes = Strings.getBytesUtf8( getSaslMechanism() );
buffer.put( TLV
.getBytes( getSaslMechanismLength() + getSaslCredentialsLength() ) );
Value.encode( buffer, mechanismBytes );
if ( credentials != null )
{
Value.encode( buffer, credentials );
}
}
catch ( BufferOverflowException boe )
{
String msg = I18n.err( I18n.ERR_04005 );
throw new EncoderException( msg );
}
}
return buffer;
}