public static String sanitize(String name) {
        try {
            String encoded = URLEncoder.encode(name, StandardCharsets.UTF_8.name());
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < encoded.length(); i++) {
                char c = encoded.charAt(i);
                if (c == '*') {         // Metric ObjectName treats * as pattern
                    builder.append("%2A");
                } else if (c == '+') {  // Space URL-encoded as +, replace with percent encoding
                    builder.append("%20");
                } else {
                    builder.append(c);
                }
            }
            return builder.toString();
        } catch (UnsupportedEncodingException e) {
            throw new KafkaException(e);
        }
    }
@Test
    public void testSanitize() {
        String principal = "CN=Some characters !@#$%&*()_-+=';:,/~";
        String sanitizedPrincipal = Sanitizer.sanitize(principal);
        assertTrue(sanitizedPrincipal.replace('%', '_').matches("[a-zA-Z0-9\\._\\-]+"));
        assertEquals(principal, Sanitizer.desanitize(sanitizedPrincipal));
    }