After some investigation I found it might be a bug in the URLUtil.java:
public static String getPathQuery(URIFactory uriFactory, String uri) {
URI u = uriFactory.createWithoutException(uri);
String query = u.getQuery(); // if change this to u.getRawQuery(); then everything works
return u.getPath() + (query == null ? "" : "?" + query);
}
However, I'm not sure if this is the designed behaviour or a bug? Maybe the u.getPath() should be replaced with u.getRawPath() and rename the whole method as getRawPathQuery()?