0

Since upgrading to the latest Vertx 3.2.2, StaticHandler returns static css, html, etc. resources from the filesystem using Windows-1252 encoding. Previous Vertx version did not tell any encoding.

Now, all utf-8 data (such as iconfont icon chars) are garbled.

I have tried to manually add UTF8 BOM to the beginning of a file, still StaticHandler serves as Windows-1252 (but at least the browser is able to recognize the utf marker and render well).

How can I either force a preferred character encoding, or make statichandler recognize the file encoding?

Gee Bee
  • 1,794
  • 15
  • 17
  • Have you tried to set the file encoding system property? `-Dfile.encoding=UTF-8` – tsegismont Aug 10 '16 at 14:25
  • No, but now I tried. It does not help, unfortunately. (I am also wondering if there is a complete list of vertx specific system properties out there. Finding vertx.cwd took me an hour.) – Gee Bee Aug 10 '16 at 14:35
  • Hm, tsegismont you might be right. However this - unlike other params - have to given as a command line argument, since System.setProperty does not work. I found a nice workaround, though. – Gee Bee Aug 10 '16 at 21:00
  • Not sure what you mean. If you the CLI you can set the java-opts parameter or the JAVA_OPT env variable. – tsegismont Aug 12 '16 at 13:55

3 Answers3

2

There's no specific Vert.x property for that. If you look at the actual StaticHandlerImpl implementation you'll see that what it does is simply:

String defaultContentEncoding = java.nio.charset.Charset.defaultCharset().name();

Where Charset is standard Java class. So setting -Dfile.encoding=UTF-8 in the VM options will work with Vert.x.

I would strongly discourage you from using reflection to solve that problem, as it's very hacky.

Alexey Soshin
  • 16,718
  • 2
  • 31
  • 40
  • Thanks for the advice! I agree using the reflection this way is not nice. On the other hand, it is not nice from vert.x to serve an utf8 file with an arbitrary platform specific charset either. I prefer keeping the reflection based solution, since accidentally leaving the runtime parameter off can cause oddities in the web app. Other platforms such as Play! framework will simply enforce everything being utf-8. It is also arbitrary, but at least reasonably good. – Gee Bee Aug 11 '16 at 21:44
0

Answering my own question:

System.setProperty("file.encoding","UTF-8");
Field charset = Charset.class.getDeclaredField("defaultCharset");
charset.setAccessible(true);
charset.set(null,null);

From: Setting the default Java character encoding?

Community
  • 1
  • 1
Gee Bee
  • 1,794
  • 15
  • 17
  • Would you mind opening an issue in vertx-web project on GitHub? Indicating which version you had before and ideally, a little reproducer? There is no 3.2.2 release BTW so maybe you meant about 3.3.2? – tsegismont Aug 12 '16 at 07:45
  • I created an issue, see [vert.x issue 221](https://github.com/vert-x3/issues/issues/221). – Timo Verhoeven Dec 15 '16 at 12:29
0

from vertx 3.4.0 you can do

StaticHandler.create().setDefaultContentEncoding("UTF-8");

docs

frx08
  • 4,222
  • 8
  • 37
  • 45