// for the requested mechanism. We then produce an Exception
if ( sc == null )
{
String message = "Cannot find a SASL factory for the " + bindRequest.getSaslMechanism() + " mechanism";
LOG.error( message );
throw new LdapException( message );
}
// Corner case : the SASL mech might send an initial challenge, and we have to
// deal with it immediately.
if ( sc.hasInitialResponse() )
{
byte[] challengeResponse = sc.evaluateChallenge( new byte[0] );
// Stores the challenge's response, and send it to the server
bindRequest.setCredentials( challengeResponse );
writeRequest( bindRequest );
// Get the server's response, blocking
bindResponse = bindFuture.get( timeout, TimeUnit.MILLISECONDS );
if ( bindResponse == null )
{
// We didn't received anything : this is an error
LOG.error( "bind failed : timeout occurred" );
throw new LdapException( TIME_OUT_ERROR );
}
result = bindResponse.getLdapResult().getResultCode();
}
else
{
// Copy the bindRequest without setting the credentials
BindRequest bindRequestCopy = new BindRequestImpl();
bindRequestCopy.setMessageId( newId );
bindRequestCopy.setName( bindRequest.getName() );
bindRequestCopy.setSaslMechanism( bindRequest.getSaslMechanism() );
bindRequestCopy.setSimple( bindRequest.isSimple() );
bindRequestCopy.setVersion3( bindRequest.getVersion3() );
bindRequestCopy.addAllControls( bindRequest.getControls().values().toArray( new Control[0] ) );
writeRequest( bindRequestCopy );
bindResponse = bindFuture.get( timeout, TimeUnit.MILLISECONDS );
if ( bindResponse == null )
{
// We didn't received anything : this is an error
LOG.error( "bind failed : timeout occurred" );
throw new LdapException( TIME_OUT_ERROR );
}
result = bindResponse.getLdapResult().getResultCode();
}
while ( !sc.isComplete()
&& ( ( result == ResultCodeEnum.SASL_BIND_IN_PROGRESS ) || ( result == ResultCodeEnum.SUCCESS ) ) )
{
response = sc.evaluateChallenge( bindResponse.getServerSaslCreds() );
if ( result == ResultCodeEnum.SUCCESS )
{
if ( response != null )
{
throw new LdapException( "protocol error" );
}
}
else
{
newId = messageId.incrementAndGet();
bindRequest.setMessageId( newId );
bindRequest.setCredentials( response );
addToFutureMap( newId, bindFuture );
writeRequest( bindRequest );
bindResponse = bindFuture.get( timeout, TimeUnit.MILLISECONDS );
if ( bindResponse == null )
{
// We didn't received anything : this is an error
LOG.error( "bind failed : timeout occurred" );
throw new LdapException( TIME_OUT_ERROR );
}
result = bindResponse.getLdapResult().getResultCode();
}
}
bindFuture.set( bindResponse );
return bindFuture;
}
catch ( LdapException e )
{
throw e;
}
catch ( Exception e )
{
e.printStackTrace();
throw new LdapException( e );
}
}