org.apache.directory.api.ldap.model.cursor.Cursor Java Examples

The following examples show how to use org.apache.directory.api.ldap.model.cursor.Cursor. 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: DefaultCoreSession.java    From MyVirtualDirectory with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Cursor<Entry> search( Dn dn, String filter, boolean ignoreReferrals ) throws LdapException
{
    OperationManager operationManager = directoryService.getOperationManager();
    ExprNode filterNode = null;

    try
    {
        filterNode = FilterParser.parse( directoryService.getSchemaManager(), filter );
    }
    catch ( ParseException pe )
    {
        throw new LdapInvalidSearchFilterException( pe.getMessage() );
    }

    SearchOperationContext searchContext = new SearchOperationContext( this, dn, SearchScope.OBJECT, filterNode,
        ( String ) null );
    searchContext.setAliasDerefMode( AliasDerefMode.DEREF_ALWAYS );
    setReferralHandling( searchContext, ignoreReferrals );

    return operationManager.search( searchContext );
}
 
Example #2
Source File: MyVDBaseCursor.java    From MyVirtualDirectory with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new entry filtering Cursor over an existing Cursor using a
 * list of filters initially: more can be added later after creation.
 * 
 * @param wrapped the underlying wrapped Cursor whose entries are filtered
 * @param operationContext the operation context that created this Cursor
 * @param invocation the search operation invocation creating this Cursor
 * @param filters a list of filters to be used
 */
public MyVDBaseCursor( Cursor<Entry> wrapped,
    SearchOperationContext operationContext,
    SchemaManager schemaManager,
    List<EntryFilter> filters )
{
    if ( IS_DEBUG )
    {
        LOG_CURSOR.debug( "Creating MyVDBaseCursor {}", this );
    }

    this.wrapped = wrapped;
    this.operationContext = operationContext;
    this.filters = new ArrayList<EntryFilter>();
    this.filters.addAll( filters );
    this.schemaManager = schemaManager;
}
 
Example #3
Source File: MyVDBaseCursor.java    From MyVirtualDirectory with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new entry filtering Cursor over an existing Cursor using a
 * list of filters initially: more can be added later after creation.
 * 
 * @param wrapped the underlying wrapped Cursor whose entries are filtered
 * @param operationContext the operation context that created this Cursor
 * @param invocation the search operation invocation creating this Cursor
 * @param filters a list of filters to be used
 */
public MyVDBaseCursor( Cursor<Entry> wrapped,
    SearchOperationContext operationContext,
    SchemaManager schemaManager,
    List<EntryFilter> filters )
{
    if ( IS_DEBUG )
    {
        LOG_CURSOR.debug( "Creating MyVDBaseCursor {}", this );
    }

    this.wrapped = wrapped;
    this.operationContext = operationContext;
    this.filters = new ArrayList<EntryFilter>();
    this.filters.addAll( filters );
    this.schemaManager = schemaManager;
}
 
Example #4
Source File: MyVDBaseCursor.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new entry filtering Cursor over an existing Cursor using a
 * no filter initially: more can be added later after creation.
 * 
 * @param wrapped the underlying wrapped Cursor whose entries are filtered
 * @param searchControls the controls of search that created this Cursor
 * @param invocation the search operation invocation creating this Cursor
 * @param filter a single filter to be used
 */
public MyVDBaseCursor( Cursor<Entry> wrapped, SearchOperationContext operationContext,
    SchemaManager schemaManager )
{
    if ( IS_DEBUG )
    {
        LOG_CURSOR.debug( "Creating MyVDBaseCursor {}", this );
    }

    this.wrapped = wrapped;
    this.operationContext = operationContext;
    this.filters = new ArrayList<EntryFilter>();
    this.schemaManager = schemaManager;
}
 
Example #5
Source File: MyVDBaseCursor.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new entry filtering Cursor over an existing Cursor using a
 * no filter initially: more can be added later after creation.
 * 
 * @param wrapped the underlying wrapped Cursor whose entries are filtered
 * @param searchControls the controls of search that created this Cursor
 * @param invocation the search operation invocation creating this Cursor
 * @param filter a single filter to be used
 */
public MyVDBaseCursor( Cursor<Entry> wrapped, SearchOperationContext operationContext,
    SchemaManager schemaManager )
{
    if ( IS_DEBUG )
    {
        LOG_CURSOR.debug( "Creating MyVDBaseCursor {}", this );
    }

    this.wrapped = wrapped;
    this.operationContext = operationContext;
    this.filters = new ArrayList<EntryFilter>();
    this.schemaManager = schemaManager;
}
 
Example #6
Source File: DefaultCoreSession.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
public Cursor<Entry> search( Dn dn, SearchScope scope, ExprNode filter, AliasDerefMode aliasDerefMode,
    String... returningAttributes ) throws LdapException
{
    OperationManager operationManager = directoryService.getOperationManager();

    SearchOperationContext searchContext = new SearchOperationContext( this, dn, scope, filter, returningAttributes );
    searchContext.setAliasDerefMode( aliasDerefMode );

    return operationManager.search( searchContext );
}
 
