Search in sources :

Example 1 with SearchException

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

the class SpannerOperationServiceImpl 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("Spanner 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.cloud.spanner.model.TableMapping) EntryConvertationException(io.jans.orm.exception.operation.EntryConvertationException) Duration(java.time.Duration) AttributeData(io.jans.orm.model.AttributeData)

Example 2 with SearchException

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

the class SpannerOperationServiceImpl method searchImpl.

private <O> PagedResult<EntryData> searchImpl(TableMapping tableMapping, String key, ConvertedExpression expression, SearchScope scope, String[] attributes, Sort[] orderBy, SpannerBatchOperationWraper<O> batchOperationWraper, SearchReturnDataType returnDataType, int start, int count, int pageSize) throws SearchException {
    BatchOperation<O> batchOperation = null;
    if (batchOperationWraper != null) {
        batchOperation = (BatchOperation<O>) batchOperationWraper.getBatchOperation();
    }
    Table table = buildTable(tableMapping);
    PlainSelect sqlSelectQuery = new PlainSelect();
    sqlSelectQuery.setFromItem(table);
    List<SelectItem> selectItems = buildSelectAttributes(tableMapping, key, attributes);
    sqlSelectQuery.addSelectItems(selectItems);
    if (expression != null) {
        applyWhereExpression(sqlSelectQuery, expression);
    }
    if (orderBy != null) {
        OrderByElement[] orderByElements = new OrderByElement[orderBy.length];
        for (int i = 0; i < orderBy.length; i++) {
            Column column = new Column(orderBy[i].getName());
            orderByElements[i] = new OrderByElement();
            orderByElements[i].setExpression(column);
            if (orderBy[i].getSortOrder() != null) {
                orderByElements[i].setAscDescPresent(true);
                orderByElements[i].setAsc(SortOrder.ASCENDING == orderBy[i].getSortOrder());
            }
        }
        sqlSelectQuery.withOrderByElements(Arrays.asList(orderByElements));
    }
    List<EntryData> searchResultList = new LinkedList<EntryData>();
    if ((SearchReturnDataType.SEARCH == returnDataType) || (SearchReturnDataType.SEARCH_COUNT == returnDataType)) {
        List<EntryData> lastResult = null;
        if (pageSize > 0) {
            boolean collectSearchResult;
            Limit limit = new Limit();
            sqlSelectQuery.setLimit(limit);
            Offset offset = new Offset();
            sqlSelectQuery.setOffset(offset);
            int currentLimit;
            try {
                int resultCount = 0;
                int lastCountRows = 0;
                do {
                    collectSearchResult = true;
                    currentLimit = pageSize;
                    if (count > 0) {
                        currentLimit = Math.min(pageSize, count - resultCount);
                    }
                    // Change limit and offset
                    limit.setRowCount(new LongValue(currentLimit));
                    offset.setOffset(start + resultCount);
                    Statement.Builder statementBuilder = Statement.newBuilder(sqlSelectQuery.toString());
                    applyParametersBinding(statementBuilder, expression);
                    Statement statement = statementBuilder.build();
                    LOG.debug("Executing query: '{}'", statement);
                    try (ResultSet resultSet = databaseClient.singleUse().executeQuery(statement)) {
                        lastResult = getEntryDataList(tableMapping.getObjectClass(), resultSet);
                    }
                    lastCountRows = lastResult.size();
                    if (batchOperation != null) {
                        collectSearchResult = batchOperation.collectSearchResult(lastCountRows);
                    }
                    if (collectSearchResult) {
                        searchResultList.addAll(lastResult);
                    }
                    if (batchOperation != null) {
                        List<O> entries = batchOperationWraper.createEntities(lastResult);
                        batchOperation.performAction(entries);
                    }
                    resultCount += lastCountRows;
                    if ((count > 0) && (resultCount >= count)) {
                        break;
                    }
                } while (lastCountRows > 0);
            } catch (SpannerException | EntryConvertationException | IncompatibleTypeException ex) {
                LOG.error("Failed to execute query with expression: '{}'", expression);
                throw new SearchException(String.format("Failed to execute query '%s'  with key: '%s'", sqlSelectQuery, key), ex);
            }
        } else {
            try {
                long currentLimit = count;
                if (currentLimit <= 0) {
                    currentLimit = connectionProvider.getDefaultMaximumResultSize();
                }
                Limit limit = new Limit();
                limit.setRowCount(new LongValue(currentLimit));
                sqlSelectQuery.setLimit(limit);
                if (start > 0) {
                    Offset offset = new Offset();
                    offset.setOffset(start);
                    sqlSelectQuery.setOffset(offset);
                }
                Statement.Builder statementBuilder = Statement.newBuilder(sqlSelectQuery.toString());
                applyParametersBinding(statementBuilder, expression);
                Statement statement = statementBuilder.build();
                LOG.debug("Executing query: '{}'", statement);
                try (ResultSet resultSet = databaseClient.singleUse().executeQuery(statement)) {
                    lastResult = getEntryDataList(tableMapping.getObjectClass(), resultSet);
                    searchResultList.addAll(lastResult);
                }
            } catch (SpannerException | EntryConvertationException | IncompatibleTypeException ex) {
                LOG.error("Failed to execute query with expression: '{}'", expression);
                throw new SearchException(String.format("Failed to execute query '%s'  with key: '%s'", sqlSelectQuery, key), ex);
            }
        }
    }
    PagedResult<EntryData> result = new PagedResult<EntryData>();
    result.setEntries(searchResultList);
    result.setEntriesCount(searchResultList.size());
    result.setStart(start);
    if ((SearchReturnDataType.COUNT == returnDataType) || (SearchReturnDataType.SEARCH_COUNT == returnDataType)) {
        PlainSelect sqlCountSelectQuery = new PlainSelect();
        sqlCountSelectQuery.setFromItem(table);
        Function countFunction = new Function();
        countFunction.setName("COUNT");
        countFunction.setAllColumns(true);
        SelectExpressionItem selectCountItem = new SelectExpressionItem(countFunction);
        selectCountItem.setAlias(new Alias("TOTAL", false));
        sqlCountSelectQuery.addSelectItems(selectCountItem);
        if (expression != null) {
            applyWhereExpression(sqlCountSelectQuery, expression);
        }
        try {
            Statement.Builder statementBuilder = Statement.newBuilder(sqlCountSelectQuery.toString());
            applyParametersBinding(statementBuilder, expression);
            Statement statement = statementBuilder.build();
            LOG.debug("Calculating count. Executing query: '{}'", statement);
            try (ResultSet countResult = databaseClient.singleUse().executeQuery(statement)) {
                if (!countResult.next()) {
                    throw new SearchException(String.format("Failed to calculate count entries. Query: '%s'", statement));
                }
                result.setTotalEntriesCount((int) countResult.getLong("TOTAL"));
            }
        } catch (SpannerException | IncompatibleTypeException ex) {
            LOG.error("Failed to execute query with expression: '{}'", expression);
            throw new SearchException(String.format("Failed to build count search entries query. Key: '%s', expression: '%s'", key, expression.expression()), ex);
        }
    }
    return result;
}
Also used : EntryData(io.jans.orm.model.EntryData) SelectExpressionItem(net.sf.jsqlparser.statement.select.SelectExpressionItem) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) SearchException(io.jans.orm.exception.operation.SearchException) Function(net.sf.jsqlparser.expression.Function) Column(net.sf.jsqlparser.schema.Column) SelectItem(net.sf.jsqlparser.statement.select.SelectItem) ResultSet(com.google.cloud.spanner.ResultSet) IncompatibleTypeException(io.jans.orm.exception.operation.IncompatibleTypeException) OrderByElement(net.sf.jsqlparser.statement.select.OrderByElement) PagedResult(io.jans.orm.model.PagedResult) Table(net.sf.jsqlparser.schema.Table) Statement(com.google.cloud.spanner.Statement) LinkedList(java.util.LinkedList) Offset(net.sf.jsqlparser.statement.select.Offset) Alias(net.sf.jsqlparser.expression.Alias) LongValue(net.sf.jsqlparser.expression.LongValue) EntryConvertationException(io.jans.orm.exception.operation.EntryConvertationException) Limit(net.sf.jsqlparser.statement.select.Limit) SpannerException(com.google.cloud.spanner.SpannerException) Builder(com.google.cloud.spanner.Statement.Builder)

