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

The following examples show how to use org.apache.directory.api.ldap.model.cursor.CursorException. 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 void ensureDNs() throws LdapException, IOException, ParseException, CursorException {
    directory.getPartitionNexus().hasEntry(new HasEntryOperationContext(directory.getAdminSession(),
            directory.getDnFactory().create(rootDN)));
    if (!directory.getPartitionNexus().hasEntry(new HasEntryOperationContext(directory.getAdminSession(),
            directory.getDnFactory().create(usersDN)))) {
        createEntry(usersDN, "organizationalUnit");
    }
    if (!directory.getPartitionNexus().hasEntry(new HasEntryOperationContext(directory.getAdminSession(),
            directory.getDnFactory().create(groupsDN)))) {
        createEntry(groupsDN, "organizationalUnit");
    }
    if (!directory.getPartitionNexus().hasEntry(new HasEntryOperationContext(directory.getAdminSession(),
            directory.getDnFactory().create(rolesDN)))) {
        createEntry(rolesDN, "organizationalUnit");
    }
}
 
Example #3
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 #4
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 #5
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 #6
Source File: LdapLoginManager.java    From openmeetings with Apache License 2.0 6 votes vote down vote up
private void importUsers(LdapWorker w, EntryCursor cursor, Long domainId, boolean print) throws LdapException, CursorException, OmException, IOException {
	while (cursor.next()) {
		try {
			Entry e = cursor.get();
			User u = userDao.getByLogin(getLogin(w.config, e), Type.LDAP, domainId);
			u = w.getUser(e, u);
			if (print) {
				log.info("Going to import user: {}", u);
			} else {
				userDao.update(u, null);
				log.info("User {}, was imported", u);
			}
		} catch (CursorLdapReferralException cle) {
			log.warn(WARN_REFERRAL);
		}
	}
}
 
Example #7
Source File: SearchCursorImpl.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * This operation is not supported in SearchCursor.
 * {@inheritDoc}
 */
@Override
public void beforeFirst() throws LdapException, CursorException
{
    throw new UnsupportedOperationException( I18n.err( I18n.ERR_13102_UNSUPPORTED_OPERATION, getClass().getName()
        .concat( "." ).concat( "beforeFirst()" ) ) );
}
 
Example #8
Source File: MyVDCursor.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
@Override
public boolean last() throws LdapException, CursorException {
	
	try {
		while (res.hasMore()) {
			buffer = res.next();
		}
		return true;
	} catch (LDAPException e) {
		throw MyVDInterceptor.generateException(e);
	}
}
 
Example #9
Source File: SearchCursorImpl.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * This operation is not supported in SearchCursor.
 * {@inheritDoc}
 */
@Override
public void afterLast() throws LdapException, CursorException
{
    throw new UnsupportedOperationException( I18n.err( I18n.ERR_13102_UNSUPPORTED_OPERATION, getClass().getName()
        .concat( "." ).concat( "afterLast()" ) ) );
}
 
Example #10
Source File: SearchCursorImpl.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * This operation is not supported in SearchCursor.
 * {@inheritDoc}
 */
@Override
public boolean last() throws LdapException, CursorException
{
    throw new UnsupportedOperationException( I18n.err( I18n.ERR_13102_UNSUPPORTED_OPERATION, getClass().getName()
        .concat( "." ).concat( "last()" ) ) );
}
 
Example #11
Source File: SearchCursorImpl.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * This operation is not supported in SearchCursor.
 * {@inheritDoc}
 */
@Override
public boolean first() throws LdapException, CursorException
{
    throw new UnsupportedOperationException( I18n.err( I18n.ERR_13102_UNSUPPORTED_OPERATION, getClass().getName()
        .concat( "." ).concat( "first()" ) ) );
}
 
Example #12
Source File: SearchCursorImpl.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * This operation is not supported in SearchCursor.
 * {@inheritDoc}
 */
