use of org.apache.directory.api.ldap.model.schema.Normalizer in project directory-ldap-api by apache.
the class BinaryValueTest method initAT.
/**
* Initialize an AttributeType and the associated MatchingRule
* and Syntax
*/
@Before
public void initAT() {
s = EntryUtils.syntaxFactory("1.1.1.1", false);
s.setSyntaxChecker(OctetStringSyntaxChecker.INSTANCE);
mr = EntryUtils.matchingRuleFactory("1.1.2.1");
mr.setSyntax(s);
mr.setLdapComparator(new ByteArrayComparator("1.1.1"));
mr.setNormalizer(new Normalizer("1.1.1") {
public static final long serialVersionUID = 1L;
public String normalize(String value) throws LdapException {
return normalize(value, PrepareString.AssertionType.ATTRIBUTE_VALUE);
}
public String normalize(String value, PrepareString.AssertionType assertionType) throws LdapException {
byte[] val = Strings.getBytesUtf8(value);
// each byte will be changed to be > 0, and spaces will be trimmed
byte[] newVal = new byte[val.length];
int i = 0;
for (byte b : val) {
newVal[i++] = (byte) (b & 0x007F);
}
return Strings.utf8ToString(Strings.trim(newVal));
}
});
at = new MutableAttributeType("1.1.3.1");
at.setEquality(mr);
at.setOrdering(mr);
at.setSubstring(mr);
at.setSyntax(s);
}
use of org.apache.directory.api.ldap.model.schema.Normalizer in project directory-ldap-api by apache.
the class Registries method unregister.
/**
* Unregister a SchemaObject from the registries
*
* @param schemaObject The SchemaObject we want to deregister
* @throws LdapException If the removal failed
*/
private SchemaObject unregister(List<Throwable> errors, SchemaObject schemaObject) throws LdapException {
LOG.debug("Unregistering {}:{}", schemaObject.getObjectType(), schemaObject.getOid());
// Check that the SchemaObject is present in the registries
if (!(schemaObject instanceof LoadableSchemaObject) && !globalOidRegistry.contains(schemaObject.getOid())) {
String msg = I18n.err(I18n.ERR_13751_UNREGISTERING_FAILED_NOT_PRESENT, schemaObject.getObjectType(), schemaObject.getOid());
LOG.error(msg);
throw new LdapUnwillingToPerformException(ResultCodeEnum.UNWILLING_TO_PERFORM, msg);
}
SchemaObject unregistered;
// First call the specific registry's register method
switch(schemaObject.getObjectType()) {
case ATTRIBUTE_TYPE:
unregistered = attributeTypeRegistry.unregister((AttributeType) schemaObject);
break;
case COMPARATOR:
unregistered = comparatorRegistry.unregister((LdapComparator<?>) schemaObject);
break;
case DIT_CONTENT_RULE:
unregistered = ditContentRuleRegistry.unregister((DitContentRule) schemaObject);
break;
case DIT_STRUCTURE_RULE:
unregistered = ditStructureRuleRegistry.unregister((DitStructureRule) schemaObject);
break;
case LDAP_SYNTAX:
unregistered = ldapSyntaxRegistry.unregister((LdapSyntax) schemaObject);
break;
case MATCHING_RULE:
unregistered = matchingRuleRegistry.unregister((MatchingRule) schemaObject);
break;
case MATCHING_RULE_USE:
unregistered = matchingRuleUseRegistry.unregister((MatchingRuleUse) schemaObject);
break;
case NAME_FORM:
unregistered = nameFormRegistry.unregister((NameForm) schemaObject);
break;
case NORMALIZER:
unregistered = normalizerRegistry.unregister((Normalizer) schemaObject);
break;
case OBJECT_CLASS:
unregistered = objectClassRegistry.unregister((ObjectClass) schemaObject);
break;
case SYNTAX_CHECKER:
unregistered = syntaxCheckerRegistry.unregister((SyntaxChecker) schemaObject);
break;
default:
throw new IllegalArgumentException(I18n.err(I18n.ERR_13718_UNEXPECTED_SCHEMA_OBJECT_TYPE, schemaObject.getObjectType()));
}
return unregistered;
}
use of org.apache.directory.api.ldap.model.schema.Normalizer in project directory-ldap-api by apache.
the class Registries method resolve.
/**
* Check if the Comparator, Normalizer and the syntax are
* existing for a matchingRule.
*/
private void resolve(MatchingRule matchingRule, List<Throwable> errors) {
// Process the Syntax. It can't be null
String syntaxOid = matchingRule.getSyntaxOid();
if (syntaxOid != null) {
// Check if the Syntax is present in the registries
try {
ldapSyntaxRegistry.lookup(syntaxOid);
} catch (LdapException ne) {
// This MR's syntax has not been loaded into the Registries.
LdapSchemaException ldapSchemaException = new LdapSchemaException(LdapSchemaExceptionCodes.OID_ALREADY_REGISTERED, I18n.err(I18n.ERR_13748_MATCHING_RULE_NO_SYNTAX, matchingRule.getOid()), ne);
ldapSchemaException.setSourceObject(matchingRule);
errors.add(ldapSchemaException);
}
} else {
// This is an error.
LdapSchemaException ldapSchemaException = new LdapSchemaException(LdapSchemaExceptionCodes.OID_ALREADY_REGISTERED, I18n.err(I18n.ERR_13748_MATCHING_RULE_NO_SYNTAX, matchingRule.getOid()));
ldapSchemaException.setSourceObject(matchingRule);
errors.add(ldapSchemaException);
}
// Process the Normalizer
Normalizer normalizer = matchingRule.getNormalizer();
if (normalizer == null) {
// Ok, no normalizer, this is an error
Throwable error = new LdapSchemaViolationException(ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, I18n.err(I18n.ERR_13220_NO_NORMALIZER, matchingRule.getOid()));
errors.add(error);
}
// Process the Comparator
LdapComparator<?> comparator = matchingRule.getLdapComparator();
if (comparator == null) {
// Ok, no comparator, this is an error
Throwable error = new LdapSchemaViolationException(ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, I18n.err(I18n.ERR_04296, matchingRule.getOid()));
errors.add(error);
}
}
use of org.apache.directory.api.ldap.model.schema.Normalizer in project directory-ldap-api by apache.
the class MatchingRuleHelper method addToRegistries.
/**
* Inject the MatchingRule into the Registries, updating the references to
* other SchemaObject
*
* @param matchingRule The MatchingRule to add to the Registries
* @param errors The errors we got while adding the MatchingRule to the registries
* @param registries The Registries
* @throws LdapException If the addition failed
*/
@SuppressWarnings("rawtypes")
public static void addToRegistries(MutableMatchingRule matchingRule, List<Throwable> errors, Registries registries) throws LdapException {
if (registries != null) {
try {
matchingRule.unlock();
LdapComparator<?> ldapComparator = null;
Normalizer normalizer = null;
LdapSyntax ldapSyntax = null;
try {
// Gets the associated Comparator
ldapComparator = registries.getComparatorRegistry().lookup(matchingRule.getOid());
} catch (LdapException ne) {
// Default to a catch all comparator
ldapComparator = new ComparableComparator(matchingRule.getOid());
}
try {
// Gets the associated Normalizer
normalizer = registries.getNormalizerRegistry().lookup(matchingRule.getOid());
} catch (LdapException ne) {
// Default to the NoOp normalizer
normalizer = new NoOpNormalizer(matchingRule.getOid());
}
try {
// Get the associated LdapSyntax
ldapSyntax = registries.getLdapSyntaxRegistry().lookup(matchingRule.getSyntaxOid());
} catch (LdapException ne) {
// The Syntax is a mandatory element, it must exist.
String msg = I18n.err(I18n.ERR_13765_MR_MUST_REFER_EXISTING_SYNTAX);
LdapSchemaException ldapSchemaException = new LdapSchemaException(LdapSchemaExceptionCodes.MR_NONEXISTENT_SYNTAX, msg, ne);
ldapSchemaException.setSourceObject(matchingRule);
ldapSchemaException.setRelatedId(matchingRule.getSyntaxOid());
errors.add(ldapSchemaException);
LOG.info(msg);
}
/**
* Add the MR references (using and usedBy) :
* MR -> C
* MR -> N
* MR -> S
*/
if (ldapComparator != null) {
registries.addReference(matchingRule, ldapComparator);
matchingRule.setLdapComparator(ldapComparator);
}
if (normalizer != null) {
registries.addReference(matchingRule, normalizer);
matchingRule.setNormalizer(normalizer);
}
if (ldapSyntax != null) {
registries.addReference(matchingRule, ldapSyntax);
matchingRule.setSyntax(ldapSyntax);
}
} finally {
matchingRule.lock();
}
}
}
use of org.apache.directory.api.ldap.model.schema.Normalizer in project directory-ldap-api by apache.
the class DefaultNormalizerRegistry method toString.
/**
* @see Object#toString()
*/
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(schemaObjectType).append(": ");
boolean isFirst = true;
for (Map.Entry<String, Normalizer> entry : byName.entrySet()) {
if (isFirst) {
isFirst = false;
} else {
sb.append(", ");
}
Normalizer normalizer = entry.getValue();
String fqcn = normalizer.getFqcn();
int lastDotPos = fqcn.lastIndexOf('.');
sb.append('<').append(normalizer.getOid()).append(", ");
if (lastDotPos > 0) {
sb.append(fqcn.substring(lastDotPos + 1));
} else {
sb.append(fqcn);
}
sb.append('>');
}
return sb.toString();
}
Aggregations