Search code examples
wso2wso2-enterprise-integrator

WSO2 - Enterprise integrator 6.5.0 returning PDF


We have an api that returns a PDF in the following format:

{ "binary": "binarycontent" }

how can we handle this response in the sequence to return a PDF?

Note: we added under axis2.xml the message

Under message formatters:
<messageFormatter contentType="application/pdf" 
                    class="org.wso2.carbon.relay.ExpandingMessageFormatter"/>

Under message builders
<messageBuilder contentType="application/pdf" 
                        class="org.wso2.carbon.relay.BinaryRelayBuilder"/>

We tried to loopback the response as it is but it did not work, it is returning an empty response with no option to download the file.


Solution

  • Solution:

               <property expression="get-property('axis2', 'HTTP_SC')" name="HTTP_STATUS_CODE" scope="default" type="STRING"/>
            <filter regex="true" source="get-property('HTTP_STATUS_CODE') = '200'">
                <then>
                    <property expression="json-eval($.binary)" name="base64Response" scope="default" type="STRING"/>
                    <log category="INFO" level="custom">
                        <property name="Encoded PDF " expression="get-property('base64Response')"/>
                    </log>
                    
                    <!-- Set the HTTP status code for the response -->
                    <property name="HTTP_SC" scope="default" type="STRING" value="200"/>
    
                    <payloadFactory media-type="xml">
                        <format>
                            <ns:binary xmlns:ns="http://ws.apache.org/commons/ns/payload">$1</ns:binary>
                        </format>
                        <args>
                            <arg evaluator="xml" expression="get-property('base64Response')"/>
                        </args>
                    </payloadFactory>
           
                    <script language="js">
                        var binaryNode = mc.getEnvelope().getBody().getFirstElement().getFirstOMChild();  
                        binaryNode.setBinary(true);
                    </script>
                    <property name="messageType" scope="axis2" type="STRING" value="application/pdf"/>
                </then>
                <else> // handle error </else> </filter>