use of org.gluu.site.ldap.persistence.exception.EntryPersistenceException in project oxCore by GluuFederation.
the class LdapEntryManager method countEntries.
public <T> int countEntries(String baseDN, Class<T> entryClass, Filter filter) {
if (StringHelper.isEmptyString(baseDN)) {
throw new MappingException("Base DN to find entries is null");
}
// Check entry class
checkEntryClass(entryClass, false);
String[] objectClasses = getTypeObjectClasses(entryClass);
// Don't load
String[] ldapReturnAttributes = new String[] { "" };
// attributes
// Find entries
Filter searchFilter;
if (objectClasses.length > 0) {
searchFilter = addObjectClassFilter(filter, objectClasses);
} else {
searchFilter = filter;
}
int countEntries = 0;
ASN1OctetString cookie = null;
SearchResult searchResult = null;
do {
Control[] controls = new Control[] { new SimplePagedResultsControl(100, cookie) };
try {
searchResult = this.ldapOperationService.search(baseDN, searchFilter, 0, 0, controls, ldapReturnAttributes);
if (!ResultCode.SUCCESS.equals(searchResult.getResultCode())) {
throw new EntryPersistenceException(String.format("Failed to calculate count entries with baseDN: %s, filter: %s", baseDN, searchFilter));
}
} catch (Exception ex) {
throw new EntryPersistenceException(String.format("Failed to calculate count entries with baseDN: %s, filter: %s", baseDN, searchFilter), ex);
}
countEntries += searchResult.getEntryCount();
// list
if ((countEntries == 0) || ((countEntries % 100) != 0)) {
break;
}
cookie = null;
for (Control control : searchResult.getResponseControls()) {
if (control instanceof SimplePagedResultsControl) {
cookie = ((SimplePagedResultsControl) control).getCookie();
break;
}
}
} while (cookie != null);
return countEntries;
}
use of org.gluu.site.ldap.persistence.exception.EntryPersistenceException in project oxCore by GluuFederation.
the class LdapEntryManager method find.
@Override
protected List<AttributeData> find(String dn, String... ldapReturnAttributes) {
try {
SearchResultEntry entry = this.ldapOperationService.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 org.gluu.site.ldap.persistence.exception.EntryPersistenceException in project oxCore by GluuFederation.
the class LdapEntryManager method getLDIF.
public List<String[]> getLDIF(String dn, String[] attributes) {
SearchResult searchResult;
try {
searchResult = this.ldapOperationService.search(dn, Filter.create("objectclass=*"), SearchScope.BASE, -1, 0, null, attributes);
if (!ResultCode.SUCCESS.equals(searchResult.getResultCode())) {
throw new EntryPersistenceException(String.format("Failed to find entries with baseDN: %s", dn));
}
} catch (Exception ex) {
throw new EntryPersistenceException(String.format("Failed to find entries with baseDN: %s, filter: %s", dn, null), ex);
}
List<String[]> result = new ArrayList<String[]>();
if (searchResult.getEntryCount() == 0) {
return result;
}
for (SearchResultEntry searchResultEntry : searchResult.getSearchEntries()) {
result.add(searchResultEntry.toLDIF());
}
return result;
}
use of org.gluu.site.ldap.persistence.exception.EntryPersistenceException in project oxCore by GluuFederation.
the class LdapEntryManager method findEntries.
public <T> List<T> findEntries(String baseDN, Class<T> entryClass, Filter filter, SearchScope scope, String[] ldapReturnAttributes, BatchOperation<T> batchOperation, int startIndex, int searchLimit, int sizeLimit) {
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 = getLdapAttributes(null, propertiesAnnotations, false);
}
// Find entries
Filter searchFilter;
if (objectClasses.length > 0) {
searchFilter = addObjectClassFilter(filter, objectClasses);
} else {
searchFilter = filter;
}
SearchResult searchResult = null;
try {
searchResult = this.ldapOperationService.search(baseDN, searchFilter, scope, batchOperation, startIndex, searchLimit, sizeLimit, null, currentLdapReturnAttributes);
if (!ResultCode.SUCCESS.equals(searchResult.getResultCode())) {
throw new EntryPersistenceException(String.format("Failed to find entries with baseDN: %s, filter: %s", baseDN, searchFilter));
}
} catch (Exception ex) {
throw new EntryPersistenceException(String.format("Failed to find entries with baseDN: %s, filter: %s", baseDN, searchFilter), ex);
}
if (searchResult.getEntryCount() == 0) {
return new ArrayList<T>(0);
}
List<T> entries = createEntities(entryClass, propertiesAnnotations, searchResult.getSearchEntries().toArray(new SearchResultEntry[searchResult.getSearchEntries().size()]));
// Default sort if needed
sortEntriesIfNeeded(entryClass, entries);
return entries;
}
use of org.gluu.site.ldap.persistence.exception.EntryPersistenceException in project oxCore by GluuFederation.
the class LdapSampleBatchJob method main.
public static void main(String[] args) {
// Prepare sample connection details
LdapSampleEntryManager ldapSampleEntryManager = new LdapSampleEntryManager();
// Create LDAP entry manager
final LdapEntryManager ldapEntryManager = ldapSampleEntryManager.createLdapEntryManager();
BatchOperation<SimpleTokenLdap> tokenLdapBatchOperation = new BatchOperation<SimpleTokenLdap>(ldapEntryManager) {
private int processedCount = 0;
@Override
protected List<SimpleTokenLdap> getChunkOrNull(int batchSize) {
log.info("Processed: " + processedCount);
final Filter filter = Filter.createPresenceFilter("oxAuthExpiration");
return ldapEntryManager.findEntries("o=gluu", SimpleTokenLdap.class, filter, SearchScope.SUB, new String[] { "oxAuthExpiration" }, this, 0, batchSize, batchSize);
}
@Override
protected void performAction(List<SimpleTokenLdap> objects) {
for (SimpleTokenLdap simpleTokenLdap : objects) {
try {
CustomAttribute customAttribute = getUpdatedAttribute("oxAuthExpiration", simpleTokenLdap.getAttribute("oxAuthExpiration"));
simpleTokenLdap.setCustomAttributes(Arrays.asList(new CustomAttribute[] { customAttribute }));
ldapEntryManager.merge(simpleTokenLdap);
processedCount++;
} catch (EntryPersistenceException ex) {
log.error("Failed to update entry", ex);
}
}
}
};
tokenLdapBatchOperation.iterateAllByChunks(100);
BatchOperation<SimpleSession> sessionBatchOperation = new BatchOperation<SimpleSession>(ldapEntryManager) {
private int processedCount = 0;
@Override
protected List<SimpleSession> getChunkOrNull(int batchSize) {
log.info("Processed: " + processedCount);
final Filter filter = Filter.createPresenceFilter("oxLastAccessTime");
return ldapEntryManager.findEntries("o=gluu", SimpleSession.class, filter, SearchScope.SUB, new String[] { "oxLastAccessTime" }, this, 0, batchSize, batchSize);
}
@Override
protected void performAction(List<SimpleSession> objects) {
for (SimpleSession simpleSession : objects) {
try {
CustomAttribute customAttribute = getUpdatedAttribute("oxLastAccessTime", simpleSession.getAttribute("oxLastAccessTime"));
simpleSession.setCustomAttributes(Arrays.asList(new CustomAttribute[] { customAttribute }));
ldapEntryManager.merge(simpleSession);
processedCount++;
} catch (EntryPersistenceException ex) {
log.error("Failed to update entry", ex);
}
}
}
};
sessionBatchOperation.iterateAllByChunks(100);
}
Aggregations