use of com.unboundid.ldap.matchingrules.MatchingRule in project ldapsdk by pingidentity.
the class Attribute method removeValues.
/**
* Creates a new attribute containing all of the values of the first attribute
* that are not contained in the second attribute. Any values contained in
* the second attribute that are not contained in the first will be ignored.
* The names of the provided attributes must be the same.
*
* @param attr1 The attribute from which to remove the values. It
* must not be {@code null}.
* @param attr2 The attribute containing the values to remove. It
* must not be {@code null}.
* @param matchingRule The matching rule to use to locate matching values.
* It may be {@code null} if the matching rule
* associated with the first attribute should be used.
*
* @return A new attribute containing all of the values of the first
* attribute not contained in the second. It may contain zero values
* if all the values of the first attribute were also contained in
* the second.
*/
@NotNull()
public static Attribute removeValues(@NotNull final Attribute attr1, @NotNull final Attribute attr2, @Nullable final MatchingRule matchingRule) {
Validator.ensureNotNull(attr1, attr2);
final String name = attr1.name;
Validator.ensureTrue(name.equalsIgnoreCase(attr2.name));
final MatchingRule mr;
if (matchingRule == null) {
mr = attr1.matchingRule;
} else {
mr = matchingRule;
}
final ArrayList<ASN1OctetString> newValues = new ArrayList<>(Arrays.asList(attr1.values));
final Iterator<ASN1OctetString> iterator = newValues.iterator();
while (iterator.hasNext()) {
if (attr2.hasValue(iterator.next(), mr)) {
iterator.remove();
}
}
final ASN1OctetString[] newValueArray = new ASN1OctetString[newValues.size()];
newValues.toArray(newValueArray);
return new Attribute(name, mr, newValueArray);
}
use of com.unboundid.ldap.matchingrules.MatchingRule in project ldapsdk by pingidentity.
the class Attribute method mergeAttributes.
/**
* Creates a new attribute containing the merged values of the provided
* attributes. Any duplicate values will only be present once in the
* resulting attribute. The names of the provided attributes must be the
* same.
*
* @param attr1 The first attribute containing the values to merge.
* It must not be {@code null}.
* @param attr2 The second attribute containing the values to merge.
* It must not be {@code null}.
* @param matchingRule The matching rule to use to locate matching values.
* It may be {@code null} if the matching rule
* associated with the first attribute should be used.
*
* @return The new attribute containing the values of both of the
* provided attributes.
*/
@NotNull()
public static Attribute mergeAttributes(@NotNull final Attribute attr1, @NotNull final Attribute attr2, @Nullable final MatchingRule matchingRule) {
Validator.ensureNotNull(attr1, attr2);
final String name = attr1.name;
Validator.ensureTrue(name.equalsIgnoreCase(attr2.name));
final MatchingRule mr;
if (matchingRule == null) {
mr = attr1.matchingRule;
} else {
mr = matchingRule;
}
ASN1OctetString[] mergedValues = new ASN1OctetString[attr1.values.length + attr2.values.length];
System.arraycopy(attr1.values, 0, mergedValues, 0, attr1.values.length);
int pos = attr1.values.length;
for (final ASN1OctetString attr2Value : attr2.values) {
if (!attr1.hasValue(attr2Value, mr)) {
mergedValues[pos++] = attr2Value;
}
}
if (pos != mergedValues.length) {
// This indicates that there were duplicate values.
final ASN1OctetString[] newMergedValues = new ASN1OctetString[pos];
System.arraycopy(mergedValues, 0, newMergedValues, 0, pos);
mergedValues = newMergedValues;
}
return new Attribute(name, mr, mergedValues);
}
use of com.unboundid.ldap.matchingrules.MatchingRule in project ldapsdk by pingidentity.
the class RDN method appendNormalizedValue.
/**
* Appends a normalized string representation of the provided attribute value
* to the given buffer.
*
* @param buffer The buffer to which the value should be appended.
* It must not be {@code null}.
* @param attributeName The name of the attribute whose value is to be
* normalized. It must not be {@code null}.
* @param value The value to be normalized. It must not be
* {@code null}.
* @param schema The schema to use to generate the normalized
* representation of the value. It may be {@code null}
* if no schema is available.
*/
static void appendNormalizedValue(@NotNull final StringBuilder buffer, @NotNull final String attributeName, @NotNull final ASN1OctetString value, @Nullable final Schema schema) {
final MatchingRule matchingRule = MatchingRule.selectEqualityMatchingRule(attributeName, schema);
ASN1OctetString rawNormValue;
try {
rawNormValue = matchingRule.normalize(value);
} catch (final Exception e) {
Debug.debugException(e);
rawNormValue = new ASN1OctetString(StaticUtils.toLowerCase(value.stringValue()));
}
final String valueString = rawNormValue.stringValue();
final int length = valueString.length();
for (int i = 0; i < length; i++) {
final char c = valueString.charAt(i);
switch(c) {
case '\\':
case '=':
case '"':
case '+':
case ',':
case ';':
case '<':
case '>':
buffer.append('\\');
buffer.append(c);
break;
case '#':
// Escape the octothorpe only if it's the first character.
if (i == 0) {
buffer.append("\\#");
} else {
buffer.append('#');
}
break;
case ' ':
// Escape this space only if it's the first or last character.
if ((i == 0) || ((i + 1) == length)) {
buffer.append("\\ ");
} else {
buffer.append(' ');
}
break;
default:
// to encode the Unicode character.
if ((c >= ' ') && (c <= '~')) {
buffer.append(c);
} else if (Character.isHighSurrogate(c)) {
if (((i + 1) < length) && Character.isLowSurrogate(valueString.charAt(i + 1))) {
final char c2 = valueString.charAt(++i);
final int codePoint = Character.toCodePoint(c, c2);
StaticUtils.hexEncode(codePoint, buffer);
} else {
// This should never happen.
StaticUtils.hexEncode(c, buffer);
}
} else {
StaticUtils.hexEncode(c, buffer);
}
break;
}
}
}
use of com.unboundid.ldap.matchingrules.MatchingRule in project ldapsdk by pingidentity.
the class RDNNameValuePair method compareTo.
/**
* Retrieves an integer value that represents the order in which this RDN
* name-value pair should be placed in relation to the provided RDN name-value
* pair in a sorted list.
*
* @param p The RDN name-value pair to be ordered relative to this RDN
* name-value pair. It must not be {@code null}.
*
* @return A negative integer if this RDN name-value pair should be ordered
* before the provided RDN name-value pair, a positive integer if
* this RDN name-value pair should be ordered after the provided RDN
* name-value pair, or zero if this RDN name-value pair is logically
* equivalent to the provided RDN name-value pair.
*/
@Override()
public int compareTo(@NotNull final RDNNameValuePair p) {
final String thisNormalizedName = getNormalizedAttributeName();
final String thatNormalizedName = p.getNormalizedAttributeName();
final int nameComparison = thisNormalizedName.compareTo(thatNormalizedName);
if (nameComparison != 0) {
return nameComparison;
}
try {
final MatchingRule matchingRule = MatchingRule.selectOrderingMatchingRule(attributeName, schema);
return matchingRule.compareValues(attributeValue, p.attributeValue);
} catch (final Exception e) {
Debug.debugException(e);
final String thisNormalizedString = toNormalizedString();
final String thatNormalizedString = p.toNormalizedString();
return thisNormalizedString.compareTo(thatNormalizedString);
}
}
use of com.unboundid.ldap.matchingrules.MatchingRule in project ldapsdk by pingidentity.
the class DefaultObjectEncoder method encodeValue.
/**
* Encodes the provided value to an LDAP attribute.
*
* @param type The type for the provided value.
* @param value The value for the field in the object to be encoded.
* @param name The name to use for the constructed attribute.
*
* @return The attribute containing the encoded representation of the
* provided field.
*
* @throws LDAPPersistException If a problem occurs while attempting to
* construct an attribute for the field.
*/
@NotNull()
private static Attribute encodeValue(@NotNull final Type type, @NotNull final Object value, @NotNull final String name) throws LDAPPersistException {
final TypeInfo typeInfo = new TypeInfo(type);
final Class<?> c = typeInfo.getBaseClass();
if (c.equals(AtomicInteger.class) || c.equals(AtomicLong.class) || c.equals(BigDecimal.class) || c.equals(BigInteger.class) || c.equals(Double.class) || c.equals(Double.TYPE) || c.equals(Float.class) || c.equals(Float.TYPE) || c.equals(Integer.class) || c.equals(Integer.TYPE) || c.equals(Long.class) || c.equals(Long.TYPE) || c.equals(Short.class) || c.equals(Short.TYPE) || c.equals(String.class) || c.equals(StringBuffer.class) || c.equals(StringBuilder.class) || c.equals(UUID.class) || c.equals(DN.class) || c.equals(Filter.class) || c.equals(LDAPURL.class) || c.equals(RDN.class)) {
final String syntaxOID = getSyntaxOID(c);
final MatchingRule matchingRule = MatchingRule.selectMatchingRuleForSyntax(syntaxOID);
return new Attribute(name, matchingRule, String.valueOf(value));
} else if (value instanceof URI) {
final URI uri = (URI) value;
return new Attribute(name, uri.toASCIIString());
} else if (value instanceof URL) {
final URL url = (URL) value;
return new Attribute(name, url.toExternalForm());
} else if (value instanceof byte[]) {
return new Attribute(name, OctetStringMatchingRule.getInstance(), (byte[]) value);
} else if (value instanceof char[]) {
return new Attribute(name, new String((char[]) value));
} else if (c.equals(Boolean.class) || c.equals(Boolean.TYPE)) {
final Boolean b = (Boolean) value;
final MatchingRule matchingRule = BooleanMatchingRule.getInstance();
if (b) {
return new Attribute(name, matchingRule, "TRUE");
} else {
return new Attribute(name, matchingRule, "FALSE");
}
} else if (c.equals(Date.class)) {
final Date d = (Date) value;
return new Attribute(name, GeneralizedTimeMatchingRule.getInstance(), StaticUtils.encodeGeneralizedTime(d));
} else if (typeInfo.isArray()) {
return encodeArray(typeInfo.getComponentType(), value, name);
} else if (typeInfo.isEnum()) {
final Enum<?> e = (Enum<?>) value;
return new Attribute(name, e.name());
} else if (Collection.class.isAssignableFrom(c)) {
return encodeCollection(typeInfo.getComponentType(), (Collection<?>) value, name);
} else if (Serializable.class.isAssignableFrom(c)) {
try {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(value);
oos.close();
return new Attribute(name, OctetStringMatchingRule.getInstance(), baos.toByteArray());
} catch (final Exception e) {
Debug.debugException(e);
throw new LDAPPersistException(ERR_DEFAULT_ENCODER_CANNOT_SERIALIZE.get(name, StaticUtils.getExceptionMessage(e)), e);
}
}
throw new LDAPPersistException(ERR_DEFAULT_ENCODER_UNSUPPORTED_TYPE.get(String.valueOf(type)));
}
Aggregations