Search in sources :

Example 41 with AttributeType

use of org.apache.directory.api.ldap.model.schema.AttributeType in project directory-ldap-api by apache.

the class DefaultEntry method readExternal.

/**
 * {@inheritDoc}
 */
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
    // Read the Dn
    dn = new Dn(schemaManager);
    dn.readExternal(in);
    // Read the number of attributes
    int nbAttributes = in.readInt();
    // Read the attributes
    for (int i = 0; i < nbAttributes; i++) {
        // Read each attribute
        Attribute attribute = new DefaultAttribute();
        attribute.readExternal(in);
        if (schemaManager != null) {
            try {
                AttributeType attributeType = schemaManager.lookupAttributeTypeRegistry(attribute.getId());
                attribute.apply(attributeType);
                attributes.put(attributeType.getOid(), attribute);
            } catch (LdapException le) {
                String message = le.getLocalizedMessage();
                LOG.error(message);
                throw new IOException(message, le);
            }
        } else {
            attributes.put(attribute.getId(), attribute);
        }
    }
}
Also used : AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) Dn(org.apache.directory.api.ldap.model.name.Dn) IOException(java.io.IOException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 42 with AttributeType

use of org.apache.directory.api.ldap.model.schema.AttributeType in project directory-ldap-api by apache.

the class DefaultEntry method remove.

/**
 * <p>
 * Removes the specified String values from an attribute.
 * </p>
 * <p>
 * If at least one value is removed, this method returns <code>true</code>.
 * </p>
 * <p>
 * If there is no more value after having removed the values, the attribute
 * will be removed too.
 * </p>
 * <p>
 * If the attribute does not exist, nothing is done and the method returns
 * <code>false</code>
 * </p>
 *
 * @param upId The attribute ID
 * @param values the attributes to be removed
 * @return <code>true</code> if at least a value is removed, <code>false</code>
 * if not all the values have been removed or if the attribute does not exist.
 */
@Override
public boolean remove(String upId, String... values) throws LdapException {
    if (Strings.isEmpty(upId)) {
        String message = I18n.err(I18n.ERR_13204_NULL_ATTRIBUTE_ID);
        LOG.info(message);
        return false;
    }
    if (schemaManager == null) {
        String id = getId(upId);
        Attribute attribute = get(id);
        if (attribute == null) {
            // Can't remove values from a not existing attribute !
            return false;
        }
        int nbOldValues = attribute.size();
        // Remove the values
        attribute.remove(values);
        if (attribute.size() == 0) {
            // No mare values, remove the attribute
            attributes.remove(id);
            return true;
        }
        return nbOldValues != attribute.size();
    } else {
        try {
            AttributeType attributeType = getAttributeType(upId);
            return remove(attributeType, values);
        } catch (LdapException ne) {
            LOG.error(I18n.err(I18n.ERR_13205_CANNOT_REMOVE_VAL_MISSING_ATTR, upId));
            return false;
        } catch (IllegalArgumentException iae) {
            LOG.error(I18n.err(I18n.ERR_13206_CANNOT_REMOVE_VAL_BAD_ATTR, upId));
            return false;
        }
    }
}
Also used : AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 43 with AttributeType

use of org.apache.directory.api.ldap.model.schema.AttributeType in project directory-ldap-api by apache.

the class DefaultEntry method remove.

/**
 * <p>
 * Removes the specified binary values from an attribute.
 * </p>
 * <p>
 * If at least one value is removed, this method returns <code>true</code>.
 * </p>
 * <p>
 * If there is no more value after having removed the values, the attribute
 * will be removed too.
 * </p>
 * <p>
 * If the attribute does not exist, nothing is done and the method returns
 * <code>false</code>
 * </p>
 *
 * @param upId The attribute ID
 * @param values the values to be removed
 * @return <code>true</code> if at least a value is removed, <code>false</code>
 * if not all the values have been removed or if the attribute does not exist.
 */
public boolean remove(String upId, byte[]... values) throws LdapException {
    if (Strings.isEmpty(upId)) {
        String message = I18n.err(I18n.ERR_13204_NULL_ATTRIBUTE_ID);
        LOG.info(message);
        return false;
    }
    if (schemaManager == null) {
        String id = getId(upId);
        Attribute attribute = get(id);
        if (attribute == null) {
            // Can't remove values from a not existing attribute !
            return false;
        }
        int nbOldValues = attribute.size();
        // Remove the values
        attribute.remove(values);
        if (attribute.size() == 0) {
            // No mare values, remove the attribute
            attributes.remove(id);
            return true;
        }
        return nbOldValues != attribute.size();
    } else {
        try {
            AttributeType attributeType = getAttributeType(upId);
            return remove(attributeType, values);
        } catch (LdapException ne) {
            LOG.error(I18n.err(I18n.ERR_13205_CANNOT_REMOVE_VAL_MISSING_ATTR, upId));
            return false;
        } catch (IllegalArgumentException iae) {
            LOG.error(I18n.err(I18n.ERR_13206_CANNOT_REMOVE_VAL_BAD_ATTR, upId));
            return false;
        }
    }
}
Also used : AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 44 with AttributeType

use of org.apache.directory.api.ldap.model.schema.AttributeType in project directory-ldap-api by apache.

the class DefaultEntry method toString.

/**
 * {@inheritDoc}
 */
@Override
public String toString(String tabs) {
    StringBuilder sb = new StringBuilder();
    sb.append(tabs).append("Entry\n");
    sb.append(tabs).append("    dn");
    if (dn.isSchemaAware()) {
        sb.append("[n]");
    }
    sb.append(": ");
    sb.append(dn.getName());
    sb.append('\n');
    // First dump the ObjectClass attribute
    if (schemaManager != null) {
        // First dump the ObjectClass attribute
        if (containsAttribute(objectClassAttributeType.getOid())) {
            Attribute objectClass = get(objectClassAttributeType);
            sb.append(objectClass.toString(tabs + "    "));
        }
    } else {
        if (containsAttribute("objectClass")) {
            Attribute objectClass = get("objectclass");
            sb.append(objectClass.toString(tabs + "    "));
        }
    }
    sb.append('\n');
    if (attributes.size() != 0) {
        for (Attribute attribute : attributes.values()) {
            String id = attribute.getId();
            if (schemaManager != null) {
                AttributeType attributeType = schemaManager.getAttributeType(id);
                if (attributeType == null) {
                    sb.append(tabs).append("id: ").append(id);
                } else if (!attributeType.equals(objectClassAttributeType)) {
                    sb.append(attribute.toString(tabs + "    "));
                    sb.append('\n');
                    continue;
                }
            } else {
                if (!id.equalsIgnoreCase(SchemaConstants.OBJECT_CLASS_AT) && !id.equals(SchemaConstants.OBJECT_CLASS_AT_OID)) {
                    sb.append(attribute.toString(tabs + "    "));
                    sb.append('\n');
                    continue;
                }
            }
        }
    }
    return sb.toString();
}
Also used : AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType)

