1
votes

I need to create Soap Operation GetFile to respond with file content and additional tags using MTOM (reponse Content-Type multipart/related):

    <Response>
        <file>
            <id>1</id>
            <name>Filename.pdf</name>
            <content>
                <xop:Include href="cid:test" xmlns:xop="http://www.w3.org/2004/08/xop/include"/>
            </content>
        </file>
    </Response>

I have proxy which calls external service to get file content and then I generate payload using PayloadFactory mediator ($body/* in this case is file binary content from external service, id and name are hardcoded for simplicity):

<payloadFactory media-type="xml">
  <format>
      <Response>
          <file>
              <id>$1</id>
              <name>$2</name>
              <content>$3</content>
          </file>
      </Response>
  </format>
  <args>
      <arg value="1"/>
      <arg value="fileName.pdf"/>
      <arg evaluator="xml"
              expression="$body/*"/>
  </args>
</payloadFactory>
<property name="enableMTOM" scope="axis2" type="STRING" value="true"/>
<respond/>

In response I get:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <Response xmlns="http://ws.apache.org/ns/synapse">
            <file>
                <id>1</id>
                <name>fileName.pdf</name>
                <content>base64content</content>
            </file>
        </Response>
    </soapenv:Body>
</soapenv:Envelope>

If I remove that payloadFactory then I get correct multipart/related response, so enableMTOM property works (but I need additional custom tags):

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <ns:binary xmlns:ns="http://ws.apache.org/commons/ns/payload">
            <xop:Include href="cid:1" xmlns:xop="http://www.w3.org/2004/08/xop/include"/>
        </ns:binary>
    </soapenv:Body>
</soapenv:Envelope>

Is custom mediator with messageContext.addAttachment only solution in this case? And whats best practice in such case - save received file content locally on server and then use it as attachment?

1

1 Answers

0
votes

If you are constructing the message payload using a Payload factory mediator, and expecting to send the response as "multipart/form-data", you need to set the message type of the response after the Payload factory mediator. You can use the following property to set the message type of the response.

<property name="messageType" value="multipart/form-data" scope="axis2"/>

Furthermore, when we manipulate the file content using the Payload factory mediator, the message context gets built inside the Payload factory mediator and returned as a base64 encoded value. In order to decode the content, you need to add the following property to your proxy configuration.

<property name="DECODE_MULTIPART_DATA" value="true" scope="axis2" action="set" type="BOOLEAN"/>

Please refer to the latest WSO2 documentation on MTOM and SwA Optimizations and Request/Response Correlation for more details on this.

In a content-aware mediation scenario (where the message gets built), you can use the following property to decode the multipart message that is being sent to the backend. Otherwise, the outgoing message will be in encoded form.