Example 3 with SearchException

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

the class SpannerOperationServiceImpl method buildSelectAttributes.

private List<SelectItem> buildSelectAttributes(TableMapping tableMapping, String key, String... attributes) throws SearchException {
    String tableName = tableMapping.getTableName();
    Map<String, StructField> columTypes = tableMapping.getColumTypes();
    // Table alias for columns
    // Column dn
    Column selectDnColumn = new Column(tableAlias, DN);
    SelectExpressionItem selectDnItem = new SelectExpressionItem(selectDnColumn);
    // Column doc_id
    Column selectDocIdColumn = new Column(tableAlias, DOC_ID);
    SelectExpressionItem selectDocIdItem = new SelectExpressionItem(selectDocIdColumn);
    if (ArrayHelper.isEmpty(attributes)) {
        // Select all columns
        AllTableColumns allColumns = new AllTableColumns(tableAlias);
        List<SelectItem> selectColumns = new ArrayList<SelectItem>();
        selectColumns.add(allColumns);
        // Add columns from child tables
        List<SelectExpressionItem> selectChildColumns = buildSelectAttributeFromChildTables(tableName);
        selectColumns.addAll(selectChildColumns);
        return selectColumns;
    } else if ((attributes.length == 1) && StringHelper.isEmpty(attributes[0])) {
        // Compatibility with base persistence layer when application pass attributes new String[] { "" }
        List<SelectItem> selectColumns = Arrays.asList(selectDnItem, selectDocIdItem);
        // Add columns from child tables
        List<SelectExpressionItem> selectChildColumns = buildSelectAttributeFromChildTables(tableName);
        selectColumns.addAll(selectChildColumns);
        return selectColumns;
    }
    List<SelectItem> expresisons = new ArrayList<SelectItem>(attributes.length + 2);
    boolean hasDn = false;
    for (String attributeName : attributes) {
        StructField attributeType = columTypes.get(attributeName.toLowerCase());
        SelectExpressionItem selectExpressionItem;
        // If column not inside table we should check if there is child table
        if (attributeType == null) {
            TableMapping childTableMapping = connectionProvider.getChildTableMappingByKey(key, tableMapping, attributeName);
            if (childTableMapping == null) {
                throw new SearchException(String.format("Failed to build select attributes. Column '%s' is undefined", attributeName));
            }
            // Add columns from child table
            selectExpressionItem = buildSelectAttributeFromChildTable(tableName, attributeName);
        } else {
            Column selectColumn = new Column(tableAlias, attributeName);
            selectExpressionItem = new SelectExpressionItem(selectColumn);
        }
        expresisons.add(selectExpressionItem);
        hasDn |= StringHelper.equals(attributeName, DN);
    }
    if (!hasDn) {
        expresisons.add(selectDnItem);
    }
    expresisons.add(selectDocIdItem);
    return expresisons;
}
Also used : SelectExpressionItem(net.sf.jsqlparser.statement.select.SelectExpressionItem) ArrayList(java.util.ArrayList) TableMapping(io.jans.orm.cloud.spanner.model.TableMapping) SearchException(io.jans.orm.exception.operation.SearchException) ValueWithStructField(io.jans.orm.cloud.spanner.model.ValueWithStructField) StructField(com.google.cloud.spanner.Type.StructField) Column(net.sf.jsqlparser.schema.Column) AllTableColumns(net.sf.jsqlparser.statement.select.AllTableColumns) SelectItem(net.sf.jsqlparser.statement.select.SelectItem) List(java.util.List) ArrayList(java.util.ArrayList) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList) LinkedList(java.util.LinkedList)

