use of org.gluu.site.ldap.persistence.exception.MappingException 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.MappingException in project oxCore by GluuFederation.
the class AbstractEntryManager method contains.
public boolean contains(Object entry) {
if (entry == null) {
throw new MappingException("Entry to persist is null");
}
// Check entry class
Class<?> entryClass = entry.getClass();
checkEntryClass(entryClass, false);
String[] objectClasses = getObjectClasses(entry, entryClass);
List<PropertyAnnotation> propertiesAnnotations = getEntryPropertyAnnotations(entryClass);
Object dnValue = getDNValue(entry, entryClass);
List<AttributeData> attributes = getAttributesListForPersist(entry, propertiesAnnotations);
String[] ldapReturnAttributes = getLdapAttributes(null, propertiesAnnotations, false);
return contains(dnValue.toString(), attributes, objectClasses, ldapReturnAttributes);
}
use of org.gluu.site.ldap.persistence.exception.MappingException in project oxCore by GluuFederation.
the class AbstractEntryManager method getEntrySortBy.
protected String[] getEntrySortBy(Class<?> entryClass) {
if (entryClass == null) {
throw new MappingException("Entry class is null");
}
// Check if entry is LDAP Entry
List<Annotation> entryAnnotations = ReflectHelper.getClassAnnotations(entryClass, LDAP_ENTRY_TYPE_ANNOTATIONS);
Annotation annotation = ReflectHelper.getAnnotationByType(entryAnnotations, LdapEntry.class);
if (annotation == null) {
return null;
}
return ((LdapEntry) annotation).sortBy();
}
use of org.gluu.site.ldap.persistence.exception.MappingException in project oxCore by GluuFederation.
the class AbstractEntryManager method getAttribute.
private AttributeData getAttribute(String propertyName, String ldapAttributeName, Getter propertyValueGetter, Object entry, boolean jsonObject) {
Object propertyValue = propertyValueGetter.get(entry);
if (propertyValue == null) {
return null;
}
String[] attributeValues = new String[1];
if (propertyValue instanceof String) {
attributeValues[0] = StringHelper.toString(propertyValue);
} else if (propertyValue instanceof Boolean) {
attributeValues[0] = propertyValue.toString();
} else if (propertyValue instanceof Integer) {
attributeValues[0] = propertyValue.toString();
} else if (propertyValue instanceof Long) {
attributeValues[0] = propertyValue.toString();
} else if (propertyValue instanceof Date) {
attributeValues[0] = encodeGeneralizedTime((Date) propertyValue);
} else if (propertyValue instanceof String[]) {
attributeValues = (String[]) propertyValue;
} else if (propertyValue instanceof List<?>) {
attributeValues = new String[((List<?>) propertyValue).size()];
int index = 0;
for (Object tmpPropertyValue : (List<?>) propertyValue) {
if (jsonObject) {
attributeValues[index++] = convertJsonToString(tmpPropertyValue);
} else {
attributeValues[index++] = StringHelper.toString(tmpPropertyValue);
}
}
} else if (propertyValue instanceof LdapEnum) {
attributeValues[0] = ((LdapEnum) propertyValue).getValue();
} else if (propertyValue instanceof LdapEnum[]) {
LdapEnum[] propertyValues = (LdapEnum[]) propertyValue;
attributeValues = new String[propertyValues.length];
for (int i = 0; i < propertyValues.length; i++) {
attributeValues[i] = (propertyValues[i] == null) ? null : propertyValues[i].getValue();
}
} else if (jsonObject) {
attributeValues[0] = convertJsonToString(propertyValue);
} else {
throw new MappingException("Entry property '" + propertyName + "' should has getter with String, String[], Boolean, Integer, Long, Date, List, LdapEnum or LdapEnum[] return type or has annotation LdapJsonObject");
}
if (log.isDebugEnabled()) {
log.debug(String.format("Property: %s, LdapProperty: %s, PropertyValue: %s", propertyName, ldapAttributeName, Arrays.toString(attributeValues)));
}
if (attributeValues.length == 0) {
attributeValues = new String[] {};
} else if ((attributeValues.length == 1) && StringHelper.isEmpty(attributeValues[0])) {
return null;
}
return new AttributeData(ldapAttributeName, attributeValues);
}
use of org.gluu.site.ldap.persistence.exception.MappingException in project oxCore by GluuFederation.
the class AbstractEntryManager method setCustomObjectClasses.
protected void setCustomObjectClasses(Object entry, Class<?> entryClass, String[] objectClasses) {
List<PropertyAnnotation> customObjectAnnotations = getEntryCustomObjectClassAnnotations(entryClass);
for (PropertyAnnotation propertiesAnnotation : customObjectAnnotations) {
String propertyName = propertiesAnnotation.getPropertyName();
Setter setter = getSetter(entryClass, propertyName);
if (setter == null) {
throw new MappingException("Entry should has setter for property " + propertyName);
}
AttributeData attribute = new AttributeData(propertyName, objectClasses);
setPropertyValue(propertyName, setter, entry, attribute, false);
break;
}
}
Aggregations