Java Code Examples for org.apache.directory.api.ldap.model.message.SearchRequest#addAbandonListener()

The following examples show how to use org.apache.directory.api.ldap.model.message.SearchRequest#addAbandonListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SearchRequestHandler.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
/**
 * Handles search requests containing the persistent search decorator but
 * delegates to doSimpleSearch() if the changesOnly parameter of the
 * decorator is set to false.
 *
 * @param session the LdapSession for which this search is conducted
 * @param req the search request containing the persistent search decorator
 * @param psearchDecorator the persistent search decorator extracted
 * @throws Exception if failures are encountered while searching
 */
private void handlePersistentSearch( LdapSession session, SearchRequest req,
    PersistentSearch psearch ) throws Exception
{
    /*
     * We want the search to complete first before we start listening to
     * events when the decorator does NOT specify changes ONLY mode.
     */
    if ( !psearch.isChangesOnly() )
    {
        SearchResultDone done = doSimpleSearch( session, req );

        // ok if normal search beforehand failed somehow quickly abandon psearch
        if ( done.getLdapResult().getResultCode() != ResultCodeEnum.SUCCESS )
        {
            session.getIoSession().write( done );
            return;
        }
    }

    if ( req.isAbandoned() )
    {
        return;
    }

    // now we process entries forever as they change
    PersistentSearchListener persistentSearchListener = new PersistentSearchListener( session, req );

    // compose notification criteria and add the listener to the event
    // service using that notification criteria to determine which events
    // are to be delivered to the persistent search issuing client
    NotificationCriteria criteria = new NotificationCriteria();
    criteria.setAliasDerefMode( req.getDerefAliases() );
    criteria.setBase( req.getBase() );
    criteria.setFilter( req.getFilter() );
    criteria.setScope( req.getScope() );
    criteria.setEventMask( EventType.getEventTypes( psearch.getChangeTypes() ) );
    getLdapServer().getDirectoryService().getEventService().addListener( persistentSearchListener, criteria );
    req.addAbandonListener( new SearchAbandonListener( ldapServer, persistentSearchListener ) );
}
 
Example 2
Source File: SearchRequestHandler.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
/**
 * Handles search requests containing the persistent search decorator but
 * delegates to doSimpleSearch() if the changesOnly parameter of the
 * decorator is set to false.
 *
 * @param session the LdapSession for which this search is conducted
 * @param req the search request containing the persistent search decorator
 * @param psearchDecorator the persistent search decorator extracted
 * @throws Exception if failures are encountered while searching
 */
private void handlePersistentSearch( LdapSession session, SearchRequest req,
    PersistentSearch psearch ) throws Exception
{
    /*
     * We want the search to complete first before we start listening to
     * events when the decorator does NOT specify changes ONLY mode.
     */
    if ( !psearch.isChangesOnly() )
    {
        SearchResultDone done = doSimpleSearch( session, req );

        // ok if normal search beforehand failed somehow quickly abandon psearch
        if ( done.getLdapResult().getResultCode() != ResultCodeEnum.SUCCESS )
        {
            session.getIoSession().write( done );
            return;
        }
    }

    if ( req.isAbandoned() )
    {
        return;
    }

    // now we process entries forever as they change
    PersistentSearchListener persistentSearchListener = new PersistentSearchListener( session, req );

    // compose notification criteria and add the listener to the event
    // service using that notification criteria to determine which events
    // are to be delivered to the persistent search issuing client
    NotificationCriteria criteria = new NotificationCriteria();
    criteria.setAliasDerefMode( req.getDerefAliases() );
    criteria.setBase( req.getBase() );
    criteria.setFilter( req.getFilter() );
    criteria.setScope( req.getScope() );
    criteria.setEventMask( EventType.getEventTypes( psearch.getChangeTypes() ) );
    getLdapServer().getDirectoryService().getEventService().addListener( persistentSearchListener, criteria );
    req.addAbandonListener( new SearchAbandonListener( ldapServer, persistentSearchListener ) );
}
 
