use of org.apache.directory.api.ldap.model.name.Dn 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;
}
use of org.apache.directory.api.ldap.model.name.Dn 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;
}
use of org.apache.directory.api.ldap.model.name.Dn in project directory-ldap-api by apache.
the class LdifAnonymizer method addNamingContext.
/**
* Add a new NamingContext
*
* @param dn The naming context to add
* @throws LdapInvalidDnException if it's an invalid naming context
*/
public void addNamingContext(String dn) throws LdapInvalidDnException {
Dn namingContext = new Dn(schemaManager, dn);
namingContexts.add(namingContext);
}
use of org.apache.directory.api.ldap.model.name.Dn in project directory-ldap-api by apache.
the class SchemaAwareEntryTest method testSerializeCompleteEntry.
/**
* Test the serialization of a complete entry
*/
@Test
public void testSerializeCompleteEntry() throws LdapException, IOException, ClassNotFoundException {
Dn dn = new Dn(schemaManager, "ou=system");
byte[] password = Strings.getBytesUtf8("secret");
Entry entry = new DefaultEntry(dn);
entry.add("ObjectClass", "top", "person");
entry.add("cn", "test1");
entry.add("userPassword", password);
Entry entrySer = deserializeValue(serializeValue(entry));
assertEquals(entry, entrySer);
}
use of org.apache.directory.api.ldap.model.name.Dn in project directory-ldap-api by apache.
the class SchemaAwareEntryTest method testGetDn.
/**
* Test method for getDN()
*/
@Test
public void testGetDn() throws LdapException {
Entry entry = new DefaultEntry(exampleDn);
assertEquals(exampleDn, entry.getDn());
Dn testDn = new Dn("cn=test");
entry.setDn(testDn);
assertEquals(testDn, entry.getDn());
}
Aggregations