3.5 和 3.7 中的 Mule 附件行为差异
Mule attachments behavioral difference in 3.5 and 3.7
我只想在 http:request 中发送附件。因此,我准备了以下流程。
<flow name="http-request-payload-multipart-preparator">
<http:listener config-ref="GlobalHTTPConnector" path="/requestmultipartprep" doc:name="HTTP"/>
<expression-component doc:name="Expression"><![CDATA[
ds = new org.mule.message.ds.StringDataSource('key1','value1','text/plain');
dh = new javax.activation.DataHandler(ds);
message.outboundAttachments['key1'] = dh;
ds = new org.mule.message.ds.StringDataSource('key2','value2','text/plain');
dh = new javax.activation.DataHandler(ds);
message.outboundAttachments['key2'] = dh;]]></expression-component>
<set-payload value="#[null]" />
<http:request config-ref="HTTP_Request_Configuration" path="/simplemultiparthandler" method="POST" doc:name="HTTP"/>
</flow>
<flow name="sile-multipart-handler">
<http:listener config-ref="GlobalHTTPConnector" path="/simplemultiparthandler" doc:name="HTTP"/>
<logger level="INFO" message="#[message.inboundAttachments.keySet().toString()]" />
<logger level="INFO" message="#[message.inboundAttachments.'key1'.dataSource.name]" doc:name="Payload Logger"/>
<logger level="INFO" message="#[message.inboundAttachments.'key1'.dataSource.content]" doc:name="Payload Logger"/>
<logger level="INFO" message="#[message.inboundAttachments.'key2'.dataSource.name]" doc:name="Payload Logger"/>
<logger level="INFO" message="#[message.inboundAttachments.'key2'.dataSource.content]" doc:name="Payload Logger"/>
<set-payload value="baba sai" />
</flow>
这在 mule 3.7 中运行良好。在这里我可以看到我有两个附件,名称分别为 'key1' 和 'key2'。
但是,如果我在 mule 3.5 中使用 http:outbound-endpoint 做同样的事情,我将无法获得密钥。它显示附件名称分别为 'value1' 和 'value2'。
为什么会这样?
为了完成这项工作,我在 3.5 中更改了如下代码。我交换了键名和值名。
<flow name="http-request-payload-multipart-preparator">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="9876" path="requestmultipart" connector-ref="NoSessionEncodingConnector" doc:name="HTTP" />
<expression-component doc:name="Expression"><![CDATA[
ds = new org.mule.message.ds.StringDataSource('value1','key1','text/plain');
dh = new javax.activation.DataHandler(ds);
message.outboundAttachments['key1'] = dh;
ds = new org.mule.message.ds.StringDataSource('value2','key2','text/plain');
dh = new javax.activation.DataHandler(ds);
message.outboundAttachments['key2'] = dh;
payload;]]></expression-component>
<set-payload value="#[null]" />
<http:outbound-endpoint exchange-pattern="request-response" host="localhost" port="9876" method="POST" doc:name="HTTP" path="simplemultiparthandler" connector-ref="NoSessionEncodingConnector" />
</flow>
<flow name="sile-multipart-handler">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="9876" path="simplemultiparthandler" connector-ref="NoSessionEncodingConnector" doc:name="HTTP" />
<logger level="INFO" message="#[message.inboundAttachments.keySet().toString()]" />
<logger level="INFO" message="#[message.inboundAttachments.'key1'.dataSource.name]" doc:name="Payload Logger"/>
<logger level="INFO" message="#[message.inboundAttachments.'key1'.dataSource.inputStream]" doc:name="Payload Logger"/>
<logger level="INFO" message="#[message.inboundAttachments.'key2'.dataSource.name]" doc:name="Payload Logger"/>
<logger level="INFO" message="#[message.inboundAttachments.'key2'.dataSource.inputStream]" doc:name="Payload Logger"/>
</flow>
为什么 3.5 和 3.7 会有这种差异?
3.7 的 HTTP 连接器完全重写了 3.5 及更早版本的 HTTP 传输。
附件支持在旧传输中一直存在问题,因此我认为 MuleSoft 决定在构建新连接器时解决问题。
我只想在 http:request 中发送附件。因此,我准备了以下流程。
<flow name="http-request-payload-multipart-preparator">
<http:listener config-ref="GlobalHTTPConnector" path="/requestmultipartprep" doc:name="HTTP"/>
<expression-component doc:name="Expression"><![CDATA[
ds = new org.mule.message.ds.StringDataSource('key1','value1','text/plain');
dh = new javax.activation.DataHandler(ds);
message.outboundAttachments['key1'] = dh;
ds = new org.mule.message.ds.StringDataSource('key2','value2','text/plain');
dh = new javax.activation.DataHandler(ds);
message.outboundAttachments['key2'] = dh;]]></expression-component>
<set-payload value="#[null]" />
<http:request config-ref="HTTP_Request_Configuration" path="/simplemultiparthandler" method="POST" doc:name="HTTP"/>
</flow>
<flow name="sile-multipart-handler">
<http:listener config-ref="GlobalHTTPConnector" path="/simplemultiparthandler" doc:name="HTTP"/>
<logger level="INFO" message="#[message.inboundAttachments.keySet().toString()]" />
<logger level="INFO" message="#[message.inboundAttachments.'key1'.dataSource.name]" doc:name="Payload Logger"/>
<logger level="INFO" message="#[message.inboundAttachments.'key1'.dataSource.content]" doc:name="Payload Logger"/>
<logger level="INFO" message="#[message.inboundAttachments.'key2'.dataSource.name]" doc:name="Payload Logger"/>
<logger level="INFO" message="#[message.inboundAttachments.'key2'.dataSource.content]" doc:name="Payload Logger"/>
<set-payload value="baba sai" />
</flow>
这在 mule 3.7 中运行良好。在这里我可以看到我有两个附件,名称分别为 'key1' 和 'key2'。 但是,如果我在 mule 3.5 中使用 http:outbound-endpoint 做同样的事情,我将无法获得密钥。它显示附件名称分别为 'value1' 和 'value2'。
为什么会这样?
为了完成这项工作,我在 3.5 中更改了如下代码。我交换了键名和值名。
<flow name="http-request-payload-multipart-preparator">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="9876" path="requestmultipart" connector-ref="NoSessionEncodingConnector" doc:name="HTTP" />
<expression-component doc:name="Expression"><![CDATA[
ds = new org.mule.message.ds.StringDataSource('value1','key1','text/plain');
dh = new javax.activation.DataHandler(ds);
message.outboundAttachments['key1'] = dh;
ds = new org.mule.message.ds.StringDataSource('value2','key2','text/plain');
dh = new javax.activation.DataHandler(ds);
message.outboundAttachments['key2'] = dh;
payload;]]></expression-component>
<set-payload value="#[null]" />
<http:outbound-endpoint exchange-pattern="request-response" host="localhost" port="9876" method="POST" doc:name="HTTP" path="simplemultiparthandler" connector-ref="NoSessionEncodingConnector" />
</flow>
<flow name="sile-multipart-handler">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="9876" path="simplemultiparthandler" connector-ref="NoSessionEncodingConnector" doc:name="HTTP" />
<logger level="INFO" message="#[message.inboundAttachments.keySet().toString()]" />
<logger level="INFO" message="#[message.inboundAttachments.'key1'.dataSource.name]" doc:name="Payload Logger"/>
<logger level="INFO" message="#[message.inboundAttachments.'key1'.dataSource.inputStream]" doc:name="Payload Logger"/>
<logger level="INFO" message="#[message.inboundAttachments.'key2'.dataSource.name]" doc:name="Payload Logger"/>
<logger level="INFO" message="#[message.inboundAttachments.'key2'.dataSource.inputStream]" doc:name="Payload Logger"/>
</flow>
为什么 3.5 和 3.7 会有这种差异?
3.7 的 HTTP 连接器完全重写了 3.5 及更早版本的 HTTP 传输。
附件支持在旧传输中一直存在问题,因此我认为 MuleSoft 决定在构建新连接器时解决问题。