Search in sources :

Example 16 with SearchException

use of io.jans.orm.exception.operation.SearchException in project jans by JanssenProject.

the class SqlEntryManager method findEntriesImpl.

protected <T> PagedResult<EntryData> findEntriesImpl(String baseDN, Class<T> entryClass, Filter filter, SearchScope scope, String[] ldapReturnAttributes, String sortBy, SortOrder sortOrder, BatchOperation<T> batchOperation, SearchReturnDataType returnDataType, int start, int count, int chunkSize) {
    // Check entry class
    checkEntryClass(entryClass, false);
    String[] objectClasses = getTypeObjectClasses(entryClass);
    List<PropertyAnnotation> propertiesAnnotations = getEntryPropertyAnnotations(entryClass);
    String[] currentLdapReturnAttributes = ldapReturnAttributes;
    if (ArrayHelper.isEmpty(currentLdapReturnAttributes)) {
        currentLdapReturnAttributes = getAttributes(null, propertiesAnnotations, false);
    }
    Filter searchFilter;
    if (objectClasses.length > 0) {
        LOG.trace("Filter: {}", filter);
        searchFilter = addObjectClassFilter(filter, objectClasses);
    } else {
        searchFilter = filter;
    }
    // Find entries
    LOG.trace("-------------------------------------------------------");
    LOG.trace("Filter: {}", filter);
    LOG.trace("objectClasses count: {} ", objectClasses.length);
    LOG.trace("objectClasses: {}", ArrayHelper.toString(objectClasses));
    LOG.trace("Search filter: {}", searchFilter);
    // Prepare default sort
    OrderSpecifier<?>[] defaultSort = getDefaultSort(entryClass);
    if (StringHelper.isNotEmpty(sortBy)) {
        OrderSpecifier<?> requestedSort = buildSort(sortBy, sortOrder);
        if (ArrayHelper.isEmpty(defaultSort)) {
            defaultSort = new OrderSpecifier[] { requestedSort };
        } else {
            defaultSort = ArrayHelper.arrayMerge(new OrderSpecifier[] { requestedSort }, defaultSort);
        }
    }
    // Prepare properties types to allow build filter properly
    Map<String, PropertyAnnotation> propertiesAnnotationsMap = prepareEntryPropertiesTypes(entryClass, propertiesAnnotations);
    ParsedKey keyWithInum = toSQLKey(baseDN);
    ConvertedExpression convertedExpression;
    try {
        convertedExpression = toSqlFilter(searchFilter, propertiesAnnotationsMap);
    } catch (SearchException ex) {
        throw new EntryPersistenceException(String.format("Failed to convert filter '%s' to expression", searchFilter));
    }
    PagedResult<EntryData> searchResult = null;
    try {
        SqlBatchOperationWraper<T> batchOperationWraper = null;
        if (batchOperation != null) {
            batchOperationWraper = new SqlBatchOperationWraper<T>(batchOperation, this, entryClass, propertiesAnnotations);
        }
        searchResult = searchImpl(keyWithInum.getKey(), objectClasses[0], convertedExpression, scope, currentLdapReturnAttributes, defaultSort, batchOperationWraper, returnDataType, start, count, chunkSize);
        if (searchResult == null) {
            throw new EntryPersistenceException(String.format("Failed to find entries with key: '%s', expression: '%s'", keyWithInum.getKey(), convertedExpression));
        }
        return searchResult;
    } catch (SearchException ex) {
        throw new EntryPersistenceException(String.format("Failed to find entries with key: '%s'", keyWithInum.getKey()), ex);
    } catch (Exception ex) {
        throw new EntryPersistenceException(String.format("Failed to find entries with key: '%s', expression: '%s'", keyWithInum.getKey(), convertedExpression), ex);
    }
}
Also used : EntryData(io.jans.orm.model.EntryData) SearchException(io.jans.orm.exception.operation.SearchException) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) MappingException(io.jans.orm.exception.MappingException) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) EntryDeleteException(io.jans.orm.exception.EntryDeleteException) SearchException(io.jans.orm.exception.operation.SearchException) DateTimeParseException(java.time.format.DateTimeParseException) AuthenticationException(io.jans.orm.exception.AuthenticationException) PropertyAnnotation(io.jans.orm.reflect.property.PropertyAnnotation) Filter(io.jans.orm.search.filter.Filter) ConvertedExpression(io.jans.orm.sql.model.ConvertedExpression) ParsedKey(io.jans.orm.impl.model.ParsedKey) OrderSpecifier(com.querydsl.core.types.OrderSpecifier)

