Search in sources :

Example 41 with Attribute

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

the class LdifAnonymizer method anonymizeAva.

/**
 * Anonymize an AVA
 */
private Ava anonymizeAva(Ava ava) throws LdapInvalidDnException, LdapInvalidAttributeValueException {
    Value value = ava.getValue();
    AttributeType attributeType = ava.getAttributeType();
    Value anonymizedValue = valueMap.get(value);
    Ava anonymizedAva;
    if (anonymizedValue == null) {
        Attribute attribute = new DefaultAttribute(attributeType);
        attribute.add(value);
        Anonymizer anonymizer = attributeAnonymizers.get(attribute.getAttributeType().getOid());
        if (value.isHumanReadable()) {
            if (anonymizer == null) {
                anonymizedAva = new Ava(schemaManager, ava.getType(), value.getValue());
            } else {
                Attribute anonymizedAttribute = anonymizer.anonymize(valueMap, valueSet, attribute);
                anonymizedAva = new Ava(schemaManager, ava.getType(), anonymizedAttribute.getString());
            }
        } else {
            if (anonymizer == null) {
                anonymizedAva = new Ava(schemaManager, ava.getType(), value.getBytes());
            } else {
                Attribute anonymizedAttribute = anonymizer.anonymize(valueMap, valueSet, attribute);
                anonymizedAva = new Ava(schemaManager, ava.getType(), anonymizedAttribute.getBytes());
            }
        }
    } else {
        if (value.isHumanReadable()) {
            anonymizedAva = new Ava(schemaManager, ava.getType(), anonymizedValue.getValue());
        } else {
            anonymizedAva = new Ava(schemaManager, ava.getType(), anonymizedValue.getBytes());
        }
    }
    return anonymizedAva;
}
Also used : DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) TelephoneNumberAnonymizer(org.apache.directory.api.ldap.model.ldif.anonymizer.TelephoneNumberAnonymizer) StringAnonymizer(org.apache.directory.api.ldap.model.ldif.anonymizer.StringAnonymizer) BinaryAnonymizer(org.apache.directory.api.ldap.model.ldif.anonymizer.BinaryAnonymizer) IntegerAnonymizer(org.apache.directory.api.ldap.model.ldif.anonymizer.IntegerAnonymizer) CaseSensitiveStringAnonymizer(org.apache.directory.api.ldap.model.ldif.anonymizer.CaseSensitiveStringAnonymizer) Anonymizer(org.apache.directory.api.ldap.model.ldif.anonymizer.Anonymizer) Value(org.apache.directory.api.ldap.model.entry.Value) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) Ava(org.apache.directory.api.ldap.model.name.Ava)

Example 42 with Attribute

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

the class LdifAnonymizer method anonymizeChangeModify.

/**
 * Anonymize a Modify change
 */
