org.apache.directory.api.ldap.model.message.SearchScope Java Examples

The following examples show how to use org.apache.directory.api.ldap.model.message.SearchScope. 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: 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 #2
Source File: SearchRequestTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Test parsing of a request with scope attribute to BaseObject value
 * @throws NamingException
 */
@Test
public void testRequestWithScopeBaseObject()
{
    Dsmlv2Parser parser = null;
    try
    {
        parser = newParser();

        parser.setInput( SearchRequestTest.class.getResource( "request_with_scope_baseObject.xml" ).openStream(),
            "UTF-8" );

        parser.parse();
    }
    catch ( Exception e )
    {
        fail( e.getMessage() );
    }

    SearchRequest searchRequest = ( SearchRequest ) parser.getBatchRequest().getCurrentRequest();

    assertEquals( SearchScope.OBJECT, searchRequest.getScope() );
}
 
Example #3
Source File: SearchRequestTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Test parsing of a request with scope attribute to SingleLevel value
 * @throws NamingException
 */
@Test
public void testRequestWithScopeSingleLevel()
{
    Dsmlv2Parser parser = null;
    try
    {
        parser = newParser();

        parser.setInput( SearchRequestTest.class.getResource( "request_with_scope_singleLevel.xml" ).openStream(),
            "UTF-8" );

        parser.parse();
    }
    catch ( Exception e )
    {
        fail( e.getMessage() );
    }

    SearchRequest searchRequest = ( SearchRequest ) parser.getBatchRequest().getCurrentRequest();

    assertEquals( SearchScope.ONELEVEL, searchRequest.getScope() );
}
 
Example #4
Source File: SearchRequestTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Test parsing of a request with scope attribute to WholeSubtree value
 * @throws NamingException
 */
@Test
public void testRequestWithScopeWholeSubtree()
{
    Dsmlv2Parser parser = null;
    try
    {
        parser = newParser();

        parser.setInput( SearchRequestTest.class.getResource( "request_with_scope_wholeSubtree.xml" ).openStream(),
            "UTF-8" );

        parser.parse();
    }
    catch ( Exception e )
    {
        fail( e.getMessage() );
    }

    SearchRequest searchRequest = ( SearchRequest ) parser.getBatchRequest().getCurrentRequest();

    assertEquals( SearchScope.SUBTREE, searchRequest.getScope() );
}
 
Example #5
Source File: DefaultCoreSession.java    From MyVirtualDirectory with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public EntryFilteringCursor 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 #6
Source File: ModelFactoryImpl.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public SearchRequest newSearchRequest( Dn baseDn, String filter,
    SearchScope scope, String... attributes )
{
    SearchRequest searchRequest = null;
    try
    {
        searchRequest = new SearchRequestImpl()
            .setBase( baseDn )
            .setFilter( filter )
            .setScope( scope == null ? SearchScope.OBJECT : scope );
        if ( attributes != null && attributes.length > 0 )
        {
            searchRequest.addAttributes( attributes );
        }
    }
    catch ( LdapException e )
    {
        throw new LdapRuntimeException( e );
    }
    return searchRequest;
}
 
Example #7
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 #8
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 #9
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 #10
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 #11
Source File: LdapUrlTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * test the setScope() method
 */
@Test
public void testDnSetScope() throws LdapURLEncodingException, LdapInvalidDnException
{
    LdapUrl url = new LdapUrl();
    assertEquals( SearchScope.OBJECT, url.getScope() );

    url.setDn( new Dn( "dc=example,dc=com" ) );

    url.setScope( SearchScope.ONELEVEL );
    assertEquals( SearchScope.ONELEVEL, url.getScope() );
    assertEquals( "ldap:///dc=example,dc=com??one", url.toString() );

    url.setScope( SearchScope.SUBTREE );
    assertEquals( SearchScope.SUBTREE, url.getScope() );
    assertEquals( "ldap:///dc=example,dc=com??sub", url.toString() );

    url.setScope( -1 );
    assertEquals( SearchScope.OBJECT, url.getScope() );
    assertEquals( "ldap:///dc=example,dc=com", url.toString() );
}
 
Example #12
Source File: LdapLoginManager.java    From openmeetings with Apache License 2.0 6 votes vote down vote up
private void fillGroups(Dn baseDn, String searchQ, List<Dn> groups) throws IOException, LdapException, CursorException {
	try (EntryCursor cursor = new EntryCursorImpl(conn.search(
			new SearchRequestImpl()
				.setBase(baseDn)
				.setFilter(searchQ)
				.setScope(SearchScope.SUBTREE)
				.addAttributes("*")
				.setDerefAliases(AliasDerefMode.DEREF_ALWAYS))))
	{
		while (cursor.next()) {
			try {
				Entry e = cursor.get();
				groups.add(e.getDn());
			} catch (CursorLdapReferralException cle) {
				log.warn(WARN_REFERRAL);
			}
		}
	}
}
 
Example #13
Source File: LdapConnectionTemplate.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public <T> T searchFirst( String baseDn, String filter, SearchScope scope,
    EntryMapper<T> entryMapper )
{
    return searchFirst(
        modelFactory.newSearchRequest( baseDn, filter, scope ),
        entryMapper );
}
 
