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

The following examples show how to use org.apache.directory.api.ldap.model.message.SearchRequest#setFilter() . 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: Dsmlv2Grammar.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void action( Dsmlv2Container container ) throws XmlPullParserException
{
    // Adding the filter to the Search Filter
    SearchRequestDsml searchRequestDecorator = ( SearchRequestDsml )
        container.getBatchRequest().getCurrentRequest();
    SearchRequest searchRequest = searchRequestDecorator.getDecorated();

    try
    {
        ExprNode exprNode = searchRequestDecorator.getFilterNode();

        if ( exprNode == null )
        {
            throw new IllegalStateException( I18n.err( I18n.ERR_03041_NO_FILTER_ELEMENT ) );
        }

        searchRequest.setFilter( exprNode );
    }
    catch ( LdapSchemaException lse )
    {

    }
}
 
Example 2
Source File: LdapNetworkConnection.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public SearchFuture searchAsync( Dn baseDn, String filter, SearchScope scope, String... attributes )
    throws LdapException
{
    // Create a new SearchRequest object
    SearchRequest searchRequest = new SearchRequestImpl();

    searchRequest.setBase( baseDn );
    searchRequest.setFilter( filter );
    searchRequest.setScope( scope );
    searchRequest.addAttributes( attributes );
    searchRequest.setDerefAliases( AliasDerefMode.DEREF_ALWAYS );

    // Process the request in blocking mode
    return searchAsync( searchRequest );
}
 
Example 3
Source File: InitSearchRequestAttributeDescList.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void action( LdapMessageContainer<SearchRequest> container ) throws DecoderException
{
    // Here, we have to inject the decoded filter into the SearchRequest
    SearchRequest searchRequest = container.getMessage();

    try
    {
        searchRequest.setFilter( transform( container.getTopFilter() ) );
    }
    catch ( LdapSchemaException lse )
    {
        throw new DecoderException( lse.getMessage(), lse ); 
    }

    // We can have an END transition
    container.setGrammarEndAllowed( true );

    if ( LOG.isDebugEnabled() )
    {
        LOG.debug( I18n.msg( I18n.MSG_05158_INITIALIZE_ATT_DESC_LIST ) );
    }
}
 
Example 4
Source File: LDAPConnectionService.java    From guacamole-client with Apache License 2.0 6 votes vote down vote up
/**
 * Generate a SearchRequest object using the given Base DN and filter
 * and retrieving other properties from the LDAP configuration service.
 * 
 * @param baseDn
 *     The LDAP Base DN at which to search the search.
 * 
 * @param filter
 *     A string representation of a LDAP filter to use for the search.
 * 
 * @return
 *     The properly-configured SearchRequest object.
 * 
 * @throws GuacamoleException
 *     If an error occurs retrieving any of the configuration values.
 */
public SearchRequest getSearchRequest(Dn baseDn, ExprNode filter)
        throws GuacamoleException {
    
    SearchRequest searchRequest = new SearchRequestImpl();
    searchRequest.setBase(baseDn);
    searchRequest.setDerefAliases(confService.getDereferenceAliases());
    searchRequest.setScope(SearchScope.SUBTREE);
    searchRequest.setFilter(filter);
    searchRequest.setSizeLimit(confService.getMaxResults());
    searchRequest.setTimeLimit(confService.getOperationTimeout());
    searchRequest.setTypesOnly(false);
    
    if (confService.getFollowReferrals())
        searchRequest.followReferrals();
    
    return searchRequest;
}
 
Example 5
Source File: LdapDataProvider.java    From directory-fortress-core with Apache License 2.0 6 votes vote down vote up
/**
 * Perform normal ldap search specifying default batch size and max entries to return.
 *
 * @param connection is LdapConnection object used for all communication with host.
 * @param baseDn     contains address of distinguished name to begin ldap search
 * @param scope      indicates depth of search starting at basedn.  0 (base dn),
 *                   1 (one level down) or 2 (infinite) are valid values.
 * @param filter     contains the search criteria
 * @param attrs      is the requested list of attritubutes to return from directory search.
 * @param attrsOnly  if true pull back attribute names only.
 * @param maxEntries specifies the maximum number of entries to return in this search query.
 * @return result set containing ldap entries returned from directory.
 * @throws LdapException thrown in the event of error in ldap client or server code.
 */
