use of io.jans.orm.exception.EntryPersistenceException in project jans by JanssenProject.
the class LdapEntryManager method countEntries.
@Override
public <T> int countEntries(String baseDN, Class<T> entryClass, Filter filter, SearchScope scope) {
if (StringHelper.isEmptyString(baseDN)) {
throw new MappingException("Base DN to count entries is null");
}
// Check entry class
checkEntryClass(entryClass, false);
String[] objectClasses = getTypeObjectClasses(entryClass);
// Find entries
Filter searchFilter;
if (objectClasses.length > 0) {
searchFilter = addObjectClassFilter(filter, objectClasses);
} else {
searchFilter = filter;
}
SearchScope searchScope = scope;
if (searchScope == null) {
searchScope = SearchScope.SUB;
}
String[] ldapReturnAttributes;
CountBatchOperation<T> batchOperation;
if (SearchScope.BASE == searchScope) {
// Don't load attributes
ldapReturnAttributes = new String[] { "numsubordinates" };
batchOperation = null;
} else {
// Don't load attributes
ldapReturnAttributes = new String[] { "" };
batchOperation = new CountBatchOperation<T>();
}
SearchResult searchResult;
try {
LdapBatchOperationWraper<T> batchOperationWraper = null;
if (batchOperation != null) {
batchOperationWraper = new LdapBatchOperationWraper<T>(batchOperation);
}
searchResult = getOperationService().search(baseDN, toLdapFilter(searchFilter), toLdapSearchScope(searchScope), batchOperationWraper, 0, 100, 0, null, ldapReturnAttributes);
} catch (Exception ex) {
throw new EntryPersistenceException(String.format("Failed to calculate the number of entries with baseDN: %s, filter: %s", baseDN, searchFilter), ex);
}
if (SearchScope.BASE != searchScope) {
return batchOperation.getCountEntries();
}
if (searchResult.getEntryCount() != 1) {
throw new EntryPersistenceException(String.format("Failed to calculate the number of entries due to missing result entry with baseDN: %s, filter: %s", baseDN, searchFilter));
}
Long result = searchResult.getSearchEntries().get(0).getAttributeValueAsLong("numsubordinates");
if (result == null) {
throw new EntryPersistenceException(String.format("Failed to calculate the number of entries due to missing attribute 'numsubordinates' with baseDN: %s, filter: %s", baseDN, searchFilter));
}
return result.intValue();
}
use of io.jans.orm.exception.EntryPersistenceException in project jans by JanssenProject.
the class LdapEntryManager method find.
@Override
protected List<AttributeData> find(String dn, String[] objectClasses, Map<String, PropertyAnnotation> propertiesAnnotationsMap, String... ldapReturnAttributes) {
try {
// Load entry
SearchResultEntry entry = getOperationService().lookup(dn, ldapReturnAttributes);
List<AttributeData> result = getAttributeDataList(entry);
if (result != null) {
return result;
}
} catch (Exception ex) {
throw new EntryPersistenceException(String.format("Failed to find entry: %s", dn), ex);
}
throw new EntryPersistenceException(String.format("Failed to find entry: %s", dn));
}
use of io.jans.orm.exception.EntryPersistenceException in project jans by JanssenProject.
the class LdapEntryManager method persist.
@Override
protected void persist(String dn, String[] objectClasses, List<AttributeData> attributes, Integer expiration) {
List<Attribute> ldapAttributes = new ArrayList<Attribute>(attributes.size());
for (AttributeData attribute : attributes) {
String attributeName = attribute.getName();
String[] attributeValues = attribute.getStringValues();
if (ArrayHelper.isNotEmpty(attributeValues) && StringHelper.isNotEmpty(attributeValues[0])) {
if (getOperationService().isCertificateAttribute(attributeName)) {
byte[][] binaryValues = toBinaryValues(attributeValues);
ldapAttributes.add(new Attribute(attributeName + ";binary", binaryValues));
} else {
ldapAttributes.add(new Attribute(attributeName, attributeValues));
}
}
}
// Persist entry
try {
boolean result = getOperationService().addEntry(dn, ldapAttributes);
if (!result) {
throw new EntryPersistenceException(String.format("Failed to persist entry: %s", dn));
}
} catch (ConnectionException ex) {
throw new EntryPersistenceException(String.format("Failed to persist entry: %s", dn), ex.getCause());
} catch (Exception ex) {
throw new EntryPersistenceException(String.format("Failed to persist entry: %s", dn), ex);
}
}
use of io.jans.orm.exception.EntryPersistenceException 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);
}
use of io.jans.orm.exception.EntryPersistenceException in project jans by JanssenProject.
the class LdapEntryManager method findPagedEntries.
@Override
public <T> PagedResult<T> findPagedEntries(String baseDN, Class<T> entryClass, Filter filter, String[] ldapReturnAttributes, String sortBy, SortOrder sortOrder, int start, int count, int chunkSize) {
if (StringHelper.isEmptyString(baseDN)) {
throw new MappingException("Base DN to find entries is null");
}
// 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);
}
// Find entries
Filter searchFilter;
if (objectClasses.length > 0) {
searchFilter = addObjectClassFilter(filter, objectClasses);
} else {
searchFilter = filter;
}
List<SearchResultEntry> searchResultEntries;
PagedResult<T> vlvResponse = new PagedResult<T>();
try {
searchResultEntries = getOperationService().searchSearchResultEntryList(baseDN, toLdapFilter(searchFilter), toLdapSearchScope(SearchScope.SUB), start, count, chunkSize, sortBy, sortOrder, vlvResponse, currentLdapReturnAttributes);
} catch (Exception ex) {
throw new EntryPersistenceException(String.format("Failed to find entries with baseDN: %s, filter: %s", baseDN, searchFilter), ex);
}
List<T> entries = new ArrayList<T>(0);
if (searchResultEntries.size() > 0) {
entries = createEntitiesVirtualListView(entryClass, propertiesAnnotations, searchResultEntries.toArray(new SearchResultEntry[] {}));
}
vlvResponse.setEntries(entries);
return vlvResponse;
}
Aggregations