Example 3
Source File: SearchRequestHandler.java    From MyVirtualDirectory with Apache License 2.0 4 votes vote down vote up
private void readPagedResults( LdapSession session, SearchRequest req, LdapResult ldapResult,
    EntryFilteringCursor cursor, long sizeLimit, int pagedLimit, PagedSearchContext pagedContext,
    PagedResultsDecorator pagedResultsControl ) throws Exception
{
    req.addAbandonListener( new SearchAbandonListener( ldapServer, cursor ) );
    setTimeLimitsOnCursor( req, session, cursor );

    if ( IS_DEBUG )
    {
        LOG.debug( "using <{},{}> for size limit", sizeLimit, pagedLimit );
    }

    int cookieValue = 0;

    int count = pagedContext.getCurrentPosition();
    int pageCount = 0;

    while ( ( count < sizeLimit ) && ( pageCount < pagedLimit ) && cursor.next() )
    {
        if ( session.getIoSession().isClosing() )
        {
            break;
        }

        Entry entry = cursor.get();
        session.getIoSession().write( generateResponse( session, req, entry ) );
        count++;
        pageCount++;
    }

    // DO NOT WRITE THE RESPONSE - JUST RETURN IT
    ldapResult.setResultCode( ResultCodeEnum.SUCCESS );

    boolean hasMoreEntry = cursor.next();

    // We have some entry, move back to the first one, as we just moved forward 
    // to get the first entry
    if ( hasMoreEntry )
    {
        cursor.previous();
    }

    if ( !hasMoreEntry )
    {
        // That means we don't have anymore entry
        // If we are here, it means we have returned all the entries
        // We have to remove the cookie from the session
        cookieValue = pagedContext.getCookieValue();
        PagedSearchContext psCookie = session.removePagedSearchContext( cookieValue );

        // Close the cursor if there is one
        if ( psCookie != null )
        {
            cursor = psCookie.getCursor();

            if ( cursor != null )
            {
                cursor.close();
            }
        }

        pagedResultsControl = new PagedResultsDecorator( ldapServer.getDirectoryService()
            .getLdapCodecService() );
        pagedResultsControl.setCritical( true );
        pagedResultsControl.setSize( 0 );
        req.getResultResponse().addControl( pagedResultsControl );

        return;
    }
    else
    {
        // We have reached one limit

        if ( count < sizeLimit )
        {
            // We stop here. We have to add a ResponseControl
            // DO NOT WRITE THE RESPONSE - JUST RETURN IT
            ldapResult.setResultCode( ResultCodeEnum.SUCCESS );
            req.getResultResponse().addControl( pagedResultsControl );

            // Stores the cursor current position
            pagedContext.incrementCurrentPosition( pageCount );
            return;
        }
        else
        {
            // Return an exception, close the cursor, and clean the session
            ldapResult.setResultCode( ResultCodeEnum.SIZE_LIMIT_EXCEEDED );

            if ( cursor != null )
            {
                cursor.close();
            }

            session.removePagedSearchContext( cookieValue );

            return;
        }
    }
}
 
Example 4
Source File: SearchRequestHandler.java    From MyVirtualDirectory with Apache License 2.0 4 votes vote down vote up
/**
 * Conducts a simple search across the result set returning each entry
 * back except for the search response done.  This is calculated but not
 * returned so the persistent search mechanism can leverage this method
 * along with standard search.<br>
 * <br>
 * @param session the LDAP session object for this request
 * @param req the search request
 * @return the result done
 * @throws Exception if there are failures while processing the request
 */