@Override
public void after( Response element ) throws LdapException, CursorException
{
    throw new UnsupportedOperationException( I18n.err( I18n.ERR_13102_UNSUPPORTED_OPERATION, getClass().getName()
        .concat( "." ).concat( "after( Response element )" ) ) );
}
 
Example #13
Source File: SearchCursorImpl.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * This operation is not supported in SearchCursor.
 * {@inheritDoc}
 */
@Override
public void before( Response element ) throws LdapException, CursorException
{
    throw new UnsupportedOperationException( I18n.err( I18n.ERR_13102_UNSUPPORTED_OPERATION, getClass().getName()
        .concat( "." ).concat( "before( Response element )" ) ) );
}
 
Example #14
Source File: EntryCursorImpl.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * This operation is not supported in SearchCursor.
 * {@inheritDoc}
 */
@Override
public boolean first() throws LdapException, CursorException
{
    throw new UnsupportedOperationException( I18n.err( I18n.ERR_13102_UNSUPPORTED_OPERATION, getClass().getName()
        .concat( "." ).concat( "first()" ) ) );
}
 
Example #15
Source File: EntryCursorImpl.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * This operation is not supported in SearchCursor.
 * {@inheritDoc}
 */
@Override
public boolean last() throws LdapException, CursorException
{
    throw new UnsupportedOperationException( I18n.err( I18n.ERR_13102_UNSUPPORTED_OPERATION, getClass().getName()
        .concat( "." ).concat( "last()" ) ) );
}
 
Example #16
Source File: EntryCursorImpl.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * This operation is not supported in SearchCursor.
 * {@inheritDoc}
 */
@Override
public boolean previous() throws LdapException, CursorException
{
    throw new UnsupportedOperationException( I18n.err( I18n.ERR_13102_UNSUPPORTED_OPERATION, getClass().getName()
        .concat( "." ).concat( "previous()" ) ) );
}
 
Example #17
Source File: SearchCursorImpl.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * This operation is not supported in SearchCursor.
 * {@inheritDoc}
 */
@Override
public boolean previous() throws LdapException, CursorException
{
    throw new UnsupportedOperationException( I18n.err( I18n.ERR_13102_UNSUPPORTED_OPERATION, getClass().getName()
        .concat( "." ).concat( "previous()" ) ) );
}
 
Example #18
Source File: EntryCursorImpl.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Entry get() throws CursorException
{
    if ( !searchCursor.available() )
    {
        throw new InvalidCursorPositionException();
    }

    try
    {
        do
        {
            if ( response instanceof SearchResultEntry )
            {
                return ( ( SearchResultEntry ) response ).getEntry();
            }

            if ( response instanceof SearchResultReference )
            {
                throw new LdapReferralException( ( ( SearchResultReference ) response ).getReferral().getLdapUrls() );
            }
        }
        while ( next() && !( response instanceof SearchResultDone ) );
    }
    catch ( LdapReferralException lre )
    {
        throw new CursorLdapReferralException( lre );
    }
    catch ( Exception e )
    {
        throw new CursorException( e );
    }

    return null;
}
 
Example #19
Source File: MyVDCursor.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
@Override
public boolean next() throws LdapException, CursorException {
	try {
		return res.hasMore();
	} catch (LDAPException e) {
		throw MyVDInterceptor.generateException(e);
	}
}
 
Example #20
Source File: LDAPIAMPoller.java    From aws-iam-ldap-bridge with Apache License 2.0 5 votes vote down vote up
private void clearDNs() throws LdapException, IOException, ParseException, CursorException {
    if (firstRun) {
        firstRun = false;
        clearDN(usersDN);
        clearDN(groupsDN);
        clearDN(rolesDN);
    }
}
 
Example #21
Source File: MyVDBaseCursor.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public boolean first() throws LdapException, CursorException
{
    if ( operationContext.isAbandoned() )
    {
        log.info( "Cursor has been abandoned." );
        close();
        throw new OperationAbandonedException();
    }

    beforeFirst();

    return next();
}
 
