Search in sources :

Example 11 with MatchingRule

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);
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) ArrayList(java.util.ArrayList) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) MatchingRule(com.unboundid.ldap.matchingrules.MatchingRule) CaseIgnoreStringMatchingRule(com.unboundid.ldap.matchingrules.CaseIgnoreStringMatchingRule) NotNull(com.unboundid.util.NotNull)

Example 12 with MatchingRule

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);
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) MatchingRule(com.unboundid.ldap.matchingrules.MatchingRule) CaseIgnoreStringMatchingRule(com.unboundid.ldap.matchingrules.CaseIgnoreStringMatchingRule) NotNull(com.unboundid.util.NotNull)

Example 13 with MatchingRule

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;
        }
    }
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) MatchingRule(com.unboundid.ldap.matchingrules.MatchingRule)

Example 14 with MatchingRule

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);
    }
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) MatchingRule(com.unboundid.ldap.matchingrules.MatchingRule)

Example 15 with MatchingRule

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)));
}
Also used : Attribute(com.unboundid.ldap.sdk.Attribute) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) URI(java.net.URI) URL(java.net.URL) LDAPURL(com.unboundid.ldap.sdk.LDAPURL) Date(java.util.Date) InvocationTargetException(java.lang.reflect.InvocationTargetException) LDAPException(com.unboundid.ldap.sdk.LDAPException) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Filter(com.unboundid.ldap.sdk.Filter) BigInteger(java.math.BigInteger) Collection(java.util.Collection) UUID(java.util.UUID) RDN(com.unboundid.ldap.sdk.RDN) MatchingRule(com.unboundid.ldap.matchingrules.MatchingRule) GeneralizedTimeMatchingRule(com.unboundid.ldap.matchingrules.GeneralizedTimeMatchingRule) OctetStringMatchingRule(com.unboundid.ldap.matchingrules.OctetStringMatchingRule) BooleanMatchingRule(com.unboundid.ldap.matchingrules.BooleanMatchingRule) CaseIgnoreStringMatchingRule(com.unboundid.ldap.matchingrules.CaseIgnoreStringMatchingRule) NotNull(com.unboundid.util.NotNull)

Aggregations

MatchingRule (com.unboundid.ldap.matchingrules.MatchingRule)19 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)18 NotNull (com.unboundid.util.NotNull)12 CaseIgnoreStringMatchingRule (com.unboundid.ldap.matchingrules.CaseIgnoreStringMatchingRule)11 Attribute (com.unboundid.ldap.sdk.Attribute)8 LDAPException (com.unboundid.ldap.sdk.LDAPException)8 GeneralizedTimeMatchingRule (com.unboundid.ldap.matchingrules.GeneralizedTimeMatchingRule)7 RDN (com.unboundid.ldap.sdk.RDN)7 ArrayList (java.util.ArrayList)5 Date (java.util.Date)5 BooleanMatchingRule (com.unboundid.ldap.matchingrules.BooleanMatchingRule)4 OctetStringMatchingRule (com.unboundid.ldap.matchingrules.OctetStringMatchingRule)4 DN (com.unboundid.ldap.sdk.DN)4 DistinguishedNameMatchingRule (com.unboundid.ldap.matchingrules.DistinguishedNameMatchingRule)3 IntegerMatchingRule (com.unboundid.ldap.matchingrules.IntegerMatchingRule)3 ASN1Exception (com.unboundid.asn1.ASN1Exception)2 LDAPMessage (com.unboundid.ldap.protocol.LDAPMessage)2 ChangeLogEntry (com.unboundid.ldap.sdk.ChangeLogEntry)2 Control (com.unboundid.ldap.sdk.Control)2 Entry (com.unboundid.ldap.sdk.Entry)2