Search in sources :

Example 1 with Ava

use of org.apache.directory.api.ldap.model.name.Ava 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 2 with Ava

use of org.apache.directory.api.ldap.model.name.Ava in project directory-ldap-api by apache.

the class LdifReader method parseEntry.

/**
 * Parse a ldif file. The following rules are processed :
 * <pre>
 * &lt;ldif-file&gt; ::= &lt;ldif-attrval-record&gt; &lt;ldif-attrval-records&gt; |
 *     &lt;ldif-change-record&gt; &lt;ldif-change-records&gt;
 * &lt;ldif-attrval-record&gt; ::= &lt;dn-spec&gt; &lt;sep&gt; &lt;attrval-spec&gt; &lt;attrval-specs&gt;
 * &lt;ldif-change-record&gt; ::= &lt;dn-spec&gt; &lt;sep&gt; &lt;controls-e&gt; &lt;changerecord&gt;
 * &lt;dn-spec&gt; ::= "dn:" &lt;fill&gt; &lt;distinguishedName&gt; | "dn::" &lt;fill&gt; &lt;base64-distinguishedName&gt;
 * &lt;changerecord&gt; ::= "changetype:" &lt;fill&gt; &lt;change-op&gt;
 * </pre>
 *
 * @return the parsed ldifEntry
 * @exception LdapException If the ldif file does not contain a valid entry
 */
