// Check if we received serverside sort Control
SortRequest sortControl = ( SortRequest ) searchRequest.getControls().get( SortRequest.OID );
SortResponse sortRespCtrl = null;
ResultResponse done = searchRequest.getResultResponse();
LdapResult ldapResult = done.getLdapResult();
if ( sortControl != null )
{
sortRespCtrl = canSort( sortControl, ldapResult, getDirectoryService().getSchemaManager() );
if ( sortControl.isCritical() && ( sortRespCtrl.getSortResult() != SortResultCode.SUCCESS ) )
{
ldapResult.setResultCode( ResultCodeEnum.UNAVAILABLE_CRITICAL_EXTENSION );
done.addControl( sortRespCtrl );
return new EmptyCursor<Entry>();
}
}
Cursor<Entry> cursor = null;
try
{
cursor = operationManager.search( searchContext );
if ( ( sortRespCtrl != null ) && ( sortRespCtrl.getSortResult() == SortResultCode.SUCCESS ) )
{
cursor = sortResults( cursor, sortControl, getDirectoryService().getSchemaManager() );
}
// the below condition is to satisfy the scenario 6 in section 2 of rfc2891
if ( sortRespCtrl != null )
{
cursor.beforeFirst();
if ( !cursor.next() )
{
sortRespCtrl = null;
}
else
{
// move the cursor back
cursor.previous();
}
}
}
catch ( LdapException e )
{
done.addAllControls( searchContext.getResponseControls() );
throw e;
}
catch ( Exception e )
{
done.addAllControls( searchContext.getResponseControls() );
throw new LdapException( e );
}
if ( sortRespCtrl != null )
{
done.addControl( sortRespCtrl );
}
done.addAllControls( searchContext.getResponseControls() );
return cursor;
}