Monday, February 20, 2017

How to enable specific Builder and Formatter in mediation flow in WSO2 ESB 4.9.0

This article explains a need where we need to enable Builder and Formatter in a mediation flow, which is only needed for a specific configuration in the Proxy Service and not for global. By default for global settings on builders and formatters, we do at axis2.xml, in this case we are going to override the global setting for a particular proxy service.

Below is the sample proxy service which handles a similar requirement.
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="BuilderMediatorCheckProxy"
       transports="https http"
       startOnLoad="true"
       trace="disable">
   <description/>
   <target>
      <inSequence>
         <log level="custom">
            <property name="STATUS"
                      value="-------------------BuilderMediatorCheckProxy IN Invoked-------------------"/>
         </log>
         <builder>           
            <messageBuilder contentType="application/json"
                            class="org.apache.synapse.commons.json.JsonStreamBuilder"
                            formatterClass="org.apache.synapse.commons.json.JsonStreamFormatter"/>           
         </builder>
         <log level="full"/>
         <send>
            <endpoint>
               <http uri-template="http://www.mocky.io/v2/58aa8e97100000060d4b626c"/>
            </endpoint>
         </send>
      </inSequence>
      <outSequence>
         <log level="custom">
            <property name="STATUS"
                      value="-------------------BuilderMediatorCheckProxy OUT Invoked-------------------"/>
         </log>
         <log level="full"/>
         <send/>
      </outSequence>
   </target>
</proxy>


Note: If you are using a send / call mediator and in the outsequence or insequence you need to specify use same builder, then you need to again define it as below, as when the send/call is made the proxy level previous setting will be overridden by the axis2.xml global settings.


A sample proxy service for that kind of scenario as below:
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="BuilderMediatorCheckProxy"
       transports="https http"
       startOnLoad="true"
       trace="disable">
   <description/>
   <target>
      <inSequence>
         <log level="custom">
            <property name="STATUS"
                      value="-------------------BuilderMediatorCheckProxy IN Invoked-------------------"/>
         </log>
         <builder>           
            <messageBuilder contentType="application/json"
                            class="org.apache.synapse.commons.json.JsonStreamBuilder"
                            formatterClass="org.apache.synapse.commons.json.JsonStreamFormatter"/>           
         </builder>
         <log level="full"/>
         <send>
            <endpoint>
               <http uri-template="http://www.mocky.io/v2/58aa8e97100000060d4b626c"/>
            </endpoint>
         </send>
      </inSequence>
      <outSequence>
         <log level="custom">
            <property name="STATUS"
                      value="-------------------BuilderMediatorCheckProxy OUT Invoked-------------------"/>
         </log>
         <builder>           
            <messageBuilder contentType="application/json"
                            class="org.apache.synapse.commons.json.JsonStreamBuilder"
                            formatterClass="org.apache.synapse.commons.json.JsonStreamFormatter"/>           
         </builder>
         <log level="full"/>
         <send/>
      </outSequence>
   </target>
</proxy>


Before and After logs are below. You can observe that as our global axis2.xml pointing to org.wso2.carbon.relay.BinaryRelayBuilder, the outsequence of the first sample proxy service build the message as binary and the second proxy message build in the json format.

