Search code examples
javajax-rsurl-encoding

Uribuilder encoding with exclamation mark


I'm trying to use Uribuilder from:

javax.ws.rs.core.UriBuilder;

To update a URI. The issue is that the parameter name gets escaped when I use replaceQueryParam.

so:

UriBuilder uriBuilder = webResource.getUriBuilder().
            replaceQueryParam("abcd!dcv, "wid").
            replaceQueryParam("format", "json");

if there is already an existing "abcd!dcv" parameter in the Uribuilder, it will escape and add a new one. so it will become

?abcd!dcv=originalvalue&abcd%21cdv=wid

instead of

?abcd!dcv=wid

How should I get around this? Thanks!


Solution

  • URIBuilder is an abstract class and the implementation gets to decide which characters need special encoding and which do not. The URIBuilder we get from a WebResource is attempting to follow the guidelines of RFC 3986. On page 12, ! is listed as a sub-delimiter and this is why it is getting encoded. From my reading of the RFC, I don't think we should be using ! as part of a query parameter. For instance, Vaading uses ! to distinguish between sub-windows of the same application.

    The simplest work around I can think of is to simply not use URIBuilder or use the fromURI method that takes a String as input. You can create the URI with everything except the part with the characters we don't want encoded, convert this to astring, manipulate astring to replace the query parameter and then call URIBuilder.fromURI(aString)