Forum Discussion

FranckLucas's avatar
FranckLucas
New Contributor
10 years ago

Encoding for JMS/SOAP BytesMessages

Hi,
I tried to send a JMS/SOAP messages as a Bytes Message, but it seems that the encoding is wrong.

I am using SOAPUI 5.0.0 on Windows, I have put in my request the character
U+00B0 ° c2 b0 DEGREE SIGN
My request property for encoding is set to UTF-8

When I send the message, I get a wrong encoding, using HermesJMS and looking at Hexadecimal tab I get 0xb0 instead of
0xc2 0xb0 as expected for UTF-8
If I send the message a a Text Message (i.e. not a BytesMessage) the encoding is correct.

So I investigated a little bit with debugger and source code and found the following piece of code from com.eviware.soapui.impl.wsdl.submit.transports.jms.HermesJmsRequestTransport.createBytesMessageFromText(SubmitContext, String, Session):

 private Message createBytesMessageFromText( SubmitContext submitContext, String requestContent, Session session )
throws JMSException
{
BytesMessage bytesMessage = session.createBytesMessage();
bytesMessage.writeBytes(requestContent.getBytes());
return bytesMessage;
}

According to javadoc java.lang.String.getBytes():
    /**
* Encodes this {@code String} into a sequence of bytes using the
* platform's default charset, storing the result into a new byte array.

Unfortunately the value set for Windows is
java.nio.charset.Charset.defaultCharset()
(sun.nio.cs.MS1252) windows-1252


My proposed implementation would be the following one that takes into account the request encoding and not the platform encoding :
 private Message createBytesMessageFromText( SubmitContext submitContext, String requestContent, Session session )
throws JMSException
{
Request request = (Request) submitContext.getProperty(WSDL_REQUEST);
BytesMessage bytesMessage = session.createBytesMessage();
bytesMessage.writeBytes( requestContent.getBytes(Charset.forName(request.getEncoding())) );
return bytesMessage;
}

Another approach is to force the file.encoding java property as proposed here viewtopic.php?f=13&t=21317&hilit=encoding+utf8

Best Regards,
Franck