Search in sources :

Example 6 with MappingException

use of org.gluu.persist.exception.mapping.MappingException in project oxCore by GluuFederation.

the class BaseEntryManager method getAttributeFromLdapAttribute.

private AttributeData getAttributeFromLdapAttribute(Object entry, Annotation ldapAttribute, PropertyAnnotation propertiesAnnotation, String propertyName) {
    Class<?> entryClass = entry.getClass();
    String ldapAttributeName = ((LdapAttribute) ldapAttribute).name();
    if (StringHelper.isEmpty(ldapAttributeName)) {
        ldapAttributeName = propertyName;
    }
    Getter getter = getGetter(entryClass, propertyName);
    if (getter == null) {
        throw new MappingException("Entry should has getter for property " + propertyName);
    }
    Annotation ldapJsonObject = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), LdapJsonObject.class);
    boolean jsonObject = ldapJsonObject != null;
    AttributeData attribute = getAttribute(propertyName, ldapAttributeName, getter, entry, jsonObject);
    return attribute;
}
Also used : Getter(org.gluu.persist.reflect.property.Getter) LdapAttribute(org.gluu.site.ldap.persistence.annotation.LdapAttribute) PropertyAnnotation(org.gluu.persist.model.PropertyAnnotation) Annotation(java.lang.annotation.Annotation) AttributeData(org.gluu.persist.model.AttributeData) MappingException(org.gluu.persist.exception.mapping.MappingException)

Example 7 with MappingException

use of org.gluu.persist.exception.mapping.MappingException in project oxCore by GluuFederation.

the class BaseEntryManager 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(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(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");
    }
}
Also used : ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) MappingException(org.gluu.persist.exception.mapping.MappingException) InvalidArgumentException(org.gluu.persist.exception.mapping.InvalidArgumentException) EntryPersistenceException(org.gluu.persist.exception.mapping.EntryPersistenceException) MappingException(org.gluu.persist.exception.mapping.MappingException) LdapEnum(org.gluu.site.ldap.persistence.annotation.LdapEnum) LdapJsonObject(org.gluu.site.ldap.persistence.annotation.LdapJsonObject)

Example 8 with MappingException

use of org.gluu.persist.exception.mapping.MappingException in project oxCore by GluuFederation.

the class BaseEntryManager method checkEntryClass.

protected boolean checkEntryClass(Class<?> entryClass, boolean isAllowSchemaEntry) {
    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 ldapSchemaEntry = ReflectHelper.getAnnotationByType(entryAnnotations, LdapSchemaEntry.class);
    Annotation ldapEntry = ReflectHelper.getAnnotationByType(entryAnnotations, LdapEntry.class);
    if (isAllowSchemaEntry) {
        if ((ldapSchemaEntry == null) && (ldapEntry == null)) {
            throw new MappingException("Entry should has LdapEntry or LdapSchemaEntry annotation");
        }
    } else {
        if (ldapEntry == null) {
            throw new MappingException("Entry should has LdapEntry annotation");
        }
    }
    return true;
}
Also used : PropertyAnnotation(org.gluu.persist.model.PropertyAnnotation) Annotation(java.lang.annotation.Annotation) MappingException(org.gluu.persist.exception.mapping.MappingException)

Example 9 with MappingException

use of org.gluu.persist.exception.mapping.MappingException in project oxCore by GluuFederation.

the class BaseEntryManager 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();
}
Also used : LdapEntry(org.gluu.site.ldap.persistence.annotation.LdapEntry) PropertyAnnotation(org.gluu.persist.model.PropertyAnnotation) Annotation(java.lang.annotation.Annotation) MappingException(org.gluu.persist.exception.mapping.MappingException)

Example 10 with MappingException

use of org.gluu.persist.exception.mapping.MappingException in project oxCore by GluuFederation.

the class BaseEntryManager method getListItem.

private Object getListItem(String propertyName, Setter propertyNameSetter, Setter propertyValueSetter, Class<?> classType, AttributeData attribute) {
    if (attribute == null) {
        return null;
    }
    Object result;
    try {
        result = ReflectHelper.createObjectByDefaultConstructor(classType);
    } catch (Exception ex) {
        throw new MappingException(String.format("Entry %s should has default constructor", classType));
    }
    propertyNameSetter.set(result, attribute.getName());
    setPropertyValue(propertyName, propertyValueSetter, result, attribute, false);
    return result;
}
Also used : LdapJsonObject(org.gluu.site.ldap.persistence.annotation.LdapJsonObject) MappingException(org.gluu.persist.exception.mapping.MappingException) InvalidArgumentException(org.gluu.persist.exception.mapping.InvalidArgumentException) EntryPersistenceException(org.gluu.persist.exception.mapping.EntryPersistenceException) MappingException(org.gluu.persist.exception.mapping.MappingException)

Aggregations

MappingException (org.gluu.persist.exception.mapping.MappingException)23 PropertyAnnotation (org.gluu.persist.model.PropertyAnnotation)15 AttributeData (org.gluu.persist.model.AttributeData)12 LdapJsonObject (org.gluu.site.ldap.persistence.annotation.LdapJsonObject)12 EntryPersistenceException (org.gluu.persist.exception.mapping.EntryPersistenceException)9 ArrayList (java.util.ArrayList)8 Filter (org.gluu.search.filter.Filter)7 SearchException (org.gluu.persist.exception.operation.SearchException)5 SearchResult (com.unboundid.ldap.sdk.SearchResult)4 Annotation (java.lang.annotation.Annotation)4 ParseException (java.text.ParseException)4 List (java.util.List)4 InvalidArgumentException (org.gluu.persist.exception.mapping.InvalidArgumentException)4 AuthenticationException (org.gluu.persist.exception.operation.AuthenticationException)4 ConnectionException (org.gluu.persist.exception.operation.ConnectionException)4 SearchScopeException (org.gluu.persist.exception.operation.SearchScopeException)4 Getter (org.gluu.persist.reflect.property.Getter)4 LdapAttributesList (org.gluu.site.ldap.persistence.annotation.LdapAttributesList)4 SearchResultEntry (com.unboundid.ldap.sdk.SearchResultEntry)3 HashMap (java.util.HashMap)2