public static String sanitizeSearchFilter(final String value, boolean acceptWildcard ) {


            StringBuilder result = new StringBuilder();

            for (int i=0; i< value.length(); i++) {

                char c = value.charAt(i);

                switch(c) {
                    case ' ':		result.append( i == 0 || i == (value.length()-1)? "\\20" : c );	break; // RFC 4514 (only at the beginning or end of the string)
                    case '!':		result.append("\\21");	break; // RFC 4515
                    case '"':		result.append("\\22");	break; // RFC 4514
                    case '#':		result.append( i == 0 ? "\\23" : c );	break; // RFC 4514 (only at the beginning of the string)
                    case '&':		result.append("\\26");	break; // RFC 4515
                    case '(':		result.append("\\28");	break; // RFC 4515
                    case ')':		result.append("\\29");	break; // RFC 4515
                    case '*':		result.append(acceptWildcard ? "*" : "\\2a");	break;  // RFC 4515
                    case '+':		result.append("\\2b");	break; // RFC 4514
                    case ',':		result.append("\\2c");	break; // RFC 4514
                    case '/':		result.append("\\2f");	break; // forward-slash  has special meaning to the JDNI. Escape it!
                    case ':':		result.append("\\3a");	break; // RFC 4515
                    case ';':		result.append("\\3b");	break; // RFC 4514
                    case '<':		result.append("\\3c");	break; // RFC 4514
                    case '>':		result.append("\\3e");	break; // RFC 4514
                    case '\\':		result.append("\\5c");	break; // RFC 4515
                    case '|':		result.append("\\7c");	break; // RFC 4515
                    case '~':		result.append("\\7e");	break; // RFC 4515
                    case '\u0000':	result.append("\\00");	break; // RFC 4515
                default:
                    if (c <= 0x7f) {
                        // regular 1-byte UTF-8 char
                        result.append(c);
                    }
                    else {
                        // higher-order 2, 3 and 4-byte UTF-8 chars
                        if ( JiveGlobals.getBooleanProperty( "ldap.encodeMultibyteCharacters", false ) )
                        {
                            byte[] utf8bytes = String.valueOf( c ).getBytes( StandardCharsets.UTF_8 );
                            for ( byte b : utf8bytes )
                            {
                                result.append( String.format( "\\%02x", b ) );
                            }
                        }
                        else
                        {
                            result.append(c);
                        }
                    }
                }
            }
            return result.toString();
    }
Escapes any special chars (RFC 4514/4515) from a string representing
a search filter assertion value, with the exception of the '*' wildcard sign

@param value The input string.
@param acceptWildcard {@code true} to accept wildcards, otherwise {@code false}

@return A assertion value string ready for insertion into a 
        search filter string.
xmppserver/src/main/java/org/jivesoftware/openfire/ldap/LdapManager.java