jsleeuw
jsleeuw

Reputation: 383

Mule: transform the results of outbound-endpoint

The following flow transforms incoming an JSON payload into a SOAP message which is used in a subflow to make a webservice request. Everything works fine -- i'm able to send a response back for the original (incoming) request, but I want to add a final step to transform the SOAP result into JSON before sending it back to the client.

Here is the flow:

<mule ...>
    <data-mapper:config name="json2xml_grf" transformationGraphPath="json2xml.grf" doc:name="DataMapper"/>
    <flow name="simpleFlow" doc:name="simpleFlow">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP"/>
        <set-property propertyName="Access-Control-Allow-Origin" value="*" doc:name="Property"/>
        <choice doc:name="Choice">
            <when expression="#[message.inboundProperties['http.method'] != 'OPTIONS']">
                <data-mapper:transform config-ref="json2xml_grf" doc:name="DataMapper"/>
                <flow-ref name="invokeCalculatorService" doc:name="invokeCalculator"/>
            </when>
            <otherwise>
                <http:response-builder status="200" doc:name="HTTP Response Builder">
                ...
                </http:response-builder>
            </otherwise>
        </choice>
    </flow>
    <flow name="invokeService" doc:name="invokeService">
        <cxf:proxy-client payload="body" enableMuleSoapHeaders="true" doc:name="Proxy Client">
        </cxf:proxy-client>
        <mulexml:xslt-transformer maxIdleTransformers="2" maxActiveTransformers="5" xsl-file="/Users/jsleeuw/MuleStudio/workspace/calc/src/main/resources/transformsoap.xslt" doc:name="XSLT"/>
        <http:outbound-endpoint exchange-pattern="request-response" doc:name="CalculatorService" method="POST" host="service-host" port="30001"/>
    </flow>
</mule>

Putting a DataMapper at the end of the subflow like this:

...
<flow-ref name="invokeCalculatorService" doc:name="invokeCalculator"/>
<data-mapper:transform config-ref="xml2json_grf" doc:name="DataMapper"/>
...

Results in this error:

********************************************************************************
Message               : Error executing graph: ERROR (com.mulesoft.mule.module.datamapper.api.exception.DataMapperExecutionException). Message payload is of type: DepthXMLStreamReader
Code                  : MULE_ERROR--2
--------------------------------------------------------------------------------
Exception stack is:
1. Content is not allowed in prolog. (org.xml.sax.SAXParseException)
  org.apache.xerces.util.ErrorHandlerWrapper:-1 (null)
2. org.xml.sax.SAXParseException: Content is not allowed in prolog. (net.sf.saxon.trans.DynamicError)
  net.sf.saxon.event.Sender:308 (null)
3. XPath evaluation failed (org.jetel.exception.JetelRuntimeException)
  org.jetel.component.tree.reader.xml.XmlXPathEvaluator:81 (null)
4. Error executing graph: ERROR (com.mulesoft.mule.module.datamapper.api.exception.DataMapperExecutionException)
  com.mulesoft.mule.module.datamapper.impl.DefaultGraphExecutor:83 (null)
5. Error executing graph: ERROR (com.mulesoft.mule.module.datamapper.api.exception.DataMapperExecutionException). Message payload is of type: DepthXMLStreamReader (com.mulesoft.mule.module.datamapper.processors.DataMapperMessageExecutionException)
  com.mulesoft.mule.module.datamapper.processors.DataMapperMessageProcessor:135 (null)
-------------------------------------------------------------------------------- 

Whereas putting the datamapper at the end of the subflow ends up blocking the response.

How should I go about transforming my response?

Upvotes: 0

Views: 3147

Answers (1)

David Dossot
David Dossot

Reputation: 33413

From the Content is not allowed in prolog exception it seems that <flow-ref name="invokeCalculatorService" /> doesn't return a payload that is parseable as XML.

This sub-flow returns a org.apache.cxf.staxutils.DepthXMLStreamReader, which is an javax.xml.stream.XMLStreamReader. The only transformer I know of that can deserialize this type to an XML string is: <mulexml:dom-to-xml-transformer />.

Can you add one just before the data-mapper element?

Upvotes: 1

Related Questions