Clinton Bosch
Clinton Bosch

Reputation: 2589

Special characters in SOAP payload not transferred correctly

I have written a SOAP web-service using CXF which is being called by a SAP system, in the payload there is a word with a special character which occurs multiple times. However, I read this word differently in some random cases, i.e. in a one payload I see the word as Kliëntbestuurder and in another as Kli��ntbestuurder.

The SAP system calling my service via SAP PI only have the one word.

UPDATE: So it seems that it was not the web-service communication that was getting confused but rather the interceptor that I had written to dump the soap envelope for me to be able to scrutinise. The interceptor is as follows:

public class WebServiceMessageInterceptor extends AbstractPhaseInterceptor<Message> {

public WebServiceMessageInterceptor() {
    super(Phase.RECEIVE);
}

@Override
public void handleMessage(Message message) throws Fault {
    final LoggingMessage buffer = new LoggingMessage("", "");

    String encoding = (String) message.get(Message.ENCODING);

    if (encoding != null) {
        buffer.getEncoding().append(encoding);
    }
    Object headers = message.get(Message.PROTOCOL_HEADERS);

    if (headers != null) {
        buffer.getHeader().append(headers);
    }

    InputStream is = message.getContent(InputStream.class);
    if (is != null) {
        CachedOutputStream outputStream = new CachedOutputStream();
        try {
            IOUtils.copy(is, outputStream);
            outputStream.flush();
            is.close();

            message.setContent(InputStream.class, outputStream.getInputStream());
            outputStream.writeCacheTo(buffer.getPayload(), "UTF-8", -1);
            outputStream.close();

            FileUtils.writeStringToFile(new File("/tmp/soap" + System.currentTimeMillis() + ".log"), buffer.toString(), "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
            throw new Fault(e);
        }
    }
}

Any further ideas why my interceptor is not using UTF-8?

Upvotes: 0

Views: 5201

Answers (2)

CodeClimber
CodeClimber

Reputation: 4664

Check the http headers on the response you are sending back from your web services. You can use the Raw tab in soapUI to view the headers. If you don't see something like

Content-Type: text/xml;charset=UTF-8

then you can force CXF to add it to the response by doing something like this in your WebMethods:

MessageContext ctx = context.getMessageContext();
ctx.put(Message.CONTENT_TYPE, "text/xml;charset=UTF-8");

where context is the javax.xml.ws.WebServiceContext injected into your class.

You should also verify that the client to your web service is also using the correct encoding. You may be sending a valid response to him.

Upvotes: 1

Aravind Yarram
Aravind Yarram

Reputation: 80194

This might be related to not using encoding consistently across and within the services. I suggest you help yourself by reading this excellent tutorial - Unicode - How to get the characters right? end to end. Then ask follow up questions once you narrowed down the scope of the error.

Upvotes: 1

Related Questions