Search in sources :

Example 71 with DefaultAttribute

use of org.apache.directory.api.ldap.model.entry.DefaultAttribute in project directory-ldap-api by apache.

the class AttributesFactory method convert.

/**
 * Converts a Schema to an Entry
 *
 * @param schema The Schema to convert
 * @param schemaManager The SchemaManager
 * @return An Entry containing the converted Schema
 * @throws LdapException If the conversion failed
 */
public Entry convert(Schema schema, SchemaManager schemaManager) throws LdapException {
    Entry entry = new DefaultEntry(schemaManager);
    entry.put(SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, MetaSchemaConstants.META_SCHEMA_OC);
    entry.put(SchemaConstants.CN_AT, schema.getSchemaName());
    entry.put(SchemaConstants.CREATORS_NAME_AT, schema.getOwner());
    entry.put(SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime());
    if (schema.isDisabled()) {
        entry.put(MetaSchemaConstants.M_DISABLED_AT, "TRUE");
    }
    String[] dependencies = schema.getDependencies();
    if (dependencies != null && dependencies.length > 0) {
        Attribute attr = new DefaultAttribute(schemaManager.getAttributeType(MetaSchemaConstants.M_DEPENDENCIES_AT));
        for (String dependency : dependencies) {
            attr.add(dependency);
        }
        entry.put(attr);
    }
    return entry;
}
Also used : DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute)

Example 72 with DefaultAttribute

use of org.apache.directory.api.ldap.model.entry.DefaultAttribute in project directory-ldap-api by apache.

the class LdifEntry method addModification.

/**
 * Add a modification with no value
 *
 * @param modOp The modification operation value. One of :
 * <ul>
 * <li>ModificationOperation.ADD_ATTRIBUTE</li>
 * <li>ModificationOperation.REMOVE_ATTRIBUTE</li>
 * <li>ModificationOperation.REPLACE_ATTRIBUTE</li>
 * </ul>
 *
 * @param id The attribute's ID
 */
public void addModification(ModificationOperation modOp, String id) {
    if (changeType == ChangeType.Modify) {
        Attribute attr = new DefaultAttribute(id);
        Modification item = new DefaultModification(modOp, attr);
        modificationList.add(item);
        modifications.put(id, item);
    }
}
Also used : DefaultModification(org.apache.directory.api.ldap.model.entry.DefaultModification) Modification(org.apache.directory.api.ldap.model.entry.Modification) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) DefaultModification(org.apache.directory.api.ldap.model.entry.DefaultModification) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute)

Example 73 with DefaultAttribute

use of org.apache.directory.api.ldap.model.entry.DefaultAttribute in project directory-ldap-api by apache.

the class LdifEntry method addModification.

/**
 * Add a modification
 *
 * @param modOp The modification operation value. One of :
 * <ul>
 * <li>ModificationOperation.ADD_ATTRIBUTE</li>
 * <li>ModificationOperation.REMOVE_ATTRIBUTE</li>
 * <li>ModificationOperation.REPLACE_ATTRIBUTE</li>
 * </ul>
 *
 * @param id The attribute's ID
 * @param value The attribute's value
 */
public void addModification(ModificationOperation modOp, String id, Object value) {
    if (changeType == ChangeType.Modify) {
        Attribute attr;
        if (value == null) {
            value = new Value((String) null);
            attr = new DefaultAttribute(id, (Value) value);
        } else {
            attr = (Attribute) value;
        }
        Modification item = new DefaultModification(modOp, attr);
        modificationList.add(item);
        modifications.put(id, item);
    }
}
Also used : DefaultModification(org.apache.directory.api.ldap.model.entry.DefaultModification) Modification(org.apache.directory.api.ldap.model.entry.Modification) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) DefaultModification(org.apache.directory.api.ldap.model.entry.DefaultModification) Value(org.apache.directory.api.ldap.model.entry.Value) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute)

Example 74 with DefaultAttribute

use of org.apache.directory.api.ldap.model.entry.DefaultAttribute in project directory-ldap-api by apache.

the class LdifUtils method convertToLdif.

/**
 * Convert an LdifEntry to LDIF
 *
 * @param entry the LdifEntry to convert
 * @param length The maximum line's length
 * @return the corresponding LDIF as a String
 * @throws LdapException If a naming exception is encountered.
 */
