Wednesday, October 28, 2015

Send SOAP Fault and Message in a Mail When SOAP Fault occurs WSO2 ESB

During this tutorial, I'm going to discuss on how to send a mail with body and attachment with WSO2 ESB 4.9.0. In this tutorial, when there is a soap fault, it will send the soap fault as attachment and the payload that cause soap fault in the body. Configure the mail transport by following [1].
<?xml version="1.0"?>
<proxy name="StockQuoteProxy" startOnLoad="true" trace="disable" transports="https http">
  <description/>
  <target>
    <endpoint>
      <address uri="http://localhost:9000/services/SimpleStockQuoteService">
        <timeout>
          <duration>30000</duration>
          <responseAction>fault</responseAction>
        </timeout>
        <suspendOnFailure>
          <errorCodes>-1</errorCodes>
          <initialDuration>0</initialDuration>
          <progressionFactor>1.0</progressionFactor>
          <maximumDuration>0</maximumDuration>
        </suspendOnFailure>
        <markForSuspension>
          <errorCodes>-1</errorCodes>
        </markForSuspension>
      </address>
    </endpoint>
    <inSequence>
      <property name="FORCE_ERROR_ON_SOAP_FAULT" value="true"/>
      <property expression="$body/*" name="messageBody" scope="default" type="STRING"/>
    </inSequence>
    <outSequence>
      <log level="custom">
        <property expression="$ctx:messageBody" name="bodyOfMessage"/>
      </log>
      <send/>
    </outSequence>
    <faultSequence>
      <sequence key="fault_email_seq"/>
      <send/>
    </faultSequence>
  </target>
  <publishWSDL uri="file:repository/samples/resources/proxy/sample_proxy_1.wsdl"/>
</proxy>

Sample Mail Sequence which sends the mail

<?xml version="1.0"?>
<sequence xmlns="http://ws.apache.org/ns/synapse" name="fault_email_seq" trace="disable">
  <clone continueParent="true">
    <target>
      <sequence>
        <property expression="fn:concat('An Error Occurred Service',' : ',$ctx:proxy.name)" name="Subject" scope="transport" type="STRING"/>
        <property name="OUT_ONLY" scope="default" type="STRING" value="true"/>
        <property name="transport.mail.Format" scope="axis2" type="STRING" value="Attachment"/>
        <property name="AttachmentFile" scope="axis2" type="STRING" value="ErrorMessage.xml"/>
        <property expression="$ctx:messageBody" name="transport.mail.bodyWhenAttached" scope="axis2" type="STRING"/>
        <header action="remove" name="To" scope="default"/>
        <property action="remove" name="NO_ENTITY_BODY" scope="axis2"/>
        <property name="RESPONSE" scope="default" type="STRING" value="true"/>
        <property name="ContentType" scope="axis2" type="STRING" value="text/html"/>
        <property name="messageType" scope="axis2" type="STRING" value="text/html"/>
        <send>
          <endpoint>
            <address trace="disable" uri="mailto:test@gmail.com"/>
          </endpoint>
        </send>
      </sequence>
    </target>
  </clone>
</sequence>
[1] - https://docs.wso2.com/display/ESB490/MailTo+Transport

No comments:

Post a Comment