WSO2 Multipart Binary Passthrough 和 MultipartFormData

WSO2 Multipart Binary Passthrough and MultipartFormData

我们目前使用 WSO EI 6.4 作为 ESB。

我们进行了配置以在 axis2.xml 中为一个碳应用程序获得行为“二进制直通”

<messageBuilder     contentType="multipart/form-data"   class="org.wso2.carbon.relay.BinaryRelayBuilder"/>
<messageFormatter   contentType="multipart/form-data"   class="org.wso2.carbon.relay.ExpandingMessageFormatter"/>

但是现在,我们需要开发一个需要创建多部分消息的新碳应用程序。 因此我们需要将配置更改为,如 this article

中所述
<messageBuilder     contentType="multipart/form-data"   class="org.apache.axis2.builder.MultipartFormDataBuilder" />
<messageFormatter   contentType="multipart/form-data"   class="org.apache.axis2.transport.http.MultipartFormDataFormatter"/>

但如果我们这样做,我们将失去“二进制直通”并破坏第一个碳应用程序。

是否可以仅针对一个碳应用程序覆盖 messageBuilder 和 messageFormatter?

谢谢


解决方案

在@tmoasz 的帮助下,我解决了这个问题。 这里是完整的解决方案

<inSequence>

      <!--Extract value from Json-->
      <property name="Data1" expression="json-eval($.Data1)"/>
      <property name="Data2" expression="json-eval($.Data2)"/>
      <property name="File1" expression="json-eval($.File1)"/>

      <!-- remove body and set MessageBuilder
        if body not removed, MultipartFormDataFormatter is not call
      -->
      <script language="js">
        mc.getEnvelope().getBody().getFirstElement().detach();
      </script>
      <builder>
        <messageBuilder contentType="multipart/form-data"
          class="org.apache.axis2.builder.MultipartFormDataBuilder"
          formatterClass="org.apache.axis2.transport.http.MultipartFormDataFormatter"/>
      </builder>

      <payloadFactory media-type="xml">
        <format>
          <root xmlns="">
            <metadata xmlns="http://org.apache.axis2/xsd/form-data"
              filename="key1"
              name="key1"></metadata>
            <!-- content is decode from base64-->
            <file xmlns="http://org.apache.axis2/xsd/form-data"
              filename="file1.1"
              name="file1.1"
              content-type="application/xml"></file>
            <!-- content is not changed-->
            <metadata xmlns="http://org.apache.axis2/xsd/form-data"
              name="file1.2"
              filename="file1.2"
              content-type="application/xml"></metadata>
          </root>
        </format>
        <args>
          <arg expression="get-property('Data1')"/>
          <arg expression="get-property('Data2')"/>
          <arg expression="get-property('File1')"/>
        </args>
      </payloadFactory>

            <!--set messageType to trigger multiPart Formater -->
            <property name="messageType"  scope="axis2" type="STRING" value="multipart/form-data"/>
            <!--remove ContentType to force generate header content-type with boundary information -->
            <property name="ContentType" scope="axis2" action="remove"/>

      <send>
        <endpoint>
          <http method="post" uri-template="http://127.0.0.1:3000/mulitPart/wso2">
            <suspendOnFailure>
              <initialDuration>-1</initialDuration>
              <progressionFactor>-1</progressionFactor>
              <maximumDuration>0</maximumDuration>
            </suspendOnFailure>
            <markForSuspension>
              <retriesBeforeSuspension>0</retriesBeforeSuspension>
            </markForSuspension>
          </http>
        </endpoint>
      </send>


    </inSequence>

卷曲测试

curl --location --request POST 'http://127.0.0.1:8280/outgoing-mail' \
--header 'Content-Type: application/json' \
--data-raw '{
    "Data1": "Value1",
    "Data2": "Value2",
    "File1" :  "PHhtbD4gICAKICAgIDxoZWxsbz5TdGFjazwvaGVsbG8+CjwveG1sPg=="
}'

已生成多部分

--MIMEBoundary_dfa6d9a4ea9eee573969c1fae03dd6667159a66375689ec7
Content-Disposition: form-data; name="key1"; filename="key1"
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: binary

Value1
--MIMEBoundary_dfa6d9a4ea9eee573969c1fae03dd6667159a66375689ec7
Content-Disposition: form-data; name="file1.1"; filename="file1.1"
Content-Type: application/pdf; charset=ISO-8859-1
Content-Transfer-Encoding: binary

<xml>   
    <hello>Stack</hello>
</xml>
--MIMEBoundary_dfa6d9a4ea9eee573969c1fae03dd6667159a66375689ec7
Content-Disposition: form-data; name="file1.2"; filename="file1.2"
Content-Type: application/pdf; charset=ISO-8859-1
Content-Transfer-Encoding: binary

PHhtbD4gICAKICAgIDxoZWxsbz5TdGFjazwvaGVsbG8+CjwveG1sPg==
--MIMEBoundary_dfa6d9a4ea9eee573969c1fae03dd6667159a66375689ec7--

也许 BuilderMediator 可以做到这一点,你会实现什么。 检查这个:Builder+Mediator documentation.