8

I have a URI like this:

java.net.URI location = UriBuilder.fromPath("../#/Login").queryParam("token", token).build();

and I am sending it as response: return Response.seeOther(location).build()

However, in the above URI, # is getting encoded to %23/. How do I create a URI with out encoding the hash #. According to official document, a fragment() method must be used to keep unencoded.

URI templates are allowed in most components of a URI but their value is restricted to a particular component. E.g.

UriBuilder.fromPath("{arg1}").build("foo#bar"); would result in encoding of the '#' such that the resulting URI is "foo%23bar". To create a URI "foo#bar" use UriBuilder.fromPath("{arg1}").fragment("{arg2}").build("foo", "bar") instead.

Looking at the example from docs, I am not sure how to apply it in my case.

The final URI should look like this:

http://localhost:7070/RTH_Sample14/#Login?token=eyJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJodHRwczpcL1wvcnRoLmNvbSIsInN1YiI6IlJUSCIsInJvbGUiOiJVU0VSIiwiZXhwIjoxNDU2Mzk4MTk1LCJlbWFpbCI6Imtpcml0aS5rOTk5QGdtYWlsLmNvbSJ9.H3d-8sy1N-VwP5VvFl1q3nhltA-htPI4ilKXuuLhprxMfIx2AmZZqfVRUPR_tTovDEbD8Gd1alIXQBA-qxPBcxR9VHLsGmTIWUAbxbyrtHMzlU51nzuhb7-jXQUVIcL3OLu9Gcssr2oRq9jTHWV2YO7eRfPmHHmxzdERtgtp348

kittu
  • 6,662
  • 21
  • 91
  • 185

2 Answers2

9

To construct the URI with fragment use

UriBuilder.fromPath("http://localhost:7070/RTH_Sample14/").fragment("Login").build()

This results in the URI string

http://localhost:7070/RTH_Sample14/#Login

But if you also add query parameters

UriBuilder.fromPath("http://localhost:7070/RTH_Sample14/").fragment("Login")
          .queryParam("token", "t").build()

then the UriBuilder always inserts the query params before the fragment:

http://localhost:7070/RTH_Sample14/?token=t#Login

which simply complies to the URL syntax.

wero
  • 32,544
  • 3
  • 59
  • 84
  • I fixed it in other way. What do you think is the best way? Check my answer below and thanks for taking time to answer my question – kittu Feb 25 '16 at 13:03
  • How can we get the fragment before query params? – greperror Sep 16 '16 at 13:16
  • greperror - The HTTP specification states that the server will ignore anything following a # character, so if you add params AFTER # they will only be visible client side (i.e in the browser/javascript/etc). Browsers can even chose not to send the part after #. – Ric Jafe Jan 24 '17 at 17:10
2

Instead of all the hassle of redirecting without encoding the hash value. I changed my code into the following:

java.net.URI location = new java.net.URI("../#/Login?token=" + token);

So the query param above is token appended to URI location. In front-end I am using angular's location.search().token to get capture the query param.

This worked for me. Looking for better answers though. Thanks

kittu
  • 6,662
  • 21
  • 91
  • 185
  • 1
    UriBuilder helps with encoding and adding path parameters. If you don't need this (as in your example, since you **know** that token needs no encoding), this approach is fine. Although in the strict sense it does not answer your original question. – wero Feb 25 '16 at 13:09
  • 1
    I think it does. I believe you have an Angular app that has a /#/ URL at the end and you want it to take a query param of your json web token. I had a similar problem. This works. – kiran.gilvaz Oct 19 '16 at 16:36
  • @kiran.gilvaz glad it helped – kittu Oct 20 '16 at 03:14
  • Any idea how I would do this for Android? https://developer.android.com/reference/android/net/Uri – Wirling Jun 04 '19 at 05:45