8

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

4

3 回答 3

8

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

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"是响应的内容。

于 2013-12-19T21:12:35.097 回答
6

无需使用CXF,标准的javax.mail.internet.MimeMultipart类就可以完成这项工作,而且非常易于使用(也可以创建 MTOM 请求)。

这是一个非常简单的示例来解码部分 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);
}
于 2015-10-21T07:38:12.997 回答
1

我有同样的问题并解决了@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;
}
于 2016-05-28T20:39:48.383 回答