Example #7
Source File: SearchRequestHandler.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
/**
 * Manage the abandoned Paged Search (when paged size = 0). We have to
 * remove the cookie and its associated cursor from the session.
 */
private SearchResultDone abandonPagedSearch( LdapSession session, SearchRequest req ) throws Exception
{
    PagedResults pagedSearchControl = ( PagedResults ) req.getControls().get( PagedResults.OID );
    byte[] cookie = pagedSearchControl.getCookie();

    if ( !Strings.isEmpty( cookie ) )
    {
        // If the cookie is not null, we have to destroy the associated
        // cursor stored into the session (if any)
        int cookieValue = pagedSearchControl.getCookieValue();
        PagedSearchContext psCookie = session.removePagedSearchContext( cookieValue );
        pagedSearchControl.setCookie( psCookie.getCookie() );
        pagedSearchControl.setSize( 0 );
        pagedSearchControl.setCritical( true );

        // Close the cursor
        Cursor<Entry> cursor = psCookie.getCursor();

        if ( cursor != null )
        {
            cursor.close();
        }
    }
    else
    {
        pagedSearchControl.setSize( 0 );
        pagedSearchControl.setCritical( true );
    }

    // and return
    // DO NOT WRITE THE RESPONSE - JUST RETURN IT
    LdapResult ldapResult = req.getResultResponse().getLdapResult();
    ldapResult.setResultCode( ResultCodeEnum.SUCCESS );
    req.getResultResponse().addControl( pagedSearchControl );

    return ( SearchResultDone ) req.getResultResponse();
}
 
Example #8
Source File: DefaultCoreSession.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
public Cursor<Entry> list( Dn dn, AliasDerefMode aliasDerefMode,
    String... returningAttributes ) throws LdapException
{
    OperationManager operationManager = directoryService.getOperationManager();

    PresenceNode filter = new PresenceNode( OBJECT_CLASS_AT );
    SearchOperationContext searchContext = new SearchOperationContext( this, dn, SearchScope.ONELEVEL, filter,
        returningAttributes );
    searchContext.setAliasDerefMode( aliasDerefMode );

    return operationManager.search( searchContext );
}
 
Example #9
Source File: SearchRequestHandler.java    From MyVirtualDirectory with Apache License 2.0 4 votes vote down vote up
/**
 * Based on the server maximum time limits configured for search and the
 * requested time limits this method determines if at all to replace the
 * default ClosureMonitor of the result set Cursor with one that closes
 * the Cursor when either server mandated or request mandated time limits
 * are reached.
 *
 * @param req the {@link SearchRequest} issued
 * @param session the {@link LdapSession} on which search was requested
 * @param cursor the {@link EntryFilteringCursor} over the search results
 */
private void setTimeLimitsOnCursor( SearchRequest req, LdapSession session,
    final Cursor<Entry> cursor )
{
    // Don't bother setting time limits for administrators
    if ( session.getCoreSession().isAnAdministrator() && req.getTimeLimit() == NO_TIME_LIMIT )
    {
        return;
    }

    /*
     * Non administrator based searches are limited by time if the server
     * has been configured with unlimited time and the request specifies
     * unlimited search time
     */
    if ( ldapServer.getMaxTimeLimit() == NO_TIME_LIMIT && req.getTimeLimit() == NO_TIME_LIMIT )
    {
        return;
    }

    /*
     * If the non-administrator user specifies unlimited time but the server
     * is configured to limit the search time then we limit by the max time
     * allowed by the configuration
     */
    if ( req.getTimeLimit() == 0 )
    {
        cursor.setClosureMonitor( new SearchTimeLimitingMonitor( ldapServer.getMaxTimeLimit(), TimeUnit.SECONDS ) );
        return;
    }

    /*
     * If the non-administrative user specifies a time limit equal to or
     * less than the maximum limit configured in the server then we
     * constrain search by the amount specified in the request
     */
    if ( ldapServer.getMaxTimeLimit() >= req.getTimeLimit() )
    {
        cursor.setClosureMonitor( new SearchTimeLimitingMonitor( req.getTimeLimit(), TimeUnit.SECONDS ) );
        return;
    }

    /*
     * Here the non-administrative user's requested time limit is greater
     * than what the server's configured maximum limit allows so we limit
     * the search to the configured limit
     */
    cursor.setClosureMonitor( new SearchTimeLimitingMonitor( ldapServer.getMaxTimeLimit(), TimeUnit.SECONDS ) );
}
 
Example #10
Source File: LdapNetworkConnection.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Entry lookup( Dn dn, Control[] controls, String... attributes ) throws LdapException
{
    Entry entry = null;

    try
    {
        SearchRequest searchRequest = new SearchRequestImpl();

        searchRequest.setBase( dn );
        searchRequest.setFilter( LdapConstants.OBJECT_CLASS_STAR );
        searchRequest.setScope( SearchScope.OBJECT );
        searchRequest.addAttributes( attributes );
        searchRequest.setDerefAliases( AliasDerefMode.DEREF_ALWAYS );

        if ( ( controls != null ) && ( controls.length > 0 ) )
        {
            searchRequest.addAllControls( controls );
        }

        try ( Cursor<Response> cursor = search( searchRequest ) )
        {
            // Read the response
            if ( cursor.next() )
            {
                // cursor will always hold SearchResultEntry objects cause there is no ManageDsaITControl passed with search request
                entry = ( ( SearchResultEntry ) cursor.get() ).getEntry();
            }

            // Pass through the SaerchResultDone, or stop
            // if we have other responses
            cursor.next();
        }
    }
    catch ( CursorException e )
    {
        throw new LdapException( e.getMessage(), e );
    }
    catch ( IOException ioe )
    {
        throw new LdapException( ioe.getMessage(), ioe );
    }

    return entry;
}
 