public static String convertToLdif(LdifEntry entry, int length) throws LdapException {
    StringBuilder sb = new StringBuilder();
    // First, dump the Dn
    if (isLDIFSafe(entry.getDn().getName())) {
        sb.append(stripLineToNChars("dn: " + entry.getDn(), length));
    } else {
        sb.append(stripLineToNChars("dn:: " + encodeBase64(entry.getDn().getName()), length));
    }
    sb.append('\n');
    // Dump the ChangeType
    String changeType = Strings.toLowerCaseAscii(entry.getChangeType().toString());
    if (entry.getChangeType() != ChangeType.None) {
        // First dump the controls if any
        if (entry.hasControls()) {
            for (LdifControl control : entry.getControls().values()) {
                StringBuilder controlStr = new StringBuilder();
                controlStr.append("control: ").append(control.getOid());
                controlStr.append(" ").append(control.isCritical());
                if (control.hasValue()) {
                    controlStr.append("::").append(Base64.encode(control.getValue()));
                }
                sb.append(stripLineToNChars(controlStr.toString(), length));
                sb.append('\n');
            }
        }
        sb.append(stripLineToNChars("changetype: " + changeType, length));
        sb.append('\n');
    }
    switch(entry.getChangeType()) {
        case None:
            if (entry.hasControls()) {
                sb.append(stripLineToNChars("changetype: " + ChangeType.Add, length));
            }
        case Add:
            if (entry.getEntry() == null) {
                throw new LdapException(I18n.err(I18n.ERR_13472_ENTRY_WITH_NO_ATTRIBUTE));
            }
            // Now, iterate through all the attributes
            for (Attribute attribute : entry.getEntry()) {
                sb.append(convertToLdif(attribute, length));
            }
            break;
        case Delete:
            if (entry.getEntry() != null) {
                throw new LdapException(I18n.err(I18n.ERR_13471_DELETED_ENTRY_WITH_ATTRIBUTES));
            }
            break;
        case ModDn:
        case ModRdn:
            if (entry.getEntry() != null) {
                throw new LdapException(I18n.err(I18n.ERR_13473_MODDN_WITH_ATTRIBUTES));
            }
            // Stores the new Rdn
            Attribute newRdn = new DefaultAttribute("newrdn", entry.getNewRdn());
            sb.append(convertToLdif(newRdn, length));
            // Stores the deleteoldrdn flag
            sb.append("deleteoldrdn: ");
            if (entry.isDeleteOldRdn()) {
                sb.append("1");
            } else {
                sb.append("0");
            }
            sb.append('\n');
            // Stores the optional newSuperior
            if (!Strings.isEmpty(entry.getNewSuperior())) {
                Attribute newSuperior = new DefaultAttribute("newsuperior", entry.getNewSuperior());
                sb.append(convertToLdif(newSuperior, length));
            }
            break;
        case Modify:
            boolean isFirst = true;
            for (Modification modification : entry.getModifications()) {
                if (isFirst) {
                    isFirst = false;
                } else {
                    sb.append("-\n");
                }
                switch(modification.getOperation()) {
                    case ADD_ATTRIBUTE:
                        sb.append("add: ");
                        break;
                    case REMOVE_ATTRIBUTE:
                        sb.append("delete: ");
                        break;
                    case REPLACE_ATTRIBUTE:
                        sb.append("replace: ");
                        break;
                    default:
                        throw new IllegalArgumentException(I18n.err(I18n.ERR_13434_UNEXPECTED_MOD_OPERATION, modification.getOperation()));
                }
                sb.append(modification.getAttribute().getUpId());
                sb.append('\n');
                sb.append(convertToLdif(modification.getAttribute(), length));
            }
            sb.append('-');
            break;
        default:
            throw new IllegalArgumentException(I18n.err(I18n.ERR_13431_UNEXPECTED_CHANGETYPE, entry.getChangeType()));
    }
    sb.append('\n');
    return sb.toString();
}
Also used : Modification(org.apache.directory.api.ldap.model.entry.Modification) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 75 with DefaultAttribute

use of org.apache.directory.api.ldap.model.entry.DefaultAttribute in project directory-ldap-api by apache.

the class BinaryAnonymizer method anonymize.

/**
 * Anonymize an attribute using pure random values (either chars of bytes, depending on the Attribute type)
 */
@Override
public Attribute anonymize(Map<Value, Value> valueMap, Set<Value> valueSet, Attribute attribute) {
    Attribute result = new DefaultAttribute(attribute.getAttributeType());
    for (Value value : attribute) {
        byte[] bytesValue = value.getBytes();
        byte[] newValue = computeNewValue(bytesValue);
        try {
            result.add(newValue);
            Value anonValue = new Value(attribute.getAttributeType(), newValue);
            valueMap.put((Value) value, anonValue);
            valueSet.add(anonValue);
        } catch (LdapInvalidAttributeValueException e) {
            throw new RuntimeException(I18n.err(I18n.ERR_13436_ERROR_ANONYMIZING_VALUE, value));
        }
    }
    return result;
}
Also used : Attribute(org.apache.directory.api.ldap.model.entry.Attribute) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) Value(org.apache.directory.api.ldap.model.entry.Value) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) LdapInvalidAttributeValueException(org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException)

Aggregations

DefaultAttribute (org.apache.directory.api.ldap.model.entry.DefaultAttribute)159 Attribute (org.apache.directory.api.ldap.model.entry.Attribute)131 Test (org.junit.Test)106 DefaultModification (org.apache.directory.api.ldap.model.entry.DefaultModification)41 Modification (org.apache.directory.api.ldap.model.entry.Modification)40 Entry (org.apache.directory.api.ldap.model.entry.Entry)36 DefaultEntry (org.apache.directory.api.ldap.model.entry.DefaultEntry)35 Value (org.apache.directory.api.ldap.model.entry.Value)20 ByteArrayInputStream (java.io.ByteArrayInputStream)13 ObjectInputStream (java.io.ObjectInputStream)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 ObjectOutputStream (java.io.ObjectOutputStream)12 Dn (org.apache.directory.api.ldap.model.name.Dn)12 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)11 AttributeType (org.apache.directory.api.ldap.model.schema.AttributeType)11 LdapInvalidAttributeValueException (org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException)8 ModifyRequest (org.apache.directory.api.ldap.model.message.ModifyRequest)7 ModifyRequestImpl (org.apache.directory.api.ldap.model.message.ModifyRequestImpl)7 ModifyResponse (org.apache.directory.api.ldap.model.message.ModifyResponse)7 HashSet (java.util.HashSet)6