Java Code Examples for org.apache.directory.server.core.api.filtering.EntryFilteringCursor#next()

The following examples show how to use org.apache.directory.server.core.api.filtering.EntryFilteringCursor#next() . 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: LDAPIAMPoller.java    From aws-iam-ldap-bridge with Apache License 2.0 6 votes vote down vote up
private void clearDN(String dnStr) throws LdapException, ParseException, IOException, CursorException {
    Dn dn = directory.getDnFactory().create(dnStr);
    dn.apply(directory.getSchemaManager());
    ExprNode filter = FilterParser.parse(directory.getSchemaManager(), "(ObjectClass=*)");
    NameComponentNormalizer ncn = new ConcreteNameComponentNormalizer( directory.getSchemaManager() );
    FilterNormalizingVisitor visitor = new FilterNormalizingVisitor( ncn, directory.getSchemaManager() );
    filter.accept(visitor);
    SearchOperationContext context = new SearchOperationContext(directory.getAdminSession(),
            dn, SearchScope.SUBTREE, filter, SchemaConstants.ALL_USER_ATTRIBUTES, SchemaConstants.ALL_OPERATIONAL_ATTRIBUTES);
    EntryFilteringCursor cursor = directory.getPartitionNexus().search(context);
    cursor.beforeFirst();
    Collection<Dn> dns = new ArrayList<Dn>();
    while (cursor.next()) {
        Entry ent = cursor.get();
        if (ent.getDn().equals(dn)) continue;
        dns.add(ent.getDn());
    }
    cursor.close();

    LOG.debug("Deleting " + dns.size() + " items from under " + dnStr);
    for (Dn deleteDn: dns) {
        directory.getAdminSession().delete(deleteDn);
    }
}
 
Example 2
Source File: LDAPIAMPoller.java    From aws-iam-ldap-bridge with Apache License 2.0 6 votes vote down vote up
private Collection<Entry> getAllEntries(String rootDN, String className) {
    try {
        Dn dn = directory.getDnFactory().create(rootDN);
        dn.apply(directory.getSchemaManager());
        ExprNode filter = FilterParser.parse(directory.getSchemaManager(), String.format("(ObjectClass=%s)", className));
        NameComponentNormalizer ncn = new ConcreteNameComponentNormalizer( directory.getSchemaManager() );
        FilterNormalizingVisitor visitor = new FilterNormalizingVisitor( ncn, directory.getSchemaManager() );
        filter.accept(visitor);
        SearchOperationContext context = new SearchOperationContext(directory.getAdminSession(),
                dn, SearchScope.SUBTREE, filter, SchemaConstants.ALL_USER_ATTRIBUTES, SchemaConstants.ALL_OPERATIONAL_ATTRIBUTES);
        EntryFilteringCursor cursor = directory.getPartitionNexus().search(context);
        cursor.beforeFirst();
        Collection<Entry> entries = new ArrayList<Entry>();
        while (cursor.next()) {
            Entry ent = cursor.get();
            if (ent.getDn().equals(dn)) continue;
            entries.add(ent);
        }
        cursor.close();
        return entries;
    } catch (Throwable e) {
        return Collections.emptyList();
    }
}
 
Example 3
Source File: SearchRequestHandler.java    From MyVirtualDirectory with Apache License 2.0 4 votes vote down vote up
private void writeResults( LdapSession session, SearchRequest req, LdapResult ldapResult,
    EntryFilteringCursor cursor, long sizeLimit ) throws Exception
{
    long count = 0;

    while ( ( count < sizeLimit ) && cursor.next() )
    {
        // Handle closed session
        if ( session.getIoSession().isClosing() )
        {
            // The client has closed the connection
            if ( IS_DEBUG )
            {
                LOG.debug( "Request terminated for message {}, the client has closed the session",
                    req.getMessageId() );
            }

            break;
        }

        if ( req.isAbandoned() )
        {
            cursor.close( new OperationAbandonedException() );

            // The cursor has been closed by an abandon request.
            if ( IS_DEBUG )
            {
                LOG.debug( "Request terminated by an AbandonRequest for message {}", req.getMessageId() );
            }

            break;
        }

        Entry entry = cursor.get();
        session.getIoSession().write( generateResponse( session, req, entry ) );

        if ( IS_DEBUG )
        {
            LOG.debug( "Sending {}", entry.getDn() );
        }

        count++;
    }

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

    if ( ( count >= sizeLimit ) && ( cursor.next() ) )
    {
        // We have reached the limit
        // Move backward on the cursor to restore the previous position, as we moved forward
        // to check if there is one more entry available
        cursor.previous();
        // Special case if the user has requested more elements than the request size limit
        ldapResult.setResultCode( ResultCodeEnum.SIZE_LIMIT_EXCEEDED );
    }
}
 
Example 4
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;
        }
    }
}