Search in sources :

Example 1 with LdapJsonObject

use of org.gluu.site.ldap.persistence.annotation.LdapJsonObject 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");
    }
}
Also used : Method(java.lang.reflect.Method) MappingException(org.gluu.site.ldap.persistence.exception.MappingException) EntryPersistenceException(org.gluu.site.ldap.persistence.exception.EntryPersistenceException) MappingException(org.gluu.site.ldap.persistence.exception.MappingException) LdapEnum(org.gluu.site.ldap.persistence.annotation.LdapEnum) LdapJsonObject(org.gluu.site.ldap.persistence.annotation.LdapJsonObject)

Example 2 with LdapJsonObject

use of org.gluu.site.ldap.persistence.annotation.LdapJsonObject 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);
}
Also used : MappingException(org.gluu.site.ldap.persistence.exception.MappingException) LdapEnum(org.gluu.site.ldap.persistence.annotation.LdapEnum) LdapJsonObject(org.gluu.site.ldap.persistence.annotation.LdapJsonObject) LdapAttributesList(org.gluu.site.ldap.persistence.annotation.LdapAttributesList)

Example 3 with LdapJsonObject

use of org.gluu.site.ldap.persistence.annotation.LdapJsonObject in project oxCore by GluuFederation.

the class AbstractEntryManager method createEntities.

