如何使用 java 解析 XOP/MTOM SOAP 响应? [英] How to parse XOP/MTOM SOAP response using java?

查看:28
本文介绍了如何使用 java 解析 XOP/MTOM SOAP 响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想知道,有没有什么简单的方法可以解析 MTOM/XOP SOAP 响应.问题是我使用普通的 HTTP 发送soap 消息和javax.xml 来解析响应.但是有些服务用 mulipart/related 来响应我,它需要更复杂的逻辑来解析它(性能很重要).所以我想知道我是否可以以某种方式利用 apache cxf、apache axiom 或任何其他库来解析 MTOM/XOP SOAP 响应?

I just want to know, is there any simple way for parsing MTOM/XOP SOAP response. The problem is that I use plain HTTP to send soap message and javax.xml for parsing response. But some services responds me with mulipart/related and it requires much more complex logic to parse it (performance matters). So I wonder may I somehow take advantage of apache cxf, apache axiom or any other library for parsing MTOM/XOP SOAP response?

推荐答案

这些单元测试 向您展示了如何使用 CXF 从 MTOM 消息中提取附件.如果将来不存在此链接,我将内联其中一项测试:

These unit tests show you how to use CXF to extract attachments out of an MTOM message. 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');
}

在您的情况下,ct 将来自响应的内容类型标头."mimedata" 将是响应的内容.

In your case, the ct will come from the content type header of the response. The "mimedata" will be the content of the response.

这篇关于如何使用 java 解析 XOP/MTOM SOAP 响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