Java Code Examples for org.apache.synapse.MessageContext#setEnvelope()

The following examples show how to use org.apache.synapse.MessageContext#setEnvelope() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HL7MessageUtils.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
public static MessageContext createSynapseMessageContext(Message message, InboundProcessorParams params)
        throws HL7Exception, AxisFault {

    MessageContext synCtx = createSynapseMessageContext(
            params.getProperties().getProperty(MLLPConstants.HL7_INBOUND_TENANT_DOMAIN));

    if (params.getProperties().getProperty(Axis2HL7Constants.HL7_VALIDATION_PASSED) != null) {
        synCtx.setProperty(Axis2HL7Constants.HL7_VALIDATION_PASSED,
                           params.getProperties().getProperty(Axis2HL7Constants.HL7_VALIDATION_PASSED));
    }

    try {
        synCtx.setEnvelope(createEnvelope(synCtx, message, params));
    } catch (Exception e) {
        throw new HL7Exception(e);
    }

    return synCtx;
}
 
Example 2
Source File: HL7MessageUtils.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
public static MessageContext createErrorMessageContext(String rawMessage, Exception errorMsg,
                                                       InboundProcessorParams params)
        throws AxisFault, HL7Exception {
    MessageContext synCtx = createSynapseMessageContext(
            params.getProperties().getProperty(MLLPConstants.HL7_INBOUND_TENANT_DOMAIN));

    if (params.getProperties().getProperty(Axis2HL7Constants.HL7_VALIDATION_PASSED) != null) {
        synCtx.setProperty(Axis2HL7Constants.HL7_VALIDATION_PASSED,
                           params.getProperties().getProperty(Axis2HL7Constants.HL7_VALIDATION_PASSED));
    }

    try {
        synCtx.setProperty(SynapseConstants.ERROR_CODE, SynapseConstants.RCV_IO_ERROR_RECEIVING);
        synCtx.setProperty(SynapseConstants.ERROR_MESSAGE, errorMsg.getMessage());
        synCtx.setProperty(SynapseConstants.ERROR_DETAIL,
                           (errorMsg.getCause() == null ? "null" : errorMsg.getCause().getMessage()));
        synCtx.setProperty(SynapseConstants.ERROR_EXCEPTION, errorMsg);
        synCtx.setEnvelope(createErrorEnvelope(synCtx, rawMessage, errorMsg.getMessage(), params));
    } catch (Exception e) {
        throw new HL7Exception(e);
    }

    return synCtx;
}
 
Example 3
Source File: JMSReplySenderTest.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
private void setSOAPEnvelopWithTextBody(MessageContext messageContext) throws AxisFault {
    SOAPFactory fac = OMAbstractFactory.getSOAP11Factory();
    SOAPEnvelope env = fac.createSOAPEnvelope();
    fac.createSOAPBody(env);
    OMElement firstEle = fac.createOMElement(BaseConstants.DEFAULT_TEXT_WRAPPER);
    firstEle.setText("TestSendBack");
    env.getBody().addChild(firstEle);
    messageContext.setEnvelope(env);
}
 
Example 4
Source File: JMSReplySenderTest.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
private void setSOAPEnvelopWithBinaryBody(MessageContext messageContext, BytesMessage message) throws AxisFault {
    SOAPFactory fac = OMAbstractFactory.getSOAP11Factory();
    SOAPEnvelope env = fac.createSOAPEnvelope();
    fac.createSOAPBody(env);
    OMElement firstEle = fac.createOMElement(BaseConstants.DEFAULT_BINARY_WRAPPER);
    DataHandler dataHandler = new DataHandler(new BytesMessageDataSource(message));
    OMText textEle = fac.createOMText(dataHandler, true);
    firstEle.addChild(textEle);
    env.getBody().addChild(firstEle);
    messageContext.setEnvelope(env);
}
 
Example 5
Source File: JMSReplySenderTest.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
private void setSOAPEnvelopWithMapMessageBody(MessageContext messageContext) throws AxisFault, XMLStreamException {
    SOAPFactory fac = OMAbstractFactory.getSOAP11Factory();
    SOAPEnvelope env = fac.createSOAPEnvelope();
    fac.createSOAPBody(env);
    OMElement mapElement1 = fac.createOMElement(new QName("Price"));
    mapElement1.setText("10");
    OMElement mapElement2 = fac.createOMElement(new QName("Name"));
    mapElement2.setText("Queue");
    OMElement firstEle = fac.createOMElement(JMSConstants.JMS_MAP_QNAME);
    firstEle.addChild(mapElement1);
    firstEle.addChild(mapElement2);
    env.getBody().addChild(firstEle);
    messageContext.setEnvelope(env);
}
 