Example #14
Source File: LdapConnectionTemplate.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public <T> List<T> search( Dn baseDn, String filter, SearchScope scope,
    String[] attributes, EntryMapper<T> entryMapper )
{
    return search(
        modelFactory.newSearchRequest( baseDn, filter, scope, attributes ),
        entryMapper );
}
 
Example #15
Source File: LdapConnectionTemplate.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public <T> T searchFirst( String baseDn, FilterBuilder filter, SearchScope scope,
    EntryMapper<T> entryMapper )
{
    return searchFirst(
        modelFactory.newSearchRequest( baseDn, filter, scope ),
        entryMapper );
}
 
Example #16
Source File: DefaultCoreSession.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
public EntryFilteringCursor 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 #17
Source File: LdapUrl.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Construct an empty LdapUrl
 */
public LdapUrl()
{
    scheme = LDAP_SCHEME;
    host = null;
    port = -1;
    dn = null;
    attributes = new ArrayList<>();
    scope = SearchScope.OBJECT;
    filter = null;
    extensionList = new ArrayList<>( 2 );
}
 
Example #18
Source File: ModelFactoryImpl.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public SearchRequest newSearchRequest( String baseDn, FilterBuilder filter,
    SearchScope scope )
{
    return newSearchRequest( newDn( baseDn ), filter.toString(), scope );
}
 
Example #19
Source File: LdapConnectionWrapper.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public EntryCursor search( String baseDn, String filter, SearchScope scope, String... attributes )
    throws LdapException
{
    return connection.search( baseDn, filter, scope, attributes );
}
 
Example #20
Source File: ModelFactoryImpl.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public SearchRequest newSearchRequest( Dn baseDn, FilterBuilder filter,
    SearchScope scope )
{
    return newSearchRequest( baseDn, filter.toString(), scope, ( String[] ) null );
}
 
Example #21
Source File: ModelFactoryImpl.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public SearchRequest newSearchRequest( Dn baseDn, String filter,
    SearchScope scope )
{
    return newSearchRequest( baseDn, filter, scope, ( String[] ) null );
}
 
Example #22
Source File: LdapConnectionTemplate.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public <T> List<T> search( String baseDn, String filter, SearchScope scope,
    String[] attributes, EntryMapper<T> entryMapper )
{
    return search(
        modelFactory.newSearchRequest( baseDn, filter, scope, attributes ),
        entryMapper );
}
 
Example #23
Source File: LdapConnectionTemplate.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public <T> List<T> search( String baseDn, FilterBuilder filter, SearchScope scope,
    String[] attributes, EntryMapper<T> entryMapper )
{
    return search(
        modelFactory.newSearchRequest( baseDn, filter, scope, attributes ),
        entryMapper );
}
 
Example #24
Source File: LdapConnectionTemplate.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public <T> List<T> search( Dn baseDn, String filter, SearchScope scope,
    EntryMapper<T> entryMapper )
{
    return search(
        modelFactory.newSearchRequest( baseDn, filter, scope ),
        entryMapper );
}
 
Example #25
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 #26
Source File: DefaultCoreSession.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
public EntryFilteringCursor 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 #27
Source File: LDAPApi.java    From mamute with Apache License 2.0 5 votes vote down vote up
private Entry lookupUser(String username) throws LdapException {
	StringBuilder userQuery = new StringBuilder();
	userQuery.append("(&(objectclass=");
	userQuery.append(userObjectClass);
	userQuery.append(")(|");
	boolean hasCondition = false;
	for (String lookupAttr : lookupAttrs) {
		String attrName = lookupAttr.trim();
		if (!attrName.isEmpty()) {
			userQuery.append('(').append(attrName).append('=').append(username).append(')');
			hasCondition = true;
		}
	}
	userQuery.append("))");

	if (!hasCondition) {
		return null;
	}

	logger.debug("LDAP user query " + userQuery.toString());

	EntryCursor responseCursor = connection.search(userDn, userQuery.toString(), SearchScope.SUBTREE);
	try {
		try {
			if (responseCursor != null && responseCursor.next()) {
				Entry match = responseCursor.get();
				logger.debug("LDAP user query result: " + match.getDn());
				return match;
			}
		} catch (CursorException e) {
			logger.debug("LDAP search error", e);
			return null;
		}
	} finally {
		responseCursor.close();
	}
	return null;
}
 
Example #28
Source File: ModelFactoryImpl.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public SearchRequest newSearchRequest( String baseDn, FilterBuilder filter,
    SearchScope scope, String... attributes )
{
    return newSearchRequest( newDn( baseDn ), filter.toString(), scope, attributes );
}
 
Example #29
Source File: LdapConnectionTemplate.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public PasswordWarning authenticate( Dn baseDn, String filter, SearchScope scope, char[] password )
    throws PasswordException
{
    return authenticate( newSearchRequest( baseDn, filter, scope ), password );
}
 
Example #30
Source File: LdapConnectionTemplate.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public PasswordWarning authenticate( String baseDn, String filter, SearchScope scope, char[] password )
    throws PasswordException
{
    return authenticate( newSearchRequest( baseDn, filter, scope ), password );
}