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;
}
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);
}
}
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);
}
}
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();
}
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;
}
Aggregations