Example 6
Source File: JMSReplySenderTest.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
private void setSOAPEnvelopWithoutTypeTextMessageBody(MessageContext messageContext)
        throws AxisFault, XMLStreamException {
    SOAPFactory fac = OMAbstractFactory.getSOAP11Factory();
    SOAPEnvelope env = fac.createSOAPEnvelope();
    fac.createSOAPBody(env);
    OMElement mapElement1 = fac.createOMElement(new QName("Price"));
    mapElement1.setText("10");
    OMElement firstEle = fac.createOMElement(new QName("First"));
    firstEle.addChild(mapElement1);
    env.getBody().addChild(firstEle);
    messageContext.setEnvelope(env);
}
 
Example 7
Source File: JMSReplySenderTest.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
private void setSOAPEnvelopWithoutTypeByteMessageBody(MessageContext messageContext, BytesMessage message)
        throws AxisFault {
    SOAPFactory fac = OMAbstractFactory.getSOAP11Factory();
    SOAPEnvelope env = fac.createSOAPEnvelope();
    fac.createSOAPBody(env);
    OMElement firstEle = fac.createOMElement(new QName("Binary"));
    DataHandler dataHandler = new DataHandler(new BytesMessageDataSource(message));
    OMText textEle = fac.createOMText(dataHandler, true);
    firstEle.addChild(textEle);
    env.getBody().addChild(firstEle);
    messageContext.setEnvelope(env);
}
 
Example 8
Source File: Utils.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
public static void setSOAPFault(MessageContext messageContext, String code, 
                                String reason, String detail) {
    SOAPFactory factory = (messageContext.isSOAP11() ?
            OMAbstractFactory.getSOAP11Factory() : OMAbstractFactory.getSOAP12Factory());

    OMDocument soapFaultDocument = factory.createOMDocument();
    SOAPEnvelope faultEnvelope = factory.getDefaultFaultEnvelope();
    soapFaultDocument.addChild(faultEnvelope);

    SOAPFault fault = faultEnvelope.getBody().getFault();
    if (fault == null) {
        fault = factory.createSOAPFault();
    }

    SOAPFaultCode faultCode = factory.createSOAPFaultCode();
    if (messageContext.isSOAP11()) {
        faultCode.setText(new QName(fault.getNamespace().getNamespaceURI(), code));
    } else {
        SOAPFaultValue value = factory.createSOAPFaultValue(faultCode);
        value.setText(new QName(fault.getNamespace().getNamespaceURI(), code));            
    }
    fault.setCode(faultCode);

    SOAPFaultReason faultReason = factory.createSOAPFaultReason();
    if (messageContext.isSOAP11()) {
        faultReason.setText(reason);
    } else {
        SOAPFaultText text = factory.createSOAPFaultText();
        text.setText(reason);
        text.setLang("en");
        faultReason.addSOAPText(text);            
    }
    fault.setReason(faultReason);

    SOAPFaultDetail soapFaultDetail = factory.createSOAPFaultDetail();
    soapFaultDetail.setText(detail);
    fault.setDetail(soapFaultDetail);
    
    // set the all headers of original SOAP Envelope to the Fault Envelope
    if (messageContext.getEnvelope() != null) {
        SOAPHeader soapHeader = messageContext.getEnvelope().getHeader();
        if (soapHeader != null) {
            for (Iterator iterator = soapHeader.examineAllHeaderBlocks(); iterator.hasNext();) {
                Object o = iterator.next();
                if (o instanceof SOAPHeaderBlock) {
                    SOAPHeaderBlock header = (SOAPHeaderBlock) o;
                    faultEnvelope.getHeader().addChild(header);
                } else if (o instanceof OMElement) {
                    faultEnvelope.getHeader().addChild((OMElement) o);
                }
            }
        }
    }

    try {
        messageContext.setEnvelope(faultEnvelope);
    } catch (AxisFault af) {
        log.error("Error while setting SOAP fault as payload", af);
        return;
    }

    if (messageContext.getFaultTo() != null) {
        messageContext.setTo(messageContext.getFaultTo());
    } else if (messageContext.getReplyTo() != null) {
        messageContext.setTo(messageContext.getReplyTo());
    } else {
        messageContext.setTo(null);
    }

    // set original messageID as relatesTo
    if (messageContext.getMessageID() != null) {
        RelatesTo relatesTo = new RelatesTo(messageContext.getMessageID());
        messageContext.setRelatesTo(new RelatesTo[] { relatesTo });
    }
}