CXF服务禁用服务器响应中的分块

时间:2018-03-05 16:47:36

标签: java .net web-services cxf

我正在使用SOAP和JAXWS将Java Web服务替换为Java CXF Web服务。客户是固定的,信不信由你,黑盒子。我正在尝试使用Java服务和原始.NET客户端以相同的方式工作。我尝试了很多方法,并确认对客户端的响应与.NET服务的响应相同(字节)。我试过从Java服务返回Windows响应无济于事,这意味着我有一个与传输有关的问题,可能是编码。我注意到响应transfer-encoding = [chunked] - 我怀疑这可能是一个问题。我还没有找到一种方法在服务器上更改它...但由于我无法修改客户端,我需要一个解决方法。我不熟悉.NET API,但这是我收到的消息:

BTW - 来自客户端的错误是[6] ERROR - [sweeper].[SweeperService.CollectSettingsInformation] <11501> (9990) There is an error in XML document (1, 2). at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events) at System.Xml.Serialization.XmlSerializer.Deserialize(TextReader textReader) at client.ReadMessageEnvelope(String xml) at client.Translate(String xml) at client.CollectSettingsInformation()

ANY 我们将不胜感激。

1 个答案:

答案 0 :(得分:0)

改编自其他几个类似的解决方案:

    public void handleMessage(Message message) {
    boolean isOutbound = false;
    isOutbound = message == message.getExchange().getOutMessage() || message == message.getExchange().getOutFaultMessage();

    if (isOutbound) {
        OutputStream os = message.getContent(OutputStream.class);
        CachedOutputStream cs = new CachedOutputStream();
        message.setContent(OutputStream.class, cs);
        message.getInterceptorChain().doIntercept(message);

        try {
            cs.flush();
            IOUtils.closeQuietly(cs);

            CachedOutputStream csnew = (CachedOutputStream) message.getContent(OutputStream.class);
            String currentEnvelopeMessage = IOUtils.toString(csnew.getInputStream(), "UTF-8");
            csnew.flush();
            IOUtils.closeQuietly(csnew);

            int bytes = Math.toIntExact(currentEnvelopeMessage.getBytes().length);

            HttpServletResponse response = (HttpServletResponse) message.get(AbstractHTTPDestination.HTTP_RESPONSE);
            if(null != response){
                log.debug("Setting Content Length: " + bytes);
                response.setContentLength(bytes);
            }

            InputStream replaceInStream = IOUtils.toInputStream(currentEnvelopeMessage, "UTF-8");
            os.flush();

            if (os instanceof CopyingOutputStream)
                ((CopyingOutputStream)os).copyFrom( replaceInStream );
            else
                IOUtils.copy(replaceInStream, os);

            replaceInStream.close();
            IOUtils.closeQuietly(replaceInStream);
            message.setContent(OutputStream.class, os);
            IOUtils.closeQuietly(os);

        } catch (IOException ioe) {
            log.warn("Unable to perform change.", ioe);
            throw new RuntimeException(ioe);
        }
    } 
}