private SearchResultDone doSimpleSearch( LdapSession session, SearchRequest req ) throws Exception
{
    LdapResult ldapResult = req.getResultResponse().getLdapResult();

    // Check if we are using the Paged Search Control
    Object control = req.getControls().get( PagedResults.OID );

    if ( control != null )
    {
        // Let's deal with the pagedControl
        return doPagedSearch( session, req, ( PagedResultsDecorator ) control );
    }

    // A normal search
    // Check that we have a cursor or not.
    // No cursor : do a search.
    EntryFilteringCursor cursor = session.getCoreSession().search( req );

    // register the request in the session
    session.registerSearchRequest( req, cursor );

    // Position the cursor at the beginning
    cursor.beforeFirst();

    /*
     * Iterate through all search results building and sending back responses
     * for each search result returned.
     */
    try
    {
        // Get the size limits
        // Don't bother setting size limits for administrators that don't ask for it
        long serverLimit = getServerSizeLimit( session, req );

        long requestLimit = req.getSizeLimit() == 0L ? Long.MAX_VALUE : req.getSizeLimit();

        req.addAbandonListener( new SearchAbandonListener( ldapServer, cursor ) );
        setTimeLimitsOnCursor( req, session, cursor );

        if ( IS_DEBUG )
        {
            LOG.debug( "using <{},{}> for size limit", requestLimit, serverLimit );
        }

        long sizeLimit = min( requestLimit, serverLimit );

        writeResults( session, req, ldapResult, cursor, sizeLimit );
    }
    finally
    {
        if ( ( cursor != null ) && !cursor.isClosed() )
        {
            try
            {
                cursor.close();
            }
            catch ( Exception e )
            {
                LOG.error( I18n.err( I18n.ERR_168 ), e );
            }
        }
    }

    return req.getResultResponse();
}
 
Example 5
Source File: SearchRequestHandler.java    From MyVirtualDirectory with Apache License 2.0 4 votes vote down vote up
private void readPagedResults( LdapSession session, SearchRequest req, LdapResult ldapResult,
    Cursor<Entry> cursor, long sizeLimit, int pagedLimit, PagedSearchContext pagedContext,
    PagedResultsDecorator pagedResultsControl ) throws Exception
{
    req.addAbandonListener( new SearchAbandonListener( ldapServer, cursor ) );
    setTimeLimitsOnCursor( req, session, cursor );

    if ( IS_DEBUG )
    {
        LOG.debug( "using <{},{}> for size limit", sizeLimit, pagedLimit );
    }

    int cookieValue = 0;

    int count = pagedContext.getCurrentPosition();
    int pageCount = 0;

    while ( ( count < sizeLimit ) && ( pageCount < pagedLimit ) && cursor.next() )
    {
        if ( session.getIoSession().isClosing() )
        {
            break;
        }

        Entry entry = cursor.get();
        session.getIoSession().write( generateResponse( session, req, entry ) );
        count++;
        pageCount++;
    }

    // DO NOT WRITE THE RESPONSE - JUST RETURN IT
    ldapResult.setResultCode( ResultCodeEnum.SUCCESS );

    boolean hasMoreEntry = cursor.next();

    // We have some entry, move back to the first one, as we just moved forward 
    // to get the first entry
    if ( hasMoreEntry )
    {
        cursor.previous();
    }

    if ( !hasMoreEntry )
    {
        // That means we don't have anymore entry
        // If we are here, it means we have returned all the entries
        // We have to remove the cookie from the session
        cookieValue = pagedContext.getCookieValue();
        PagedSearchContext psCookie = session.removePagedSearchContext( cookieValue );

        // Close the cursor if there is one
        if ( psCookie != null )
        {
            cursor = psCookie.getCursor();

            if ( cursor != null )
            {
                cursor.close();
            }
        }

        pagedResultsControl = new PagedResultsDecorator( ldapServer.getDirectoryService()
            .getLdapCodecService() );
        pagedResultsControl.setCritical( true );
        pagedResultsControl.setSize( 0 );
        req.getResultResponse().addControl( pagedResultsControl );

        return;
    }
    else
    {
        // We have reached one limit

        if ( count < sizeLimit )
        {
            // We stop here. We have to add a ResponseControl
            // DO NOT WRITE THE RESPONSE - JUST RETURN IT
            ldapResult.setResultCode( ResultCodeEnum.SUCCESS );
            req.getResultResponse().addControl( pagedResultsControl );

            // Stores the cursor current position
            pagedContext.incrementCurrentPosition( pageCount );
            return;
        }
        else
        {
            // Return an exception, close the cursor, and clean the session
            ldapResult.setResultCode( ResultCodeEnum.SIZE_LIMIT_EXCEEDED );

            if ( cursor != null )
            {
                cursor.close();
            }

            session.removePagedSearchContext( cookieValue );

            return;
        }
    }
}