I have a simple JEE 7 application, running on Wildfly 10.1.0.Final, with an encoding problem after URL decoding.
I am using the org.omnifaces.util.FacesLocal
class from Omnifaces 2.6.3 to redirect to another page with additional GET parameters as part of the URL, e.g. /<context-root>/search.xhtml?topic=1&term=Österreich
The redirect is working as expected and the URL seems to be correctly URL encoded. /<context-root>/search.xhtml?topic=1&term=%D6sterreich
The URL encoding happens internally in the org.omnifaces.util.Utils
class, by using UTF-8 as encoding.
public static String encodeURL(String string) {
if (string == null) {
return null;
}
try {
return URLEncoder.encode(string, UTF_8.name());
}
catch (UnsupportedEncodingException e) {
throw new UnsupportedOperationException(ERROR_UNSUPPORTED_ENCODING, e);
}
}
After the redirect I am trying to access the GET parameter from the ExternalContext
, but the encoding seems to be broken. For the given example I get ?sterreich
as string.
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
// Delivers the GET-parmeter with broken encoding
String term = externalContext.getRequestParameterMap().get("term");
// Delivers the complete query string with correct encoding
HttpServletRequest servletRequest = (HttpServletRequest) externalContext.getRequest();
String queryString = servletRequest.getQueryString();
I already checked the following encodings.
Platform encoding is: UTF-8
Wildlfy encodings are also set to: UTF-8
<servlet-container name="default" default-encoding="UTF-8">
<http-listener name="default" socket-binding="http" redirect-socket="https" enable-http2="true" url-encoding="UTF-8" />
I also tried adding the Omnifaces CharacterEncodingFilter to web.xml to set the request character encoding to UTF-8.
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.omnifaces.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
What encoding setting am I missing?
Is it possible that Undertow is falling back to ISO_8859_1?
After some more digging I found out that the problem seems to be the encoding part and not the decoding one.
For testing I set the url-charset to ISO-8859-1 (via standalone.xml) and everything was working as expected.
<http-listener name="default" socket-binding="http" redirect-socket="https" enable-http2="true" decode-url="true" url-charset="ISO-8859-1" />
After this finding I re-checked the code that is responsible for the encoding and found out that the URL is no longer encoded by the Omnifaces Utils method, that I mentioned previously (another developer rewrote the code). Therefore I testet it again by manually encoding the URL with UTF-8 charset and received a new URL that is now correctly UTF-8 encoded.
/<context-root>/search.xhtml?topic=1&term=%C3%96sterreich
To fully understand the issue I have the following open questions:
- Who is responsible for automatic URL encoding, if it's not done by me? Is it JSF or Undertow?
- Where can I change the encoding, that is used for the encoding?
If there is no solution I will correctly encode the URL by myself but it would be nice if the automatic way (thats already happening somewhere) would also work correctly.