Search in sources :

Example 1 with SearchScope

use of io.jans.orm.model.SearchScope in project jans by JanssenProject.

the class LdapEntryManager method countEntries.

@Override
public <T> int countEntries(String baseDN, Class<T> entryClass, Filter filter, SearchScope scope) {
    if (StringHelper.isEmptyString(baseDN)) {
        throw new MappingException("Base DN to count entries is null");
    }
    // Check entry class
    checkEntryClass(entryClass, false);
    String[] objectClasses = getTypeObjectClasses(entryClass);
    // Find entries
    Filter searchFilter;
    if (objectClasses.length > 0) {
        searchFilter = addObjectClassFilter(filter, objectClasses);
    } else {
        searchFilter = filter;
    }
    SearchScope searchScope = scope;
    if (searchScope == null) {
        searchScope = SearchScope.SUB;
    }
    String[] ldapReturnAttributes;
    CountBatchOperation<T> batchOperation;
    if (SearchScope.BASE == searchScope) {
        // Don't load attributes
        ldapReturnAttributes = new String[] { "numsubordinates" };
        batchOperation = null;
    } else {
        // Don't load attributes
        ldapReturnAttributes = new String[] { "" };
        batchOperation = new CountBatchOperation<T>();
    }
    SearchResult searchResult;
    try {
        LdapBatchOperationWraper<T> batchOperationWraper = null;
        if (batchOperation != null) {
            batchOperationWraper = new LdapBatchOperationWraper<T>(batchOperation);
        }
        searchResult = getOperationService().search(baseDN, toLdapFilter(searchFilter), toLdapSearchScope(searchScope), batchOperationWraper, 0, 100, 0, null, ldapReturnAttributes);
    } catch (Exception ex) {
        throw new EntryPersistenceException(String.format("Failed to calculate the number of entries with baseDN: %s, filter: %s", baseDN, searchFilter), ex);
    }
    if (SearchScope.BASE != searchScope) {
        return batchOperation.getCountEntries();
    }
    if (searchResult.getEntryCount() != 1) {
        throw new EntryPersistenceException(String.format("Failed to calculate the number of entries due to missing result entry with baseDN: %s, filter: %s", baseDN, searchFilter));
    }
    Long result = searchResult.getSearchEntries().get(0).getAttributeValueAsLong("numsubordinates");
    if (result == null) {
        throw new EntryPersistenceException(String.format("Failed to calculate the number of entries due to missing attribute 'numsubordinates' with baseDN: %s, filter: %s", baseDN, searchFilter));
    }
    return result.intValue();
}
Also used : Filter(io.jans.orm.search.filter.Filter) SearchScope(io.jans.orm.model.SearchScope) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) SearchResult(com.unboundid.ldap.sdk.SearchResult) MappingException(io.jans.orm.exception.MappingException) ParseException(java.text.ParseException) EntryDeleteException(io.jans.orm.exception.EntryDeleteException) SearchScopeException(io.jans.orm.exception.operation.SearchScopeException) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) ConnectionException(io.jans.orm.exception.operation.ConnectionException) SearchException(io.jans.orm.exception.operation.SearchException) AuthenticationException(io.jans.orm.exception.AuthenticationException) MappingException(io.jans.orm.exception.MappingException)

Example 2 with SearchScope

use of io.jans.orm.model.SearchScope in project jans by JanssenProject.

the class LdapEntryManager method contains.

@Override
protected <T> boolean contains(String baseDN, String[] objectClasses, Class<T> entryClass, List<PropertyAnnotation> propertiesAnnotations, Filter filter, String[] ldapReturnAttributes) {
    if (StringHelper.isEmptyString(baseDN)) {
        throw new MappingException("Base DN to check contain entries is null");
    }
    // Create filter
    Filter searchFilter;
    if (objectClasses.length > 0) {
        searchFilter = addObjectClassFilter(filter, objectClasses);
    } else {
        searchFilter = filter;
    }
    SearchScope scope = SearchScope.SUB;
    SearchResult searchResult = null;
    try {
        searchResult = getOperationService().search(baseDN, toLdapFilter(searchFilter), toLdapSearchScope(scope), null, 0, 1, 1, null, ldapReturnAttributes);
        if ((searchResult == null) || !ResultCode.SUCCESS.equals(searchResult.getResultCode())) {
            throw new EntryPersistenceException(String.format("Failed to find entry with baseDN: %s, filter: %s", baseDN, searchFilter));
        }
    } catch (SearchScopeException ex) {
        throw new AuthenticationException(String.format("Failed to convert scope: %s", scope), ex);
    } catch (SearchException ex) {
        if (!(ResultCode.NO_SUCH_OBJECT_INT_VALUE == ex.getErrorCode())) {
            throw new EntryPersistenceException(String.format("Failed to find entry with baseDN: %s, filter: %s", baseDN, searchFilter), ex);
        }
    }
    return (searchResult != null) && (searchResult.getEntryCount() > 0);
}
Also used : Filter(io.jans.orm.search.filter.Filter) AuthenticationException(io.jans.orm.exception.AuthenticationException) SearchScope(io.jans.orm.model.SearchScope) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) SearchException(io.jans.orm.exception.operation.SearchException) SearchResult(com.unboundid.ldap.sdk.SearchResult) MappingException(io.jans.orm.exception.MappingException) SearchScopeException(io.jans.orm.exception.operation.SearchScopeException)

