【问题标题】:WSO2 ESB 7.1.0 Binary response is truncated to 375BWSO2 ESB 7.1.0 二进制响应被截断为 375B
【发布时间】:2021-05-11 07:14:09
【问题描述】:

我在 WSO2 ESB 7.1.0 中遇到二进制响应问题 - 看起来答案被截断为 375B(正文部分 - 二进制数据)。

这是我的序列片段 - 响应构建:

<payloadFactory description="Build Payload Response" media-type="xml">
  <format>
    <ns:binary xmlns:ns="http://ws.apache.org/commons/ns/payload">BASE64_FILE_CONTENT</ns:binary>
  </format>
  <args/>
</payloadFactory>
<script language="js"><![CDATA[var binaryNode = mc.getEnvelope().getBody().getFirstElement().getFirstOMChild();  
  binaryNode.setBinary(true);]]></script>
<propertyGroup description="Set Content-Type">
  <property action="remove" name="TRANSPORT_HEADERS" scope="axis2"/>
  <property name="DISABLE_CHUNKING" scope="axis2" type="STRING" value="true"/>
  <property name="messageType" scope="axis2" type="STRING" value="application/octet-stream"/>
  <property name="ContentType" scope="axis2" type="STRING" value="application/pdf"/>
</propertyGroup>
<header description="Set Content-Disposition" name="Content-Disposition" scope="transport" value="attachment; filename=&quot;raport.pdf&quot;"/>

当然在 BASE64_FILE_CONTENT 我把真实的文件内容用 BASE64 编码。 消息格式化程序配置(我使用 application/octeat-stream 或 application/pdf -> org.wso2.carbon.relay.ExpandingMessageFormatter 结果相同):

<messageFormatters>
  <messageFormatter contentType="application/x-www-form-urlencoded" class="org.apache.synapse.commons.formatters.XFormURLEncodedFormatter"/>
  <messageFormatter contentType="multipart/form-data" class="org.apache.axis2.transport.http.MultipartFormDataFormatter"/>
  <messageFormatter contentType="application/xml" class="org.apache.axis2.transport.http.ApplicationXMLFormatter"/>
  <messageFormatter contentType="text/xml" class="org.apache.axis2.transport.http.SOAPMessageFormatter"/>
  <messageFormatter contentType="application/soap+xml" class="org.apache.axis2.transport.http.SOAPMessageFormatter"/>
  <messageFormatter contentType="text/plain" class="org.apache.axis2.format.PlainTextFormatter"/>
  <messageFormatter contentType="application/json" class="org.wso2.micro.integrator.core.json.JsonStreamFormatter"/>
  <messageFormatter contentType="application/json/badgerfish" class="org.apache.axis2.json.JSONBadgerfishMessageFormatter"/>
  <messageFormatter contentType="text/javascript" class="org.apache.axis2.json.JSONMessageFormatter"/>
  <messageFormatter contentType="application/octet-stream" class="org.wso2.carbon.relay.ExpandingMessageFormatter"/>
  <messageFormatter contentType="application/binary" class="org.apache.axis2.format.BinaryFormatter"/>
  <messageFormatter contentType="application/pdf" class="org.wso2.carbon.relay.ExpandingMessageFormatter"/>
</messageFormatters>

我尝试了一些示例文件,结果始终相同 - 标头和二进制数据的开头看起来不错,但数据在 375B 上被截断:

Postman screenshot - 375B for body

这个值 (375B) 是否来自任何参数?谁能建议如何在 WSO2 中正确返回文件(作为响应中的二进制数据)?

最好的问候

马辛·奥林斯基

【问题讨论】:

    标签: binary wso2 binary-data wso2ei


    【解决方案1】:

    我也有同样的问题,有趣的是,内容被剪切在同一个字节。我花了两天时间,但我设法解决了这个问题。

    问题存在于 WSO2 的深处,更具体地说是 Woodstox 解析器。很有可能,您已经在 WSO2 根目录中创建了 XMLInputFactory.propertiesjavax.xml.stream.isCoalescing=false,以在 PayloadFactory 中介器中启用 CDATA。

    禁用合并后,Woodstox 会利用文本缓冲。不幸的是,这会导致多个子文本节点。如果您查看第 170 行的 org.wso2.carbon.relay.ExpandingMessageFormatter 的源代码(在撰写本文时在 master 分支中),您可以看到,只有第一个文本节点被序列化为二进制内容。

    OMNode node = contentEle.getFirstOMChild();
    OMText binaryDataNode = (OMText) node;
    DataHandler dh = (DataHandler) binaryDataNode.getDataHandler();
    ...
    dh.writeTo(out);
    

    Here 是更多 Woodstox 属性的列表。这是相关的:

    P_MIN_TEXT_SEGMENT(默认值:64):当不使用“合并”模式时,此设置定义可能报告的最小部分文本段(即,一个连续文本段的一部分) - 旨在减少返回小段的可能性同时允许解析器避免必须完全缓冲较长的段。 将此值设置为 Integer.MAX_VALUE 将有效地防止分割单个段而不强制合并相邻段,因此这是一种常见的覆盖

    所以解决方法还好比较简单,在XMLInputFactory.properties后面加上com.ctc.wstx.minTextSegment=2147483647就可以了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-24
      • 1970-01-01
      • 2014-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多