ISO-8859 Based URL Encoding in Grails

11 / Apr / 2011 by Vivek Krishna 0 comments

The web application we are developing at the moment interacts with quite a few third party services via REST calls. Most of the third party services use UTF-8 encoding and there was no issues with using the URLCodec, that grails provides out of the box. However, one of the applications our application interacts with, was using ISO-8859-1 encoding. This meant that the use of encodeAsURL() provided by grails resulted in misinterpretation of data sent by us to those services. (The audience to which the site caters has languages extensive use of characters like å, Å, ä, Ä etc).

On going through the URLCodec which grails provides out of the box, we found that the encoding is UTF-8 by default and can be overridden only by the encoding flag in the incoming HTTP request. However, the REST calls we make were outside of HTTP requests and the encoding for the rest of the application is UTF-8.

Therefore, we ended up writing a custom IsoURLCodec, which could be used while dealing with this particular service, which needed ISO-8859-1 encoding of URLs as opposed to UTF-8 used by all other services.

class IsoURLCodec {
static encode = { obj ->
URLEncoder.encode(obj.toString(), "ISO-8859-1")
static decode = { obj ->
URLDecoder.decode(obj.toString(), "ISO-8859-1")

Now, we could make use of calls like

"${‘<span style="font-family: sans-serif;">Philippe Pétain</span>’.encodeAsIsoURL()}"

to encode the name to the corresponding ISO-8859-1 encoded URL.
Hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *