如何在SAAJ API中压缩SOAPMessage?

时间:2019-02-14 15:35:50

标签: java xml soap gzip saaj

我正在尝试使用SAAJ api使用gzip发送带有附件的SOAP请求。我刚刚开始使用SAAJ,所以我并不十分熟悉,但是我无法在Internet上找到任何与之相同的示例/用法。我所看到的所有内容都只是添加了标题“ accept-encoding”,“ gzip”,但这并没有完成任何事情,它只是告诉服务器对我的期望。我试图gzip我的原始消息,并在发送之前将其写到新消息中,但这使我收到一条错误消息,指出缺少起始边界。下面是我用来生成消息并在发送之前对其进行gzip压缩的代码。

    SOAPConnectionFactory cf = null;
    SOAPConnection conn = null;
    SOAPMessage message = null;
    SOAPMessage response = null;
    TransformerFactory tf = null;
    Transformer transformer = null;
    StringWriter writer = null;
    MessageFactory fac = null;
    TransmissionPayload payload = null;
    SOAPMessage gzipMessage = null;
    try
    {
        tf =  TransformerFactory.newInstance();
        transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        writer = new StringWriter();
        transformer.transform(new DOMSource(doc), new StreamResult(writer));

        fac = MessageFactory.newInstance();
        message = fac.createMessage(new MimeHeaders(), new ByteArrayInputStream(writer.getBuffer().toString().getBytes()));

        message.getMimeHeaders().addHeader("Accept-Encoding", "gzip,deflate");
        //ADD ATTACHMENT
        message.addAttachmentPart(message.createAttachmentPart(
                new StreamSource(
                        new ByteArrayInputStream(FileUtils.readFileToByteArray(file))), "text/xml"));

        message.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true");
        message.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "utf-8");

        cf = SOAPConnectionFactory.newInstance();
        conn = cf.createConnection();

        //GZIP ORIGINAL MESSAGE HERE
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        message.writeTo(out);
        byte[] msg = out.toByteArray();

        ByteArrayOutputStream out2 = new ByteArrayOutputStream();

        GZIPOutputStream os = new GZIPOutputStream(out2);
        os.write(msg);
        os.flush();
        os.close();

        //CREATING NEW MESSAGE FROM GZIP. 
        gzipMessage = fac.createMessage(message.getMimeHeaders(), new ByteArrayInputStream(out2.toByteArray()));

        response = conn.call(gzipMessage, new URL(IRS_BRQ_URL));
        if(response != null)
        {
            Source src = response.getSOAPPart().getContent();
            tf = TransformerFactory.newInstance();
            transformer = tf.newTransformer();
            DOMResult res = new DOMResult();
            transformer.transform(src, res);

            payload = new TransmissionPayload(200, (Document)res.getNode(), null);
        }
    }catch(Exception e)
    {
        throw new AirException("A2AConnection.SAAJ: Exception: ", e);
    }finally
    {
        cf = null;
        conn = null;
        message = null;
        tf = null;
        transformer = null;
        writer = null;
        fac = null;
    }

如果完全有可能,我假设我只需要gzip SOAPMessage的一部分,而不是全部?我无法选择不进行gzip压缩,因为端点需要它。

感谢您的帮助或指导。如果需要更多信息,请告诉我。

0 个答案:

没有答案