use of org.apache.directory.api.ldap.model.schema.ObjectClass in project directory-ldap-api by apache.
the class QuirkySchemaTest method testLoadQuirkySchema.
/**
* Try to load a quirky schema. This schema has a lot of issues that violate the
* standards. Therefore load the schema in relaxed mode. We should be able to work
* with this schema anyway. E.g. the loader and schema manager should not die on
* null pointer or similar trivial error.
*/
@Test
public void testLoadQuirkySchema() throws Exception {
LdapConnection connection = createFakeConnection("src/test/resources/schema-quirky.ldif");
DefaultSchemaLoader loader = new DefaultSchemaLoader(connection, true);
Collection<Schema> allEnabled = loader.getAllEnabled();
assertEquals(1, allEnabled.size());
Schema schema = allEnabled.iterator().next();
assertNotNull(schema);
SchemaManager schemaManager = new DefaultSchemaManager(loader);
boolean loaded = schemaManager.loadAllEnabledRelaxed();
if (!loaded) {
fail("Schema load failed : " + Exceptions.printErrors(schemaManager.getErrors()));
}
assertTrue("Surprisingly no errors after load", schemaManager.getErrors().size() > 0);
assertTrue(schemaManager.getRegistries().getAttributeTypeRegistry().contains("cn"));
ObjectClass person = schemaManager.getRegistries().getObjectClassRegistry().lookup("person");
assertNotNull(person);
assertEquals(2, person.getMustAttributeTypes().size());
assertEquals(5, person.getMayAttributeTypes().size());
}
use of org.apache.directory.api.ldap.model.schema.ObjectClass in project directory-ldap-api by apache.
the class Registries method resolve.
private void resolve(ObjectClass objectClass, List<Throwable> errors) {
// This set is used to avoid having more than one error
// for an ObjectClass. It's mandatory when processing
// the Superiors, as they may be broken and referenced more than once.
Set<String> processed = new HashSet<>();
// Store the ObjectClass itself in the processed, to avoid cycle
processed.add(objectClass.getOid());
// Call the recursive method, as we may have superiors to deal with
resolveRecursive(objectClass, processed, errors);
// Check that the MAY and MUST AT are consistent (no AT in MAY and in MUST
// in one of its superior
List<AttributeType> musts = getMustRecursive(new ArrayList<AttributeType>(), new HashSet<ObjectClass>(), objectClass);
if (musts != null) {
for (AttributeType may : objectClass.getMayAttributeTypes()) {
if (musts.contains(may)) {
// This is not allowed.
LdapSchemaException ldapSchemaException = new LdapSchemaException(LdapSchemaExceptionCodes.OC_DUPLICATE_AT_IN_MAY_AND_MUST);
ldapSchemaException.setSourceObject(objectClass);
ldapSchemaException.setOtherObject(may);
errors.add(ldapSchemaException);
}
}
}
}
use of org.apache.directory.api.ldap.model.schema.ObjectClass 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.ObjectClass in project directory-ldap-api by apache.
the class Registries method check.
/**
* Check the registries for invalid relations. This check stops at the first error.
*
* @return true if the Registries is consistent, false otherwise
*/
public boolean check() {
// Check the Syntaxes : check for a SyntaxChecker
LOG.debug("Checking Syntaxes");
for (LdapSyntax syntax : ldapSyntaxRegistry) {
// Check that each Syntax has a SyntaxChecker
if (syntax.getSyntaxChecker() == null) {
LOG.debug("The Syntax {} has no SyntaxChecker", syntax);
return false;
}
if (!syntaxCheckerRegistry.contains(syntax.getSyntaxChecker().getOid())) {
LOG.debug("Cannot find the SyntaxChecker {} for the Syntax {}", syntax.getSyntaxChecker().getOid(), syntax);
return false;
}
// Check the references : Syntax -> SyntaxChecker and SyntaxChecker -> Syntax
if (!checkReferences(syntax, syntax.getSyntaxChecker(), "SyntaxChecker")) {
return false;
}
}
// Check the MatchingRules : check for a Normalizer, a Comparator and a Syntax
LOG.debug("Checking MatchingRules...");
for (MatchingRule matchingRule : matchingRuleRegistry) {
// Check that each MatchingRule has a Normalizer
if (matchingRule.getNormalizer() == null) {
LOG.debug("The MatchingRule {} has no Normalizer", matchingRule);
return false;
}
// Check that each MatchingRule has a Normalizer
if (!normalizerRegistry.contains(matchingRule.getNormalizer().getOid())) {
LOG.debug("Cannot find the Normalizer {} for the MatchingRule {}", matchingRule.getNormalizer().getOid(), matchingRule);
return false;
}
// Check that each MatchingRule has a Comparator
if (matchingRule.getLdapComparator() == null) {
LOG.debug("The MatchingRule {} has no Comparator", matchingRule);
return false;
}
if (!comparatorRegistry.contains(matchingRule.getLdapComparator().getOid())) {
LOG.debug("Cannot find the Comparator {} for the MatchingRule {}", matchingRule.getLdapComparator().getOid(), matchingRule);
return false;
}
// Check that each MatchingRule has a Syntax
if (matchingRule.getSyntax() == null) {
LOG.debug("The MatchingRule {} has no Syntax", matchingRule);
return false;
}
if (!ldapSyntaxRegistry.contains(matchingRule.getSyntax().getOid())) {
LOG.debug("Cannot find the Syntax {} for the MatchingRule {}", matchingRule.getSyntax().getOid(), matchingRule);
return false;
}
// Check the references : MR -> S and S -> MR
if (!checkReferences(matchingRule, matchingRule.getSyntax(), "Syntax")) {
return false;
}
// Check the references : MR -> N
if (!checkReferences(matchingRule, matchingRule.getNormalizer(), "Normalizer")) {
return false;
}
// Check the references : MR -> C and C -> MR
if (!checkReferences(matchingRule, matchingRule.getLdapComparator(), "Comparator")) {
return false;
}
}
// Check the ObjectClasses : check for MAY, MUST, SUPERIORS
LOG.debug("Checking ObjectClasses...");
for (ObjectClass objectClass : objectClassRegistry) {
// Check that each ObjectClass has all the MAY AttributeTypes
if (objectClass.getMayAttributeTypes() != null) {
for (AttributeType may : objectClass.getMayAttributeTypes()) {
if (!attributeTypeRegistry.contains(may.getOid())) {
LOG.debug("Cannot find the AttributeType {} for the ObjectClass {} MAY", may, objectClass);
return false;
}
// Check the references : OC -> AT and AT -> OC (MAY)
if (!checkReferences(objectClass, may, "AttributeType")) {
return false;
}
}
}
// Check that each ObjectClass has all the MUST AttributeTypes
if (objectClass.getMustAttributeTypes() != null) {
for (AttributeType must : objectClass.getMustAttributeTypes()) {
if (!attributeTypeRegistry.contains(must.getOid())) {
LOG.debug("Cannot find the AttributeType {} for the ObjectClass {} MUST", must, objectClass);
return false;
}
// Check the references : OC -> AT and AT -> OC (MUST)
if (!checkReferences(objectClass, must, "AttributeType")) {
return false;
}
}
}
// Check that each ObjectClass has all the SUPERIORS ObjectClasses
if (objectClass.getSuperiors() != null) {
for (ObjectClass superior : objectClass.getSuperiors()) {
if (!objectClassRegistry.contains(objectClass.getOid())) {
LOG.debug("Cannot find the ObjectClass {} for the ObjectClass {} SUPERIORS", superior, objectClass);
return false;
}
// Check the references : OC -> OC and OC -> OC (SUPERIORS)
if (!checkReferences(objectClass, superior, "ObjectClass")) {
return false;
}
}
}
}
// Check the AttributeTypes : check for MatchingRules, Syntaxes
LOG.debug("Checking AttributeTypes...");
for (AttributeType attributeType : attributeTypeRegistry) {
// Check that each AttributeType has a SYNTAX
if (attributeType.getSyntax() == null) {
LOG.debug("The AttributeType {} has no Syntax", attributeType);
return false;
}
if (!ldapSyntaxRegistry.contains(attributeType.getSyntax().getOid())) {
LOG.debug("Cannot find the Syntax {} for the AttributeType {}", attributeType.getSyntax().getOid(), attributeType);
return false;
}
// Check the references for AT -> S and S -> AT
if (!checkReferences(attributeType, attributeType.getSyntax(), "AttributeType")) {
return false;
}
// Check the EQUALITY MatchingRule
if (attributeType.getEquality() != null) {
if (!matchingRuleRegistry.contains(attributeType.getEquality().getOid())) {
LOG.debug("Cannot find the MatchingRule {} for the AttributeType {}", attributeType.getEquality().getOid(), attributeType);
return false;
}
// Check the references for AT -> MR and MR -> AT
if (!checkReferences(attributeType, attributeType.getEquality(), "AttributeType")) {
return false;
}
}
// Check the ORDERING MatchingRule
if (attributeType.getOrdering() != null) {
if (!matchingRuleRegistry.contains(attributeType.getOrdering().getOid())) {
LOG.debug("Cannot find the MatchingRule {} for the AttributeType {}", attributeType.getOrdering().getOid(), attributeType);
return false;
}
// Check the references for AT -> MR and MR -> AT
if (!checkReferences(attributeType, attributeType.getOrdering(), "AttributeType")) {
return false;
}
}
// Check the SUBSTR MatchingRule
if (attributeType.getSubstring() != null) {
if (!matchingRuleRegistry.contains(attributeType.getSubstring().getOid())) {
LOG.debug("Cannot find the MatchingRule {} for the AttributeType {}", attributeType.getSubstring().getOid(), attributeType);
return false;
}
// Check the references for AT -> MR and MR -> AT
if (!checkReferences(attributeType, attributeType.getSubstring(), "AttributeType")) {
return false;
}
}
// Check the SUP
if (attributeType.getSuperior() != null) {
AttributeType superior = attributeType.getSuperior();
if (!attributeTypeRegistry.contains(superior.getOid())) {
LOG.debug("Cannot find the AttributeType {} for the AttributeType {} SUPERIOR", superior, attributeType);
return false;
}
// Check the references : AT -> AT and AT -> AT (SUPERIOR)
if (!checkReferences(attributeType, superior, "AttributeType")) {
return false;
}
}
}
return true;
}
use of org.apache.directory.api.ldap.model.schema.ObjectClass in project directory-ldap-api by apache.
the class DefaultObjectClassRegistry method descendants.
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
@Override
public Iterator<ObjectClass> descendants(String ancestorId) throws LdapException {
try {
String oid = getOidByName(ancestorId);
Set<ObjectClass> descendants = oidToDescendants.get(oid);
if (descendants == null) {
return Collections.EMPTY_SET.iterator();
}
return descendants.iterator();
} catch (LdapException ne) {
throw new LdapNoSuchAttributeException(ne.getMessage(), ne);
}
}
Aggregations