protected SearchCursor search( LdapConnection connection, String baseDn, SearchScope scope, String filter,
    String[] attrs, boolean attrsOnly, int maxEntries ) throws LdapException
{
    COUNTERS.incrementSearch();

    SearchRequest searchRequest = new SearchRequestImpl();

    searchRequest.setBase( new Dn( baseDn ) );
    searchRequest.setFilter( filter );
    searchRequest.setScope( scope );
    searchRequest.setSizeLimit( maxEntries );
    searchRequest.setTypesOnly( attrsOnly );
    searchRequest.addAttributes( attrs );

    return connection.search( searchRequest );
}
 
Example 6
Source File: LdapDataProvider.java    From directory-fortress-core with Apache License 2.0 6 votes vote down vote up
/**
 * This method will search the directory and return at most one record.  If more than one record is found
 * an ldap exception will be thrown.
 *
 * @param connection is LdapConnection object used for all communication with host.
 * @param baseDn     contains address of distinguished name to begin ldap search
 * @param scope      indicates depth of search starting at basedn.  0 (base dn),
 *                   1 (one level down) or 2 (infinite) are valid values.
 * @param filter     contains the search criteria
 * @param attrs      is the requested list of attritubutes to return from directory search.
 * @param attrsOnly  if true pull back attribute names only.
 * @return entry   containing target ldap node.
 * @throws LdapException   thrown in the event of error in ldap client or server code.
 * @throws CursorException If we weren't able to fetch an element from the search result
 */
protected Entry searchNode( LdapConnection connection, String baseDn, SearchScope scope, String filter,
    String[] attrs, boolean attrsOnly ) throws LdapException, CursorException
{
    SearchRequest searchRequest = new SearchRequestImpl();

    searchRequest.setBase( new Dn( baseDn ) );
    searchRequest.setFilter( filter );
    searchRequest.setScope( scope );
    searchRequest.setTypesOnly( attrsOnly );
    searchRequest.addAttributes( attrs );

    SearchCursor result = connection.search( searchRequest );

    Entry entry = result.getEntry();

    if ( result.next() )
    {
        throw new LdapException( "searchNode failed to return unique record for LDAP search of base DN [" +
            baseDn + "] filter [" + filter + "]" );
    }

    return entry;
}
 
Example 7
Source File: LdapDataProvider.java    From directory-fortress-core with Apache License 2.0 6 votes vote down vote up
/**
 * This search method uses OpenLDAP Proxy Authorization Control to assert arbitrary user identity onto connection.
 *
 * @param connection is LdapConnection object used for all communication with host.
 * @param baseDn     contains address of distinguished name to begin ldap search
 * @param scope      indicates depth of search starting at basedn.  0 (base dn),
 *                   1 (one level down) or 2 (infinite) are valid values.
 * @param filter     contains the search criteria
 * @param attrs      is the requested list of attritubutes to return from directory search.
 * @param attrsOnly  if true pull back attribute names only.
 * @param userDn     string value represents the identity of user on who's behalf the request was initiated.  The
 *                   value will be stored in openldap auditsearch record AuthZID's attribute.
 * @return entry   containing target ldap node.
 * @throws LdapException   thrown in the event of error in ldap client or server code.
 * @throws CursorException If we weren't able to fetch an element from the search result
 */
protected Entry searchNode( LdapConnection connection, String baseDn, SearchScope scope, String filter,
    String[] attrs, boolean attrsOnly, String userDn ) throws LdapException, CursorException
{
    COUNTERS.incrementSearch();

    SearchRequest searchRequest = new SearchRequestImpl();

    searchRequest.setBase( new Dn( baseDn ) );
    searchRequest.setFilter( filter );
    searchRequest.setScope( scope );
    searchRequest.setTypesOnly( attrsOnly );
    searchRequest.addAttributes( attrs );

    SearchCursor result = connection.search( searchRequest );

    Entry entry = result.getEntry();

    if ( result.next() )
    {
        throw new LdapException( "searchNode failed to return unique record for LDAP search of base DN [" +
            baseDn + "] filter [" + filter + "]" );
    }

    return entry;
}
 
