Search in sources :

Example 51 with SearchCursor

use of org.apache.directory.api.ldap.model.cursor.SearchCursor in project directory-fortress-core by apache.

the class OrgUnitDAO method getAllDescendants.

/**
 * @param orgUnit
 * @return
 * @throws FinderException
 */
List<Graphable> getAllDescendants(OrgUnit orgUnit) throws FinderException {
    String orgUnitRoot = getOrgRoot(orgUnit);
    String[] DESC_ATRS = { SchemaConstants.OU_AT, GlobalIds.PARENT_NODES };
    List<Graphable> descendants = new ArrayList<>();
    LdapConnection ld = null;
    String filter = null;
    try {
        filter = GlobalIds.FILTER_PREFIX + ORGUNIT_OBJECT_CLASS_NM + ")(" + GlobalIds.PARENT_NODES + "=*))";
        ld = getAdminConnection();
        SearchCursor searchResults = search(ld, orgUnitRoot, SearchScope.ONELEVEL, filter, DESC_ATRS, false, GlobalIds.BATCH_SIZE);
        long sequence = 0;
        while (searchResults.next()) {
            descendants.add(unloadDescendants(searchResults.getEntry(), sequence++, orgUnit.getContextId()));
        }
    } catch (LdapException e) {
        String error = "getAllDescendants filter [" + filter + "] caught LdapException=" + e.getMessage();
        throw new FinderException(GlobalErrIds.ARLE_SEARCH_FAILED, error, e);
    } catch (CursorException e) {
        String error = "getAllDescendants filter [" + filter + "] caught CursorException=" + e.getMessage();
        throw new FinderException(GlobalErrIds.ARLE_SEARCH_FAILED, error, e);
    } finally {
        closeAdminConnection(ld);
    }
    return descendants;
}
Also used : FinderException(org.apache.directory.fortress.core.FinderException) CursorException(org.apache.directory.api.ldap.model.cursor.CursorException) ArrayList(java.util.ArrayList) SearchCursor(org.apache.directory.api.ldap.model.cursor.SearchCursor) Graphable(org.apache.directory.fortress.core.model.Graphable) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection)

Example 52 with SearchCursor

use of org.apache.directory.api.ldap.model.cursor.SearchCursor in project directory-fortress-core by apache.

the class AuditDAO method searchAdminMods.

/**
 * @param audit
 * @return
 * @throws FinderException
 */
List<Mod> searchAdminMods(UserAudit audit) throws FinderException {
    List<Mod> modList = new ArrayList<>();
    LdapConnection ld = null;
    String auditRoot = Config.getInstance().getProperty(AUDIT_ROOT);
    try {
        String filter = "(&(|(objectclass=" + ACCESS_MOD_CLASS_NM + ")";
        filter += "(objectclass=" + ACCESS_ADD_CLASS_NM + "))";
        if (StringUtils.isNotEmpty(audit.getDn())) {
            filter += "(" + REQDN + "=" + audit.getDn() + ")";
        }
        if (StringUtils.isNotEmpty(audit.getObjName())) {
            filter += "(|(" + REQMOD + "=" + GlobalIds.FT_MODIFIER_CODE + ":= " + audit.getObjName() + ".";
            if (StringUtils.isNotEmpty(audit.getOpName())) {
                filter += audit.getOpName();
            }
            filter += "*)";
            filter += "(" + REQMOD + "=" + GlobalIds.FT_MODIFIER_CODE + ":+ " + audit.getObjName() + ".";
            if (StringUtils.isNotEmpty(audit.getOpName())) {
                filter += audit.getOpName();
            }
            filter += "*))";
        }
        if (StringUtils.isNotEmpty(audit.getInternalUserId())) {
            filter += "(|(" + REQMOD + "=" + GlobalIds.FT_MODIFIER + ":= " + audit.getInternalUserId() + ")";
            filter += "(" + REQMOD + "=" + GlobalIds.FT_MODIFIER + ":+ " + audit.getInternalUserId() + "))";
        }
        if (audit.getBeginDate() != null) {
            String szTime = TUtil.encodeGeneralizedTime(audit.getBeginDate());
            filter += "(" + REQEND + ">=" + szTime + ")";
        }
        if (audit.getEndDate() != null) {
            String szTime = TUtil.encodeGeneralizedTime(audit.getEndDate());
            filter += "(" + REQEND + "<=" + szTime + ")";
        }
        filter += ")";
        // log.warn("filter=" + filter);
        ld = getLogConnection();
        SearchCursor searchResults = search(ld, auditRoot, SearchScope.ONELEVEL, filter, AUDIT_MOD_ATRS, false, GlobalIds.BATCH_SIZE);
        long sequence = 0;
        while (searchResults.next()) {
            modList.add(getModEntityFromLdapEntry(searchResults.getEntry(), sequence++));
        }
    } catch (LdapException e) {
        String error = "searchAdminMods caught LdapException id=" + e.getMessage();
        throw new FinderException(GlobalErrIds.AUDT_MOD_ADMIN_SEARCH_FAILED, error, e);
    } catch (CursorException e) {
        String error = "searchAdminMods caught CursorException id=" + e.getMessage();
        throw new FinderException(GlobalErrIds.AUDT_MOD_ADMIN_SEARCH_FAILED, error, e);
    } finally {
        closeLogConnection(ld);
    }
    return modList;
}
Also used : FinderException(org.apache.directory.fortress.core.FinderException) Mod(org.apache.directory.fortress.core.model.Mod) CursorException(org.apache.directory.api.ldap.model.cursor.CursorException) ArrayList(java.util.ArrayList) SearchCursor(org.apache.directory.api.ldap.model.cursor.SearchCursor) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection)

