use of org.gluu.site.ldap.persistence.exception.EntryPersistenceException in project oxCore by GluuFederation.
the class LdapEntryManager method removeSubtreeThroughIteration.
private void removeSubtreeThroughIteration(String dn) {
SearchResult searchResult = null;
try {
searchResult = this.ldapOperationService.search(dn, Filter.createPresenceFilter("objectClass"), 0, 0, null, "dn");
if (!ResultCode.SUCCESS.equals(searchResult.getResultCode())) {
throw new EntryPersistenceException(String.format("Failed to find sub-entries of entry '%s' for removal", dn));
}
} catch (LDAPSearchException ex) {
throw new EntryPersistenceException(String.format("Failed to find sub-entries of entry '%s' for removal", dn), ex);
}
List<String> removeEntriesDn = new ArrayList<String>(searchResult.getEntryCount());
for (SearchResultEntry searchResultEntry : searchResult.getSearchEntries()) {
removeEntriesDn.add(searchResultEntry.getDN());
}
Collections.sort(removeEntriesDn, LINE_LENGHT_COMPARATOR);
for (String removeEntryDn : removeEntriesDn) {
remove(removeEntryDn);
}
}
use of org.gluu.site.ldap.persistence.exception.EntryPersistenceException in project oxCore by GluuFederation.
the class LdapEntryManager method merge.
@Override
public void merge(String dn, List<AttributeDataModification> attributeDataModifications) {
// Update entry
try {
List<Modification> modifications = new ArrayList<Modification>(attributeDataModifications.size());
for (AttributeDataModification attributeDataModification : attributeDataModifications) {
AttributeData attribute = attributeDataModification.getAttribute();
AttributeData oldAttribute = attributeDataModification.getOldAttribute();
Modification modification = null;
if (AttributeModificationType.ADD.equals(attributeDataModification.getModificationType())) {
modification = new Modification(ModificationType.ADD, attribute.getName(), attribute.getValues());
} else if (AttributeModificationType.REMOVE.equals(attributeDataModification.getModificationType())) {
modification = new Modification(ModificationType.DELETE, oldAttribute.getName(), oldAttribute.getValues());
} else if (AttributeModificationType.REPLACE.equals(attributeDataModification.getModificationType())) {
if (attribute.getValues().length == 1) {
modification = new Modification(ModificationType.REPLACE, attribute.getName(), attribute.getValues());
} else {
String[] oldValues = ArrayHelper.arrayClone(oldAttribute.getValues());
String[] newValues = ArrayHelper.arrayClone(attribute.getValues());
Arrays.sort(oldValues);
Arrays.sort(newValues);
boolean[] retainOldValues = new boolean[oldValues.length];
Arrays.fill(retainOldValues, false);
List<String> addValues = new ArrayList<String>();
List<String> removeValues = new ArrayList<String>();
// Add new values
for (String value : newValues) {
int idx = Arrays.binarySearch(oldValues, value, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.toLowerCase().compareTo(o2.toLowerCase());
}
});
if (idx >= 0) {
// Old values array contains new value. Retain
// old value
retainOldValues[idx] = true;
} else {
// This is new value
addValues.add(value);
}
}
// Remove values which we don't have in new values
for (int i = 0; i < oldValues.length; i++) {
if (!retainOldValues[i]) {
removeValues.add(oldValues[i]);
}
}
if (removeValues.size() > 0) {
Modification removeModification = new Modification(ModificationType.DELETE, attribute.getName(), removeValues.toArray(new String[removeValues.size()]));
modifications.add(removeModification);
}
if (addValues.size() > 0) {
Modification addModification = new Modification(ModificationType.ADD, attribute.getName(), addValues.toArray(new String[addValues.size()]));
modifications.add(addModification);
}
}
}
if (modification != null) {
modifications.add(modification);
}
}
if (modifications.size() > 0) {
boolean result = this.ldapOperationService.updateEntry(dn, modifications);
if (!result) {
throw new EntryPersistenceException(String.format("Failed to update entry: %s", dn));
}
}
} catch (ConnectionException ex) {
throw new EntryPersistenceException(String.format("Failed to update entry: %s", dn), ex.getCause());
} catch (Exception ex) {
throw new EntryPersistenceException(String.format("Failed to update entry: %s", dn), ex);
}
}
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;
}
Aggregations