private LdifEntry anonymizeChangeModify(LdifEntry ldifEntry) throws LdapException {
    Dn entryDn = ldifEntry.getDn();
    LdifEntry newLdifEntry = new LdifEntry(schemaManager);
    newLdifEntry.setChangeType(ChangeType.Modify);
    // Process the DN first
    Dn anonymizedDn = anonymizeDn(entryDn);
    newLdifEntry.setDn(anonymizedDn);
    // Now, process the entry's attributes
    for (Modification modification : ldifEntry.getModifications()) {
        Attribute attribute = modification.getAttribute();
        AttributeType attributeType = schemaManager.getAttributeType(attribute.getId());
        if (attributeType == null) {
            System.out.println("\nUnknown AttributeType : " + attribute.getId() + " for entry " + entryDn);
            return null;
        }
        attribute.apply(attributeType);
        // Deal with the special case of a DN syntax
        if (attributeType.getSyntax().getSyntaxChecker() instanceof DnSyntaxChecker) {
            Value[] anonymizedValues = new Value[attribute.size()];
            int pos = 0;
            for (Value dnValue : modification.getAttribute()) {
                Dn dn = new Dn(schemaManager, dnValue.getValue());
                Dn newdDn = anonymizeDn(dn);
                anonymizedValues[pos++] = new Value(newdDn.toString());
            }
            Modification anonymizedModification = new DefaultModification(modification.getOperation(), attributeType, anonymizedValues);
            newLdifEntry.addModification(anonymizedModification);
        } else {
            Anonymizer anonymizer = attributeAnonymizers.get(attributeType.getOid());
            if (anonymizer == null) {
                newLdifEntry.addModification(modification);
            } else {
                Attribute anonymizedAttribute = anonymizer.anonymize(valueMap, valueSet, attribute);
                Modification anonymizedModification = new DefaultModification(modification.getOperation(), anonymizedAttribute);
                newLdifEntry.addModification(anonymizedModification);
            }
        }
    }
    return newLdifEntry;
}
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) AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) TelephoneNumberAnonymizer(org.apache.directory.api.ldap.model.ldif.anonymizer.TelephoneNumberAnonymizer) StringAnonymizer(org.apache.directory.api.ldap.model.ldif.anonymizer.StringAnonymizer) BinaryAnonymizer(org.apache.directory.api.ldap.model.ldif.anonymizer.BinaryAnonymizer) IntegerAnonymizer(org.apache.directory.api.ldap.model.ldif.anonymizer.IntegerAnonymizer) CaseSensitiveStringAnonymizer(org.apache.directory.api.ldap.model.ldif.anonymizer.CaseSensitiveStringAnonymizer) Anonymizer(org.apache.directory.api.ldap.model.ldif.anonymizer.Anonymizer) Value(org.apache.directory.api.ldap.model.entry.Value) Dn(org.apache.directory.api.ldap.model.name.Dn) DnSyntaxChecker(org.apache.directory.api.ldap.model.schema.syntaxCheckers.DnSyntaxChecker) LdifEntry(org.apache.directory.api.ldap.model.ldif.LdifEntry)

Example 43 with Attribute

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

the class LdifAnonymizer method anonymizeEntry.

/**
 * Anonymize the full entry
 */
private Entry anonymizeEntry(LdifEntry ldifEntry) throws LdapException {
    Entry entry = ldifEntry.getEntry();
    Entry newEntry = new DefaultEntry(schemaManager);
    // Process the DN first
    Dn entryDn = entry.getDn();
    Dn anonymizedDn = anonymizeDn(entryDn);
    // Now, process the entry's attributes
    for (Attribute attribute : entry) {
        AttributeType attributeType = attribute.getAttributeType();
        // Deal with the special case of DN
        if (attributeType.getSyntax().getSyntaxChecker() instanceof DnSyntaxChecker) {
            for (Value dnValue : attribute) {
                Dn dn = new Dn(schemaManager, dnValue.getValue());
                Dn newdDn = anonymizeDn(dn);
                newEntry.add(attributeType, newdDn.toString());
            }
        } else // Deal with the special case of a NameAndOptionalUID
        if (attributeType.getSyntax().getSyntaxChecker() instanceof NameAndOptionalUIDSyntaxChecker) {
            for (Value dnValue : attribute) {
                // Get rid of the # part (UID)
                String valueStr = dnValue.getValue();
                int uidPos = valueStr.indexOf('#');
                String uid = null;
                if (uidPos != -1) {
                    uid = valueStr.substring(uidPos + 1);
                    valueStr = valueStr.substring(0, uidPos);
                }
                Dn dn = new Dn(schemaManager, valueStr);
                Dn newDn = anonymizeDn(dn);
                String newDnStr = newDn.toString();
                if (uid != null) {
                    newDnStr = newDnStr + '#' + uid;
                }
                newEntry.add(attributeType, newDnStr);
            }
        } else {
            Anonymizer anonymizer = attributeAnonymizers.get(attribute.getAttributeType().getOid());
            if (anonymizer == null) {
                newEntry.add(attribute);
            } else {
                Attribute anonymizedAttribute = anonymizer.anonymize(valueMap, valueSet, attribute);
                if (anonymizedAttribute != null) {
                    newEntry.add(anonymizedAttribute);
                }
            }
        }
    }
    newEntry.setDn(anonymizedDn);
    return newEntry;
}
Also used : DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) LdifEntry(org.apache.directory.api.ldap.model.ldif.LdifEntry) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) NameAndOptionalUIDSyntaxChecker(org.apache.directory.api.ldap.model.schema.syntaxCheckers.NameAndOptionalUIDSyntaxChecker) AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) TelephoneNumberAnonymizer(org.apache.directory.api.ldap.model.ldif.anonymizer.TelephoneNumberAnonymizer) StringAnonymizer(org.apache.directory.api.ldap.model.ldif.anonymizer.StringAnonymizer) BinaryAnonymizer(org.apache.directory.api.ldap.model.ldif.anonymizer.BinaryAnonymizer) IntegerAnonymizer(org.apache.directory.api.ldap.model.ldif.anonymizer.IntegerAnonymizer) CaseSensitiveStringAnonymizer(org.apache.directory.api.ldap.model.ldif.anonymizer.CaseSensitiveStringAnonymizer) Anonymizer(org.apache.directory.api.ldap.model.ldif.anonymizer.Anonymizer) Value(org.apache.directory.api.ldap.model.entry.Value) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) Dn(org.apache.directory.api.ldap.model.name.Dn) DnSyntaxChecker(org.apache.directory.api.ldap.model.schema.syntaxCheckers.DnSyntaxChecker)