Example 53 with SearchCursor

use of org.apache.directory.api.ldap.model.cursor.SearchCursor in project directory-fortress-core by apache.

the class AuditDAO method searchBinds.

/**
 * @param audit
 * @return
 * @throws org.apache.directory.fortress.core.FinderException
 */
List<Bind> searchBinds(UserAudit audit) throws FinderException {
    List<Bind> auditList = new ArrayList<>();
    LdapConnection ld = null;
    String auditRoot = Config.getInstance().getProperty(AUDIT_ROOT);
    String userRoot = getRootDn(audit.getContextId(), GlobalIds.USER_ROOT);
    try {
        String filter;
        if (audit.getUserId() != null && audit.getUserId().length() > 0) {
            filter = GlobalIds.FILTER_PREFIX + ACCESS_BIND_CLASS_NM + ")(" + REQDN + "=" + SchemaConstants.UID_AT + "=" + audit.getUserId() + "," + userRoot + ")";
            if (audit.isFailedOnly()) {
                filter += "(" + REQRESULT + ">=" + 1 + ")";
            }
            if (audit.getBeginDate() != null) {
                String szTime = TUtil.encodeGeneralizedTime(audit.getBeginDate());
                filter += "(" + REQEND + ">=" + szTime + ")";
            }
            filter += ")";
        } else {
            filter = GlobalIds.FILTER_PREFIX + ACCESS_BIND_CLASS_NM + ")";
            if (audit.isFailedOnly()) {
                filter += "(" + REQRESULT + ">=" + 1 + ")";
            }
            if (audit.getBeginDate() != null) {
                String szTime = TUtil.encodeGeneralizedTime(audit.getBeginDate());
                filter += "(" + REQEND + ">=" + szTime + ")";
            }
            filter += ")";
        }
        // log.warn("filter=" + filter);
        ld = getLogConnection();
        SearchCursor searchResults = search(ld, auditRoot, SearchScope.ONELEVEL, filter, AUDIT_BIND_ATRS, false, GlobalIds.BATCH_SIZE);
        long sequence = 0;
        while (searchResults.next()) {
            auditList.add(getBindEntityFromLdapEntry(searchResults.getEntry(), sequence++));
        }
    } catch (LdapException e) {
        String error = "LdapException in AuditDAO.searchBinds id=" + e.getMessage();
        throw new FinderException(GlobalErrIds.AUDT_BIND_SEARCH_FAILED, error, e);
    } catch (CursorException e) {
        String error = "CursorException in AuditDAO.searchBinds id=" + e.getMessage();
        throw new FinderException(GlobalErrIds.AUDT_BIND_SEARCH_FAILED, error, e);
    } finally {
        closeLogConnection(ld);
    }
    return auditList;
}
Also used : FinderException(org.apache.directory.fortress.core.FinderException) Bind(org.apache.directory.fortress.core.model.Bind) CursorException(org.apache.directory.api.ldap.model.cursor.CursorException) ArrayList(java.util.ArrayList) SearchCursor(org.apache.directory.api.ldap.model.cursor.SearchCursor) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection)

Example 54 with SearchCursor

use of org.apache.directory.api.ldap.model.cursor.SearchCursor in project directory-fortress-core by apache.

the class LdapDataProvider method deleteRecursive.

/**
 * Used to recursively remove all nodes up to record pointed to by dn attribute.
 *
 * @param dn             contains distinguished node of entry targeted for removal..
 * @param connection     handle to ldap connection.
 * @param recursiveCount keeps track of how many iterations have been performed.
 * @throws LdapException   in the event system error occurs.
 * @throws CursorException
 */