Example 8
Source File: LDAPConnectionService.java    From guacamole-client with Apache License 2.0 6 votes vote down vote up
/**
 * Generate a SearchRequest object using the given Base DN and filter
 * and retrieving other properties from the LDAP configuration service.
 * 
 * @param baseDn
 *     The LDAP Base DN at which to search the search.
 * 
 * @param filter
 *     A string representation of a LDAP filter to use for the search.
 * 
 * @return
 *     The properly-configured SearchRequest object.
 * 
 * @throws GuacamoleException
 *     If an error occurs retrieving any of the configuration values.
 */
public SearchRequest getSearchRequest(Dn baseDn, ExprNode filter)
        throws GuacamoleException {
    
    SearchRequest searchRequest = new SearchRequestImpl();
    searchRequest.setBase(baseDn);
    searchRequest.setDerefAliases(confService.getDereferenceAliases());
    searchRequest.setScope(SearchScope.SUBTREE);
    searchRequest.setFilter(filter);
    searchRequest.setSizeLimit(confService.getMaxResults());
    searchRequest.setTimeLimit(confService.getOperationTimeout());
    searchRequest.setTypesOnly(false);
    
    if (confService.getFollowReferrals())
        searchRequest.followReferrals();
    
    return searchRequest;
}
 
Example 9
Source File: LdapNetworkConnection.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public EntryCursor search( Dn baseDn, String filter, SearchScope scope, String... attributes )
    throws LdapException
{
    if ( baseDn == null )
    {
        if ( LOG.isDebugEnabled() )
        {
            LOG.debug( I18n.msg( I18n.MSG_04138_NULL_DN_SEARCH ) );
        }
        
        throw new IllegalArgumentException( I18n.err( I18n.ERR_04129_NULL_BASE_DN ) );
    }

    // Create a new SearchRequest object
    SearchRequest searchRequest = new SearchRequestImpl();

    searchRequest.setBase( baseDn );
    searchRequest.setFilter( filter );
    searchRequest.setScope( scope );
    searchRequest.addAttributes( attributes );
    searchRequest.setDerefAliases( AliasDerefMode.DEREF_ALWAYS );

    // Process the request in blocking mode
    return new EntryCursorImpl( search( searchRequest ) );
}
 
Example 10
Source File: LdapDataProvider.java    From directory-fortress-core with Apache License 2.0 5 votes vote down vote up
/**
 * Perform normal ldap search accepting default batch size.
 *
 * @param connection is LdapConnection object used for all communication with host.
 * @param baseDn     contains address of distinguished name to begin ldap search
 * @param scope      indicates depth of search starting at basedn.  0 (base dn),
 *                   1 (one level down) or 2 (infinite) are valid values.
 * @param filter     contains the search criteria
 * @param attrs      is the requested list of attritubutes to return from directory search.
 * @param attrsOnly  if true pull back attribute names only.
 * @return result set containing ldap entries returned from directory.
 * @throws LdapException thrown in the event of error in ldap client or server code.
 */
protected SearchCursor search( LdapConnection connection, String baseDn, SearchScope scope, String filter,
    String[] attrs, boolean attrsOnly ) throws LdapException
{
    COUNTERS.incrementSearch();

    SearchRequest searchRequest = new SearchRequestImpl();
    searchRequest.setBase( new Dn( baseDn ) );
    searchRequest.setScope( scope );
    searchRequest.setFilter( filter );
    searchRequest.setTypesOnly( attrsOnly );
    searchRequest.addAttributes( attrs );

    return connection.search( searchRequest );
}
 
Example 11
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 12
Source File: SearchRequestHandler.java    From MyVirtualDirectory with Apache License 2.0 4 votes vote down vote up
/**
 * Alters the filter expression based on the presence of the
 * ManageDsaIT decorator.  If the decorator is not present, the search
 * filter will be altered to become a disjunction with two terms.
 * The first term is the original filter.  The second term is a
 * (objectClass=referral) assertion.  When OR'd together these will
 * make sure we get all referrals so we can process continuations
 * properly without having the filter remove them from the result
 * set.
 *
 * NOTE: original filter is first since most entries are not referrals
 * so it has a higher probability on average of accepting and shorting
 * evaluation before having to waste cycles trying to evaluate if the
 * entry is a referral.
 *
 * @param session the session to use to construct the filter (schema access)
 * @param req the request to get the original filter from
 * @throws Exception if there are schema access problems
 */
