2013-08-09 5 views
5

मैं सिर्फ जानना चाहता हूं, क्या एमटीओएम/एक्सओपी एसओएपी प्रतिक्रिया को पार्स करने का कोई आसान तरीका है। समस्या यह है कि मैं पार्सिंग प्रतिक्रिया के लिए साबुन संदेश और javax.xml भेजने के लिए सादा HTTP का उपयोग करता हूं। लेकिन कुछ सेवाएं मुझे मल्टीपार्ट/संबंधित के साथ प्रतिक्रिया देती हैं और इसे पार्स करने के लिए इसे और अधिक जटिल तर्क की आवश्यकता होती है (प्रदर्शन मामलों)। तो मुझे आश्चर्य है कि क्या मैं किसी भी तरह से एमटीओएम/एक्सओपी एसओएपी प्रतिक्रिया को पार्स करने के लिए अपाचे सीएक्सएफ, अपाचे एक्सीम या किसी अन्य लाइब्रेरी का लाभ उठा सकता हूं?जावा का उपयोग कर एक्सओपी/एमटीओएम एसओएपी प्रतिक्रिया का विश्लेषण कैसे करें?

+0

के रूप में हल क्या तुमने कभी एक जवाब मिला? –

उत्तर

6

These unit tests आपको दिखाता है कि एमटीओएम संदेश से अनुलग्नक निकालने के लिए सीएक्सएफ का उपयोग कैसे करें। मैं परीक्षण के मामले में इस लिंक भविष्य में मौजूद नहीं है में से एक इनलाइन होगी:

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=\"<[email protected]>\"; " 
       + "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" प्रतिक्रिया की सामग्री होगी।

+0

मैंने इसे उसी तरह से किया है। बस साझा करने के लिए समय नहीं है। उत्तर के लिए धन्यवाद! –

+0

प्रिय डैनियल, क्या आप मुझे प्रश्न से संबंधित सामग्री-प्रकार के बारे में मार्गदर्शन कर सकते हैं http://stackoverflow.com/questions/37455584/manually-parse-mtom-message –

3

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

मैं एक ही मुद्दा था और @Nicolas अल्बर्ट

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; 
} 
संबंधित मुद्दे