Log for Proxy - 1
[2017-02-20 14:11:29,531]  INFO - ProxyService Stopped the proxy service : BuilderMediatorCheckProxy
[2017-02-20 14:11:29,531]  INFO - DeploymentInterceptor Removing Axis2 Service: BuilderMediatorCheckProxy {super-tenant}
[2017-02-20 14:11:29,535]  INFO - ProxyService Building Axis service for Proxy service : BuilderMediatorCheckProxy
[2017-02-20 14:11:29,536]  INFO - ProxyService Adding service BuilderMediatorCheckProxy to the Axis2 configuration
[2017-02-20 14:11:29,537]  INFO - DeploymentInterceptor Deploying Axis2 service: BuilderMediatorCheckProxy {super-tenant}
[2017-02-20 14:11:29,537]  INFO - ProxyService Successfully created the Axis2 service for Proxy service : BuilderMediatorCheckProxy
[2017-02-20 14:11:29,537]  INFO - ProxyServiceDeployer ProxyService named 'BuilderMediatorCheckProxy' has been update from file : /home/ajanthan/wso2/blog/builders_check/wso2esb-4.9.0/repository/deployment/server/synapse-configs/default/proxy-services/BuilderMediatorCheckProxy.xml
[2017-02-20 14:11:34,590] DEBUG - wire >> "POST /services/BuilderMediatorCheckProxy HTTP/1.1[\r][\n]"
[2017-02-20 14:11:34,591] DEBUG - wire >> "Host: ajanthan-thinkpad-t440p:8280[\r][\n]"
[2017-02-20 14:11:34,591] DEBUG - wire >> "Connection: keep-alive[\r][\n]"
[2017-02-20 14:11:34,591] DEBUG - wire >> "Content-Length: 23[\r][\n]"
[2017-02-20 14:11:34,591] DEBUG - wire >> "Postman-Token: f0010840-1ba4-8989-12be-9e0cd29b09c4[\r][\n]"
[2017-02-20 14:11:34,591] DEBUG - wire >> "Cache-Control: no-cache[\r][\n]"
[2017-02-20 14:11:34,592] DEBUG - wire >> "Origin: chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop[\r][\n]"
[2017-02-20 14:11:34,592] DEBUG - wire >> "User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36[\r][\n]"
[2017-02-20 14:11:34,592] DEBUG - wire >> "Content-Type: application/json[\r][\n]"
[2017-02-20 14:11:34,592] DEBUG - wire >> "Accept: */*[\r][\n]"
[2017-02-20 14:11:34,592] DEBUG - wire >> "Accept-Encoding: gzip, deflate[\r][\n]"
[2017-02-20 14:11:34,592] DEBUG - wire >> "Accept-Language: en-US,en;q=0.8[\r][\n]"
[2017-02-20 14:11:34,593] DEBUG - wire >> "[\r][\n]"
[2017-02-20 14:11:34,593] DEBUG - wire >> "{"test":"from postman"}"
[2017-02-20 14:11:34,594]  INFO - LogMediator STATUS = -------------------BuilderMediatorCheckProxy IN Invoked-------------------
[2017-02-20 14:11:34,595]  INFO - LogMediator To: /services/BuilderMediatorCheckProxy, MessageID: urn:uuid:7029af4a-3845-4dae-b4d2-625ce55986bb, Direction: request, Payload: {"test":"from postman"}
[2017-02-20 14:11:34,911] DEBUG - wire << "POST /v2/58aaaac8100000b90e4b62ba HTTP/1.1[\r][\n]"
[2017-02-20 14:11:34,911] DEBUG - wire << "Accept-Language: en-US,en;q=0.8[\r][\n]"
[2017-02-20 14:11:34,911] DEBUG - wire << "Accept-Encoding: gzip, deflate[\r][\n]"
[2017-02-20 14:11:34,912] DEBUG - wire << "Origin: chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop[\r][\n]"
[2017-02-20 14:11:34,912] DEBUG - wire << "Postman-Token: f0010840-1ba4-8989-12be-9e0cd29b09c4[\r][\n]"
[2017-02-20 14:11:34,912] DEBUG - wire << "Content-Type: application/json[\r][\n]"
[2017-02-20 14:11:34,912] DEBUG - wire << "Accept: */*[\r][\n]"
[2017-02-20 14:11:34,912] DEBUG - wire << "Cache-Control: no-cache[\r][\n]"
[2017-02-20 14:11:34,912] DEBUG - wire << "Transfer-Encoding: chunked[\r][\n]"
[2017-02-20 14:11:34,912] DEBUG - wire << "Host: www.mocky.io:80[\r][\n]"
[2017-02-20 14:11:34,912] DEBUG - wire << "Connection: Keep-Alive[\r][\n]"
[2017-02-20 14:11:34,912] DEBUG - wire << "User-Agent: Synapse-PT-HttpComponents-NIO[\r][\n]"
[2017-02-20 14:11:34,913] DEBUG - wire << "[\r][\n]"
[2017-02-20 14:11:34,913] DEBUG - wire << "17[\r][\n]"
[2017-02-20 14:11:34,913] DEBUG - wire << "{"test":"from postman"}[\r][\n]"
[2017-02-20 14:11:34,913] DEBUG - wire << "0[\r][\n]"
[2017-02-20 14:11:34,913] DEBUG - wire << "[\r][\n]"
[2017-02-20 14:11:35,165] DEBUG - wire >> "HTTP/1.1 500 Internal Server Error[\r][\n]"
[2017-02-20 14:11:35,165] DEBUG - wire >> "Server: Cowboy[\r][\n]"
[2017-02-20 14:11:35,165] DEBUG - wire >> "Connection: close[\r][\n]"
[2017-02-20 14:11:35,165] DEBUG - wire >> "Content-Type: application/json; charset=utf-8[\r][\n]"
[2017-02-20 14:11:35,166] DEBUG - wire >> "Date: Mon, 20 Feb 2017 08:41:34 GMT[\r][\n]"
[2017-02-20 14:11:35,166] DEBUG - wire >> "Via: 1.1 vegur[\r][\n]"
[2017-02-20 14:11:35,166] DEBUG - wire >> "[\r][\n]"
[2017-02-20 14:11:35,166] DEBUG - wire >> "{[\n]"
[2017-02-20 14:11:35,166] DEBUG - wire >> "    "status": "INTERNAL PROBLEM",[\n]"
[2017-02-20 14:11:35,166] DEBUG - wire >> "    "errors": [{[\n]"
[2017-02-20 14:11:35,167] DEBUG - wire >> "[0x9][0x9]"error_code": "500123",[\n]"
[2017-02-20 14:11:35,167] DEBUG - wire >> "[0x9][0x9]"error_name": "Custom Error Message",[\n]"
[2017-02-20 14:11:35,167] DEBUG - wire >> "[0x9][0x9]"error_message": "Class not Specified"[\n]"
[2017-02-20 14:11:35,167] DEBUG - wire >> "[0x9]}][\n]"
[2017-02-20 14:11:35,167] DEBUG - wire >> "}"
[2017-02-20 14:11:35,169]  INFO - LogMediator STATUS = -------------------BuilderMediatorCheckProxy OUT Invoked-------------------
[2017-02-20 14:11:35,170]  INFO - LogMediator To: http://www.w3.org/2005/08/addressing/anonymous, WSAction: , SOAPAction: , MessageID: urn:uuid:9e55fc0b-45f1-4443-bf57-e379780697d8, Direction: response, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"><soapenv:Body><ns:binary xmlns:ns="http://ws.apache.org/commons/ns/payload">ewogICAgInN0YXR1cyI6ICJJTlRFUk5BTCBQUk9CTEVNIiwKICAgICJlcnJvcnMiOiBbewoJCSJlcnJvcl9jb2RlIjogIjUwMDEyMyIsCgkJImVycm9yX25hbWUiOiAiQ3VzdG9tIEVycm9yIE1lc3NhZ2UiLAoJCSJlcnJvcl9tZXNzYWdlIjogIkNsYXNzIG5vdCBTcGVjaWZpZWQiCgl9XQp9</ns:binary></soapenv:Body></soapenv:Envelope>
[2017-02-20 14:11:35,171] DEBUG - wire << "HTTP/1.1 500 Internal Server Error[\r][\n]"
[2017-02-20 14:11:35,172] DEBUG - wire << "Via: 1.1 vegur[\r][\n]"
[2017-02-20 14:11:35,172] DEBUG - wire << "Content-Type: application/json; charset=utf-8; charset=utf-8[\r][\n]"
[2017-02-20 14:11:35,172] DEBUG - wire << "Date: Mon, 20 Feb 2017 08:41:35 GMT[\r][\n]"
[2017-02-20 14:11:35,172] DEBUG - wire << "Transfer-Encoding: chunked[\r][\n]"
[2017-02-20 14:11:35,173] DEBUG - wire << "Connection: keep-alive[\r][\n]"
[2017-02-20 14:11:35,173] DEBUG - wire << "[\r][\n]"
[2017-02-20 14:11:35,173] DEBUG - wire << "a5[\r][\n]"
[2017-02-20 14:11:35,173] DEBUG - wire << "{[\n]"
[2017-02-20 14:11:35,173] DEBUG - wire << "    "status": "INTERNAL PROBLEM",[\n]"
[2017-02-20 14:11:35,173] DEBUG - wire << "    "errors": [{[\n]"
[2017-02-20 14:11:35,174] DEBUG - wire << "[0x9][0x9]"error_code": "500123",[\n]"
[2017-02-20 14:11:35,174] DEBUG - wire << "[0x9][0x9]"error_name": "Custom Error Message",[\n]"
[2017-02-20 14:11:35,174] DEBUG - wire << "[0x9][0x9]"error_message": "Class not Specified"[\n]"
[2017-02-20 14:11:35,174] DEBUG - wire << "[0x9]}][\n]"
[2017-02-20 14:11:35,174] DEBUG - wire << "}[\r][\n]"
[2017-02-20 14:11:35,174] DEBUG - wire << "0[\r][\n]"
[2017-02-20 14:11:35,175] DEBUG - wire << "[\r][\n]"


