Search in sources :

Example 1 with AttributeData

use of io.jans.orm.model.AttributeData 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 AttributeData

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

the class SpannerOperationServiceImpl method lookup.

@Override
public List<AttributeData> lookup(String key, String objectClass, String... attributes) throws SearchException, EntryConvertationException {
    Instant startTime = OperationDurationUtil.instance().now();
    TableMapping tableMapping = connectionProvider.getTableMappingByKey(key, objectClass);
    List<AttributeData> result = lookupImpl(tableMapping, key, attributes);
    Duration duration = OperationDurationUtil.instance().duration(startTime);
    OperationDurationUtil.instance().logDebug("SQL operation: lookup, duration: {}, table: {}, key: {}, attributes: {}", duration, tableMapping.getTableName(), key, attributes);
    return result;
}
Also used : Instant(java.time.Instant) TableMapping(io.jans.orm.cloud.spanner.model.TableMapping) Duration(java.time.Duration) AttributeData(io.jans.orm.model.AttributeData)

Example 3 with AttributeData

use of io.jans.orm.model.AttributeData 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 4 with AttributeData

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

the class SpannerEntryManager method updateMergeChanges.

@Override
protected <T> void updateMergeChanges(String baseDn, T entry, boolean isConfigurationUpdate, Class<?> entryClass, Map<String, AttributeData> attributesFromDbMap, List<AttributeDataModification> attributeDataModifications, boolean forceUpdate) {
    if (forceUpdate) {
        // SQL ORM can't update objectClass because it select table by objectClass name
        return;
    }
    // Update object classes if entry contains custom object classes
    if (!isConfigurationUpdate) {
        String[] objectClasses = getObjectClasses(entry, entryClass);
        if (ArrayHelper.isEmpty(objectClasses)) {
            throw new UnsupportedOperationException(String.format("There is no attribute with objectClasses to persist! Entry is invalid: '%s'", entry));
        }
        AttributeData objectClassAttributeData = attributesFromDbMap.get(OBJECT_CLASS.toLowerCase());
        if (objectClassAttributeData == null) {
            throw new UnsupportedOperationException(String.format("There is no attribute with objectClasses in DB! Entry is invalid: '%s'", entry));
        }
        String[] objectClassesFromDb = objectClassAttributeData.getStringValues();
        if (ArrayHelper.isEmpty(objectClassesFromDb)) {
            throw new UnsupportedOperationException(String.format("There is no attribute with objectClasses in DB! Entry is invalid: '%s'", entry));
        }
        // We need to check only first element of each array because objectCLass in SQL is single value attribute
        if (!StringHelper.equals(objectClassesFromDb[0], objectClasses[0])) {
            throw new UnsupportedOperationException(String.format("It's not possible to change objectClasses of already persisted entry! Entry is invalid: '%s'", entry));
        }
    }
}
Also used : AttributeData(io.jans.orm.model.AttributeData)

Example 5 with AttributeData

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

the class SpannerEntryManager method merge.

@Override
public void merge(String dn, String[] objectClasses, List<AttributeDataModification> attributeDataModifications, Integer expirationValue) {
    // Update entry
    try {
        List<AttributeDataModification> modifications = new ArrayList<AttributeDataModification>(attributeDataModifications.size());
        for (AttributeDataModification attributeDataModification : attributeDataModifications) {
            AttributeData attribute = attributeDataModification.getAttribute();
            AttributeData oldAttribute = attributeDataModification.getOldAttribute();
            String attributeName = null;
            Object[] attributeValues = null;
            Boolean multiValued = null;
            if (attribute != null) {
                attributeName = attribute.getName();
                attributeValues = attribute.getValues();
                multiValued = attribute.getMultiValued();
            }
            String oldAttributeName = null;
            Object[] oldAttributeValues = null;
            if (oldAttribute != null) {
                oldAttributeName = oldAttribute.getName();
                oldAttributeValues = oldAttribute.getValues();
            }
            AttributeDataModification modification = null;
            AttributeModificationType modificationType = attributeDataModification.getModificationType();
            if ((AttributeModificationType.ADD == modificationType) || (AttributeModificationType.FORCE_UPDATE == modificationType)) {
                modification = createModification(modificationType, toInternalAttribute(attributeName), multiValued, attributeValues, oldAttributeValues);
            } else {
                if ((AttributeModificationType.REMOVE == modificationType)) {
                    if ((attribute == null) && isEmptyAttributeValues(oldAttribute)) {
                        // It's RDBS case. We don't need to set null to already empty table cell
                        continue;
                    }
                    modification = createModification(AttributeModificationType.REMOVE, toInternalAttribute(oldAttributeName), multiValued, oldAttributeValues, null);
                } else if ((AttributeModificationType.REPLACE == modificationType)) {
                    modification = createModification(AttributeModificationType.REPLACE, toInternalAttribute(attributeName), multiValued, attributeValues, oldAttributeValues);
                }
            }
            if (modification != null) {
                modifications.add(modification);
            }
        }
        if (modifications.size() > 0) {
            boolean result = getOperationService().updateEntry(toSQLKey(dn).getKey(), objectClasses[0], modifications);
            if (!result) {
                throw new EntryPersistenceException(String.format("Failed to update entry: '%s'", dn));
            }
        }
    } catch (Exception ex) {
        throw new EntryPersistenceException(String.format("Failed to update entry: '%s'", dn), ex);
    }
}
Also used : AttributeDataModification(io.jans.orm.model.AttributeDataModification) AttributeModificationType(io.jans.orm.model.AttributeDataModification.AttributeModificationType) ArrayList(java.util.ArrayList) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) AttributeData(io.jans.orm.model.AttributeData) 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)

Aggregations

AttributeData (io.jans.orm.model.AttributeData)62 MappingException (io.jans.orm.exception.MappingException)29 ArrayList (java.util.ArrayList)23 SearchException (io.jans.orm.exception.operation.SearchException)22 EntryPersistenceException (io.jans.orm.exception.EntryPersistenceException)17 AuthenticationException (io.jans.orm.exception.AuthenticationException)15 EntryDeleteException (io.jans.orm.exception.EntryDeleteException)15 PropertyAnnotation (io.jans.orm.reflect.property.PropertyAnnotation)14 AttributeDataModification (io.jans.orm.model.AttributeDataModification)13 JsonObject (io.jans.orm.annotation.JsonObject)10 LinkedList (java.util.LinkedList)10 DateTimeParseException (java.time.format.DateTimeParseException)8 List (java.util.List)8 ParsedKey (io.jans.orm.impl.model.ParsedKey)7 JsonObject (com.couchbase.client.java.document.json.JsonObject)6 AttributeName (io.jans.orm.annotation.AttributeName)6 PersistenceException (io.jans.orm.exception.operation.PersistenceException)6 Annotation (java.lang.annotation.Annotation)6 AttributeModificationType (io.jans.orm.model.AttributeDataModification.AttributeModificationType)5 LinkedHashMap (java.util.LinkedHashMap)5