protected LdifEntry parseEntry() throws LdapException {
    if ((lines == null) || lines.isEmpty()) {
        LOG.debug(I18n.msg(I18n.MSG_13408_END_OF_LDIF));
        return null;
    }
    // The entry must start with a dn: or a dn::
    String line = lines.get(0);
    lineNumber -= (lines.size() - 1);
    String name = parseDn(line);
    Dn dn = null;
    try {
        dn = new Dn(schemaManager, name);
    } catch (LdapInvalidDnException lide) {
        // Deal with the RDN whihc is not in the schema
        // First parse the DN without the schema
        dn = new Dn(name);
        Rdn rdn = dn.getRdn();
        // Process each Ava
        for (Ava ava : rdn) {
            if ((schemaManager != null) && (schemaManager.getAttributeType(ava.getType()) == null) && schemaManager.isRelaxed()) {
                // Not found : create a new one
                MutableAttributeType newAttributeType = new MutableAttributeType("1.3.6.1.4.1.18060.0.9999." + oidCounter++);
                newAttributeType.setNames(ava.getType());
                newAttributeType.setSyntax(schemaManager.getLdapSyntaxRegistry().get(SchemaConstants.DIRECTORY_STRING_SYNTAX));
                schemaManager.add(newAttributeType);
            }
        }
        dn = new Dn(schemaManager, name);
    }
    // Ok, we have found a Dn
    LdifEntry entry = createLdifEntry(schemaManager);
    entry.setLengthBeforeParsing(entryLen);
    entry.setOffset(entryOffset);
    entry.setDn(dn);
    // We remove this dn from the lines
    lines.remove(0);
    // Now, let's iterate through the other lines
    Iterator<String> iter = lines.iterator();
    // This flag is used to distinguish between an entry and a change
    int type = LDIF_ENTRY;
    // The following boolean is used to check that a control is *not*
    // found elswhere than just after the dn
    boolean controlSeen = false;
    // We use this boolean to check that we do not have AttributeValues
    // after a change operation
    boolean changeTypeSeen = false;
    ChangeType operation = ChangeType.Add;
    String lowerLine;
    Control control;
    while (iter.hasNext()) {
        lineNumber++;
        // Each line could start either with an OID, an attribute type, with
        // "control:" or with "changetype:"
        line = iter.next();
        lowerLine = Strings.toLowerCaseAscii(line);
        // 3) The first line after the Dn is anything else
        if (lowerLine.startsWith("control:")) {
            if (containsEntries) {
                LOG.error(I18n.err(I18n.ERR_13401_CHANGE_NOT_ALLOWED, lineNumber));
                throw new LdapLdifException(I18n.err(I18n.ERR_13440_NO_CHANGE));
            }
            containsChanges = true;
            if (controlSeen) {
                LOG.error(I18n.err(I18n.ERR_13418_CONTROL_ALREADY_FOUND, lineNumber));
                throw new LdapLdifException(I18n.err(I18n.ERR_13457_MISPLACED_CONTROL));
            }
            // Parse the control
            control = parseControl(line.substring("control:".length()));
            entry.addControl(control);
        } else if (lowerLine.startsWith("changetype:")) {
            if (containsEntries) {
                LOG.error(I18n.err(I18n.ERR_13401_CHANGE_NOT_ALLOWED, lineNumber));
                throw new LdapLdifException(I18n.err(I18n.ERR_13440_NO_CHANGE));
            }
            containsChanges = true;
            if (changeTypeSeen) {
                LOG.error(I18n.err(I18n.ERR_13419_CHANGETYPE_ALREADY_FOUND, lineNumber));
                throw new LdapLdifException(I18n.err(I18n.ERR_13458_MISPLACED_CHANGETYPE));
            }
            // A change request
            type = CHANGE;
            controlSeen = true;
            operation = parseChangeType(line);
            // Parse the change operation in a separate function
            parseChange(entry, iter, operation);
            changeTypeSeen = true;
        } else if (line.indexOf(':') > 0) {
            if (containsChanges) {
                LOG.error(I18n.err(I18n.ERR_13401_CHANGE_NOT_ALLOWED, lineNumber));
                throw new LdapLdifException(I18n.err(I18n.ERR_13440_NO_CHANGE));
            }
            containsEntries = true;
            if (controlSeen || changeTypeSeen) {
                LOG.error(I18n.err(I18n.ERR_13420_AT_VALUE_NOT_ALLOWED_AFTER_CONTROL, lineNumber));
                throw new LdapLdifException(I18n.err(I18n.ERR_13459_MISPLACED_ATTRIBUTETYPE));
            }
            parseAttributeValue(entry, line, lowerLine);
            type = LDIF_ENTRY;
        } else {
            // Invalid attribute Value
            LOG.error(I18n.err(I18n.ERR_13421_ATTRIBUTE_TYPE_EXPECTED, lineNumber));
            throw new LdapLdifException(I18n.err(I18n.ERR_13460_BAD_ATTRIBUTE));
        }
    }
    if (type == LDIF_ENTRY) {
        LOG.debug(I18n.msg(I18n.MSG_13406_READ_ENTRY, entry));
    } else if (type == CHANGE) {
        entry.setChangeType(operation);
        LOG.debug(I18n.msg(I18n.MSG_13404_READ_MODIF, entry));
    } else {
        LOG.error(I18n.err(I18n.ERR_13422_UNKNOWN_ENTRY_TYPE, lineNumber));
        throw new LdapLdifException(I18n.err(I18n.ERR_13461_UNKNOWN_ENTRY));
    }
    return entry;
}
Also used : Dn(org.apache.directory.api.ldap.model.name.Dn) Ava(org.apache.directory.api.ldap.model.name.Ava) Control(org.apache.directory.api.ldap.model.message.Control) MutableAttributeType(org.apache.directory.api.ldap.model.schema.MutableAttributeType) Rdn(org.apache.directory.api.ldap.model.name.Rdn) LdapInvalidDnException(org.apache.directory.api.ldap.model.exception.LdapInvalidDnException)

Example 3 with Ava

use of org.apache.directory.api.ldap.model.name.Ava in project directory-ldap-api by apache.

the class LdifRevertor method reverseMoveAndRename.

/**
 * Revert a Dn to it's previous version by removing the first Rdn and adding the given Rdn.
 * It's a rename operation. The biggest issue is that we have many corner cases, depending
 * on the RDNs we are manipulating, and on the content of the initial entry.
 *
 * @param entry The initial Entry
 * @param newSuperior The new superior Dn (can be null if it's just a rename)
 * @param newRdn The new Rdn
 * @param deleteOldRdn A flag which tells to delete the old Rdn AVAs
 * @return A list of LDIF reverted entries
 * @throws LdapInvalidDnException If the name reverting failed
 */
