HttpUtil { public static String encodeUtf8UriParam(final String value) { if (value != null) { final StringBuilder safe = new StringBuilder(value.length() * 3); boolean changed = false; for (int i = 0; i < value.length(); i++) { char c = value.charAt(i); int b = c; if (b <= 31 || b == 127) { changed = true; continue; } if ("\"'$&+,/:;=?@<>#%{} |\\^~[]`".indexOf(c) != -1) { changed = true; safe.append('%'); char hex1 = Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, 16)); char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, 16)); safe.append(hex1); safe.append(hex2); } else { safe.append(c); } } if (changed) { return safe.toString(); } return value; } return null; }  static String dumpRequest(final HttpServletRequest request); static String decodeUtf8UriParam(final String value); static String encodeUtf8UriParam(final String value); static Map<String, List<String>> getParameters(final String requestURL,
                                                          final Set<String> pathVariables); static Map<String, List<String>> getParameters(final String requestURL,
                                                          final Set<String> pathVariables,
                                                          final boolean removeDuplicates); static Map<String, List<String>> getParameters(final HttpServletRequest request,
                                                          final Set<String> pathVariables); static Map<String, List<String>> getParameters(final HttpServletRequest request,
                                                          final Set<String> pathVariables,
                                                          final boolean removeDuplicates); static Map<String, List<String>> allParameters(final HttpServletRequest request,
                                                          final Set<String> pathVariables); static Map<String, List<String>> allParameters(final HttpServletRequest request,
                                                          final Set<String> pathVariables,
                                                          final boolean removeDuplicates); static String getSingleValue(final Object param); static String escapeForHTML(final String raw); static String escapeForHTMLAttribute(final String raw);  }
@Test public void testEncodeUtf8UriParam() throws Exception { assertEquals("Z%2BR", HttpUtil.encodeUtf8UriParam("Z+R")); assertEquals("Z%25R", HttpUtil.encodeUtf8UriParam("Z%R")); assertEquals("Z%2FR", HttpUtil.encodeUtf8UriParam("Z/R")); assertEquals("Z%23R", HttpUtil.encodeUtf8UriParam("Z#R")); assertEquals("Z%20R", HttpUtil.encodeUtf8UriParam("Z R")); assertEquals("белый", HttpUtil.encodeUtf8UriParam("белый")); assertEquals("бе%2Fлый", HttpUtil.encodeUtf8UriParam("бе/лый")); assertEquals("бе%7B%2F%7Dлый", HttpUtil.encodeUtf8UriParam("бе{/}лый")); }
@Test public void testEncodeUtf8_RFC6265() throws Exception { assertEquals("Bearer%20XYZ", HttpUtil.encodeUtf8UriParam("Bearer XYZ")); assertEquals("Bearer%20XYZ%3A%22%2C%3B%5C", HttpUtil.encodeUtf8UriParam("Bearer XYZ:\",;\\")); assertEquals("Bearer%20WFla", HttpUtil.encodeUtf8UriParam("Bearer " + Base64.getEncoder().encodeToString("XYZ".getBytes()))); }