Based on @Roy answer, this code works:
public static URI toUri(String uri) throws URISyntaxException {
StringBuilder stringBuilder = new StringBuilder(uri);
int index = stringBuilder.indexOf("%u");
while (index > -1) {
try {
String substring = stringBuilder.substring(index, index + 6).replaceAll("%u", "\\\\u");
String encoded = URLEncoder.encode(StringEscapeUtils.unescapeJava(substring), StandardCharsets.UTF_8);
stringBuilder.replace(index, index + 6, encoded);
index = stringBuilder.indexOf("%u", index + 6);
} catch (Exception e) {
throw new URISyntaxException(uri, e.getMessage());
}
}
return new URI(stringBuilder.toString());
}
The idea is to replace every group %uxxxx
with the encoded value of the unicode character \uxxxx
.
This way http://host?xyz=abc%u021B
becomes http://host?xyz=abc%C8%9B
and the last one is a standard URI.