Example 4 with SearchException

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

the class SpannerOperationServiceImpl method lookupImpl.

private List<AttributeData> lookupImpl(TableMapping tableMapping, String key, String... attributes) throws SearchException, EntryConvertationException {
    try {
        String tableName = tableMapping.getTableName();
        // If all requested attributes belong to one table get row by primary key
        Set<String> childTables = connectionProvider.getTableChildAttributes(tableName);
        List<AttributeData> result = null;
        if (childTables == null) {
            // All attributes in one table
            if (attributes == null) {
                // Request all attributes
                try (ResultSet resultSet = databaseClient.singleUse().read(tableName, KeySet.singleKey(Key.of(key)), tableMapping.getColumTypes().keySet())) {
                    result = getAttributeDataList(tableMapping.getObjectClass(), resultSet, true);
                }
            } else {
                // Request only required attributes
                try (ResultSet resultSet = databaseClient.singleUse().read(tableName, KeySet.singleKey(Key.of(key)), Arrays.asList(attributes))) {
                    result = getAttributeDataList(tableMapping.getObjectClass(), resultSet, true);
                }
            }
        } else {
            Table table = buildTable(tableMapping);
            PlainSelect sqlSelectQuery = new PlainSelect();
            sqlSelectQuery.setFromItem(table);
            List<SelectItem> selectItems = buildSelectAttributes(tableMapping, key, attributes);
            sqlSelectQuery.addSelectItems(selectItems);
            Column leftColumn = new Column(tableAlias, DOC_ID);
            UserVariable rightValue = new UserVariable(DOC_ID);
            EqualsTo whereExp = new EqualsTo(leftColumn, rightValue);
            sqlSelectQuery.setWhere(whereExp);
            Limit limit = new Limit();
            limit.setRowCount(new LongValue(1));
            sqlSelectQuery.setLimit(limit);
            Statement statement = Statement.newBuilder(sqlSelectQuery.toString()).bind(DOC_ID).to(key).build();
            LOG.debug("Executing lookup query: '{}'", statement);
            try (ResultSet resultSet = databaseClient.singleUse().executeQuery(statement)) {
                result = getAttributeDataList(tableMapping.getObjectClass(), resultSet, true);
            }
        }
        if (result != null) {
            return result;
        }
    } catch (SpannerException 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 : Table(net.sf.jsqlparser.schema.Table) Statement(com.google.cloud.spanner.Statement) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) SearchException(io.jans.orm.exception.operation.SearchException) UserVariable(net.sf.jsqlparser.expression.UserVariable) Column(net.sf.jsqlparser.schema.Column) SelectItem(net.sf.jsqlparser.statement.select.SelectItem) ResultSet(com.google.cloud.spanner.ResultSet) LongValue(net.sf.jsqlparser.expression.LongValue) EqualsTo(net.sf.jsqlparser.expression.operators.relational.EqualsTo) Limit(net.sf.jsqlparser.statement.select.Limit) SpannerException(com.google.cloud.spanner.SpannerException) AttributeData(io.jans.orm.model.AttributeData)

Example 5 with SearchException

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

the class SpannerEntryManager 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
    Sort[] defaultSort = getDefaultSort(entryClass);
    if (StringHelper.isNotEmpty(sortBy)) {
        Sort requestedSort = buildSort(sortBy, sortOrder);
        if (ArrayHelper.isEmpty(defaultSort)) {
            defaultSort = new Sort[] { requestedSort };
        } else {
            defaultSort = ArrayHelper.arrayMerge(new Sort[] { requestedSort }, defaultSort);
        }
    }
    // Prepare properties types to allow build filter properly
    Map<String, PropertyAnnotation> propertiesAnnotationsMap = prepareEntryPropertiesTypes(entryClass, propertiesAnnotations);
    String key = toSQLKey(baseDN).getKey();
    ConvertedExpression convertedExpression;
    try {
        convertedExpression = toSqlFilter(key, objectClasses[0], searchFilter, propertiesAnnotationsMap);
    } catch (SearchException ex) {
        throw new EntryPersistenceException(String.format("Failed to convert filter '%s' to expression", searchFilter), ex);
    }
    PagedResult<EntryData> searchResult = null;
    try {
        SpannerBatchOperationWraper<T> batchOperationWraper = null;
        if (batchOperation != null) {
            batchOperationWraper = new SpannerBatchOperationWraper<T>(batchOperation, this, entryClass, propertiesAnnotations);
        }
        searchResult = searchImpl(key, 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'", key, convertedExpression));
        }
        return searchResult;
    } catch (SearchException ex) {
        throw new EntryPersistenceException(String.format("Failed to find entries with key: '%s'", key), ex);
    } catch (Exception ex) {
        throw new EntryPersistenceException(String.format("Failed to find entries with key: '%s', expression: '%s'", key, 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) AuthenticationException(io.jans.orm.exception.AuthenticationException) PropertyAnnotation(io.jans.orm.reflect.property.PropertyAnnotation) Filter(io.jans.orm.search.filter.Filter) ConvertedExpression(io.jans.orm.cloud.spanner.model.ConvertedExpression) Sort(io.jans.orm.model.Sort)

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