Manually Parse MTOM Message

南楼画角 提交于 2019-12-10 11:36:16

问题


I have below mtom response as string

--uuid:6b6fab3b-c481-4648-9eb5-8690096eb54d
Content-Type: application/xop+xml; charset=UTF-8; type="application/soap+xml";
Content-Transfer-Encoding: binary
Content-ID: <root.message@cxf.apache.org>

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"><soap:Body><ns2:receiveDocumentFileResponse xmlns:ns2="http://webservice.edefter.gib.gov.tr/"><return><fileName>GIB-XXXX-201512-KB-000000.zip</fileName><binaryData><xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:e5482268-b98e-443c-970a-af6edf52ea25-8729@cxf.apache.org"/></binaryData></return></ns2:receiveDocumentFileResponse></soap:Body></soap:Envelope>
--uuid:6b6fab3b-c481-4648-9eb5-8690096eb54d
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
Content-ID: <e5482268-b98e-443c-970a-af6edf52ea25-8729@cxf.apache.org>
Content-Disposition: attachment;name="GIB-XXXX-201512-KB-000000.zip"

/***********MTOM Content****************/
--uuid:6b6fab3b-c481-4648-9eb5-8690096eb54d--

I am using CXF unit test code to parse above content regarding to xop/mtom parse

Attachment is always null and when executing itr.next(); line, I get exception java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

        InputStream is = null;
        InputStream attIs = null;
        try {
            org.apache.cxf.message.Message cxfMessage = new MessageImpl();
            is = new ByteArrayInputStream(response.getBytes("UTF-8"));
            // FileInputStream fis = new FileInputStream(new File(
            // "D:/wss/gibreturn.txt"));
            // 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\"";
            cxfMessage.setContent(InputStream.class, is);
            cxfMessage.put(org.apache.cxf.message.Message.CONTENT_TYPE,
                    "application/xop+xml; charset=UTF-8; type=\"application/soap+xml\";");

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

            InputStream attBody = cxfMessage.getContent(InputStream.class);

            System.out.println(deserializer.toString());
            Collection<org.apache.cxf.message.Attachment> atts = cxfMessage
                    .getAttachments();
            Iterator<org.apache.cxf.message.Attachment> itr = atts.iterator();
            org.apache.cxf.message.Attachment a = itr.next();
            attIs = a.getDataHandler().getInputStream();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (Exception e) {

                }
            }
            if (attIs != null) {
                try {
                    is.close();
                } catch (Exception e) {

                }
            }
        }

Thank You Kind Regards


回答1:


I solved it with manual mime parsing

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;
}


来源:https://stackoverflow.com/questions/37455584/manually-parse-mtom-message

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!