0
votes

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?

3
Have you tried to set the file encoding system property? -Dfile.encoding=UTF-8 - tsegismont
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
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
Not sure what you mean. If you the CLI you can set the java-opts parameter or the JAVA_OPT env variable. - tsegismont

3 Answers

2
votes

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.

0
votes

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?

0
votes

from vertx 3.4.0 you can do

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

docs