Example usage for javax.naming NamingEnumeration toString

List of usage examples for javax.naming NamingEnumeration toString

Introduction

In this page you can find the example usage for javax.naming NamingEnumeration toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:org.nuxeo.ecm.directory.ldap.LDAPSession.java

@SuppressWarnings("unchecked")
protected Object getFieldValue(Attribute attribute, String fieldName, String entryId, boolean fetchReferences)
        throws DirectoryException {

    Field field = schemaFieldMap.get(fieldName);
    Type type = field.getType();//w  w  w. j a  v a2 s  .c  om
    Object defaultValue = field.getDefaultValue();
    String typeName = type.getName();
    if (attribute == null) {
        return defaultValue;
    }
    Object value;
    try {
        value = attribute.get();
    } catch (NamingException e) {
        throw new DirectoryException("Could not fetch value for " + attribute, e);
    }
    if (value == null) {
        return defaultValue;
    }
    String trimmedValue = value.toString().trim();
    if ("string".equals(typeName)) {
        return trimmedValue;
    } else if ("integer".equals(typeName) || "long".equals(typeName)) {
        if ("".equals(trimmedValue)) {
            return defaultValue;
        }
        try {
            return Long.valueOf(trimmedValue);
        } catch (NumberFormatException e) {
            log.error(String.format(
                    "field %s of type %s has non-numeric value found on server: '%s' (ignoring and using default value instead)",
                    fieldName, typeName, trimmedValue));
            return defaultValue;
        }
    } else if (type.isListType()) {
        List<String> parsedItems = new LinkedList<String>();
        NamingEnumeration<Object> values = null;
        try {
            values = (NamingEnumeration<Object>) attribute.getAll();
            while (values.hasMore()) {
                parsedItems.add(values.next().toString().trim());
            }
            return parsedItems;
        } catch (NamingException e) {
            log.error(String.format(
                    "field %s of type %s has non list value found on server: '%s' (ignoring and using default value instead)",
                    fieldName, typeName, values != null ? values.toString() : trimmedValue));
            return defaultValue;
        } finally {
            if (values != null) {
                try {
                    values.close();
                } catch (NamingException e) {
                    log.error(e, e);
                }
            }
        }
    } else if ("date".equals(typeName)) {
        if ("".equals(trimmedValue)) {
            return defaultValue;
        }
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss'Z'");
            dateFormat.setTimeZone(new SimpleTimeZone(0, "Z"));
            Date date = dateFormat.parse(trimmedValue);
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            return cal;
        } catch (ParseException e) {
            log.error(String.format(
                    "field %s of type %s has invalid value found on server: '%s' (ignoring and using default value instead)",
                    fieldName, typeName, trimmedValue));
            return defaultValue;
        }
    } else if ("content".equals(typeName)) {
        return Blobs.createBlob((byte[]) value);
    } else {
        throw new DirectoryException("Field type not supported in directories: " + typeName);
    }
}