return abandonPagedSearch( session, req );
}
// Now, depending on the cookie, we will deal with case 2, 3, 4 and 5
byte[] cookie = pagedSearchControl.getCookie();
LdapResult ldapResult = req.getResultResponse().getLdapResult();
if ( Strings.isEmpty( cookie ) )
{
// No cursor : do a search.
cursor = session.getCoreSession().search( req );
// Position the cursor at the beginning
cursor.beforeFirst();
// This is a new search. We have a special case when the paged size
// is above the server size limit : in this case, we default to a
// standard search
if ( pagedLimit > sizeLimit )
{
// Normal search : create the cursor, and set pagedControl to false
try
{
// And write the entries
writeResults( session, req, ldapResult, cursor, sizeLimit );
}
finally
{
try
{
cursor.close();
}
catch ( Exception e )
{
LOG.error( I18n.err( I18n.ERR_168 ), e );
}
}
// If we had a cookie in the session, remove it
removeContext( session, pagedContext );
return req.getResultResponse();
}
else
{
// Case 2 : create the context
pagedContext = new PagedSearchContext( req );
session.addPagedSearchContext( pagedContext );
cookie = pagedContext.getCookie();
pagedResultsControl = new PagedResultsDecorator( ldapServer.getDirectoryService()
.getLdapCodecService() );
pagedResultsControl.setCookie( cookie );
pagedResultsControl.setSize( 0 );
pagedResultsControl.setCritical( true );
// And stores the cursor into the session
pagedContext.setCursor( cursor );
}
}
else
{
// We have a cookie
// Either case 3, 4 or 5
int cookieValue = pagedSearchControl.getCookieValue();
pagedContext = session.getPagedSearchContext( cookieValue );
if ( pagedContext == null )
{
// We didn't found the cookie into the session : it must be invalid
// send an error.
ldapResult.setDiagnosticMessage( "Invalid cookie for this PagedSearch request." );
ldapResult.setResultCode( ResultCodeEnum.UNWILLING_TO_PERFORM );
return req.getResultResponse();
}
if ( pagedContext.hasSameRequest( req, session ) )