Wednesday, March 31, 2010

Loading Java properties in UTF-8

One of the latest stupid problems I've encountered: Java *.properties files are always loaded in ISO-8859-1 encoding and there is no way to change that. But in our project we wanted to have them in UTF-8 so that non-Latin files could be human readable. We use JSTL "fmt" tag library which loads messages from *.properties files (and guess in which encoding).

The simple solution I've come up is build-time native2ascii conversion. This can be done using the native2ascii-maven-plugin. This example shows what to add to pom.xml (assuming that your *.properties files are in src/main/resources/l10n):

<build>
 ...
 <resources>
     <resource>
         <directory>src/main/resources</directory>
         <excludes>
             <!-- exclude resources handled by native2ascii-maven-plugin (otherwise they will be overwritten again) -->
             <exclude>l10n/*</exclude>
         </excludes>
     </resource>
 </resources>

<plugins>
 <plugin>
     <groupId>org.codehaus.mojo</groupId>
     <artifactId>native2ascii-maven-plugin</artifactId>
     <executions>
         <execution>
             <goals>
                 <goal>native2ascii</goal>
             </goals>
             <configuration>
                 <src>src/main/resources</src>
                 <includes>l10n/*</includes>
                 <encoding>UTF8</encoding>
             </configuration>
         </execution>
     </executions>
 </plugin>

Wednesday, March 17, 2010

Servlet + SSI + Apache HTTPD

I was confronted with a requirement to add some SSI (Server Side Includes) processing to my Java web app. The application would get some external text containing SSI tags, prepend it with text containing SSI #set tags which will set some needed variables and all this would be processed by SSI to get the final HTML. Sounds easy enough.

What I've found is that SSI is not actually a standard - it is more like an Apache HTTPD proprietary feature. Some other webservers claim to support it (most notably Tomcat), but certainly not 100%. For example, Tomcat does not understand regular expressions in #if tags and this makes Tomcat SSI totally unusable for my purpose.

So, the only working solution right now is to have Apache in front of Tomcat (or other servlet container) with help of mod_jk or mod_proxy.

After couple of hours try-and-fails, I have found how to configure it in httpd.conf:

<Proxy *>
  Options IncludesNoExec
  SetOutputFilter INCLUDES
  Order deny,allow
  Allow from all
</Proxy>

The critical part here is the first two lines inside