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