Example 17 with SearchException

use of io.jans.orm.exception.operation.SearchException in project jans by JanssenProject.

the class SqlOperationServiceImpl method lookupImpl.

private List<AttributeData> lookupImpl(TableMapping tableMapping, String key, String... attributes) throws SearchException, EntryConvertationException {
    try {
        RelationalPathBase<Object> tableRelationalPath = buildTableRelationalPath(tableMapping);
        Predicate whereExp = ExpressionUtils.eq(Expressions.stringPath(SqlOperationService.DOC_ID), Expressions.constant(key));
        Expression<?> attributesExp = buildSelectAttributes(attributes);
        SQLQuery<?> sqlSelectQuery = sqlQueryFactory.select(attributesExp).from(tableRelationalPath).where(whereExp).limit(1);
        try (ResultSet resultSet = sqlSelectQuery.getResults()) {
            List<AttributeData> result = getAttributeDataList(tableMapping, resultSet, true);
            if (result != null) {
                return result;
            }
        }
    } catch (SQLException | QueryException ex) {
        throw new SearchException(String.format("Failed to lookup query by key: '%s'", key), ex);
    }
    throw new SearchException(String.format("Failed to lookup entry by key: '%s'", key));
}
Also used : QueryException(com.querydsl.core.QueryException) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) SearchException(io.jans.orm.exception.operation.SearchException) AttributeData(io.jans.orm.model.AttributeData) Predicate(com.querydsl.core.types.Predicate)

Example 18 with SearchException

use of io.jans.orm.exception.operation.SearchException in project jans by JanssenProject.

the class LdapEntryManager method exportEntry.

@Override
public List<AttributeData> exportEntry(String dn) {
    try {
        SearchResultEntry searchResultEntry = getOperationService().lookup(dn, (String[]) null);
        List<AttributeData> result = getAttributeDataList(searchResultEntry);
        if (result != null) {
            return result;
        }
        return null;
    } catch (ConnectionException | SearchException ex) {
        throw new EntryPersistenceException(String.format("Failed to find entry: %s", dn), ex);
    }
}
Also used : SearchException(io.jans.orm.exception.operation.SearchException) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) AttributeData(io.jans.orm.model.AttributeData) ConnectionException(io.jans.orm.exception.operation.ConnectionException) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry)

Example 19 with SearchException

use of io.jans.orm.exception.operation.SearchException in project jans by JanssenProject.

the class LdapFilterConverter method convertToLdapFilter.

public com.unboundid.ldap.sdk.Filter convertToLdapFilter(Filter genericFilter) throws SearchException {
    FilterType type = genericFilter.getType();
    if (FilterType.RAW == type) {
        try {
            return com.unboundid.ldap.sdk.Filter.create(genericFilter.getFilterString());
        } catch (com.unboundid.ldap.sdk.LDAPException ex) {
            throw new SearchException("Failed to parse RAW Ldap filter", ex, ex.getResultCode().intValue());
        }
    }
    if ((FilterType.NOT == type) || (FilterType.AND == type) || (FilterType.OR == type)) {
        Filter[] genericFilters = genericFilter.getFilters();
        com.unboundid.ldap.sdk.Filter[] ldapFilters = new com.unboundid.ldap.sdk.Filter[genericFilters.length];
        if (genericFilters != null) {
            for (int i = 0; i < genericFilters.length; i++) {
                ldapFilters[i] = convertToLdapFilter(genericFilters[i]);
            }
            if (FilterType.NOT == type) {
                return com.unboundid.ldap.sdk.Filter.createNOTFilter(ldapFilters[0]);
            } else if (FilterType.AND == type) {
                return com.unboundid.ldap.sdk.Filter.createANDFilter(ldapFilters);
            } else if (FilterType.OR == type) {
                return com.unboundid.ldap.sdk.Filter.createORFilter(ldapFilters);
            }
        }
    }
    if (FilterType.EQUALITY == type) {
        String attributeName;
        if (ArrayHelper.isEmpty(genericFilter.getFilters())) {
            attributeName = genericFilter.getAttributeName();
        } else {
            attributeName = convertToLdapFilter(genericFilter.getFilters()[0]).getAttributeName();
        }
        return com.unboundid.ldap.sdk.Filter.createEqualityFilter(attributeName, String.valueOf(genericFilter.getAssertionValue()));
    }
    if (FilterType.LESS_OR_EQUAL == type) {
        return com.unboundid.ldap.sdk.Filter.createLessOrEqualFilter(genericFilter.getAttributeName(), String.valueOf(genericFilter.getAssertionValue()));
    }
    if (FilterType.GREATER_OR_EQUAL == type) {
        return com.unboundid.ldap.sdk.Filter.createGreaterOrEqualFilter(genericFilter.getAttributeName(), String.valueOf(genericFilter.getAssertionValue()));
    }
    if (FilterType.PRESENCE == type) {
        return com.unboundid.ldap.sdk.Filter.createPresenceFilter(genericFilter.getAttributeName());
    }
    if (FilterType.APPROXIMATE_MATCH == type) {
        return com.unboundid.ldap.sdk.Filter.createApproximateMatchFilter(genericFilter.getAttributeName(), String.valueOf(genericFilter.getAssertionValue()));
    }
    if (FilterType.SUBSTRING == type) {
        return com.unboundid.ldap.sdk.Filter.createSubstringFilter(genericFilter.getAttributeName(), genericFilter.getSubInitial(), genericFilter.getSubAny(), genericFilter.getSubFinal());
    }
    if (FilterType.LOWERCASE == type) {
        // Return Dummy filter because case sensitivity is defined at LDAP schema level
        return com.unboundid.ldap.sdk.Filter.createPresenceFilter(genericFilter.getAttributeName());
    }
    throw new SearchException(String.format("Unknown filter type '%s'", type), com.unboundid.ldap.sdk.ResultCode.PROTOCOL_ERROR_INT_VALUE);
}
Also used : FilterType(io.jans.orm.search.filter.FilterType) Filter(io.jans.orm.search.filter.Filter) SearchException(io.jans.orm.exception.operation.SearchException)

Example 20 with SearchException

use of io.jans.orm.exception.operation.SearchException 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)

Aggregations

SearchException (io.jans.orm.exception.operation.SearchException)34 Filter (io.jans.orm.search.filter.Filter)19 AuthenticationException (io.jans.orm.exception.AuthenticationException)18 EntryPersistenceException (io.jans.orm.exception.EntryPersistenceException)16 MappingException (io.jans.orm.exception.MappingException)16 EntryDeleteException (io.jans.orm.exception.EntryDeleteException)14 PropertyAnnotation (io.jans.orm.reflect.property.PropertyAnnotation)13 DateTimeParseException (java.time.format.DateTimeParseException)9 EntryData (io.jans.orm.model.EntryData)8 JsonObject (com.couchbase.client.java.document.json.JsonObject)7 ArrayList (java.util.ArrayList)7 ParsedKey (io.jans.orm.impl.model.ParsedKey)6 ConvertedExpression (io.jans.orm.couchbase.model.ConvertedExpression)5 ConvertedExpression (io.jans.orm.cloud.spanner.model.ConvertedExpression)4 AttributeData (io.jans.orm.model.AttributeData)4 FilterType (io.jans.orm.search.filter.FilterType)4 ConvertedExpression (io.jans.orm.sql.model.ConvertedExpression)4 DateTimeException (java.time.DateTimeException)4 Duration (java.time.Duration)4 Instant (java.time.Instant)4