private void deleteRecursive(String dn, LdapConnection connection, int recursiveCount) throws LdapException, CursorException {
    String method = "deleteRecursive";
    // Sanity check - only allow max tree depth of 100
    if (recursiveCount++ > MAX_DEPTH) {
        // too deep inside of a recursive sequence;
        String error = "." + method + " dn [" + dn + "] depth error in recursive";
        throw new LdapOperationErrorException(error);
    }
    String theDN;
    // Find child nodes
    SearchCursor cursor = search(connection, dn, SearchScope.ONELEVEL, "(objectclass=*)", SchemaConstants.NO_ATTRIBUTE_ARRAY, false, 0);
    // Iterate over all entries under this entry
    while (cursor.next()) {
        try {
            // Next directory entry
            Entry entry = cursor.getEntry();
            theDN = entry.getDn().getName();
            // continue down:
            deleteRecursive(theDN, connection, recursiveCount);
            recursiveCount--;
        } catch (LdapException le) {
            // cannot continue;
            String error = "." + method + " dn [" + dn + "] caught LdapException=" + le.getMessage();
            throw new LdapException(error);
        }
    }
    // delete the node:
    COUNTERS.incrementDelete();
    delete(connection, dn);
}
Also used : Entry(org.apache.directory.api.ldap.model.entry.Entry) LdapOperationErrorException(org.apache.directory.api.ldap.model.exception.LdapOperationErrorException) SearchCursor(org.apache.directory.api.ldap.model.cursor.SearchCursor) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 55 with SearchCursor

use of org.apache.directory.api.ldap.model.cursor.SearchCursor in project midpoint by Evolveum.

the class AbstractLdapTest method ldapSearch.

protected List<Entry> ldapSearch(LdapNetworkConnection connection, String baseDn, String filter, SearchScope scope, String... attributes) throws LdapException, CursorException {
    logger.trace("LDAP search base={}, filter={}, scope={}, attributes={}", baseDn, filter, scope, attributes);
    SearchRequest searchRequest = new SearchRequestImpl();
    searchRequest.setBase(new Dn(baseDn));
    searchRequest.setFilter(filter);
    searchRequest.setScope(scope);
    searchRequest.addAttributes(attributes);
    searchRequest.ignoreReferrals();
    List<Entry> entries = new ArrayList<>();
    try {
        SearchCursor searchCursor = connection.search(searchRequest);
        while (searchCursor.next()) {
            Response response = searchCursor.get();
            if (response instanceof SearchResultEntry) {
                Entry entry = ((SearchResultEntry) response).getEntry();
                entries.add(entry);
            }
        }
        searchCursor.close();
    } catch (IOException e) {
        throw new IllegalStateException("IO Error: " + e.getMessage(), e);
    } catch (CursorLdapReferralException e) {
        throw new IllegalStateException("Got referral to: " + e.getReferralInfo(), e);
    }
    return entries;
}
Also used : DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) CursorLdapReferralException(org.apache.directory.api.ldap.model.cursor.CursorLdapReferralException) SearchCursor(org.apache.directory.api.ldap.model.cursor.SearchCursor) Dn(org.apache.directory.api.ldap.model.name.Dn) IOException(java.io.IOException)

Aggregations

SearchCursor (org.apache.directory.api.ldap.model.cursor.SearchCursor)55 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)52 CursorException (org.apache.directory.api.ldap.model.cursor.CursorException)50 LdapConnection (org.apache.directory.ldap.client.api.LdapConnection)49 FinderException (org.apache.directory.fortress.core.FinderException)48 ArrayList (java.util.ArrayList)44 Entry (org.apache.directory.api.ldap.model.entry.Entry)11 DefaultEntry (org.apache.directory.api.ldap.model.entry.DefaultEntry)7 Permission (org.apache.directory.fortress.core.model.Permission)7 Dn (org.apache.directory.api.ldap.model.name.Dn)5 User (org.apache.directory.fortress.core.model.User)5 IOException (java.io.IOException)4 SearchRequest (org.apache.directory.api.ldap.model.message.SearchRequest)4 SearchRequestImpl (org.apache.directory.api.ldap.model.message.SearchRequestImpl)4 HashSet (java.util.HashSet)3 Response (org.apache.directory.api.ldap.model.message.Response)3 SearchResultEntry (org.apache.directory.api.ldap.model.message.SearchResultEntry)3 AuthZ (org.apache.directory.fortress.core.model.AuthZ)3 SDSet (org.apache.directory.fortress.core.model.SDSet)3 HashMap (java.util.HashMap)2