Search in sources :

Example 21 with SearchCursor

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

the class RoleDAO method findAssignedRoles.

/**
 * @param userDn
 * @param contextId
 * @return
 * @throws FinderException
 */
List<String> findAssignedRoles(String userDn, String contextId) throws FinderException {
    List<String> roleNameList = new ArrayList<>();
    LdapConnection ld = null;
    String roleRoot = getRootDn(contextId, GlobalIds.ROLE_ROOT);
    try {
        String filter = GlobalIds.FILTER_PREFIX + GlobalIds.ROLE_OBJECT_CLASS_NM + ")";
        filter += "(" + SchemaConstants.ROLE_OCCUPANT_AT + "=" + userDn + "))";
        ld = getAdminConnection();
        SearchCursor searchResults = search(ld, roleRoot, SearchScope.ONELEVEL, filter, ROLE_NM_ATR, false, GlobalIds.BATCH_SIZE);
        while (searchResults.next()) {
            roleNameList.add(getAttribute(searchResults.getEntry(), ROLE_NM));
        }
    } catch (LdapException e) {
        String error = "findAssignedRoles userDn [" + userDn + "] caught LdapException=" + e.getMessage();
        throw new FinderException(GlobalErrIds.ROLE_OCCUPANT_SEARCH_FAILED, error, e);
    } catch (CursorException e) {
        String error = "findAssignedRoles userDn [" + userDn + "] caught CursorException=" + e.getMessage();
        throw new FinderException(GlobalErrIds.ROLE_OCCUPANT_SEARCH_FAILED, error, e);
    } finally {
        closeAdminConnection(ld);
    }
    return roleNameList;
}
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) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection)

Example 22 with SearchCursor

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

the class RoleDAO method findRoles.

/**
 * @param role
 * @param limit
 * @return
 * @throws org.apache.directory.fortress.core.FinderException
 */
List<String> findRoles(Role role, int limit) throws FinderException {
    List<String> roleList = new ArrayList<>();
    LdapConnection ld = null;
    String roleRoot = getRootDn(role.getContextId(), GlobalIds.ROLE_ROOT);
    String filter = null;
    try {
        String searchVal = encodeSafeText(role.getName(), GlobalIds.ROLE_LEN);
        filter = GlobalIds.FILTER_PREFIX + GlobalIds.ROLE_OBJECT_CLASS_NM + ")(" + ROLE_NM + "=" + searchVal + "*))";
        ld = getAdminConnection();
        SearchCursor searchResults = search(ld, roleRoot, SearchScope.ONELEVEL, filter, ROLE_NM_ATR, false, limit);
        while (searchResults.next()) {
            Entry entry = searchResults.getEntry();
            roleList.add(getAttribute(entry, ROLE_NM));
        }
    } catch (LdapException e) {
        String error = "findRoles filter [" + filter + "] caught LdapException=" + e.getMessage();
        throw new FinderException(GlobalErrIds.ROLE_SEARCH_FAILED, error, e);
    } catch (CursorException e) {
        String error = "findRoles filter [" + filter + "] caught CursorException=" + e.getMessage();
        throw new FinderException(GlobalErrIds.ROLE_SEARCH_FAILED, error, e);
    } finally {
        closeAdminConnection(ld);
    }
    return roleList;
}
Also used : FinderException(org.apache.directory.fortress.core.FinderException) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) 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 23 with SearchCursor

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

the class LdapDataProvider method searchNode.

/**
 * 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;
}
Also used : SearchRequest(org.apache.directory.api.ldap.model.message.SearchRequest) Entry(org.apache.directory.api.ldap.model.entry.Entry) SearchRequestImpl(org.apache.directory.api.ldap.model.message.SearchRequestImpl) SearchCursor(org.apache.directory.api.ldap.model.cursor.SearchCursor) Dn(org.apache.directory.api.ldap.model.name.Dn) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 24 with SearchCursor

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

the class ExampleDAO method findExamples.

/**
 * @param searchVal
 * @return
 * @throws org.apache.directory.fortress.core.FinderException
 */
