Response transformers on Mule outbound endpoints

A coworker found a message on the mule-user mailing list that said that you have to apply response transformers on inbound endpoints, not on outbound endpoints. Sounds a bit weird to me, at least not very intuitive. Time for a test.

Response transformer in a pass-through-router

Here’s the mule config:

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesource.org/schema/mule/core/2.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:http="http://www.mulesource.org/schema/mule/http/2.2" xmlns:vm="http://www.mulesource.org/schema/mule/vm/2.2"
    xmlns:scripting="http://www.mulesource.org/schema/mule/scripting/2.2"
    xsi:schemaLocation="
http://www.mulesource.org/schema/mule/scripting/2.2 
http://www.mulesource.org/schema/mule/scripting/2.2/mule-scripting.xsd
http://www.mulesource.org/schema/mule/core/2.2 
http://www.mulesource.org/schema/mule/core/2.2/mule.xsd
http://www.mulesource.org/schema/mule/http/2.2 
http://www.mulesource.org/schema/mule/http/2.2/mule-http.xsd
http://www.mulesource.org/schema/mule/vm/2.2 
http://www.mulesource.org/schema/mule/vm/2.2/mule-vm.xsd">

  <scripting:transformer name="xTransformer">
    <scripting:script engine="groovy">
      <![CDATA[
      payload + "x"
      ]]>
    </scripting:script>
  </scripting:transformer>
  
  <byte-array-to-string-transformer name="byteArrayToString" />

  <model name="sync-multicast-example">

    <service name="s1">
      <inbound>
        <http:inbound-endpoint host="localhost" port="11110" path="response-transformer-example" synchronous="true"
          name="s1.inbound.http"  />
      </inbound>     
      <outbound>
        <pass-through-router>
          <http:outbound-endpoint host="localhost" port="11110" path="service1" synchronous="true"
          name="s1.outbound.http.1" responseTransformer-refs="byteArrayToString xTransformer" />
        </pass-through-router>
      </outbound>
    </service>

    <service name="service1">
      <inbound>
        <http:inbound-endpoint host="localhost" port="11110" path="service1" synchronous="true"
          name="service1.inbound.http"  />
      </inbound>
      <scripting:component>
        <scripting:script engine="groovy">
          <![CDATA[
          String s = "Service 1 result"
          return s
          ]]>
        </scripting:script>
      </scripting:component>
    </service>

  </model>
</mule>

Here’s how i start Mule:

#!/bin/bash
export MULE_HOME=/home/akoelewijn/programs/mule-standalone-2.2.1/
$MULE_HOME/bin/mule -config ./mule-config.xml

And here’s my test file:

#!/bin/bash
echo "<hello>World</echo>" | 
  curl -X POST -d @- http://localhost:11110/response-transformer-example

This results in the following:

Service 1 resultx

The result seems fine, the response transformer is called, and an x is appended to the result of calling service1. Maybe the problem has to do with chaining routers? Lets try that.

Response transformer in a chaining router

Here’s the mule config:

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesource.org/schema/mule/core/2.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:http="http://www.mulesource.org/schema/mule/http/2.2" xmlns:vm="http://www.mulesource.org/schema/mule/vm/2.2"
    xmlns:scripting="http://www.mulesource.org/schema/mule/scripting/2.2"
    xsi:schemaLocation="
http://www.mulesource.org/schema/mule/scripting/2.2 
http://www.mulesource.org/schema/mule/scripting/2.2/mule-scripting.xsd
http://www.mulesource.org/schema/mule/core/2.2 
http://www.mulesource.org/schema/mule/core/2.2/mule.xsd
http://www.mulesource.org/schema/mule/http/2.2 
http://www.mulesource.org/schema/mule/http/2.2/mule-http.xsd
http://www.mulesource.org/schema/mule/vm/2.2 
http://www.mulesource.org/schema/mule/vm/2.2/mule-vm.xsd">

  <scripting:transformer name="xTransformer">
    <scripting:script engine="groovy">
      <![CDATA[
      payload + "x"
      ]]>
    </scripting:script>
  </scripting:transformer>
  
  <byte-array-to-string-transformer name="byteArrayToString" />

  <model name="sync-multicast-example">

    <service name="s1">
      <inbound>
        <http:inbound-endpoint host="localhost" port="11110" path="chaining-response-transformer-example" synchronous="true"
          name="s1.inbound.http"  />
      </inbound>     
      <outbound>
        <chaining-router>
          <http:outbound-endpoint host="localhost" port="11110" path="service1" synchronous="true"
          name="s1.outbound.http.1" responseTransformer-refs="byteArrayToString xTransformer"/>
          <http:outbound-endpoint host="localhost" port="11110" path="service2" synchronous="true"
          name="s1.outbound.http.2" responseTransformer-refs="byteArrayToString xTransformer"/>
        </chaining-router>
      </outbound>
    </service>

    <service name="service1">
      <inbound>
        <http:inbound-endpoint host="localhost" port="11110" path="service1" synchronous="true"
          name="service1.inbound.http"  />
      </inbound>
      <scripting:component>
        <scripting:script engine="groovy">
          <![CDATA[
          String s = "Service 1 result"
          return s
          ]]>
        </scripting:script>
      </scripting:component>
    </service>

    <service name="service2">
      <inbound>
        <http:inbound-endpoint host="localhost" port="11110" path="service2" synchronous="true"
          name="service2.inbound.http" transformer-refs="byteArrayToString" />
      </inbound>
      <scripting:component>
        <scripting:script engine="groovy">
          <![CDATA[
          log.info("payload: ${payload}")
          String s = "Service 2 result"
          return payload + s
          ]]>
        </scripting:script>
      </scripting:component>
    </service>

  </model>
</mule>

The result of calling http://localhost:11110/chaining-response-transformer-example with curl:

Service 1 resultxService 2 resultx

Again, the response transformers seem to work fine. As far as i can tell, there’s no reason not to use response transformers on outbound endpoints.

blog comments powered by Disqus