I'd like to append key-value pair as a query parameter to an existing URL. While I could do this by checking for the existence of whether the URL has a query part or a fragment part and doing the append by jumping though a bunch of if-clauses but I was wondering if there was clean way if doing this through the Apache Commons libraries or something equivalent.
http://example.com
would be http://example.com?name=John
http://example.com#fragment
would be http://example.com?name=John#fragment
http://example.com?email=john.doe@email.com
would be http://example.com?email=john.doe@email.com&name=John
http://example.com?email=john.doe@email.com#fragment
would be http://example.com?email=john.doe@email.com&name=John#fragment
I've run this scenario many times before and I'd like to do this without breaking the URL in any way.