Example 45 with AttributeType

use of org.apache.directory.api.ldap.model.schema.AttributeType in project directory-ldap-api by apache.

the class LdifReader method parseAttributeValue.

/**
 * Parse an AttributeType/AttributeValue
 *
 * @param entry The entry where to store the value
 * @param line The line to parse
 * @param lowerLine The same line, lowercased
 * @throws LdapException If anything goes wrong
 */
public void parseAttributeValue(LdifEntry entry, String line, String lowerLine) throws LdapException {
    int colonIndex = line.indexOf(':');
    String attributeType = lowerLine.substring(0, colonIndex);
    // We should *not* have a Dn twice
    if ("dn".equals(attributeType)) {
        LOG.error(I18n.err(I18n.ERR_13400_ENTRY_WITH_TWO_DNS, lineNumber));
        throw new LdapLdifException(I18n.err(I18n.ERR_13439_LDIF_ENTRY_WITH_TWO_DNS));
    }
    Object attributeValue = parseValue(attributeType, line, colonIndex);
    if (schemaManager != null) {
        AttributeType at = schemaManager.getAttributeType(attributeType);
        if (at != null) {
            if (at.getSyntax().isHumanReadable()) {
                if (attributeValue == null) {
                    attributeValue = "";
                } else if (attributeValue instanceof byte[]) {
                    attributeValue = Strings.utf8ToString((byte[]) attributeValue);
                }
            } else {
                if (attributeValue instanceof String) {
                    attributeValue = Strings.getBytesUtf8((String) attributeValue);
                }
            }
        }
    }
    // Update the entry
    try {
        entry.addAttribute(attributeType, attributeValue);
    } catch (Exception e) {
        // The attribute does not exist already, create a fake one
        if ((schemaManager != null) && schemaManager.isRelaxed()) {
            MutableAttributeType newAttributeType = new MutableAttributeType("1.3.6.1.4.1.18060.0.9999." + oidCounter++);
            newAttributeType.setNames(attributeType);
            newAttributeType.setSyntax(schemaManager.getLdapSyntaxRegistry().get(SchemaConstants.DIRECTORY_STRING_SYNTAX));
            schemaManager.add(newAttributeType);
            entry.addAttribute(attributeType, attributeValue);
        }
    }
}
Also used : AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) MutableAttributeType(org.apache.directory.api.ldap.model.schema.MutableAttributeType) MutableAttributeType(org.apache.directory.api.ldap.model.schema.MutableAttributeType) LdapInvalidAttributeValueException(org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException) NoSuchElementException(java.util.NoSuchElementException) NotImplementedException(org.apache.directory.api.util.exception.NotImplementedException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) LdapInvalidDnException(org.apache.directory.api.ldap.model.exception.LdapInvalidDnException) FileNotFoundException(java.io.FileNotFoundException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Aggregations

AttributeType (org.apache.directory.api.ldap.model.schema.AttributeType)135 Test (org.junit.Test)68 MutableAttributeType (org.apache.directory.api.ldap.model.schema.MutableAttributeType)42 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)25 ParseException (java.text.ParseException)15 DefaultAttribute (org.apache.directory.api.ldap.model.entry.DefaultAttribute)15 Value (org.apache.directory.api.ldap.model.entry.Value)15 Attribute (org.apache.directory.api.ldap.model.entry.Attribute)11 LdapInvalidAttributeValueException (org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException)11 HashSet (java.util.HashSet)10 LdapSchemaException (org.apache.directory.api.ldap.model.exception.LdapSchemaException)10 ObjectClass (org.apache.directory.api.ldap.model.schema.ObjectClass)10 MatchingRule (org.apache.directory.api.ldap.model.schema.MatchingRule)9 IOException (java.io.IOException)7 LdapInvalidDnException (org.apache.directory.api.ldap.model.exception.LdapInvalidDnException)7 Before (org.junit.Before)7 SchemaManager (org.apache.directory.api.ldap.model.schema.SchemaManager)6 DefaultSchemaManager (org.apache.directory.api.ldap.schema.manager.impl.DefaultSchemaManager)6 ArrayList (java.util.ArrayList)5 Dn (org.apache.directory.api.ldap.model.name.Dn)4