2
votes

I want to use the Jetty component in a Camel environment. This is an excerpt of my spring-config.xml:

...
<bean id="webEnc" class="web.WebEnc" />
<camelContext>
    <route>
        <from uri="jetty:http://0.0.0.0/enc" />
        <process ref="webEnc" />
    </route>
</camelContext>
...

And here is the code used to return a String:

import org.apache.camel.Exchange;
import org.apache.camel.Processor;

public class WebEnc implements Processor{

    @Override
    public void process(Exchange exchange) throws Exception {
        exchange.getOut().setBody("abcäöüß\"€一二三"); //the last three symbols are chinese for '123'
    }
}

The local address works (http://127.0.0.1/enc) but the browser does not display the string correctly (displayed as 'abcäöüß"€一二三'). I assume the problem is some encoding. How to set the encoding like 'utf-8'?
I can't find any hint here (http://camel.apache.org/jetty.html) concerning encoding or charset or something like that.

2

2 Answers

2
votes

I think you have to set Content-Type header with utf-8 charset in the output message, something like this:

@Override
public void process(Exchange exchange) throws Exception {
    exchange.getOut().setBody("abcäöüß\"€一二三"); //the last three symbols are chinese for '123'
    exchange.getOut().setHeader(Exchange.CONTENT_TYPE, "text/html; charset=utf-8");
}
1
votes

After some help and hints from Łukasz (thx for your time) i found a good solution. This forces Jetty to deliver bytes, not a string:

exchange.getOut().setBody("abcäöüß\"€一二三".getBytes("utf-32"));

To help the browsers to display the correct symbols i did this:

exchange.getOut().setHeader(Exchange.CONTENT_TYPE, "text/html; charset=utf-32");

My firefox cannot display the chinese symbols with utf-32, it's fine with utf-8. Chrome does it with both encodings.
But actually i was not interested in that, my intention was to deliver bytes. getBytes() does the job.