How to convert bytes body to string using http header charset definition in camel RouteBuilder?

时间:2015-11-12 10:40:24

标签: java apache-camel

In a camel instance I would like to convert the body of a rest message to a string using the specified encoding of the HTTP header.

The route definition I came up with so far looks like the following:

    from("cxfrs:bean:rsServer")
    .convertBodyTo(String.class, header(Exchange.HTTP_CHARACTER_ENCODING).evaluate(refToCurrentExchange, String.class))
    .inOnly("activemq:jms:foo");

However I don't know how to evaluate the Exchange.HTTP_CHARACTER_ENCODING header in order to use its value as the target charset for convertBodyTo.

If the body isn't converted, the message send to the jms queue will be a jms bytes message, but I would like it to be a jms text message.

How can I use the Exchange.HTTP_CHARACTER_ENCODING value as an argument to convertBodyTo?

1 个答案:

答案 0 :(得分:1)

我实施了一个新的处理器来完成这项工作:

public static final class ConvertBodyToStringProcessor implements Processor {
    @Override
    public void process(Exchange exchange) throws Exception { // NOPMD
        new ConvertBodyProcessor(String.class, (String) new HeaderExpression(Exchange.HTTP_CHARACTER_ENCODING).evaluate(exchange)).process(exchange);
    }
}

现在路线的定义如下:

from("cxfrs:bean:rsServer")
.process(new ConvertBodyToStringProcessor())
.inOnly("activemq:jms:foo");