1

Wildfly 8, Omnifaces 2.2, Primefaces 5.2, JSF 2.2.11 (Mojarra)

I am using Ominifaces CharacterEncodingFilter to ensure that file names are encoded correctly on server. Oddly if Primefaces using Jsf internal upload, the file name are not encoded. And if Primefaces use older approach with Appache Commons it is ok.

Example: 'Hällo.jpg' becomes 'Hällo.jpg'

Web.xml configurations: Apache solution (correct):

<filter>
         <filter-name>PrimeFaces FileUpload Filter</filter-name>
         <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
   </filter>
   <filter-mapping>
         <filter-name>PrimeFaces FileUpload Filter</filter-name>
         <servlet-name>Faces Servlet</servlet-name>
   </filter-mapping>
   <context-param>
         <param-name>primefaces.UPLOADER</param-name>
         <param-value>commons</param-value>
   </context-param>

<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>

Jsf Upload (characters are not encoded). Other parameters are removed.

<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>

EDIT: due to answer this is a server bug. I've tried to configure the server:

<jboss-web>
 <default-encoding>UTF-8</default-encoding>
</jboss-web>

and

<servlet-container name="default" default-encoding="UTF-8">

but it doesn't help.

Tony
  • 2,266
  • 4
  • 33
  • 54

1 Answers1

1

I reproduced it, WildFly isn't at all considering request request body encoding for multipart/form-data requests. You really have to configure it in server end (like as you would do for GET requests).

Open /standalone/configuration/standalone.xml, peek the following line

<servlet-container name="default">

change it to

<servlet-container name="default" default-encoding="UTF-8">

and restart. This at least worked for me on WildFly 10.0.0. I created issue WFLY-6226 to let it consider request body encoding first so there's no need to edit standalone.xml on that.

In WildFly 8.x (I tested 8.2.1) this unfortunately still won't work as it does not at all consider the above setting. Your best bet is to keep using Apache Commons FileUpload until you can upgrade WildFly.

If you really want to keep native upload, then you could consider to explicitly decode the broken filename to bytes using ISO-8859-1 and then re-encode it using UTF-8.

String fileName = new String(uploadedFile.getFileName().getBytes("ISO-8859-1"), "UTF-8");

This is however brittle and not portable as it would break when deployed to a server where this encoding problem doesn't manifest. So you really need to remember to revert the workaround when upgrading/migrating.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you for you answer! Unfortunately your solution doesn't help. I've change the line, and emptied WF tmp/ and data/ directories. – Tony Feb 16 '16 at 14:21
  • OK, to exclude one and other, where exactly did you inspect the new value? I got `Hällo.jpg` "as expected", not `H??llo.jpg`. Question marks usually only appear when e.g. `System.out.println()` encoding is wrong, or when JDBC driver encoding is wrong. – BalusC Feb 16 '16 at 18:50
  • Yes you are right I've got `Hällo.jpg` also. I'll correct the question. It was my (wrong) simplification . – Tony Feb 17 '16 at 08:32
  • I tested on WF10 yesterday. I just tried WF 8.2.1 and it indeed doesn't work. WF8 appears to have another bug whereby the multipart/form-data encoding is totally left unconsidered, sorry about that. I'll update the answer. – BalusC Feb 17 '16 at 08:38