Tuesday, 10 September 2019

ESB 8
WSO2 ESB Tutorial:
Creating SOAP Faults and
Changing the Direction of
Messages

Index

1. Introduction

2. Fault Mediator

3. EI configuration example 1

4. Running example 1.

5. EI configuration example 2.

6. Running example 2

7. Conclusion


1. Introduction

This example uses a makefaul mediator to construct custom SOAP fault and change the direction (in/out) of the
message, this is done with the use of the TO header property as we will see. The request is sent by the Client to the
backend through an ESB. If there is a problem, an error message is sent back to the client.
Note: EI means Enterprise Integrator.

2. Fault Mediator

The Fault Mediator (it is also called the Makefault Mediator) transforms the current message into a fault message
that is sent to the Client. Do not forget the Send Mediator to send a fault message created via the Fault mediator, because the makefault mediator does not send the message back to the Client by default. The fault message's To
header is set to the Fault-To of the original message (if such a header exists in the original message). You can
create the fault message as a SOAP 1.1, SOAP 1.2, or plain-old XML (POX) fault.

Example:
<makefault version=”soap12”>
   <code xmlns:tns="http://www.w3.org/2003/05/soap-envelope" value="tns:Receiver"/>
   <reason expression="get-property('ERROR_MESSAGE')"/>
</makefault>

This is to specify the fault string for server side errors:
<code xmlns:tns="http://www.w3.org/2003/05/soap-envelope" value="tns:Receiver"/>

The fault string is specified as an expression.
<reason expression="get-property('ERROR_MESSAGE')"/>

Note:
Tns:Receiver -> For Server side errors.
Tns:Sender   -> For Client side errors.

3. EI configuration example 1

The property RESPONSE is set to 'true' after the makefault mediator. This instructs the EI to change the direction
of messages to 'response' as messages are transformed into SOAP faults. The value of
ReplyTo: http://www.w3.org/2005/08/addressing/anonymous, it means that the answers is sent to the Client that
sent the request.




4. Running example 1.

Start the Axis2 server with the back-end service SimpleStockQuoteService. The sample client used here is the
Stock Quote Client. You can use SOAPUI as the Client too.
The client sends a stock quote request with the symbol: MSFT but the backend responds with an unknown host
exception. EI captures the error and returns to the Client a SOAP fault response. For the second request the
backends sends a connection refused exception as another SOAP fault response.

-       An unknown host exception would be generated for the MSFT stock request. Run the following command from
the <ESB_HOME>/samples/axis2Client directory to trigger a MSFT stock quote request to the back-end service.

Request:
ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/
-Dsymbol=MSFT

This is the request that is sent and it can be used with SOAPUI too:



Response from EI:


-       A connection refused exception would be generated for the SUN stock request. 
Run the following command from the <ESB_HOME>/samples/axis2Client directory to trigger a SUN stock quote
request to the back-end service.

Request:
ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=SUN

This is the request that is sent and it can be used with SOAPUI too:


Response from EI:


 

5. EI configuration example 2

The following proxy validates the content type using the Filter Mediator based on the Content-Type header property.
If the result is true, it sends an exception back to the client using the Fault Mediator. Else, if the result is false, it
continues the flow.


 

6. Running example 2

To send a few requests first of all import WSDL file into a SOAPUI project to be able to
generate requests. As we show, the proxy publishes for us the methods used by the service. It is visible through a
browser:


The same URI is used in SOAPUI to generate the requests:


This is an example of a SOAP request against the service using a correct Content-Type (text/xml):


The message is sent to the backend. Now I am going to send the same request but with an invalid Content-Type
(application/xhtml+xml):


As you can see, and accordingly to the ESB configuration, a fault message is sent to the Client.

7. Conclusion

As it has been demonstrated, the use of fault mediator, manages the fault generation of messages following the
good practices. 


No comments:

Post a Comment