public List<Example> findExamples(String searchVal) throws FinderException {
    List<Example> exampleList = new ArrayList<>();
    LdapConnection ld = null;
    String exampleRoot = Config.getInstance().getProperty(EIds.EXAMPLE_ROOT);
    if (LOG.isDebugEnabled()) {
        LOG.debug("findExamples: " + EIds.EXAMPLE_ROOT + " [" + exampleRoot + "]");
    }
    try {
        searchVal = encodeSafeText(searchVal, GlobalIds.ROLE_LEN);
        ld = getAdminConnection();
        String filter = GlobalIds.FILTER_PREFIX + Arrays.toString(EIds.EXAMPLE_OBJ_CLASS) + ")(" + EIds.EXAMPLE_NM + "=" + searchVal + "*))";
        SearchCursor searchResults = search(ld, exampleRoot, SearchScope.SUBTREE, filter, EXAMPLE_ATRS, false, GlobalIds.BATCH_SIZE);
        while (searchResults.next()) {
            exampleList.add(getEntityFromLdapEntry(searchResults.getEntry()));
        }
    } catch (LdapException e) {
        String error = "findExamples caught LDAPException=" + e;
        LOG.warn(error);
        throw new FinderException(EErrIds.EXAMPLE_SEARCH_FAILED, error);
    } catch (CursorException e) {
        String error = "findExamples caught CursorException=" + e;
        throw new FinderException(EErrIds.EXAMPLE_SEARCH_FAILED, error, e);
    } finally {
        closeAdminConnection(ld);
    }
    return exampleList;
}
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) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection)

Example 25 with SearchCursor

use of org.apache.directory.api.ldap.model.cursor.SearchCursor in project jackrabbit-oak by apache.

the class LdapIdentityProvider method getEntry.

@CheckForNull
private Entry getEntry(@Nonnull LdapConnection connection, @Nonnull LdapProviderConfig.Identity idConfig, @Nonnull String id, @Nonnull String[] customAttributes) throws CursorException, LdapException {
    String searchFilter = idConfig.getSearchFilter(id);
    // Create the SearchRequest object
    SearchRequest req = new SearchRequestImpl();
    req.setScope(SearchScope.SUBTREE);
    if (customAttributes.length == 0) {
        req.addAttributes(SchemaConstants.ALL_USER_ATTRIBUTES);
    } else {
        req.addAttributes(customAttributes);
    }
    req.setTimeLimit((int) config.getSearchTimeout());
    req.setBase(new Dn(idConfig.getBaseDN()));
    req.setFilter(searchFilter);
    if (log.isDebugEnabled()) {
        log.debug("getEntry: using SearchRequest {}.", req);
    }
    // Process the request
    SearchCursor searchCursor = null;
    Entry resultEntry = null;
    try {
        searchCursor = connection.search(req);
        while (searchCursor.next()) {
            if (resultEntry != null) {
                log.warn("search for {} returned more than one entry. discarding additional ones.", searchFilter);
            } else {
                // process the SearchResultEntry
                Response response = searchCursor.get();
                if (response instanceof SearchResultEntry) {
                    resultEntry = ((SearchResultEntry) response).getEntry();
                }
            }
        }
    } finally {
        if (searchCursor != null) {
            try {
                searchCursor.close();
            } catch (IOException e) {
                log.warn("Failed to close search cursor.", e);
            }
        }
    }
    if (log.isDebugEnabled()) {
        if (resultEntry == null) {
            log.debug("getEntry: search below {} with {} found 0 entries.", idConfig.getBaseDN(), searchFilter);
        } else {
            log.debug("getEntry: search below {} with {} found {}", idConfig.getBaseDN(), searchFilter, resultEntry.getDn());
        }
    }
    return resultEntry;
}
Also used : Response(org.apache.directory.api.ldap.model.message.Response) SearchRequest(org.apache.directory.api.ldap.model.message.SearchRequest) Entry(org.apache.directory.api.ldap.model.entry.Entry) SearchResultEntry(org.apache.directory.api.ldap.model.message.SearchResultEntry) SearchRequestImpl(org.apache.directory.api.ldap.model.message.SearchRequestImpl) SearchCursor(org.apache.directory.api.ldap.model.cursor.SearchCursor) Dn(org.apache.directory.api.ldap.model.name.Dn) IOException(java.io.IOException) SearchResultEntry(org.apache.directory.api.ldap.model.message.SearchResultEntry) CheckForNull(javax.annotation.CheckForNull)

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