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;
}
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;
}
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));
}
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));
}
}
}
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);
}
}
Aggregations