Example #11
Source File: DefaultCoreSession.java    From MyVirtualDirectory with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Cursor<Entry> search( Dn dn, String filter ) throws LdapException
{
    return search( dn, filter, true );
}
 
Example #12
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;
        }
    }
}
 
Example #13
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,
    Cursor<Entry> 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++;
    }

    // check if the result code is not already set
    // the result code might be set when sort control is present
    if ( ldapResult.getResultCode() == null )
    {
        // 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 #14
Source File: CoreSession.java    From MyVirtualDirectory with Apache License 2.0 2 votes vote down vote up
/**
 * An optimized search operation using one level search scope which 
 * returns all the children of an entry specified by distinguished name.
 * This is equivalent to a search operation with one level scope using
 * the <code>(objectClass=*)</code> filter.
 *
 * @param dn the distinguished name of the entry to list the children of
 * @param aliasDerefMode the alias dereferencing mode used
 * @param returningAttributes the attributes to return
 * @throws Exception if there are failures while listing children
 */
Cursor<Entry> list( Dn dn, AliasDerefMode aliasDerefMode,
    String... returningAttributes ) throws LdapException;
 
Example #15
Source File: CoreSession.java    From MyVirtualDirectory with Apache License 2.0 2 votes vote down vote up
/**
 * Searches the directory using a specified filter. The scope is defaulting
 * to 'base'. The alias dereferencing default to 'always'. the returned attributes 
 * defaults to 'all the user attributes)
 *
 * @param dn the distinguished name of the entry to list the children of
 * @param filter the search filter
 * @throws Exception if there are failures while listing children
 */
Cursor<Entry> search( Dn dn, String filter ) throws LdapException;
 
Example #16
Source File: CoreSession.java    From MyVirtualDirectory with Apache License 2.0 2 votes vote down vote up
/**
 * Searches the directory using a specified filter. The scope is defaulting
 * to 'base'. The alias dereferencing default to 'always'. the returned attributes 
 * defaults to 'all the user attributes)
 *
 * @param dn the distinguished name of the entry to list the children of
 * @param filter the search filter
 * @param ignoreReferrals a flag to tell the server to ignore referrals
 * @throws Exception if there are failures while listing children
 */
Cursor<Entry> search( Dn dn, String filter, boolean ignoreReferrals ) throws LdapException;
 
Example #17
Source File: CoreSession.java    From MyVirtualDirectory with Apache License 2.0 2 votes vote down vote up
/**
 * Searches the directory using a specified search scope and filter.
 *
 * @param dn the distinguished name of the entry to list the children of
 * @param scope the search scope to apply
 * @param filter the search filter
 * @param aliasDerefMode the alias dereferencing mode used
 * @param returningAttributes the attributes to return
 * @throws Exception if there are failures while listing children
 */
Cursor<Entry> search( Dn dn, SearchScope scope, ExprNode filter, AliasDerefMode aliasDerefMode,
    String... returningAttributes ) throws LdapException;
 
Example #18
Source File: MyVDBaseCursor.java    From MyVirtualDirectory with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new entry filtering Cursor over an existing Cursor using a
 * single filter initially: more can be added later after creation.
 * 
 * @param wrapped the underlying wrapped Cursor whose entries are filtered
 * @param searchControls the controls of search that created this Cursor
 * @param invocation the search operation invocation creating this Cursor
 * @param filter a single filter to be used
 */
public MyVDBaseCursor( Cursor<Entry> wrapped,
    SearchOperationContext operationContext, SchemaManager schemaManager, EntryFilter filter )
{
    this( wrapped, operationContext, schemaManager, Collections.singletonList( filter ) );
}
 
Example #19
Source File: MyVDBaseCursor.java    From MyVirtualDirectory with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new entry filtering Cursor over an existing Cursor using a
 * single filter initially: more can be added later after creation.
 * 
 * @param wrapped the underlying wrapped Cursor whose entries are filtered
 * @param searchControls the controls of search that created this Cursor
 * @param invocation the search operation invocation creating this Cursor
 * @param filter a single filter to be used
 */
public MyVDBaseCursor( Cursor<Entry> wrapped,
    SearchOperationContext operationContext, SchemaManager schemaManager, EntryFilter filter )
{
    this( wrapped, operationContext, schemaManager, Collections.singletonList( filter ) );
}
 
Example #20
Source File: CoreSession.java    From MyVirtualDirectory with Apache License 2.0 votes vote down vote up
Cursor<Entry> search( SearchRequest searchRequest ) throws LdapException;