users@jsr311.java.net

JAX-RS: escaping UriBuilder query params

From: Manger, James H <James.H.Manger_at_team.telstra.com>
Date: Wed, 23 Jul 2008 13:04:42 +1000

I suggest renaming
  UriBuilder replaceQueryParams(String query);
to
  UriBuilder replaceQuery(String query);
or (better still) to
  UriBuilder query(String query);


It seems trivial but it is sort of important as query parameters need different %-escaping than a whole query.

queryParam(String name, Object… values) should %-escape ‘=’ and ‘&’ in the name, and ‘&’ in the values (and similarly for any referenced placeholder variable values). replaceQueryParam(String, Object…) should behave the same.

replaceQueryParams(String), however, should not %-escape ‘=’ or ‘&” in its argument or (it follows) in placeholder variable values it references.

For instance,

  UriBuilder.fromPath(“search”).replaceQueryParam(“q”, “{term}”).build(“Jack&Jill=water”)
  -> “search?q=Jack%26Jill%3Dwater”

  UriBuilder.fromPath(“search”).replaceQueryParams(“q={term}”).build(“Jack&Jill=water”)
  -> “search?q=Jack&Jill=water”

Removing “Params” from the latter gives some hint that it is not equivalent to the former, in that it will not be careful to preserve any parameter structure.

  UriBuilder.fromPath(“search”).query(“q={term}”).build(“Jack&Jill=water”)
  -> “search?q=Jack&Jill=water”

James Manger