Search in sources :

Example 1 with InvalidMessage

use of quickfix.InvalidMessage in project wso2-synapse by wso2.

the class FIXMessageBuilder method processDocument.

public OMElement processDocument(InputStream inputStream, String contentType, MessageContext messageContext) throws AxisFault {
    Reader reader = null;
    StringBuilder messageString = new StringBuilder();
    quickfix.Message message = null;
    try {
        String charSetEncoding = (String) messageContext.getProperty(Constants.Configuration.CHARACTER_SET_ENCODING);
        if (charSetEncoding == null) {
            charSetEncoding = MessageContext.DEFAULT_CHAR_SET_ENCODING;
        }
        reader = new InputStreamReader(inputStream, charSetEncoding);
        try {
            int data = reader.read();
            while (data != -1) {
                char dataChar = (char) data;
                data = reader.read();
                messageString.append(dataChar);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            log.error("Error In creating FIX SOAP envelope ...", e);
            throw new AxisFault(e.getMessage());
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        log.error("Error In creating FIX SOAP envelope ...", e);
        throw new AxisFault(e.getMessage());
    }
    try {
        DefaultDataDictionaryProvider dataDictionary = new DefaultDataDictionaryProvider();
        String beginString = MessageUtils.getStringField(messageString.toString(), BeginString.FIELD);
        DataDictionary dataDic = dataDictionary.getSessionDataDictionary(beginString);
        message = new quickfix.Message(messageString.toString(), null, false);
    } catch (InvalidMessage e) {
        // TODO Auto-generated catch block
        log.error("Error In creating FIX SOAP envelope ...", e);
        throw new AxisFault(e.getMessage());
    }
    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, ""));
    msg.addAttribute(soapFactory.createOMAttribute(FIXConstants.FIX_MESSAGE_COUNTER, null, String.valueOf("-1")));
    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<quickfix.Field<?>> iter = message.getHeader().iterator();
    if (iter != null) {
        while (iter.hasNext()) {
            quickfix.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 = messageContext.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, messageContext);
    // process FIX trailer
    iter = message.getTrailer().iterator();
    if (iter != null) {
        while (iter.hasNext()) {
            quickfix.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 = messageContext.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);
    messageContext.setEnvelope(envelope);
    return msg;
}
Also used : AxisFault(org.apache.axis2.AxisFault) InputStreamReader(java.io.InputStreamReader) DefaultDataDictionaryProvider(quickfix.DefaultDataDictionaryProvider) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) OMElement(org.apache.axiom.om.OMElement) BeginString(quickfix.field.BeginString) InvalidMessage(quickfix.InvalidMessage) DataHandler(javax.activation.DataHandler) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) DataDictionary(quickfix.DataDictionary) SOAPFactory(org.apache.axiom.soap.SOAPFactory) ByteArrayDataSource(org.apache.axiom.attachments.ByteArrayDataSource) DataSource(javax.activation.DataSource) SOAP11Factory(org.apache.axiom.soap.impl.llom.soap11.SOAP11Factory) ByteArrayDataSource(org.apache.axiom.attachments.ByteArrayDataSource)

Example 2 with InvalidMessage

use of quickfix.InvalidMessage in project camel by apache.

the class QuickfixjConverters method toMessage.

@Converter
public static Message toMessage(byte[] value, Exchange exchange) throws InvalidMessage, ConfigError, UnsupportedEncodingException {
    DataDictionary dataDictionary = getDataDictionary(exchange);
    String charsetName = IOHelper.getCharsetName(exchange);
    String message;
    if (charsetName != null) {
        message = new String(value, charsetName);
    } else {
        message = new String(value);
    }
    // if message ends with any sort of newline trim it so QuickfixJ's doesn't fail while parsing the string
    if (message.endsWith("\r\n")) {
        message = message.substring(0, message.length() - 2);
    } else if (message.endsWith("\r") || message.endsWith("\n")) {
        message = message.substring(0, message.length() - 1);
    }
    return new Message(message, dataDictionary, false);
}
Also used : InvalidMessage(quickfix.InvalidMessage) Message(quickfix.Message) DataDictionary(quickfix.DataDictionary) Converter(org.apache.camel.Converter)

Aggregations

DataDictionary (quickfix.DataDictionary)2 InvalidMessage (quickfix.InvalidMessage)2 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 DataHandler (javax.activation.DataHandler)1 DataSource (javax.activation.DataSource)1 ByteArrayDataSource (org.apache.axiom.attachments.ByteArrayDataSource)1 OMElement (org.apache.axiom.om.OMElement)1 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)1 SOAPFactory (org.apache.axiom.soap.SOAPFactory)1 SOAP11Factory (org.apache.axiom.soap.impl.llom.soap11.SOAP11Factory)1 AxisFault (org.apache.axis2.AxisFault)1 Converter (org.apache.camel.Converter)1 DefaultDataDictionaryProvider (quickfix.DefaultDataDictionaryProvider)1 Message (quickfix.Message)1 BeginString (quickfix.field.BeginString)1