Example 44 with Attribute

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

the class SchemaAwareAttributeSerializationTest method testEntryAttributeNoBinaryValueSerialization.

@Test
public void testEntryAttributeNoBinaryValueSerialization() throws IOException, ClassNotFoundException, LdapInvalidAttributeValueException, LdapInvalidAttributeValueException {
    Attribute attribute1 = new DefaultAttribute(userCertificate);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(baos);
    attribute1.writeExternal(out);
    ObjectInputStream in = null;
    byte[] data = baos.toByteArray();
    in = new ObjectInputStream(new ByteArrayInputStream(data));
    Attribute attribute2 = new DefaultAttribute(userCertificate);
    attribute2.readExternal(in);
    attribute2.apply(userCertificate);
    assertEquals(attribute1, attribute2);
}
Also used : DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) ByteArrayInputStream(java.io.ByteArrayInputStream) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 45 with Attribute

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

the class SchemaAwareAttributeSerializationTest method testEntryAttributeManyBinaryValuesSerialization.

@Test
public void testEntryAttributeManyBinaryValuesSerialization() throws IOException, ClassNotFoundException, LdapInvalidAttributeValueException {
    Attribute attribute1 = new DefaultAttribute("UserCertificate", userCertificate, data1, data2, data3);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(baos);
    attribute1.writeExternal(out);
    ObjectInputStream in = null;
    byte[] data = baos.toByteArray();
    in = new ObjectInputStream(new ByteArrayInputStream(data));
    Attribute attribute2 = new DefaultAttribute(userCertificate);
    attribute2.readExternal(in);
    attribute2.apply(userCertificate);
    assertEquals(attribute1, attribute2);
    assertEquals("UserCertificate", attribute2.getUpId());
}
Also used : DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) ByteArrayInputStream(java.io.ByteArrayInputStream) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Aggregations

Attribute (org.apache.directory.api.ldap.model.entry.Attribute)269 Test (org.junit.Test)180 DefaultAttribute (org.apache.directory.api.ldap.model.entry.DefaultAttribute)168 Entry (org.apache.directory.api.ldap.model.entry.Entry)94 Modification (org.apache.directory.api.ldap.model.entry.Modification)56 Value (org.apache.directory.api.ldap.model.entry.Value)52 DefaultEntry (org.apache.directory.api.ldap.model.entry.DefaultEntry)46 DefaultModification (org.apache.directory.api.ldap.model.entry.DefaultModification)35 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)23 EncoderException (org.apache.directory.api.asn1.EncoderException)20 ByteBuffer (java.nio.ByteBuffer)18 DecoderException (org.apache.directory.api.asn1.DecoderException)18 Asn1Decoder (org.apache.directory.api.asn1.ber.Asn1Decoder)18 LdapMessageContainer (org.apache.directory.api.ldap.codec.api.LdapMessageContainer)18 AbstractCodecServiceTest (org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)18 SearchResultEntry (org.apache.directory.api.ldap.model.message.SearchResultEntry)18 LdapInvalidAttributeValueException (org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException)16 LdifEntry (org.apache.directory.api.ldap.model.ldif.LdifEntry)16 ModifyRequest (org.apache.directory.api.ldap.model.message.ModifyRequest)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13