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

Friday, October 23, 2015

Base64 Encode Payload Body WSO2 ESB

In this tutorial, I have posted sample ESB API which encode the payload to Base64 and return it back. This has achieved through the xpath base64Encode function.

<api context="/test" name="test">
      <resource faultsequence="fault" methods="POST" url-mapping="/status">
         <insequence>
            <property expression="$body/*" name="messageBody" scope="default">
            <property expression="base64Encode(get-property('messageBody'))" name="EncodeBody" scope="default">
            <log level="custom">
               <property expression="get-property('messageBody')" name="Request Payload">
            </property></log>
            <enrich>
               <source clone="true" type="inline"></source>
                  <soapenv:envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
                     <soapenv:body>
                        <encoded xmlns="">
                     </encoded></soapenv:body>
                  </soapenv:envelope>
               <target type="envelope">
            </target></enrich>
            <enrich>
               <source clone="true" xmlns:ns="http://org.apache.synapse/xsd" xpath="get-property('EncodeBody')"></source>
               <target type="body">
            </target></enrich>
            <property name="messageType" scope="axis2" type="STRING" value="application/xml">
            <respond>
         </respond></property></property></property></property></insequence>
      </resource>
</api>


Using a rest client post following xml content

<testencode><encode>abc</encode></testencode>

Result will be returning in following format
<encoded>{ENCODED_STRIN}</encoded>