Example 3 with SearchScope

use of io.jans.orm.model.SearchScope in project jans by JanssenProject.

the class LdapEntryManager method removeSubtreeThroughIteration.

private void removeSubtreeThroughIteration(String dn) {
    SearchScope scope = SearchScope.SUB;
    SearchResult searchResult = null;
    try {
        searchResult = getOperationService().search(dn, toLdapFilter(Filter.createPresenceFilter("objectClass")), toLdapSearchScope(scope), null, 0, 0, 0, null, "dn");
        if (!ResultCode.SUCCESS.equals(searchResult.getResultCode())) {
            throw new EntryPersistenceException(String.format("Failed to find sub-entries of entry '%s' for removal", dn));
        }
    } catch (SearchScopeException ex) {
        throw new AuthenticationException(String.format("Failed to convert scope: %s", scope), ex);
    } catch (SearchException ex) {
        throw new EntryDeleteException(String.format("Failed to find sub-entries of entry '%s' for removal", dn), ex);
    }
    List<String> removeEntriesDn = new ArrayList<String>(searchResult.getEntryCount());
    for (SearchResultEntry searchResultEntry : searchResult.getSearchEntries()) {
        removeEntriesDn.add(searchResultEntry.getDN());
    }
    Collections.sort(removeEntriesDn, LINE_LENGHT_COMPARATOR);
    for (String removeEntryDn : removeEntriesDn) {
        remove(removeEntryDn);
    }
}
Also used : AuthenticationException(io.jans.orm.exception.AuthenticationException) SearchScope(io.jans.orm.model.SearchScope) ArrayList(java.util.ArrayList) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) SearchException(io.jans.orm.exception.operation.SearchException) SearchResult(com.unboundid.ldap.sdk.SearchResult) EntryDeleteException(io.jans.orm.exception.EntryDeleteException) SearchScopeException(io.jans.orm.exception.operation.SearchScopeException) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry)

Example 4 with SearchScope

use of io.jans.orm.model.SearchScope in project jans by JanssenProject.

the class LdapEntryManager method authenticate.

@Override
public <T> boolean authenticate(String baseDN, Class<T> entryClass, String userName, String password) {
    if (StringHelper.isEmptyString(baseDN)) {
        throw new MappingException("Base DN to count entries is null");
    }
    // Check entry class
    checkEntryClass(entryClass, false);
    String[] objectClasses = getTypeObjectClasses(entryClass);
    // Find entries
    Filter searchFilter = Filter.createEqualityFilter(LdapOperationService.UID, userName);
    if (objectClasses.length > 0) {
        searchFilter = addObjectClassFilter(searchFilter, objectClasses);
    }
    SearchScope scope = SearchScope.SUB;
    try {
        SearchResult searchResult = getOperationService().search(baseDN, toLdapFilter(searchFilter), toLdapSearchScope(scope), null, 0, 1, 1, null, LdapOperationService.UID_ARRAY);
        if ((searchResult == null) || (searchResult.getEntryCount() != 1)) {
            return false;
        }
        String bindDn = searchResult.getSearchEntries().get(0).getDN();
        return getOperationService().authenticate(bindDn, password, null);
    } catch (ConnectionException ex) {
        throw new AuthenticationException(String.format("Failed to authenticate user: %s", userName), ex);
    } catch (SearchScopeException ex) {
        throw new AuthenticationException(String.format("Failed to convert scope: %s", scope), ex);
    } catch (SearchException ex) {
        throw new AuthenticationException(String.format("Failed to find user DN: %s", userName), ex);
    }
}
Also used : Filter(io.jans.orm.search.filter.Filter) AuthenticationException(io.jans.orm.exception.AuthenticationException) SearchScope(io.jans.orm.model.SearchScope) SearchException(io.jans.orm.exception.operation.SearchException) SearchResult(com.unboundid.ldap.sdk.SearchResult) ConnectionException(io.jans.orm.exception.operation.ConnectionException) MappingException(io.jans.orm.exception.MappingException) SearchScopeException(io.jans.orm.exception.operation.SearchScopeException)

Aggregations

SearchResult (com.unboundid.ldap.sdk.SearchResult)4 AuthenticationException (io.jans.orm.exception.AuthenticationException)4 SearchException (io.jans.orm.exception.operation.SearchException)4 SearchScopeException (io.jans.orm.exception.operation.SearchScopeException)4 SearchScope (io.jans.orm.model.SearchScope)4 EntryPersistenceException (io.jans.orm.exception.EntryPersistenceException)3 MappingException (io.jans.orm.exception.MappingException)3 Filter (io.jans.orm.search.filter.Filter)3 EntryDeleteException (io.jans.orm.exception.EntryDeleteException)2 ConnectionException (io.jans.orm.exception.operation.ConnectionException)2 SearchResultEntry (com.unboundid.ldap.sdk.SearchResultEntry)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1