Example #22
Source File: MyVDBaseCursor.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public boolean last() throws LdapException, CursorException
{
    if ( operationContext.isAbandoned() )
    {
        log.info( "Cursor has been abandoned." );
        close();
        throw new OperationAbandonedException();
    }

    afterLast();

    return previous();
}
 
Example #23
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 #24
Source File: MyVDCursor.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
@Override
public void afterLast() throws LdapException, CursorException {
	
	if (le != null) {
		throw le;
	}
	
	try {
		while (res.hasMore()) res.next();
		le = null;
	} catch (LDAPException e) {
		throw MyVDInterceptor.generateException(e);
	}
	
}
 
Example #25
Source File: LdapLoginManager.java    From openmeetings with Apache License 2.0 5 votes vote down vote up
private static Map.Entry<Dn, Entry> searchAndBind(LdapWorker w, String login, String passwd) throws LdapException, CursorException, OmException, IOException {
	Dn userDn = null;
	Entry entry = null;
	bindAdmin(w.conn, w.options);
	Dn baseDn = new Dn(w.options.searchBase);
	String searchQ = String.format(w.options.searchQuery, login);

	try (EntryCursor cursor = new EntryCursorImpl(w.conn.search(
			new SearchRequestImpl()
				.setBase(baseDn)
				.setFilter(searchQ)
				.setScope(w.options.scope)
				.addAttributes("*")
				.setDerefAliases(w.options.derefMode))))
	{
		while (cursor.next()) {
			try {
				Entry e = cursor.get();
				if (userDn != null) {
					log.error("more than 1 user found in LDAP");
					throw UNKNOWN;
				}
				userDn = e.getDn();
				if (w.options.useAdminForAttrs) {
					entry = e;
				}
			} catch (CursorLdapReferralException cle) {
				log.warn(WARN_REFERRAL);
			}
		}
	}
	if (userDn == null) {
		log.error("NONE users found in LDAP");
		throw BAD_CREDENTIALS;
	}
	w.conn.bind(userDn, passwd);
	return new AbstractMap.SimpleEntry<>(userDn, entry);
}
 
Example #26
Source File: MyVDCursor.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
@Override
public boolean last() throws LdapException, CursorException {
	
	try {
		while (res.hasMore()) {
			buffer = res.next();
		}
		return true;
	} catch (LDAPException e) {
		throw MyVDInterceptor.generateException(e);
	}
}
 
Example #27
Source File: MyVDCursor.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
@Override
public boolean next() throws LdapException, CursorException {
	try {
		return res.hasMore();
	} catch (LDAPException e) {
		throw MyVDInterceptor.generateException(e);
	}
}
 
Example #28
Source File: LdapDataProvider.java    From directory-fortress-core with Apache License 2.0 5 votes vote down vote up
/**
 * Delete exiting ldap entry and all descendants from the directory.  Add audit context.  This method will call
 * modify prior to delete which will
 * force corresponding audit record to be written to slapd access log.
 *
 * @param connection handle to ldap connection.
 * @param dn         contains distinguished node of entry targeted for removal..
 * @param entity     contains audit context.
 * @throws LdapException   in the event system error occurs.
 * @throws CursorException
 */
protected void deleteRecursive( LdapConnection connection, String dn, FortEntity entity ) throws LdapException,
    CursorException
{
    List<Modification> mods = new ArrayList<Modification>();
    audit( mods, entity );

    if ( mods.size() > 0 )
    {
        modify( connection, dn, mods );
    }

    deleteRecursive( connection, dn );
}
 
Example #29
Source File: MyVDBaseCursor.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public boolean last() throws LdapException, CursorException
{
    if ( operationContext.isAbandoned() )
    {
        log.info( "Cursor has been abandoned." );
        close();
        throw new OperationAbandonedException();
    }

    afterLast();

    return previous();
}
 
Example #30
Source File: MyVDBaseCursor.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public boolean first() throws LdapException, CursorException
{
    if ( operationContext.isAbandoned() )
    {
        log.info( "Cursor has been abandoned." );
        close();
        throw new OperationAbandonedException();
    }

    beforeFirst();

    return next();
}