简体   繁体   English

如何使用java解析XOP / MTOM SOAP响应?

[英]How to parse XOP/MTOM SOAP response using java?

I just want to know, is there any simple way for parsing MTOM/XOP SOAP response. 我只想知道,有没有简单的方法来解析MTOM / XOP SOAP响应。 The problem is that I use plain HTTP to send soap message and javax.xml for parsing response. 问题是我使用普通HTTP发送soap消息和javax.xml来解析响应。 But some services responds me with mulipart/related and it requires much more complex logic to parse it (performance matters). 但是有些服务用mulipart / related响应我,它需要更复杂的逻辑来解析它(性能很重要)。 So I wonder may I somehow take advantage of apache cxf, apache axiom or any other library for parsing MTOM/XOP SOAP response? 所以我想我可以以某种方式利用apache cxf,apache axiom或任何其他库来解析MTOM / XOP SOAP响应?

These unit tests show you how to use CXF to extract attachments out of an MTOM message. 这些单元测试向您展示如何使用CXF从MTOM消息中提取附件。 I'll inline one of the tests in case this link doesn't exist in the future: 如果将来不存在此链接,我将内联其中一个测试:

private MessageImpl msg;

@Before
public void setUp() throws Exception {
    msg = new MessageImpl();
    Exchange exchange = new ExchangeImpl();
    msg.setExchange(exchange);
}

@Test
public void testDeserializerMtom() throws Exception {
    InputStream is = getClass().getResourceAsStream("mimedata");
    String ct = "multipart/related; type=\"application/xop+xml\"; "
                + "start=\"<soap.xml@xfire.codehaus.org>\"; "
                + "start-info=\"text/xml; charset=utf-8\"; "
                + "boundary=\"----=_Part_4_701508.1145579811786\"";

    msg.put(Message.CONTENT_TYPE, ct);
    msg.setContent(InputStream.class, is);

    AttachmentDeserializer deserializer = new AttachmentDeserializer(msg);
    deserializer.initializeAttachments();

    InputStream attBody = msg.getContent(InputStream.class);
    assertTrue(attBody != is);
    assertTrue(attBody instanceof DelegatingInputStream);

    Collection<Attachment> atts = msg.getAttachments();
    assertNotNull(atts);

    Iterator<Attachment> itr = atts.iterator();
    assertTrue(itr.hasNext());

    Attachment a = itr.next();
    assertNotNull(a);

    InputStream attIs = a.getDataHandler().getInputStream();

    // check the cached output stream
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    IOUtils.copy(attBody, out);
    assertTrue(out.toString().startsWith("<env:Envelope"));

    // try streaming a character off the wire
    assertTrue(attIs.read() == '/');
    assertTrue(attIs.read() == '9');
}

In your case, the ct will come from the content type header of the response. 在您的情况下, ct将来自响应的内容类型标头。 The "mimedata" will be the content of the response. "mimedata"将是回应的内容。

No need to use CXF , the standard javax.mail.internet.MimeMultipart class do the job and it's very easy to use (also to create MTOM request). 无需使用CXF ,标准的javax.mail.internet.MimeMultipart类可以完成这项工作,并且它非常易于使用(也可以创建MTOM请求)。

Here a very simple sample to decode parts of a MTOM response: 这是一个非常简单的示例,用于解码部分MTOM响应:

MimeMultipart mp = new MimeMultipart(new ByteArrayDataSource(data, contentType));
int count = mp.getCount();
for (int i = 0; i < count; i++) {
    BodyPart bp = mp.getBodyPart(i);
    bp.saveFile(filepath + "_" + i);
}

I had same issue and solved as @Nicolas Albert 我有同样的问题,并解决了@Nicolas Albert

public byte[] mimeParser(InputStream isMtm) {
    ByteArrayOutputStream baos = null;
    try {
        MimeMultipart mp = new MimeMultipart(new ByteArrayDataSource(isMtm,
                ct));
        int count = mp.getCount();
        baos = new ByteArrayOutputStream();
        for (int i = 0; i < count; i++) {
            BodyPart bodyPart = mp.getBodyPart(i);
            if (!Part.ATTACHMENT
                    .equalsIgnoreCase(bodyPart.getDisposition())
                    && !StringUtils.isNotBlank(bodyPart.getFileName())) {
                continue; // dealing with attachments only
            }
            bodyPart.writeTo(baos);
        }

        byte[] attachment = baos.toByteArray();
        FileUtils.writeByteArrayToFile(new File("E:/wss/attachment.zip"), attachment);
        return attachment;
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        if (baos != null) {
            try {
                baos.close();
            } catch (Exception ex) {

            }
        }
    }
    return null;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM