Search in sources :

Example 31 with SearchException

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

the class SqlEntryManager 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;
    }
    // Prepare properties types to allow build filter properly
    Map<String, PropertyAnnotation> propertiesAnnotationsMap = prepareEntryPropertiesTypes(entryClass, propertiesAnnotations);
    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 {
        ParsedKey keyWithInum = toSQLKey(baseDN);
        searchResult = searchImpl(keyWithInum.getKey(), objectClasses[0], convertedExpression, SearchScope.SUB, ldapReturnAttributes, null, null, SearchReturnDataType.SEARCH, 0, 1, 0);
        if (searchResult == null) {
            throw new EntryPersistenceException(String.format("Failed to find entry with baseDN: '%s', filter: '%s'", baseDN, searchFilter));
        }
    } catch (Exception ex) {
        throw new EntryPersistenceException(String.format("Failed to find entry with baseDN: '%s', filter: '%s'", baseDN, searchFilter), ex);
    }
    return (searchResult != null) && (searchResult.getEntriesCount() > 0);
}
Also used : EntryData(io.jans.orm.model.EntryData) Filter(io.jans.orm.search.filter.Filter) ConvertedExpression(io.jans.orm.sql.model.ConvertedExpression) ParsedKey(io.jans.orm.impl.model.ParsedKey) 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) MappingException(io.jans.orm.exception.MappingException) PropertyAnnotation(io.jans.orm.reflect.property.PropertyAnnotation)

Example 32 with SearchException

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

the class SqlEntryManager method removeImpl.

protected <T> int removeImpl(String dn, Class<T> entryClass, Filter filter, int count) {
    // Check entry class
    checkEntryClass(entryClass, false);
    String[] objectClasses = getTypeObjectClasses(entryClass);
    Filter searchFilter;
    if (objectClasses.length > 0) {
        LOG.trace("Filter: {}", filter);
        searchFilter = addObjectClassFilter(filter, objectClasses);
    } else {
        throw new EntryDeleteException(String.format("Failed to delete entries with DN: '%s', filter: '%s' because objectClass is not specified", dn, filter));
    }
    // Find entries
    LOG.trace("-------------------------------------------------------");
    LOG.trace("Filter: {}", filter);
    LOG.trace("objectClasses count: {} ", objectClasses.length);
    LOG.trace("objectClasses: {}", objectClasses.toString());
    LOG.trace("Search filter: {}", searchFilter);
    // Prepare properties types to allow build filter properly
    List<PropertyAnnotation> propertiesAnnotations = getEntryPropertyAnnotations(entryClass);
    Map<String, PropertyAnnotation> propertiesAnnotationsMap = prepareEntryPropertiesTypes(entryClass, propertiesAnnotations);
    ParsedKey keyWithInum = toSQLKey(dn);
    ConvertedExpression convertedExpression;
    try {
        convertedExpression = toSqlFilterWithEmptyAlias(searchFilter, propertiesAnnotationsMap);
    } catch (SearchException ex) {
        throw new EntryDeleteException(String.format("Failed to convert filter '%s' to expression", searchFilter), ex);
    }
    try {
        int processed = (int) getOperationService().delete(keyWithInum.getKey(), objectClasses[0], convertedExpression, count);
        return processed;
    } catch (Exception ex) {
        throw new EntryDeleteException(String.format("Failed to delete entries with key: '%s', expression: '%s'", keyWithInum.getKey(), convertedExpression), ex);
    }
}
Also used : Filter(io.jans.orm.search.filter.Filter) ConvertedExpression(io.jans.orm.sql.model.ConvertedExpression) ParsedKey(io.jans.orm.impl.model.ParsedKey) SearchException(io.jans.orm.exception.operation.SearchException) EntryDeleteException(io.jans.orm.exception.EntryDeleteException) 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)

Example 33 with SearchException

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

the class SqlFilterConverter method convertToSqlFilterImpl.

private ConvertedExpression convertToSqlFilterImpl(Filter genericFilter, Map<String, PropertyAnnotation> propertiesAnnotationsMap, Map<String, Class<?>> jsonAttributes, Function<? super Filter, Boolean> processor, boolean skipAlias) throws SearchException {
    if (genericFilter == null) {
        return null;
    }
    Filter currentGenericFilter = genericFilter;
    FilterType type = currentGenericFilter.getType();
    if (FilterType.RAW == type) {
        LOG.warn("RAW Ldap filter to SQL convertion will be removed in new version!!!");
        currentGenericFilter = ldapFilterConverter.convertRawLdapFilterToFilter(currentGenericFilter.getFilterString());
        type = currentGenericFilter.getType();
    }
    if (processor != null) {
        processor.apply(currentGenericFilter);
    }
    if ((FilterType.NOT == type) || (FilterType.AND == type) || (FilterType.OR == type)) {
        Filter[] genericFilters = currentGenericFilter.getFilters();
        Predicate[] expFilters = new Predicate[genericFilters.length];
        if (genericFilters != null) {
            // We can replace only multiple OR with IN
            boolean canJoinOrFilters = FilterType.OR == type;
            List<Filter> joinOrFilters = new ArrayList<Filter>();
            String joinOrAttributeName = null;
            for (int i = 0; i < genericFilters.length; i++) {
                Filter tmpFilter = genericFilters[i];
                expFilters[i] = (Predicate) convertToSqlFilterImpl(tmpFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias).expression();
                // Check if we can replace OR with IN
                if (!canJoinOrFilters) {
                    continue;
                }
                if (tmpFilter.getMultiValued() != null) {
                    canJoinOrFilters = false;
                    continue;
                }
                if ((FilterType.EQUALITY != tmpFilter.getType()) || (tmpFilter.getFilters() != null)) {
                    canJoinOrFilters = false;
                    continue;
                }
                Boolean isMultiValuedDetected = determineMultiValuedByType(tmpFilter.getAttributeName(), propertiesAnnotationsMap);
                if (!Boolean.FALSE.equals(isMultiValuedDetected)) {
                    if (!Boolean.FALSE.equals(currentGenericFilter.getMultiValued())) {
                        canJoinOrFilters = false;
                        continue;
                    }
                }
                if (joinOrAttributeName == null) {
                    joinOrAttributeName = tmpFilter.getAttributeName();
                    joinOrFilters.add(tmpFilter);
                    continue;
                }
                if (!joinOrAttributeName.equals(tmpFilter.getAttributeName())) {
                    canJoinOrFilters = false;
                    continue;
                }
                joinOrFilters.add(tmpFilter);
            }
            if (FilterType.NOT == type) {
                return ConvertedExpression.build(ExpressionUtils.predicate(Ops.NOT, expFilters[0]), jsonAttributes);
            } else if (FilterType.AND == type) {
                return ConvertedExpression.build(ExpressionUtils.allOf(expFilters), jsonAttributes);
            } else if (FilterType.OR == type) {
                if (canJoinOrFilters) {
                    List<Object> rightObjs = new ArrayList<>(joinOrFilters.size());
                    Filter lastEqFilter = null;
                    for (Filter eqFilter : joinOrFilters) {
                        lastEqFilter = eqFilter;
                        Object assertionValue = eqFilter.getAssertionValue();
                        if (assertionValue instanceof AttributeEnum) {
                            assertionValue = ((AttributeEnum) assertionValue).getValue();
                        }
                        rightObjs.add(assertionValue);
                    }
                    return ConvertedExpression.build(ExpressionUtils.in(buildTypedPath(lastEqFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias), rightObjs), jsonAttributes);
                } else {
                    return ConvertedExpression.build(ExpressionUtils.anyOf(expFilters), jsonAttributes);
                }
            }
        }
    }
    if (FilterType.EQUALITY == type) {
        if (isMultiValue(currentGenericFilter, propertiesAnnotationsMap)) {
            Expression expression = buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias);
            Operation<Boolean> operation = ExpressionUtils.predicate(SqlOps.JSON_CONTAINS, expression, buildTypedExpression(currentGenericFilter, true), Expressions.constant("$.v"));
            return ConvertedExpression.build(operation, jsonAttributes);
        } else {
            Filter usedFilter = currentGenericFilter;
            Expression expression = buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias);
            return ConvertedExpression.build(ExpressionUtils.eq(expression, buildTypedExpression(usedFilter)), jsonAttributes);
        }
    }
    if (FilterType.LESS_OR_EQUAL == type) {
        if (isMultiValue(currentGenericFilter, propertiesAnnotationsMap)) {
            if (currentGenericFilter.getMultiValuedCount() > 1) {
                Collection<Predicate> expressions = new ArrayList<>(currentGenericFilter.getMultiValuedCount());
                for (int i = 0; i < currentGenericFilter.getMultiValuedCount(); i++) {
                    Operation<Boolean> operation = ExpressionUtils.predicate(SqlOps.JSON_EXTRACT, buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias), Expressions.constant("$.v[" + i + "]"));
                    Predicate predicate = Expressions.asComparable(operation).loe(buildTypedExpression(currentGenericFilter));
                    expressions.add(predicate);
                }
                Expression expression = ExpressionUtils.anyOf(expressions);
                return ConvertedExpression.build(expression, jsonAttributes);
            }
            Operation<Boolean> operation = ExpressionUtils.predicate(SqlOps.JSON_EXTRACT, buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias), Expressions.constant("$.v[0]"));
            Expression expression = Expressions.asComparable(operation).loe(buildTypedExpression(currentGenericFilter));
            return ConvertedExpression.build(expression, jsonAttributes);
        } else {
            return ConvertedExpression.build(Expressions.asComparable(buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias)).loe(buildTypedExpression(currentGenericFilter)), jsonAttributes);
        }
    }
    if (FilterType.GREATER_OR_EQUAL == type) {
        if (isMultiValue(currentGenericFilter, propertiesAnnotationsMap)) {
            if (currentGenericFilter.getMultiValuedCount() > 1) {
                Collection<Predicate> expressions = new ArrayList<>(currentGenericFilter.getMultiValuedCount());
                for (int i = 0; i < currentGenericFilter.getMultiValuedCount(); i++) {
                    Operation<Boolean> operation = ExpressionUtils.predicate(SqlOps.JSON_EXTRACT, buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias), Expressions.constant("$.v[" + i + "]"));
                    Predicate predicate = Expressions.asComparable(operation).goe(buildTypedExpression(currentGenericFilter));
                    expressions.add(predicate);
                }
                Expression expression = ExpressionUtils.anyOf(expressions);
                return ConvertedExpression.build(expression, jsonAttributes);
            }
            Operation<Boolean> operation = ExpressionUtils.predicate(SqlOps.JSON_EXTRACT, buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias), Expressions.constant("$.v[0]"));
            Expression expression = Expressions.asComparable(operation).goe(buildTypedExpression(currentGenericFilter));
            return ConvertedExpression.build(expression, jsonAttributes);
        } else {
            return ConvertedExpression.build(Expressions.asComparable(buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias)).goe(buildTypedExpression(currentGenericFilter)), jsonAttributes);
        }
    }
    if (FilterType.PRESENCE == type) {
        Expression expression;
        if (isMultiValue(currentGenericFilter, propertiesAnnotationsMap)) {
            if (currentGenericFilter.getMultiValuedCount() > 1) {
                Collection<Predicate> expressions = new ArrayList<>(currentGenericFilter.getMultiValuedCount());
                for (int i = 0; i < currentGenericFilter.getMultiValuedCount(); i++) {
                    Predicate predicate = ExpressionUtils.isNotNull(ExpressionUtils.predicate(SqlOps.JSON_EXTRACT, buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias), Expressions.constant("$.v[" + i + "]")));
                    expressions.add(predicate);
                }
                Predicate predicate = ExpressionUtils.anyOf(expressions);
                return ConvertedExpression.build(predicate, jsonAttributes);
            }
            expression = ExpressionUtils.predicate(SqlOps.JSON_EXTRACT, buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias), Expressions.constant("$.v[0]"));
        } else {
            expression = buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias);
        }
        return ConvertedExpression.build(ExpressionUtils.isNotNull(expression), jsonAttributes);
    }
    if (FilterType.APPROXIMATE_MATCH == type) {
        throw new SearchException("Convertion from APPROXIMATE_MATCH LDAP filter to SQL filter is not implemented");
    }
    if (FilterType.SUBSTRING == type) {
        StringBuilder like = new StringBuilder();
        if (currentGenericFilter.getSubInitial() != null) {
            like.append(currentGenericFilter.getSubInitial());
        }
        like.append("%");
        String[] subAny = currentGenericFilter.getSubAny();
        if ((subAny != null) && (subAny.length > 0)) {
            for (String any : subAny) {
                like.append(any);
                like.append("%");
            }
        }
        if (currentGenericFilter.getSubFinal() != null) {
            like.append(currentGenericFilter.getSubFinal());
        }
        Expression expression;
        if (isMultiValue(currentGenericFilter, propertiesAnnotationsMap)) {
            if (currentGenericFilter.getMultiValuedCount() > 1) {
                Collection<Predicate> expressions = new ArrayList<>(currentGenericFilter.getMultiValuedCount());
                for (int i = 0; i < currentGenericFilter.getMultiValuedCount(); i++) {
                    Operation<Boolean> operation = ExpressionUtils.predicate(SqlOps.JSON_EXTRACT, buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias), Expressions.constant("$.v[" + i + "]"));
                    Predicate predicate = Expressions.booleanOperation(Ops.LIKE, operation, Expressions.constant(like.toString()));
                    expressions.add(predicate);
                }
                Predicate predicate = ExpressionUtils.anyOf(expressions);
                return ConvertedExpression.build(predicate, jsonAttributes);
            }
            expression = ExpressionUtils.predicate(SqlOps.JSON_EXTRACT, buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias), Expressions.constant("$.v[0]"));
        } else {
            expression = buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias);
        }
        return ConvertedExpression.build(Expressions.booleanOperation(Ops.LIKE, expression, Expressions.constant(like.toString())), jsonAttributes);
    }
    if (FilterType.LOWERCASE == type) {
        return ConvertedExpression.build(ExpressionUtils.toLower(buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias)), jsonAttributes);
    }
    throw new SearchException(String.format("Unknown filter type '%s'", type));
}
Also used : AttributeEnum(io.jans.orm.annotation.AttributeEnum) ArrayList(java.util.ArrayList) SearchException(io.jans.orm.exception.operation.SearchException) Predicate(com.querydsl.core.types.Predicate) FilterType(io.jans.orm.search.filter.FilterType) Filter(io.jans.orm.search.filter.Filter) ConvertedExpression(io.jans.orm.sql.model.ConvertedExpression) Expression(com.querydsl.core.types.Expression)

Example 34 with SearchException

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

the class SqlOperationServiceImpl method authenticateImpl.

private boolean authenticateImpl(String key, String password, String objectClass) throws SearchException {
    Instant startTime = OperationDurationUtil.instance().now();
    boolean result = false;
    if (password != null) {
        try {
            List<AttributeData> attributes = lookup(key, objectClass, USER_PASSWORD);
            Object userPasswordObj = null;
            for (AttributeData attribute : attributes) {
                if (StringHelper.equalsIgnoreCase(attribute.getName(), USER_PASSWORD)) {
                    userPasswordObj = attribute.getValue();
                }
            }
            String userPassword = null;
            if (userPasswordObj instanceof String) {
                userPassword = (String) userPasswordObj;
            }
            if (userPassword != null) {
                if (persistenceExtension == null) {
                    result = PasswordEncryptionHelper.compareCredentials(password, userPassword);
                } else {
                    result = persistenceExtension.compareHashedPasswords(password, userPassword);
                }
            }
        } catch (EntryConvertationException ex) {
            throw new SearchException(String.format("Failed to get '%s' attribute", USER_PASSWORD), ex);
        }
    }
    Duration duration = OperationDurationUtil.instance().duration(startTime);
    TableMapping tableMapping = connectionProvider.getTableMappingByKey(key, objectClass);
    OperationDurationUtil.instance().logDebug("SQL operation: bind, duration: {}, table: {}, key: {}", duration, tableMapping.getTableName(), key);
    return result;
}
Also used : Instant(java.time.Instant) SearchException(io.jans.orm.exception.operation.SearchException) TableMapping(io.jans.orm.sql.model.TableMapping) EntryConvertationException(io.jans.orm.exception.operation.EntryConvertationException) Duration(java.time.Duration) AttributeData(io.jans.orm.model.AttributeData)

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