protected <T> List<T> createEntities(Class<T> entryClass, List<PropertyAnnotation> propertiesAnnotations, Map<String, List<AttributeData>> entriesAttributes, boolean doSort) {
    // Check if entry has DN property
    String dnProperty = getDNPropertyName(entryClass);
    // Get DN value
    Setter dnSetter = getSetter(entryClass, dnProperty);
    if (dnSetter == null) {
        throw new MappingException("Entry should has getter for property " + dnProperty);
    }
    // Type object classes
    String[] typeObjectClasses = getTypeObjectClasses(entryClass);
    Arrays.sort(typeObjectClasses);
    List<T> results = new ArrayList<T>(entriesAttributes.size());
    for (Entry<String, List<AttributeData>> entryAttributes : entriesAttributes.entrySet()) {
        String dn = entryAttributes.getKey();
        List<AttributeData> attributes = entryAttributes.getValue();
        Map<String, AttributeData> attributesMap = getAttributesMap(attributes);
        T entry;
        List<String> customObjectClasses = null;
        try {
            entry = ReflectHelper.createObjectByDefaultConstructor(entryClass);
        } catch (Exception ex) {
            throw new MappingException(String.format("Entry %s should has default constructor", entryClass));
        }
        results.add(entry);
        dnSetter.set(entry, dn);
        // Process properties with LdapAttribute annotation
        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 attributeData = attributesMap.get(ldapAttributeName);
                // Remove processed attributes
                attributesMap.remove(ldapAttributeName);
                if (((LdapAttribute) ldapAttribute).ignoreDuringRead()) {
                    continue;
                }
                Setter setter = getSetter(entryClass, propertyName);
                if (setter == null) {
                    throw new MappingException("Entry should has setter for property " + propertyName);
                }
                Annotation ldapJsonObject = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), LdapJsonObject.class);
                boolean jsonObject = ldapJsonObject != null;
                setPropertyValue(propertyName, setter, entry, attributeData, jsonObject);
            }
        }
        // Process properties with LdapAttributesList annotation
        for (PropertyAnnotation propertiesAnnotation : propertiesAnnotations) {
            String propertyName = propertiesAnnotation.getPropertyName();
            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);
                }
                Setter setter = getSetter(entryClass, propertyName);
                if (setter == null) {
                    throw new MappingException("Entry should has setter for property " + propertyName);
                }
                List<Object> propertyValue = new ArrayList<Object>();
                setter.set(entry, propertyValue);
                Class<?> entryItemType = ReflectHelper.getListType(setter);
                if (entryItemType == null) {
                    throw new MappingException("Entry property " + propertyName + " should has setter with specified element type");
                }
                String entryPropertyName = ((LdapAttributesList) ldapAttribute).name();
                Setter entryPropertyNameSetter = getSetter(entryItemType, entryPropertyName);
                if (entryPropertyNameSetter == null) {
                    throw new MappingException("Entry should has setter for property " + propertyName + "." + entryPropertyName);
                }
                String entryPropertyValue = ((LdapAttributesList) ldapAttribute).value();
                Setter entryPropertyValueSetter = getSetter(entryItemType, entryPropertyValue);
                if (entryPropertyValueSetter == null) {
                    throw new MappingException("Entry should has getter for property " + propertyName + "." + entryPropertyValue);
                }
                for (AttributeData entryAttribute : attributesMap.values()) {
                    if (OBJECT_CLASS.equalsIgnoreCase(entryAttribute.getName())) {
                        String[] objectClasses = entryAttribute.getValues();
                        if (ArrayHelper.isEmpty(objectClasses)) {
                            continue;
                        }
                        if (customObjectClasses == null) {
                            customObjectClasses = new ArrayList<String>();
                        }
                        for (String objectClass : objectClasses) {
                            int idx = Arrays.binarySearch(typeObjectClasses, objectClass, new Comparator<String>() {

                                @Override
                                public int compare(String o1, String o2) {
                                    return o1.toLowerCase().compareTo(o2.toLowerCase());
                                }
                            });
                            if (idx < 0) {
                                customObjectClasses.add(objectClass);
                            }
                        }
                        continue;
                    }
                    LdapAttribute ldapAttributeConfiguration = ldapAttributesConfiguration.get(entryAttribute.getName());
                    if ((ldapAttributeConfiguration != null) && ldapAttributeConfiguration.ignoreDuringRead()) {
                        continue;
                    }
                    Object listItem = getListItem(propertyName, entryPropertyNameSetter, entryPropertyValueSetter, entryItemType, entryAttribute);
                    if (listItem != null) {
                        propertyValue.add(listItem);
                    }
                }
                if (doSort) {
                    sortLdapAttributesListIfNeeded((LdapAttributesList) ldapAttribute, entryItemType, propertyValue);
                }
            }
        }
        if ((customObjectClasses != null) && (customObjectClasses.size() > 0)) {
            setCustomObjectClasses(entry, entryClass, customObjectClasses.toArray(new String[0]));
        }
    }
    return results;
}
Also used : MappingException(org.gluu.site.ldap.persistence.exception.MappingException) LdapAttribute(org.gluu.site.ldap.persistence.annotation.LdapAttribute) LdapAttributesList(org.gluu.site.ldap.persistence.annotation.LdapAttributesList) MappingException(org.gluu.site.ldap.persistence.exception.MappingException) EntryPersistenceException(org.gluu.site.ldap.persistence.exception.EntryPersistenceException) Annotation(java.lang.annotation.Annotation) LdapAttributesList(org.gluu.site.ldap.persistence.annotation.LdapAttributesList) Setter(org.gluu.site.ldap.persistence.property.Setter) LdapJsonObject(org.gluu.site.ldap.persistence.annotation.LdapJsonObject)

Aggregations

LdapJsonObject (org.gluu.site.ldap.persistence.annotation.LdapJsonObject)3 MappingException (org.gluu.site.ldap.persistence.exception.MappingException)3 LdapAttributesList (org.gluu.site.ldap.persistence.annotation.LdapAttributesList)2 LdapEnum (org.gluu.site.ldap.persistence.annotation.LdapEnum)2 EntryPersistenceException (org.gluu.site.ldap.persistence.exception.EntryPersistenceException)2 Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 LdapAttribute (org.gluu.site.ldap.persistence.annotation.LdapAttribute)1 Setter (org.gluu.site.ldap.persistence.property.Setter)1