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 merge.
@SuppressWarnings("unchecked")
private <T> T merge(T entry, boolean isSchemaUpdate, AttributeModificationType schemaModificationType) {
if (entry == null) {
throw new MappingException("Entry to persist is null");
}
Class<?> entryClass = entry.getClass();
List<PropertyAnnotation> propertiesAnnotations = getEntryPropertyAnnotations(entryClass);
Object dnValue = getDNValue(entry, entryClass);
List<AttributeData> attributesToPersist = getAttributesListForPersist(entry, propertiesAnnotations);
Map<String, AttributeData> attributesToPersistMap = getAttributesMap(attributesToPersist);
// Load entry
List<AttributeData> attributesFromLdap;
if (isSchemaUpdate) {
// If it's schema modification request we don't need to load
// attributes from LDAP
attributesFromLdap = new ArrayList<AttributeData>();
} else {
List<String> currentLdapReturnAttributesList = getLdapAttributesList(entry, propertiesAnnotations, false);
currentLdapReturnAttributesList.add("objectClass");
attributesFromLdap = find(dnValue.toString(), currentLdapReturnAttributesList.toArray(EMPTY_STRING_ARRAY));
}
Map<String, AttributeData> attributesFromLdapMap = getAttributesMap(attributesFromLdap);
// Prepare list of modifications
// Process properties with LdapAttribute annotation
List<AttributeDataModification> attributeDataModifications = new ArrayList<AttributeDataModification>();
for (PropertyAnnotation propertiesAnnotation : propertiesAnnotations) {
String propertyName = propertiesAnnotation.getPropertyName();
Annotation ldapAttribute;
ldapAttribute = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), LdapAttribute.class);
if (ldapAttribute != null) {
String ldapAttributeName = ((LdapAttribute) ldapAttribute).name();
if (StringHelper.isEmpty(ldapAttributeName)) {
ldapAttributeName = propertyName;
}
ldapAttributeName = ldapAttributeName.toLowerCase();
AttributeData attributeToPersist = attributesToPersistMap.get(ldapAttributeName);
AttributeData attributeFromLdap = attributesFromLdapMap.get(ldapAttributeName);
// Remove processed attributes
attributesToPersistMap.remove(ldapAttributeName);
attributesFromLdapMap.remove(ldapAttributeName);
LdapAttribute ldapAttributeAnnotation = (LdapAttribute) ldapAttribute;
if (ldapAttributeAnnotation.ignoreDuringUpdate()) {
continue;
}
if (attributeFromLdap != null && attributeToPersist != null) {
// Modify DN entry attribute in DS
if (!attributeFromLdap.equals(attributeToPersist)) {
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REPLACE, attributeToPersist, attributeFromLdap));
}
} else if ((attributeFromLdap == null) && (attributeToPersist != null)) {
// Add entry attribute or change schema
if (isSchemaUpdate && (attributeToPersist.getValue() == null && Arrays.equals(attributeToPersist.getValues(), new String[] {}))) {
continue;
}
AttributeModificationType modType = isSchemaUpdate ? schemaModificationType : AttributeModificationType.ADD;
if (AttributeModificationType.ADD.equals(modType)) {
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.ADD, attributeToPersist));
} else {
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REMOVE, null, attributeToPersist));
}
} else if ((attributeFromLdap != null) && (attributeToPersist == null)) {
// or updateOnly = true
if (!ldapAttributeAnnotation.ignoreDuringRead() && !ldapAttributeAnnotation.updateOnly()) {
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REMOVE, null, attributeFromLdap));
}
}
}
}
// Process properties with LdapAttributesList annotation
for (PropertyAnnotation propertiesAnnotation : propertiesAnnotations) {
Annotation ldapAttribute;
ldapAttribute = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), LdapAttributesList.class);
if (ldapAttribute != null) {
Map<String, LdapAttribute> ldapAttributesConfiguration = new HashMap<String, LdapAttribute>();
for (LdapAttribute ldapAttributeConfiguration : ((LdapAttributesList) ldapAttribute).attributesConfiguration()) {
ldapAttributesConfiguration.put(ldapAttributeConfiguration.name(), ldapAttributeConfiguration);
}
// Prepare attributes for removal
for (AttributeData attributeFromLdap : attributesFromLdapMap.values()) {
String attributeName = attributeFromLdap.getName();
if (OBJECT_CLASS.equalsIgnoreCase(attributeName)) {
continue;
}
LdapAttribute ldapAttributeConfiguration = ldapAttributesConfiguration.get(attributeName);
if ((ldapAttributeConfiguration != null) && ldapAttributeConfiguration.ignoreDuringUpdate()) {
continue;
}
if (!attributesToPersistMap.containsKey(attributeName.toLowerCase())) {
// true
if ((ldapAttributeConfiguration == null) || ((ldapAttributeConfiguration != null) && !ldapAttributeConfiguration.ignoreDuringRead())) {
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REMOVE, null, attributeFromLdap));
}
}
}
// Prepare attributes for adding and replace
for (AttributeData attributeToPersist : attributesToPersistMap.values()) {
String attributeName = attributeToPersist.getName();
LdapAttribute ldapAttributeConfiguration = ldapAttributesConfiguration.get(attributeName);
if ((ldapAttributeConfiguration != null) && ldapAttributeConfiguration.ignoreDuringUpdate()) {
continue;
}
AttributeData attributeFromLdap = attributesFromLdapMap.get(attributeName.toLowerCase());
if (attributeFromLdap == null) {
// Add entry attribute or change schema
AttributeModificationType modType = isSchemaUpdate ? schemaModificationType : AttributeModificationType.ADD;
if (AttributeModificationType.ADD.equals(modType)) {
String[] attributeToPersistValues = attributeToPersist.getValues();
if (!(ArrayHelper.isEmpty(attributeToPersistValues) || ((attributeToPersistValues.length == 1) && StringHelper.isEmpty(attributeToPersistValues[0])))) {
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.ADD, attributeToPersist));
}
} else {
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REMOVE, null, attributeToPersist));
}
} else if ((attributeFromLdap != null) && (Arrays.equals(attributeToPersist.getValues(), new String[] {}))) {
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REMOVE, null, attributeFromLdap));
} else {
if (!attributeFromLdap.equals(attributeToPersist)) {
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REPLACE, attributeToPersist, attributeFromLdap));
}
}
}
}
}
// Update object classes if entry contains custom object classes
if (getSupportedLDAPVersion() > 2) {
if (!isSchemaUpdate) {
String[] objectClasses = getObjectClasses(entry, entryClass);
String[] objectClassesFromLdap = attributesFromLdapMap.get(OBJECT_CLASS.toLowerCase()).getValues();
if (!Arrays.equals(objectClassesFromLdap, objectClasses)) {
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REPLACE, new AttributeData(OBJECT_CLASS, objectClasses), new AttributeData(OBJECT_CLASS, objectClassesFromLdap)));
}
}
}
log.debug(String.format("LDAP attributes for merge: %s", attributeDataModifications));
merge(dnValue.toString(), attributeDataModifications);
return (T) find(entryClass, dnValue.toString(), null, propertiesAnnotations);
}
use of org.gluu.site.ldap.persistence.exception.MappingException in project oxCore by GluuFederation.
the class AbstractEntryManager method getCustomObjectClasses.
protected String[] getCustomObjectClasses(Object entry, Class<?> entryClass) {
List<String> result = new ArrayList<String>();
List<PropertyAnnotation> customObjectAnnotations = getEntryCustomObjectClassAnnotations(entryClass);
for (PropertyAnnotation propertiesAnnotation : customObjectAnnotations) {
String propertyName = propertiesAnnotation.getPropertyName();
Getter getter = getGetter(entryClass, propertyName);
if (getter == null) {
throw new MappingException("Entry should has getter for property " + propertyName);
}
AttributeData attribute = getAttribute(propertyName, propertyName, getter, entry, false);
if (attribute != null) {
for (String objectClass : attribute.getValues()) {
if (objectClass != null) {
result.add(objectClass);
}
}
}
break;
}
return result.toArray(new String[0]);
}
use of org.gluu.site.ldap.persistence.exception.MappingException in project oxCore by GluuFederation.
the class AbstractEntryManager method setPropertyValue.
private void setPropertyValue(String propertyName, Setter propertyValueSetter, Object entry, AttributeData attribute, boolean jsonObject) {
if (attribute == null) {
return;
}
log.debug(String.format("LdapProperty: %s, AttributeName: %s, AttributeValue: %s", propertyName, attribute.getName(), Arrays.toString(attribute.getValues())));
Class<?> parameterType = ReflectHelper.getSetterType(propertyValueSetter);
if (parameterType.equals(String.class)) {
propertyValueSetter.set(entry, attribute.getValue());
} else if (parameterType.equals(Boolean.class) || parameterType.equals(Boolean.TYPE)) {
propertyValueSetter.set(entry, attribute.getValue() == null ? null : Boolean.valueOf(attribute.getValue()));
} else if (parameterType.equals(Integer.class) || parameterType.equals(Integer.TYPE)) {
propertyValueSetter.set(entry, attribute.getValue() == null ? null : Integer.valueOf(attribute.getValue()));
} else if (parameterType.equals(Long.class) || parameterType.equals(Long.TYPE)) {
propertyValueSetter.set(entry, attribute.getValue() == null ? null : Long.valueOf(attribute.getValue()));
} else if (parameterType.equals(Date.class)) {
propertyValueSetter.set(entry, decodeGeneralizedTime(attribute.getValue()));
} else if (parameterType.equals(String[].class)) {
propertyValueSetter.set(entry, attribute.getValues());
} else if (ReflectHelper.assignableFrom(parameterType, List.class)) {
if (jsonObject) {
String[] stringValues = attribute.getValues();
List<Object> jsonValues = new ArrayList<Object>(stringValues.length);
for (String stringValue : stringValues) {
Object jsonValue = convertStringToJson(entry, ReflectHelper.getListType(propertyValueSetter), stringValue);
jsonValues.add(jsonValue);
}
propertyValueSetter.set(entry, jsonValues);
} else {
propertyValueSetter.set(entry, Arrays.asList(attribute.getValues()));
}
} else if (ReflectHelper.assignableFrom(parameterType, LdapEnum.class)) {
try {
propertyValueSetter.set(entry, parameterType.getMethod("resolveByValue", String.class).invoke(parameterType.getEnumConstants()[0], attribute.getValue()));
} catch (Exception ex) {
throw new MappingException("Failed to resolve Enum by value " + attribute.getValue(), ex);
}
} else if (ReflectHelper.assignableFrom(parameterType, LdapEnum[].class)) {
Class<?> itemType = parameterType.getComponentType();
Method enumResolveByValue;
try {
enumResolveByValue = itemType.getMethod("resolveByValue", String.class);
} catch (Exception ex) {
throw new MappingException("Failed to resolve Enum by value " + Arrays.toString(attribute.getValues()), ex);
}
String[] attributeValues = attribute.getValues();
LdapEnum[] ldapEnums = (LdapEnum[]) ReflectHelper.createArray(itemType, attributeValues.length);
for (int i = 0; i < attributeValues.length; i++) {
try {
ldapEnums[i] = (LdapEnum) enumResolveByValue.invoke(itemType.getEnumConstants()[0], attributeValues[i]);
} catch (Exception ex) {
throw new MappingException("Failed to resolve Enum by value " + Arrays.toString(attribute.getValues()), ex);
}
}
propertyValueSetter.set(entry, ldapEnums);
} else if (jsonObject) {
String stringValue = attribute.getValue();
Object jsonValue = convertStringToJson(entry, parameterType, stringValue);
propertyValueSetter.set(entry, jsonValue);
} else {
throw new MappingException("Entry property '" + propertyName + "' should has setter with String, Boolean, Integer, Long, Date, String[], List, LdapEnum or LdapEnum[] parameter type or has annotation LdapJsonObject");
}
}
Aggregations