1 Reply Latest reply: Mar 13, 2013 4:18 PM by EJP RSS

    LDAP Attribute value types

    823810
      There is a class javax.naming.Attribute

      And i can put some values in it with javax.naming.Attribute.add(Object object);

      I want to ask

      Might these objects be only byte[] or String?

      If so it will be OK for me because then the choices standardized into two types(String and byte[]) in somewhere that i have to control the object types.


      I have come to this point from the code of Spring LDAP library's maptocontext method


      private void mapToContext(Object entry, DirContextOperations context) {
      ObjectMetaData metaData=getEntityData(entry.getClass()).metaData;

      // Object classes are set from the metadata obtained from the @Entity annotation
      int numOcs=metaData.getObjectClasses().size();
      CaseIgnoreString[] metaDataObjectClasses=metaData.getObjectClasses().toArray(new CaseIgnoreString[numOcs]);

      String[] stringOcs=new String[numOcs];
      for (int ocIndex=0; ocIndex<numOcs; ocIndex++) {
      stringOcs[ocIndex]=metaDataObjectClasses[ocIndex].toString();
      }
      context.setAttributeValues(OBJECT_CLASS_ATTRIBUTE, stringOcs);

      // Loop through each of the fields in the object to write to LDAP
      for (Field field : metaData) {
      // Grab the meta data for the current field
      AttributeMetaData attributeInfo = metaData.getAttribute(field);
      // We dealt with the object class field about, and the DN is set by the call to write the object to LDAP
      if (!attributeInfo.isId() && !(attributeInfo.isObjectClass())) {
      try {
      // If this is a "binary" object the JNDI expects a byte[] otherwise a String
      Class<?> targetClass = (attributeInfo.isBinary()) ? byte[].class : String.class;
      // Multi valued?
      if (!attributeInfo.isList()) {
      // Single valued - get the value of the field
      Object fieldValue = field.get(entry);
      // Ignore null field values
      if (fieldValue != null) {
      // Convert the field value to the required type and write it into the JNDI context
      context.setAttributeValue(attributeInfo.getName().toString(), converterManager.convert(fieldValue,
      attributeInfo.getSyntax(), targetClass));
      }
      } else { // Multi-valued
      // We need to build up a list of of the values
      List<String> attributeValues = new ArrayList<String>();
      // Get the list of values
      Collection<?> fieldValues = (Collection<?>)field.get(entry);
      // Ignore null lists
      if (fieldValues != null) {
      for (final Object o : fieldValues) {
      // Ignore null values
      if (o != null) {
      attributeValues.add((String)converterManager.convert(o, attributeInfo.getSyntax(),
      targetClass));
      }
      }
      context.setAttributeValues(attributeInfo.getName().toString(), attributeValues.toArray());
      }
      }
      } catch (IllegalAccessException e) {
      throw new InvalidEntryException(String.format("Can't set attribute %1$s", attributeInfo.getName()),
      e);
      }
      }
      }
      }