if (log.isDebugEnabled()) {
log.debug("Creating SOAP envelope for FIX message...");
}
SOAPFactory soapFactory = new SOAP11Factory();
OMElement msg = soapFactory.createOMElement(FIXConstants.FIX_MESSAGE, null);
msg.addAttribute(soapFactory.createOMAttribute(FIXConstants.FIX_MESSAGE_INCOMING_SESSION,
null, sessionID));
msg.addAttribute(soapFactory.createOMAttribute
(FIXConstants.FIX_MESSAGE_COUNTER, null, String.valueOf(counter)));
OMElement header = soapFactory.createOMElement(FIXConstants.FIX_HEADER, null);
OMElement body = soapFactory.createOMElement(FIXConstants.FIX_BODY, null);
OMElement trailer = soapFactory.createOMElement(FIXConstants.FIX_TRAILER, null);
//process FIX header
Iterator<Field<?>> iter = message.getHeader().iterator();
if (iter != null) {
while (iter.hasNext()) {
Field<?> field = iter.next();
OMElement msgField = soapFactory.createOMElement(FIXConstants.FIX_FIELD, null);
msgField.addAttribute(soapFactory.createOMAttribute(FIXConstants.FIX_FIELD_ID,
null, String.valueOf(field.getTag())));
Object value = field.getObject();
if (value instanceof byte[]) {
DataSource dataSource = new ByteArrayDataSource((byte[]) value);
DataHandler dataHandler = new DataHandler(dataSource);
String contentID = msgCtx.addAttachment(dataHandler);
OMElement binaryData = soapFactory.createOMElement(
FIXConstants.FIX_BINARY_FIELD, null);
String binaryCID = "cid:" + contentID;
binaryData.addAttribute(FIXConstants.FIX_MESSAGE_REFERENCE, binaryCID, null);
msgField.addChild(binaryData);
} else {
soapFactory.createOMText(msgField, value.toString(),
OMElement.CDATA_SECTION_NODE);
}
header.addChild(msgField);
}
}
//process FIX body
convertFIXBodyToXML(message, body, soapFactory, msgCtx);
//process FIX trailer
iter = message.getTrailer().iterator();
if (iter != null) {
while (iter.hasNext()) {
Field<?> field = iter.next();
OMElement msgField = soapFactory.createOMElement(FIXConstants.FIX_FIELD, null);
msgField.addAttribute(soapFactory.
createOMAttribute(FIXConstants.FIX_FIELD_ID, null,
String.valueOf(field.getTag())));
Object value = field.getObject();
if (value instanceof byte[]) {
DataSource dataSource = new ByteArrayDataSource((byte[]) value);
DataHandler dataHandler = new DataHandler(dataSource);
String contentID = msgCtx.addAttachment(dataHandler);
OMElement binaryData = soapFactory.createOMElement(
FIXConstants.FIX_BINARY_FIELD, null);
String binaryCID = "cid:" + contentID;
binaryData.addAttribute(FIXConstants.FIX_MESSAGE_REFERENCE, binaryCID, null);
msgField.addChild(binaryData);
} else {
soapFactory.createOMText(msgField, value.toString(),
OMElement.CDATA_SECTION_NODE);
}
trailer.addChild(msgField);
}
}
msg.addChild(header);
msg.addChild(body);
msg.addChild(trailer);
SOAPEnvelope envelope = soapFactory.getDefaultEnvelope();
envelope.getBody().addChild(msg);
msgCtx.setEnvelope(envelope);
}