Camel CXF rsClient : Unable read French characters in UTF-8

715 Views Asked by At

Here I am calling a REST webservice using CamelCxf rsClient. The webservice returns a JSON response with encoding : ISO-8859-1. And CamelCxf is trying to read it in that encoding which is changing the French characters in json string.

I wanted to change the charset encoding to UTF-8 to read both English and French characters.

I have tried to read the cxfrs client response as

<convertBodyTo type="String"/>

Which changed the French character è to é

I have also tried to convert the response into byte[] and then created a UTF-8 string from the byte[]

<convertBodyTo type="byte[]"/>

and in the processor

byte[] body =(byte[]) exchange.getIn().getBody();

String convertedString = new String(body, "utf8");

This attempt also failed to read french characters properly.

Response headers from external webservice

Content-Type: application/json Encoding: ISO-8859-1

How do we make camel Cxf rsClient to ignore the encoding coming with json response and change to

Content-Type: application/json;charset=UTF-8 Encoding: UTF-8

Can we use an interceptor to do so?

Update :

Tried

<convertBodyTo type="java.lang.String" charset=UTF-8"/>

It works in local windows server but NOT in remote redhat Linux servers.

1

There are 1 best solutions below

0
On

How do we make camel Cxf rsClient to ignore the encoding coming with json response and change to

You can instruct Camel to use a particular charset encoding when performing an HTTP request over CXF by setting the Content-Type exchange property to the desired value as below (given that upstream service would support content negotiation properly and provide the desired encoding:

exchange.getMessage().setHeader(Exchange.CONTENT_TYPE, "application/json;charset=UTF-8");

For sure, you need to find a proper way of injecting this harder into your route given you haven't described how it is all plugged together. This may resolve your issue if requested resource is sent properly encoded.

Alternative solution

Which changed the French character è to é

This translation goes against what you described in your main topic. Having the è characters translate to é means that the input was encoded initially in UTF-8 then it has been evaluated as being encoded in ISO-8859-1 standard.

As you haven't described how your route declaration, here down a generic solution where you can declare a bean / processor translating your incoming Message body from latin1 to UTF-8 encoded strings:

public class CharsetConverter {

    public CharsetConverter() {
    }

    public String convert(Exchange exchange) {
        // given `exchange.getMessage().getBody()` is a `String` encoded in ISO-8859-1
        return new String(exchange.getMessage().getBody(String.class).getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8);
    }
}

Additional notes

It works in local windows server but NOT in remote redhat Linux servers.

First you may need to check the default charset (aka encoding scheme) used in your application as its behavior varies between environments. You can inspect the default Charset used across Camel Exchanges by inspecting the below expression value:

ExchangeHelper.getCharsetName(exchange); // where you would / can intercept different exchanges