public static List<LdifEntry> reverseMoveAndRename(Entry entry, Dn newSuperior, Rdn newRdn, boolean deleteOldRdn) throws LdapInvalidDnException {
    Dn parentDn = entry.getDn();
    Dn newDn;
    if (newRdn == null) {
        throw new IllegalArgumentException(I18n.err(I18n.ERR_13469_NULL_READ_DN));
    }
    if (parentDn == null) {
        throw new IllegalArgumentException(I18n.err(I18n.ERR_13467_NULL_MODIFIED_DN));
    }
    if (parentDn.size() == 0) {
        throw new IllegalArgumentException(I18n.err(I18n.ERR_13470_DONT_RENAME_ROOTDSE));
    }
    parentDn = entry.getDn();
    Rdn oldRdn = parentDn.getRdn();
    newDn = parentDn;
    newDn = newDn.getParent();
    newDn = newDn.add(newRdn);
    List<LdifEntry> entries = new ArrayList<>(1);
    LdifEntry reverted;
    // Start with the cases here
    if (newRdn.size() == 1) {
        // We have a simple new Rdn, something like A=a
        reverted = revertEntry(entry, newDn, newSuperior, oldRdn, newRdn);
        entries.add(reverted);
    } else {
        // We have a composite new Rdn, something like A=a+B=b
        if (oldRdn.size() == 1) {
            // The old Rdn is simple
            boolean existInEntry = false;
            // Is the new Rdn AVAs contained into the entry?
            for (Ava atav : newRdn) {
                if (!atav.equals(oldRdn.getAva()) && (entry.contains(atav.getNormType(), atav.getValue().getValue()))) {
                    existInEntry = true;
                }
            }
            // The new Rdn includes the old one
            if (existInEntry) {
                // Some of the new Rdn AVAs existed in the entry
                // We have to restore them, but we also have to remove
                // the new values
                reverted = generateReverted(newSuperior, newRdn, newDn, oldRdn, KEEP_OLD_RDN);
                entries.add(reverted);
                // Now, restore the initial values
                LdifEntry restored = generateModify(parentDn, entry, oldRdn, newRdn);
                entries.add(restored);
            } else {
                // This is the simplest case, we don't have to restore
                // some existing values (case 8.1 and 9.1)
                reverted = generateReverted(newSuperior, newRdn, newDn, oldRdn, DELETE_OLD_RDN);
                entries.add(reverted);
            }
        } else {
            // We have a composite new Rdn, something like A=a+B=b
            // Does the Rdn overlap ?
            boolean overlapping = false;
            boolean existInEntry = false;
            Set<Ava> oldAtavs = new HashSet<>();
            // We first build a set with all the oldRDN ATAVs
            for (Ava atav : oldRdn) {
                oldAtavs.add(atav);
            }
            // and if the newRdn ATAVs are present in the entry
            for (Ava atav : newRdn) {
                if (oldAtavs.contains(atav)) {
                    overlapping = true;
                } else if (entry.contains(atav.getNormType(), atav.getValue().getValue())) {
                    existInEntry = true;
                }
            }
            if (overlapping) {
                // They overlap
                if (existInEntry) {
                    // In this case, we have to reestablish the removed ATAVs
                    // (Cases 12.2 and 13.2)
                    reverted = generateReverted(newSuperior, newRdn, newDn, oldRdn, KEEP_OLD_RDN);
                    entries.add(reverted);
                } else {
                    // We can simply remove all the new Rdn atavs, as the
                    // overlapping values will be re-created.
                    // (Cases 12.1 and 13.1)
                    reverted = generateReverted(newSuperior, newRdn, newDn, oldRdn, DELETE_OLD_RDN);
                    entries.add(reverted);
                }
            } else {
                // No overlapping
                if (existInEntry) {
                    // In this case, we have to reestablish the removed ATAVs
                    // (Cases 10.2 and 11.2)
                    reverted = generateReverted(newSuperior, newRdn, newDn, oldRdn, KEEP_OLD_RDN);
                    entries.add(reverted);
                    LdifEntry restored = generateModify(parentDn, entry, oldRdn, newRdn);
                    entries.add(restored);
                } else {
                    // We are safe ! We can delete all the new Rdn ATAVs
                    // (Cases 10.1 and 11.1)
                    reverted = generateReverted(newSuperior, newRdn, newDn, oldRdn, DELETE_OLD_RDN);
                    entries.add(reverted);
                }
            }
        }
    }
    return entries;
}
Also used : ArrayList(java.util.ArrayList) Dn(org.apache.directory.api.ldap.model.name.Dn) Rdn(org.apache.directory.api.ldap.model.name.Rdn) Ava(org.apache.directory.api.ldap.model.name.Ava) HashSet(java.util.HashSet)

Example 4 with Ava

use of org.apache.directory.api.ldap.model.name.Ava in project directory-ldap-api by apache.

the class AvaSerializationTest method testStringAtavSerialization.

/**
 * Test serialization of a simple ATAV
 */
@Test
public void testStringAtavSerialization() throws LdapException, IOException, ClassNotFoundException {
    Ava atav = new Ava(schemaManager, "CN", "Test");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(baos);
    atav.writeExternal(out);
    ObjectInputStream in = null;
    byte[] data = baos.toByteArray();
    in = new ObjectInputStream(new ByteArrayInputStream(data));
    Ava atav2 = new Ava(schemaManager);
    atav2.readExternal(in);
    assertEquals(atav, atav2);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) Ava(org.apache.directory.api.ldap.model.name.Ava) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 5 with Ava

use of org.apache.directory.api.ldap.model.name.Ava in project directory-ldap-api by apache.

the class AvaSerializationTest method testNullNormValueSerialization.

@Test
public void testNullNormValueSerialization() throws LdapException, IOException, ClassNotFoundException {
    Ava atav = new Ava(schemaManager, "CN", (String) null);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(baos);
    try {
        atav.writeExternal(out);
        fail();
    } catch (IOException ioe) {
        String message = ioe.getMessage();
        assertEquals("ERR_13619_CANNOT_SERIALIZE_AVA_VALUE_NULL Cannot serialize a wrong ATAV, the value should not be null", message);
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) Ava(org.apache.directory.api.ldap.model.name.Ava) Test(org.junit.Test)

Aggregations

Ava (org.apache.directory.api.ldap.model.name.Ava)44 Test (org.junit.Test)39 ByteArrayOutputStream (java.io.ByteArrayOutputStream)14 ObjectOutputStream (java.io.ObjectOutputStream)14 Rdn (org.apache.directory.api.ldap.model.name.Rdn)12 ByteArrayInputStream (java.io.ByteArrayInputStream)8 ObjectInputStream (java.io.ObjectInputStream)8 IOException (java.io.IOException)5 Dn (org.apache.directory.api.ldap.model.name.Dn)3 DefaultAttribute (org.apache.directory.api.ldap.model.entry.DefaultAttribute)2 LdapInvalidDnException (org.apache.directory.api.ldap.model.exception.LdapInvalidDnException)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Attribute (org.apache.directory.api.ldap.model.entry.Attribute)1 DefaultModification (org.apache.directory.api.ldap.model.entry.DefaultModification)1 Modification (org.apache.directory.api.ldap.model.entry.Modification)1 Value (org.apache.directory.api.ldap.model.entry.Value)1 Anonymizer (org.apache.directory.api.ldap.model.ldif.anonymizer.Anonymizer)1 BinaryAnonymizer (org.apache.directory.api.ldap.model.ldif.anonymizer.BinaryAnonymizer)1 CaseSensitiveStringAnonymizer (org.apache.directory.api.ldap.model.ldif.anonymizer.CaseSensitiveStringAnonymizer)1