/** * Applies the specified parameter to this filter template and returns * the resulting LDAP filter string. Special filter characters in the * parameter are escaped. * * <p>Example: * * <p>Template: "(|(uid=%u)(mail=%u))" * * <p>Placeholder: "%u" * * <p>Parameter: "alice" * * <p>Resulting filter: "(|(uid=alice)(mail=alice))" * * @param param The parameter to apply. Must not be {@code null}. * * @return The resulting LDAP filter string. */ public String apply(final String param) { // Escape special chars String sanitizedParam = Filter.encodeValue(param); return template.replaceAll(placeholder, sanitizedParam); }
/** * Encodes the provided value into a form suitable for use as the assertion * value in the string representation of a search filter. Parentheses, * asterisks, backslashes, null characters, and any non-ASCII characters will * be escaped using a backslash before the hexadecimal representation of each * byte in the character to escape. * * @param value The value to be encoded. It must not be {@code null}. * * @return The encoded representation of the provided string. */ public static String encodeValue(final byte[]value) { ensureNotNull(value); final StringBuilder buffer = new StringBuilder(); encodeValue(new ASN1OctetString(value), buffer); return buffer.toString(); }
/** * Encodes the provided value into a form suitable for use as the assertion * value in the string representation of a search filter. Parentheses, * asterisks, backslashes, null characters, and any non-ASCII characters will * be escaped using a backslash before the hexadecimal representation of each * byte in the character to escape. * * @param value The value to be encoded. It must not be {@code null}. * * @return The encoded representation of the provided string. */ public static String encodeValue(final byte[]value) { ensureNotNull(value); final StringBuilder buffer = new StringBuilder(); encodeValue(new ASN1OctetString(value), buffer); return buffer.toString(); }
/** * Encodes the provided value into a form suitable for use as the assertion * value in the string representation of a search filter. Parentheses, * asterisks, backslashes, null characters, and any non-ASCII characters will * be escaped using a backslash before the hexadecimal representation of each * byte in the character to escape. * * @param value The value to be encoded. It must not be {@code null}. * * @return The encoded representation of the provided string. */ public static String encodeValue(final String value) { ensureNotNull(value); final StringBuilder buffer = new StringBuilder(); encodeValue(new ASN1OctetString(value), buffer); return buffer.toString(); }
/** * Encodes the provided value into a form suitable for use as the assertion * value in the string representation of a search filter. Parentheses, * asterisks, backslashes, null characters, and any non-ASCII characters will * be escaped using a backslash before the hexadecimal representation of each * byte in the character to escape. * * @param value The value to be encoded. It must not be {@code null}. * * @return The encoded representation of the provided string. */ public static String encodeValue(final String value) { ensureNotNull(value); final StringBuilder buffer = new StringBuilder(); encodeValue(new ASN1OctetString(value), buffer); return buffer.toString(); }
buffer.append(attrName); buffer.append('='); encodeValue(assertionValue, buffer); buffer.append(')'); break; encodeValue(subInitial, buffer); encodeValue(s, buffer); buffer.append('*'); encodeValue(subFinal, buffer); buffer.append(attrName); buffer.append(">="); encodeValue(assertionValue, buffer); buffer.append(')'); break; buffer.append(attrName); buffer.append("<="); encodeValue(assertionValue, buffer); buffer.append(')'); break; buffer.append(attrName); buffer.append("~="); encodeValue(assertionValue, buffer); buffer.append(')');
buffer.append(attrName); buffer.append('='); encodeValue(assertionValue, buffer); buffer.append(')'); break; encodeValue(subInitial, buffer); encodeValue(s, buffer); buffer.append('*'); encodeValue(subFinal, buffer); buffer.append(attrName); buffer.append(">="); encodeValue(assertionValue, buffer); buffer.append(')'); break; buffer.append(attrName); buffer.append("<="); encodeValue(assertionValue, buffer); buffer.append(')'); break; buffer.append(attrName); buffer.append("~="); encodeValue(assertionValue, buffer); buffer.append(')');
buffer.append(toLowerCase(attrName)); buffer.append('='); encodeValue(mr.normalize(assertionValue), buffer); buffer.append(')'); break; encodeValue(mr.normalizeSubstring(subInitial, MatchingRule.SUBSTRING_TYPE_SUBINITIAL), buffer); encodeValue(mr.normalizeSubstring(s, MatchingRule.SUBSTRING_TYPE_SUBANY), buffer); buffer.append('*'); encodeValue(mr.normalizeSubstring(subFinal, MatchingRule.SUBSTRING_TYPE_SUBFINAL), buffer); buffer.append(toLowerCase(attrName)); buffer.append(">="); encodeValue(mr.normalize(assertionValue), buffer); buffer.append(')'); break; buffer.append(toLowerCase(attrName)); buffer.append("<="); encodeValue(mr.normalize(assertionValue), buffer); buffer.append(')'); break; buffer.append(toLowerCase(attrName));
buffer.append(toLowerCase(attrName)); buffer.append('='); encodeValue(mr.normalize(assertionValue), buffer); buffer.append(')'); break; encodeValue(mr.normalizeSubstring(subInitial, MatchingRule.SUBSTRING_TYPE_SUBINITIAL), buffer); encodeValue(mr.normalizeSubstring(s, MatchingRule.SUBSTRING_TYPE_SUBANY), buffer); buffer.append('*'); encodeValue(mr.normalizeSubstring(subFinal, MatchingRule.SUBSTRING_TYPE_SUBFINAL), buffer); buffer.append(toLowerCase(attrName)); buffer.append(">="); encodeValue(mr.normalize(assertionValue), buffer); buffer.append(')'); break; buffer.append(toLowerCase(attrName)); buffer.append("<="); encodeValue(mr.normalize(assertionValue), buffer); buffer.append(')'); break; buffer.append(toLowerCase(attrName));
final String sanitizedAttribute = Filter.encodeValue(attribute);