use of org.apache.directory.api.ldap.model.schema.SyntaxChecker in project directory-ldap-api by apache.
the class SchemaEntityFactory method getSyntaxChecker.
/**
* {@inheritDoc}
*/
@Override
public SyntaxChecker getSyntaxChecker(SchemaManager schemaManager, Entry entry, Registries targetRegistries, String schemaName) throws LdapException {
checkEntry(entry, SchemaConstants.SYNTAX_CHECKER);
// The SyntaxChecker OID
String oid = getOid(entry, SchemaConstants.SYNTAX_CHECKER, schemaManager.isStrict());
// Get the schema
if (!schemaManager.isSchemaLoaded(schemaName)) {
// The schema is not loaded. We can't create the requested Normalizer
String msg = I18n.err(I18n.ERR_16019_CANNOT_ADD_SC, entry.getDn().getName(), schemaName);
LOG.warn(msg);
throw new LdapUnwillingToPerformException(ResultCodeEnum.UNWILLING_TO_PERFORM, msg);
}
Schema schema = getSchema(schemaName, targetRegistries);
if (schema == null) {
// The schema is disabled. We still have to update the backend
String msg = I18n.err(I18n.ERR_16020_CANNOT_ADD_SC_IN_REGISTRY, entry.getDn().getName(), schemaName);
LOG.info(msg);
schema = schemaManager.getLoadedSchema(schemaName);
}
// The FQCN
String className = getFqcn(entry, SchemaConstants.SYNTAX_CHECKER);
// The ByteCode
Attribute byteCode = entry.get(MetaSchemaConstants.M_BYTECODE_AT);
try {
// Class load the syntaxChecker
SyntaxChecker syntaxChecker = classLoadSyntaxChecker(schemaManager, oid, className, byteCode);
// Update the common fields
setSchemaObjectProperties(syntaxChecker, entry, schema);
// return the resulting syntaxChecker
return syntaxChecker;
} catch (Exception e) {
throw new LdapUnwillingToPerformException(ResultCodeEnum.UNWILLING_TO_PERFORM, e.getMessage(), e);
}
}
use of org.apache.directory.api.ldap.model.schema.SyntaxChecker in project directory-ldap-api by apache.
the class EntryUtils method getCaseIgnoringAttributeNoNumbersType.
/* no protection*/
static AttributeType getCaseIgnoringAttributeNoNumbersType() {
MutableAttributeType attributeType = new MutableAttributeType("1.1.3.1");
LdapSyntax syntax = new LdapSyntax("1.1.1.1", "", true);
syntax.setSyntaxChecker(new SyntaxChecker("1.1.2.1") {
public static final long serialVersionUID = 1L;
public boolean isValidSyntax(Object value) {
if (value == null) {
return true;
}
if (!(value instanceof String)) {
return false;
}
String strval = (String) value;
for (char c : strval.toCharArray()) {
if (Character.isDigit(c)) {
return false;
}
}
return true;
}
});
MutableMatchingRule matchingRule = new MutableMatchingRule("1.1.2.1");
matchingRule.setSyntax(syntax);
matchingRule.setLdapComparator(new LdapComparator<String>(matchingRule.getOid()) {
public static final long serialVersionUID = 1L;
public int compare(String o1, String o2) {
return (o1 == null ? (o2 == null ? 0 : -1) : (o2 == null ? 1 : o1.compareTo(o2)));
}
});
Normalizer normalizer = 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 {
return Strings.toLowerCaseAscii(value);
}
};
matchingRule.setNormalizer(normalizer);
attributeType.setEquality(matchingRule);
attributeType.setSyntax(syntax);
return attributeType;
}
use of org.apache.directory.api.ldap.model.schema.SyntaxChecker in project directory-ldap-api by apache.
the class EntryUtils method getBytesAttributeType.
/* No protection */
static AttributeType getBytesAttributeType() {
MutableAttributeType attributeType = new MutableAttributeType("1.2");
LdapSyntax syntax = new LdapSyntax("1.2.1", "", false);
syntax.setSyntaxChecker(new SyntaxChecker("1.2.1") {
public static final long serialVersionUID = 1L;
public boolean isValidSyntax(Object value) {
return (value == null) || (((byte[]) value).length < 5);
}
});
MutableMatchingRule matchingRule = new MutableMatchingRule("1.2.2");
matchingRule.setSyntax(syntax);
matchingRule.setLdapComparator(new ByteArrayComparator("1.2.2"));
matchingRule.setNormalizer(new NoOpNormalizer("1.1.1"));
attributeType.setEquality(matchingRule);
attributeType.setSyntax(syntax);
return attributeType;
}
use of org.apache.directory.api.ldap.model.schema.SyntaxChecker in project directory-ldap-api by apache.
the class StringValueAttributeTypeTest method testConstrainedString.
/**
* Presumes an attribute which constrains it's values to some constant
* strings: LOW, MEDIUM, HIGH. Normalization does nothing. MatchingRules
* are exact case matching.
*
* @throws Exception on errors
*/
@Test
public void testConstrainedString() throws LdapInvalidAttributeValueException {
s.setSyntaxChecker(new SyntaxChecker("1.1.1.1") {
public static final long serialVersionUID = 1L;
public boolean isValidSyntax(Object value) {
if (value instanceof String) {
String strval = (String) value;
return strval.equals("HIGH") || strval.equals("LOW") || strval.equals("MEDIUM");
}
return false;
}
});
mr.setSyntax(s);
mr.setNormalizer(new NoOpNormalizer(mr.getOid()));
at.setEquality(mr);
at.setSyntax(s);
// check that normalization and syntax checks work as expected
Value value = new Value(at, "HIGH");
assertEquals(value.getValue(), value.getValue());
try {
new Value(at, "high");
fail();
} catch (LdapInvalidAttributeValueException liave) {
// expected
}
// create a bunch to best tested for equals and in containers
Value v0 = new Value(at, "LOW");
Value v1 = new Value(at, "LOW");
Value v2 = new Value(at, "MEDIUM");
Value v3 = new Value(at, "HIGH");
// check equals
assertTrue(v0.equals(v1));
assertTrue(v1.equals(v0));
assertEquals(0, v0.compareTo(v1));
assertFalse(v2.equals(v3));
assertFalse(v3.equals(v2));
assertTrue(v2.compareTo(v3) > 0);
assertTrue(v3.compareTo(v2) < 0);
// add all except v1 and v5 to a set
HashSet<Value> set = new HashSet<Value>();
set.add(v0);
set.add(v2);
set.add(v3);
// check contains method
assertTrue("since v1.equals( v0 ) and v0 was added then this should be true", set.contains(v1));
// check ordering based on the comparator
List<Value> list = new ArrayList<Value>();
list.add(v1);
list.add(v3);
list.add(v0);
list.add(v2);
Collections.sort(list);
// High, low, low, medium
assertTrue("since v0 equals v1 either could be at index 0 & 1", list.get(0).equals(v3));
assertTrue("since v0 equals v1 either could be at index 0 & 1", list.get(1).equals(v0));
assertTrue("since v2 \"MEDIUM\" should be at index 2", list.get(2).equals(v1));
assertTrue("since v3 \"HIGH\" should be at index 3", list.get(3).equals(v2));
assertEquals(4, list.size());
}
Aggregations