org.springframework.ldap.odm.annotations.DnAttribute Java Examples

The following examples show how to use org.springframework.ldap.odm.annotations.DnAttribute. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AttributeMetaData.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
public AttributeMetaData(Field field) {
    this.field=field;

    this.dnAttribute = field.getAnnotation(DnAttribute.class);
    if(this.dnAttribute != null && !field.getType().equals(String.class)) {
        throw new MetaDataException(String.format("%s is of type %s, but only String attributes can be declared as @DnAttributes",
                field.toString(),
                field.getType().toString()));
    }

    Transient transientAnnotation = field.getAnnotation(Transient.class);
    if(transientAnnotation != null) {
        this.isTransient = true;
        return;
    }

    // Reflection data
    determineFieldType(field);


    // Data from the @Attribute annotation
    boolean foundAttributeAnnotation=processAttributeAnnotation(field);

    // Data from the @Id annotation
    boolean foundIdAnnoation=processIdAnnotation(field, valueClass);

    // Check that the field has not been annotated with both @Attribute and with @Id
    if (foundAttributeAnnotation && foundIdAnnoation) {
        throw new MetaDataException(
                String.format("You may not specifiy an %1$s annoation and an %2$s annotation on the same field, error in field %3$s in Entry class %4$s",
                        Id.class, Attribute.class, field.getName(), field.getDeclaringClass()));
    }
    
    // If this is the objectclass attribute then it must be of type List<String>
    if (isObjectClass() && (!isCollection() || valueClass!=String.class)) {
        throw new MetaDataException(String.format("The type of the objectclass attribute must be List<String> in classs %1$s",
                field.getDeclaringClass()));
    }
}
 
Example #2
Source File: DefaultObjectDirectoryMapper.java    From spring-ldap with Apache License 2.0 4 votes vote down vote up
@Override
public <T> T mapFromLdapDataEntry(LdapDataEntry context, Class<T> clazz) {
    if (LOG.isDebugEnabled()) {
        LOG.debug(String.format("Converting to Java Entry class %1$s from %2$s", clazz, context));
    }

    // The Java representation of the LDAP entry
    T result;

    ObjectMetaData metaData=getEntityData(clazz).metaData;

    try {
        // The result class must have a zero argument constructor
        result = clazz.newInstance();

        // Build a map of JNDI attribute names to values
        Map<CaseIgnoreString, Attribute> attributeValueMap = new HashMap<CaseIgnoreString, Attribute>();
        // Get a NamingEnumeration to loop through the JNDI attributes in the entry
        Attributes attributes = context.getAttributes();
        NamingEnumeration<? extends Attribute> attributesEnumeration = attributes.getAll();
        // Loop through all of the JNDI attributes
        while (attributesEnumeration.hasMoreElements()) {
            Attribute currentAttribute = attributesEnumeration.nextElement();
            // Add the current attribute to the map keyed on the lowercased (case indep) id of the attribute
            attributeValueMap.put(new CaseIgnoreString(currentAttribute.getID()), currentAttribute);
        }


        // If this is the objectclass attribute then check that values correspond to the metadata we have
        // for the Java representation
        Attribute ocAttribute = attributeValueMap.get(OBJECT_CLASS_ATTRIBUTE_CI);
        if (ocAttribute != null) {
            // Get all object class values from the JNDI attribute
            Set<CaseIgnoreString> objectClassesFromJndi = new HashSet<CaseIgnoreString>();
            NamingEnumeration<?> objectClassesFromJndiEnum = ocAttribute.getAll();
            while (objectClassesFromJndiEnum.hasMoreElements()) {
                objectClassesFromJndi.add(new CaseIgnoreString((String)objectClassesFromJndiEnum.nextElement()));
            }
            // OK - checks its the same as the meta-data we have
            if(!collectionContainsAll(objectClassesFromJndi, metaData.getObjectClasses())) {
                return null;
            }
        } else {
            throw new InvalidEntryException(String.format("No object classes were returned for class %1$s",
                    clazz.getName()));
        }

        // Now loop through all the fields in the Java representation populating it with values from the
        // attributeValueMap
        for (Field field : metaData) {
            // Get the current field
            AttributeMetaData attributeInfo = metaData.getAttribute(field);
            // We deal with the Id field specially
            Name dn = context.getDn();
            if (!attributeInfo.isTransient() && !attributeInfo.isId()) {
                // Not the ID - but is is multi valued?
                if (!attributeInfo.isCollection()) {
                    // No - its single valued, grab the JNDI attribute that corresponds to the metadata on the
                    // current field
                    populateSingleValueField(result, attributeValueMap, field, attributeInfo);
                } else {
                    // We are dealing with a multi valued attribute
                    populateMultiValueField(result, attributeValueMap, field, attributeInfo);
                }
            } else if(attributeInfo.isId()) { // The id field
                field.set(result, converterManager.convert(dn, attributeInfo.getSyntax(),
                        attributeInfo.getValueClass()));
            }

            DnAttribute dnAttribute = attributeInfo.getDnAttribute();
            if(dnAttribute != null) {
                String dnValue;
                int index = dnAttribute.index();

                if(index != -1) {
                    dnValue = LdapUtils.getStringValue(dn, index);
                } else {
                    dnValue = LdapUtils.getStringValue(dn, dnAttribute.value());
                }
                field.set(result, dnValue);
            }
        }
    } catch (NamingException ne) {
        throw new InvalidEntryException(String.format("Problem creating %1$s from LDAP Entry %2$s",
                clazz, context), ne);
    } catch (IllegalAccessException iae) {
        throw new InvalidEntryException(String.format(
                "Could not create an instance of %1$s could not access field", clazz.getName()), iae);
    } catch (InstantiationException ie) {
        throw new InvalidEntryException(String.format("Could not instantiate %1$s", clazz), ie);
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug(String.format("Converted object - %1$s", result));
    }

    return result;
}
 
Example #3
Source File: AttributeMetaData.java    From spring-ldap with Apache License 2.0 4 votes vote down vote up
public DnAttribute getDnAttribute() {
    return dnAttribute;
}