How can I prevent Java 8/Tomcat 7 from URL encoding the URL Path on Response.sendRedirect
Asked Answered
P

1

7

I have a fully localized site that has some characters in the URL path, that are getting HTML encoded through the Response.sendTemporaryRedirect method:

String toReturn = /*StringEscapeUtils.unescapeHtml4(redirect)*/ redirect + "?" + URLEncoder.encode(json, "UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json; charset=UTF-8");

java.net.URI location = new java.net.URI(toReturn);
return Response.temporaryRedirect(location).build();

The redirect is getting a 404 because the "%C3%B1%C3%AD" URL piece is being misinterpreted. Decoded, it looks like this: ñí.

I've tried URLDecoder.decode(url, 'UTF-8') and StringEscapeUtils.escapeHtml4 prior to the redirect with no luck. The code works fine othwerise.

Why is javax.ws.rs.core.Response.sendTemporaryRedirect URLEncoding the URL path?

Psychotomimetic answered 18/7, 2018 at 13:31 Comment(11)
Also, I've verified that the URI object has the correct path. It's definitely Response.temporaryRedirectPsychotomimetic
Another update, the HTTPServletResponse response also had the same problem. It seems to be at the servlet level.Psychotomimetic
which is the constructor of the response instance here?Kashmiri
The package for the Response is javax.ws.rs.core.Response, but this is also happening if I use the HttpServletResponse. There is no constructor.Psychotomimetic
Sorry, response is the HTTPServletResponse injected via @ContextPsychotomimetic
I think you have to check the actual implementation of your JAX-RS provider. For example Apache CXF actually does a decoding. See hereKashmiri
Looks like a double encoding issue to me, can you post a sample of jsonvariable? Also, there's no parameter name after "?", that can confuse the URI parser. Last, try the multi-param constructor to avoid the implicit parsing of the single param constructor new URI("https", "some_host:port", "/", json, null).Alisa
Can you provide an example of the final url you want to redirect to, in an unencoded form?Horten
You will need to give an example of what is your issue, because using the HttpServletResponse like response.sendRedirect("http://your.domain:port/and/your/page?"+URLEncoder.encode(json,"utf-8")); where json is a json, is ok.Barraza
Would be great to see debug info (i.e. the output of each line in the debugger) for your problematic data. That would really help with answering instead of just guessing.Tenerife
It's helpful to extract a simple working code to simulate your case.Advocaat
G
0

Just set the "Location" response header to the string you want. It annoys me so much to see people fighting libraries when the core api is right there.

Geniality answered 7/3, 2019 at 14:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.