Issue #33 resolved

Unable to set charset on HTTP Sender

mclarkuk
created an issue

Create a simple proxy to accept HTTP incoming traffic and send on to a service using the default HTTP sender.

Send UTF-8 encoded content to the proxy with the appropriate Content-Type header value - for example text/xml; charset=UTF-8

The output encoding is scrambled because it has been re-encoded using ISO-8859-1.

There doesn't appear to be any way to override the default setting because the method called in StringEntity sets a default value of ISO-8859-1.

We added the following method as a quick fix - I am fairly sure there is a more robust method available in the httpcomponents library:

...
    public static final String DEFAULT_CHARSET = "UTF-8";
    public static final String CHARSET_ELEMENT_NAME = "charset";
...
    public String getContentCharSet(String contentType) {
        String charset = DEFAULT_CHARSET;
        if(null != contentType) {
            Map<String, String> map = new LinkedHashMap<String, String>();
            for(String keyValue : contentType.split(";")) {
                String[] pairs = keyValue.split("=", 2);
                map.put(pairs[0].trim(), pairs.length == 1 ? "" : pairs[1].trim());
            }
            if(map.containsKey(CHARSET_ELEMENT_NAME))
                charset = map.get(CHARSET_ELEMENT_NAME);
        }
        return charset;
    }

and then replace

entity = new StringEntity((message.getCurrentPayload()).toString());

with

String charset = getContentCharSet(message.getContentType());
entity = new StringEntity((message.getCurrentPayload()).toString(), charset);

Comments (1)

  1. Log in to comment