Log for Proxy - 2
[2017-02-20 14:08:21,792] DEBUG - wire >> "POST /services/BuilderMediatorCheckProxy HTTP/1.1[\r][\n]"
[2017-02-20 14:08:21,792] DEBUG - wire >> "Host: ajanthan-thinkpad-t440p:8280[\r][\n]"
[2017-02-20 14:08:21,792] DEBUG - wire >> "Connection: keep-alive[\r][\n]"
[2017-02-20 14:08:21,792] DEBUG - wire >> "Content-Length: 23[\r][\n]"
[2017-02-20 14:08:21,792] DEBUG - wire >> "Postman-Token: 0c6f6ce0-73e9-236e-4c01-158a6679d8cf[\r][\n]"
[2017-02-20 14:08:21,792] DEBUG - wire >> "Cache-Control: no-cache[\r][\n]"
[2017-02-20 14:08:21,793] DEBUG - wire >> "Origin: chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop[\r][\n]"
[2017-02-20 14:08:21,793] DEBUG - wire >> "User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36[\r][\n]"
[2017-02-20 14:08:21,793] DEBUG - wire >> "Content-Type: application/json[\r][\n]"
[2017-02-20 14:08:21,793] DEBUG - wire >> "Accept: */*[\r][\n]"
[2017-02-20 14:08:21,793] DEBUG - wire >> "Accept-Encoding: gzip, deflate[\r][\n]"
[2017-02-20 14:08:21,793] DEBUG - wire >> "Accept-Language: en-US,en;q=0.8[\r][\n]"
[2017-02-20 14:08:21,793] DEBUG - wire >> "[\r][\n]"
[2017-02-20 14:08:21,793] DEBUG - wire >> "{"test":"from postman"}"
[2017-02-20 14:08:21,795]  INFO - LogMediator STATUS = -------------------BuilderMediatorCheckProxy IN Invoked-------------------
[2017-02-20 14:08:21,795]  INFO - LogMediator To: /services/BuilderMediatorCheckProxy, MessageID: urn:uuid:aa1eb6e8-a889-4ac8-a3a6-fd7bd20ab336, Direction: request, Payload: {"test":"from postman"}
[2017-02-20 14:08:22,133] DEBUG - wire << "POST /v2/58aaaac8100000b90e4b62ba HTTP/1.1[\r][\n]"
[2017-02-20 14:08:22,133] DEBUG - wire << "Accept-Language: en-US,en;q=0.8[\r][\n]"
[2017-02-20 14:08:22,133] DEBUG - wire << "Accept-Encoding: gzip, deflate[\r][\n]"
[2017-02-20 14:08:22,133] DEBUG - wire << "Origin: chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop[\r][\n]"
[2017-02-20 14:08:22,134] DEBUG - wire << "Postman-Token: 0c6f6ce0-73e9-236e-4c01-158a6679d8cf[\r][\n]"
[2017-02-20 14:08:22,134] DEBUG - wire << "Content-Type: application/json[\r][\n]"
[2017-02-20 14:08:22,134] DEBUG - wire << "Accept: */*[\r][\n]"
[2017-02-20 14:08:22,134] DEBUG - wire << "Cache-Control: no-cache[\r][\n]"
[2017-02-20 14:08:22,134] DEBUG - wire << "Transfer-Encoding: chunked[\r][\n]"
[2017-02-20 14:08:22,134] DEBUG - wire << "Host: www.mocky.io:80[\r][\n]"
[2017-02-20 14:08:22,134] DEBUG - wire << "Connection: Keep-Alive[\r][\n]"
[2017-02-20 14:08:22,134] DEBUG - wire << "User-Agent: Synapse-PT-HttpComponents-NIO[\r][\n]"
[2017-02-20 14:08:22,134] DEBUG - wire << "[\r][\n]"
[2017-02-20 14:08:22,134] DEBUG - wire << "17[\r][\n]"
[2017-02-20 14:08:22,135] DEBUG - wire << "{"test":"from postman"}[\r][\n]"
[2017-02-20 14:08:22,135] DEBUG - wire << "0[\r][\n]"
[2017-02-20 14:08:22,135] DEBUG - wire << "[\r][\n]"
[2017-02-20 14:08:22,397] DEBUG - wire >> "HTTP/1.1 500 Internal Server Error[\r][\n]"
[2017-02-20 14:08:22,397] DEBUG - wire >> "Server: Cowboy[\r][\n]"
[2017-02-20 14:08:22,397] DEBUG - wire >> "Connection: close[\r][\n]"
[2017-02-20 14:08:22,397] DEBUG - wire >> "Content-Type: application/json; charset=utf-8[\r][\n]"
[2017-02-20 14:08:22,397] DEBUG - wire >> "Date: Mon, 20 Feb 2017 08:38:22 GMT[\r][\n]"
[2017-02-20 14:08:22,397] DEBUG - wire >> "Via: 1.1 vegur[\r][\n]"
[2017-02-20 14:08:22,397] DEBUG - wire >> "[\r][\n]"
[2017-02-20 14:08:22,398] DEBUG - wire >> "{[\n]"
[2017-02-20 14:08:22,398] DEBUG - wire >> "    "status": "INTERNAL PROBLEM",[\n]"
[2017-02-20 14:08:22,398] DEBUG - wire >> "    "errors": [{[\n]"
[2017-02-20 14:08:22,398] DEBUG - wire >> "[0x9][0x9]"error_code": "500123",[\n]"
[2017-02-20 14:08:22,398] DEBUG - wire >> "[0x9][0x9]"error_name": "Custom Error Message",[\n]"
[2017-02-20 14:08:22,398] DEBUG - wire >> "[0x9][0x9]"error_message": "Class not Specified"[\n]"
[2017-02-20 14:08:22,398] DEBUG - wire >> "[0x9]}][\n]"
[2017-02-20 14:08:22,398] DEBUG - wire >> "}"
[2017-02-20 14:08:22,399]  INFO - LogMediator STATUS = -------------------BuilderMediatorCheckProxy OUT Invoked-------------------
[2017-02-20 14:08:22,400]  INFO - LogMediator To: http://www.w3.org/2005/08/addressing/anonymous, WSAction: , SOAPAction: , MessageID: urn:uuid:7757cf75-1883-443e-8e6b-dbe86d8348b1, Direction: response, Payload: {
    "status": "INTERNAL PROBLEM",
    "errors": [{
  "error_code": "500123",
  "error_name": "Custom Error Message",
  "error_message": "Class not Specified"
 }]
}
[2017-02-20 14:08:22,401] DEBUG - wire << "HTTP/1.1 500 Internal Server Error[\r][\n]"
[2017-02-20 14:08:22,401] DEBUG - wire << "Via: 1.1 vegur[\r][\n]"
[2017-02-20 14:08:22,401] DEBUG - wire << "Content-Type: application/json; charset=utf-8; charset=utf-8[\r][\n]"
[2017-02-20 14:08:22,401] DEBUG - wire << "Date: Mon, 20 Feb 2017 08:38:22 GMT[\r][\n]"
[2017-02-20 14:08:22,401] DEBUG - wire << "Transfer-Encoding: chunked[\r][\n]"
[2017-02-20 14:08:22,402] DEBUG - wire << "Connection: keep-alive[\r][\n]"
[2017-02-20 14:08:22,402] DEBUG - wire << "[\r][\n]"
[2017-02-20 14:08:22,402] DEBUG - wire << "a5[\r][\n]"
[2017-02-20 14:08:22,402] DEBUG - wire << "{[\n]"
[2017-02-20 14:08:22,402] DEBUG - wire << "    "status": "INTERNAL PROBLEM",[\n]"
[2017-02-20 14:08:22,403] DEBUG - wire << "    "errors": [{[\n]"
[2017-02-20 14:08:22,403] DEBUG - wire << "[0x9][0x9]"error_code": "500123",[\n]"
[2017-02-20 14:08:22,403] DEBUG - wire << "[0x9][0x9]"error_name": "Custom Error Message",[\n]"
[2017-02-20 14:08:22,403] DEBUG - wire << "[0x9][0x9]"error_message": "Class not Specified"[\n]"
[2017-02-20 14:08:22,403] DEBUG - wire << "[0x9]}][\n]"
[2017-02-20 14:08:22,403] DEBUG - wire << "}[\r][\n]"
[2017-02-20 14:08:22,404] DEBUG - wire << "0[\r][\n]"
[2017-02-20 14:08:22,404] DEBUG - wire << "[\r][\n]"

No comments:

Post a Comment