OutputStream out;
StringWriter sw;
if (useBytesMessage) {
BytesMessage bytesMsg = session.createBytesMessage();
sw = null;
out = new BytesMessageOutputStream(bytesMsg);
message = bytesMsg;
} else {
sw = new StringWriter();
try {
out = new WriterOutputStream(sw, format.getCharSetEncoding());
} catch (UnsupportedCharsetException ex) {
handleException("Unsupported encoding " + format.getCharSetEncoding(), ex);
return null;
}
}
try {
messageFormatter.writeTo(msgContext, format, out, true);
out.close();
} catch (IOException e) {
handleException("IO Error while creating BytesMessage", e);
}
if (!useBytesMessage) {
TextMessage txtMsg = session.createTextMessage();
txtMsg.setText(sw.toString());
message = txtMsg;
}
if (contentTypeProperty != null) {
message.setStringProperty(contentTypeProperty, contentType);
}
} else if (JMSConstants.JMS_BYTE_MESSAGE.equals(jmsPayloadType)) {
message = session.createBytesMessage();
BytesMessage bytesMsg = (BytesMessage) message;
OMElement wrapper = msgContext.getEnvelope().getBody().
getFirstChildWithName(BaseConstants.DEFAULT_BINARY_WRAPPER);
OMNode omNode = wrapper.getFirstOMChild();
if (omNode != null && omNode instanceof OMText) {
Object dh = ((OMText) omNode).getDataHandler();
if (dh != null && dh instanceof DataHandler) {
try {
((DataHandler) dh).writeTo(new BytesMessageOutputStream(bytesMsg));
} catch (IOException e) {
handleException("Error serializing binary content of element : " +
BaseConstants.DEFAULT_BINARY_WRAPPER, e);
}
}