use of org.apache.directory.api.ldap.model.schema.Normalizer in project directory-ldap-api by apache.
the class TestEntryUtils method getBytesAttributeType.
/* No protection */
static AttributeType getBytesAttributeType() {
MutableAttributeType attributeType = new MutableAttributeType("1.2");
LdapSyntax syntax = new LdapSyntax("1.2.1", "", true);
syntax.setSyntaxChecker(new SyntaxChecker("1.2.1") {
/**
* The mandatory serialVersionUID field
*/
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 Normalizer("1.1.1") {
/**
* The mandatory serialVersionUID field
*/
public static final long serialVersionUID = 1L;
public String normalize(String value) throws LdapException {
return normalize(value, 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));
}
});
attributeType.setEquality(matchingRule);
attributeType.setSyntax(syntax);
return attributeType;
}
use of org.apache.directory.api.ldap.model.schema.Normalizer in project directory-ldap-api by apache.
the class Value method computeNormValue.
/**
* Compute the normalized value
*
* @throws LdapException If we were'nt able to normalize the value
*/
private void computeNormValue() throws LdapException {
if (upValue == null) {
return;
}
Normalizer normalizer;
// We should have a Equality MatchingRule
MatchingRule equality = attributeType.getEquality();
if (equality == null) {
// Let's try with the Substring MatchingRule
MatchingRule subString = attributeType.getSubstring();
if (subString == null) {
// last chance : ordering matching rule
MatchingRule ordering = attributeType.getOrdering();
if (ordering == null) {
// Ok, no luck. Use a NoOp normalizer
normalizer = new NoOpNormalizer();
} else {
normalizer = ordering.getNormalizer();
}
} else {
normalizer = subString.getNormalizer();
}
} else {
normalizer = equality.getNormalizer();
}
if (normalizer == null) {
throw new IllegalArgumentException(I18n.err(I18n.ERR_13220_NO_NORMALIZER));
}
// Now, normalize the upValue
normValue = normalizer.normalize(upValue);
}
use of org.apache.directory.api.ldap.model.schema.Normalizer in project directory-ldap-api by apache.
the class Value method equals.
/**
* We compare two values using their Comparator, if any.
*
* @see Object#equals(Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof String) {
String other = (String) obj;
if (!isHR) {
return false;
}
if (attributeType == null) {
if (upValue != null) {
return upValue.equals(other);
} else {
return obj == null;
}
} else {
// We have an AttributeType, we use the associated comparator
try {
LdapComparator<String> comparator = (LdapComparator<String>) getLdapComparator();
Normalizer normalizer = null;
if (attributeType.getEquality() != null) {
normalizer = attributeType.getEquality().getNormalizer();
}
if (normalizer == null) {
if (comparator == null) {
return normValue.equals(other);
} else {
return comparator.compare(normValue, other) == 0;
}
}
String thisNormValue = normValue;
String otherNormValue = normalizer.normalize(other);
// Compare normalized values
if (comparator == null) {
return thisNormValue.equals(otherNormValue);
} else {
return comparator.compare(thisNormValue, otherNormValue) == 0;
}
} catch (LdapException ne) {
return false;
}
}
}
if (!(obj instanceof Value)) {
return false;
}
Value other = (Value) obj;
// Check if the values aren't of the same type
if (isHR != other.isHR) {
// Both values must be HR or not HR
return false;
}
if (!isHR) {
// Shortcut for binary values
return Arrays.equals(bytes, other.bytes);
}
// HR values
if (bytes == null) {
return other.bytes == null;
}
// Special case
if (other.bytes == null) {
return false;
}
// Not null, but empty. We try to avoid a spurious String Preparation
if (bytes.length == 0) {
return other.bytes.length == 0;
} else if (other.bytes.length == 0) {
return false;
}
// Ok, now, let's see if we have an AttributeType at all. If both have one,
// and if they aren't equal, then we get out. If one of them has an AttributeType and
// not the other, we will assume that this is the AttributeType to use.
MatchingRule equalityMR;
if (attributeType == null) {
if (other.attributeType != null) {
// Use the Other value AT
equalityMR = other.attributeType.getEquality();
// We may not have an Equality MR, and in tjis case, we compare the bytes
if (equalityMR == null) {
return Arrays.equals(bytes, other.bytes);
}
LdapComparator<Object> ldapComparator = equalityMR.getLdapComparator();
if (ldapComparator == null) {
// This is an error !
LOG.error(I18n.err(I18n.ERR_13249_NO_COMPARATOR_FOR_AT, other.attributeType));
return false;
}
return ldapComparator.compare(normValue, other.normValue) == 0;
} else {
// or the bytes otherwise.
if (upValue != null) {
return upValue.equals(other.upValue);
} else {
return Arrays.equals(bytes, other.bytes);
}
}
} else {
if (other.attributeType != null) {
// Both attributeType must be equal
if (!attributeType.equals(other.attributeType)) {
return false;
}
// We have an AttributeType, we use the associated comparator
try {
LdapComparator<String> comparator = (LdapComparator<String>) getLdapComparator();
if (other.attributeType.getEquality() == null) {
// No equality ? Default to comparing using a String comparator
return stringComparator.compare(normValue, other.normValue) == 0;
}
Normalizer normalizer = other.attributeType.getEquality().getNormalizer();
if (normalizer == null) {
if (comparator == null) {
return normValue.equals(other.normValue);
} else {
return comparator.compare(normValue, other.normValue) == 0;
}
}
String thisNormValue = normalizer.normalize(normValue);
// Compare normalized values
if (comparator == null) {
return thisNormValue.equals(other.normValue);
} else {
return comparator.compare(thisNormValue, other.normValue) == 0;
}
} catch (LdapException ne) {
return false;
}
}
// No attributeType
if (normValue == null) {
return other.normValue == null;
} else {
return normValue.equals(other.normValue);
}
}
}
use of org.apache.directory.api.ldap.model.schema.Normalizer in project directory-ldap-api by apache.
the class DefaultSchemaManager method addNormalizers.
/**
* Add all the Schema's MatchingRuleUses
*/
// Not yet implemented, but may be used
// @SuppressWarnings("PMD.UnusedFormalParameter")
// private void addMatchingRuleUses( Schema schema, Registries registries ) throws LdapException, IOException
// {
// if ( !schema.getSchemaLoader().loadMatchingRuleUses( schema ).isEmpty() )
// {
// throw new NotImplementedException( I18n.err( I18n.ERR_11005 ) );
// }
// // for ( Entry entry : schema.getSchemaLoader().loadMatchingRuleUses( schema ) )
// // {
// // throw new NotImplementedException( I18n.err( I18n.ERR_11005 ) );
// // }
// }
/**
* Add all the Schema's NameForms
*/
// Not yet implemented, but may be used
// @SuppressWarnings("PMD.UnusedFormalParameter")
// private void addNameForms( Schema schema, Registries registries ) throws LdapException, IOException
// {
// if ( !schema.getSchemaLoader().loadNameForms( schema ).isEmpty() )
// {
// throw new NotImplementedException( I18n.err( I18n.ERR_11006 ) );
// }
// }
/**
* Add all the Schema's Normalizers
*/
private void addNormalizers(Schema schema, Registries registries) throws LdapException, IOException {
if (schema.getSchemaLoader() == null) {
return;
}
for (Entry entry : schema.getSchemaLoader().loadNormalizers(schema)) {
Normalizer normalizer = factory.getNormalizer(this, entry, registries, schema.getSchemaName());
addSchemaObject(registries, normalizer, schema);
}
}
use of org.apache.directory.api.ldap.model.schema.Normalizer in project directory-ldap-api by apache.
the class SchemaEntityFactory method classLoadNormalizer.
/**
* Class load a normalizer instances
*/
private Normalizer classLoadNormalizer(SchemaManager schemaManager, String oid, String className, Attribute byteCode) throws LdapException {
// Try to class load the normalizer
Class<?> clazz;
Normalizer normalizer;
String byteCodeStr = StringConstants.EMPTY;
if (byteCode == null) {
try {
clazz = Class.forName(className);
} catch (ClassNotFoundException cnfe) {
LOG.error(I18n.err(I18n.ERR_16068_CANNOT_FIND_NORM_CTOR, className));
throw new LdapSchemaException(I18n.err(I18n.ERR_16069_CANNOT_FIND_NORM_CLASS, cnfe.getMessage()));
}
} else {
classLoader.setAttribute(byteCode);
try {
clazz = classLoader.loadClass(className);
} catch (ClassNotFoundException cnfe) {
LOG.error(I18n.err(I18n.ERR_16070_CANNOT_LOAD_NORM_CTOR, className));
throw new LdapSchemaException(I18n.err(I18n.ERR_16071_CANNOT_LOAD_NORM_CLASS, cnfe.getMessage()));
}
byteCodeStr = new String(Base64.encode(byteCode.getBytes()));
}
// Create the normalizer instance
try {
normalizer = (Normalizer) clazz.newInstance();
} catch (InstantiationException ie) {
LOG.error(I18n.err(I18n.ERR_16072_CANNOT_INST_NORM_CTOR_CLASS, className));
throw new LdapSchemaException(I18n.err(I18n.ERR_16073_CANNOT_INST_NORM_CLASS, ie.getMessage()));
} catch (IllegalAccessException iae) {
LOG.error(I18n.err(I18n.ERR_16074_CANNOT_ACCESS_NORM_CTOR_CLASS, className));
throw new LdapSchemaException(I18n.err(I18n.ERR_16075_CANNOT_ACCESS_NORM_CTOR, iae.getMessage()));
}
// Update the common fields
normalizer.setBytecode(byteCodeStr);
normalizer.setFqcn(className);
// Inject the new OID, as the loaded normalizer might have its own
normalizer.setOid(oid);
// Inject the SchemaManager for the normalizer who needs it
normalizer.setSchemaManager(schemaManager);
return normalizer;
}
Aggregations