private void modifyFilter( LdapSession session, SearchRequest req ) throws Exception
{
    if ( req.hasControl( ManageDsaIT.OID ) )
    {
        return;
    }

    /*
     * Most of the time the search filter is just (objectClass=*) and if
     * this is the case then there's no reason at all to OR this with an
     * (objectClass=referral).  If we detect this case then we leave it
     * as is to represent the OR condition:
     *
     *  (| (objectClass=referral)(objectClass=*)) == (objectClass=*)
     */
    if ( req.getFilter() instanceof PresenceNode )
    {
        PresenceNode presenceNode = ( PresenceNode ) req.getFilter();

        if ( presenceNode.isSchemaAware() )
        {
            AttributeType attributeType = presenceNode.getAttributeType();

            if ( attributeType.equals( OBJECT_CLASS_AT ) )
            {
                return;
            }
        }
        else
        {
            String attribute = presenceNode.getAttribute();

            if ( attribute.equalsIgnoreCase( SchemaConstants.OBJECT_CLASS_AT )
                || attribute.equalsIgnoreCase( SchemaConstants.OBJECT_CLASS_AT_OID ) )
            {
                return;
            }
        }
    }

    /*
     * Do not add the OR'd (objectClass=referral) expression if the user
     * searches for the subSchemaSubEntry as the SchemaIntercepter can't
     * handle an OR'd filter.
     */
    if ( isSubSchemaSubEntrySearch( session, req ) )
    {
        return;
    }

    // using varags to add two expressions to an OR node
    req.setFilter( new OrNode( req.getFilter(), newIsReferralEqualityNode( session ) ) );
}
 
Example 13
Source File: SearchRequestHandler.java    From MyVirtualDirectory with Apache License 2.0 4 votes vote down vote up
/**
 * Alters the filter expression based on the presence of the
 * ManageDsaIT decorator.  If the decorator is not present, the search
 * filter will be altered to become a disjunction with two terms.
 * The first term is the original filter.  The second term is a
 * (objectClass=referral) assertion.  When OR'd together these will
 * make sure we get all referrals so we can process continuations
 * properly without having the filter remove them from the result
 * set.
 *
 * NOTE: original filter is first since most entries are not referrals
 * so it has a higher probability on average of accepting and shorting
 * evaluation before having to waste cycles trying to evaluate if the
 * entry is a referral.
 *
 * @param session the session to use to construct the filter (schema access)
 * @param req the request to get the original filter from
 * @throws Exception if there are schema access problems
 */
private void modifyFilter( LdapSession session, SearchRequest req ) throws Exception
{
    if ( req.hasControl( ManageDsaIT.OID ) )
    {
        return;
    }

    /*
     * Most of the time the search filter is just (objectClass=*) and if
     * this is the case then there's no reason at all to OR this with an
     * (objectClass=referral).  If we detect this case then we leave it
     * as is to represent the OR condition:
     *
     *  (| (objectClass=referral)(objectClass=*)) == (objectClass=*)
     */
    if ( req.getFilter() instanceof PresenceNode )
    {
        PresenceNode presenceNode = ( PresenceNode ) req.getFilter();

        if ( presenceNode.isSchemaAware() )
        {
            AttributeType attributeType = presenceNode.getAttributeType();

            if ( attributeType.equals( OBJECT_CLASS_AT ) )
            {
                return;
            }
        }
        else
        {
            String attribute = presenceNode.getAttribute();

            if ( attribute.equalsIgnoreCase( SchemaConstants.OBJECT_CLASS_AT )
                || attribute.equalsIgnoreCase( SchemaConstants.OBJECT_CLASS_AT_OID ) )
            {
                return;
            }
        }
    }

    /*
     * Do not add the OR'd (objectClass=referral) expression if the user
     * searches for the subSchemaSubEntry as the SchemaIntercepter can't
     * handle an OR'd filter.
     */
    if ( isSubSchemaSubEntrySearch( session, req ) )
    {
        return;
    }

    // using varags to add two expressions to an OR node
    req.setFilter( new OrNode( req.getFilter(), newIsReferralEqualityNode( session ) ) );
}