Search code examples
javasoapmtom

Java: Extract content from MTOM


I'm trying to extract content of a MTOM using code below

Iterator i = msg.getAttachments();
while (i.hasNext())
{
  AttachmentPart att = (AttachmentPart)i.next();
  Object obj = att.getContent();
}

where msg is SOAPMessage MIME type but the rawContent comes as null and will crash on getting AttachmentPart

Is there any other way to get MTOM content? Getting boundaries and looping through?


Solution

  • I have ended up with the code below

     MimeMultipart mp = new MimeMultipart(new ByteArrayDataSource(InputStream, "multipart/related"));
     int count = mp.getCount();
     for (int i = 0; i < count; i++) {
         BodyPart bodyPart = mp.getBodyPart(i);
         String content = new String(read(bodyPart));
    
         String partContentType =  bodyPart.getContentType();
    
    
      if(partContentType.toLowerCase().contains(SOAPConstants.SOAP_1_2_CONTENT_TYPE)) {
    
            //process SOAP 1.2
         }
    
        if(partContentType.toLowerCase().contains(SOAPConstants.SOAP_1_1_CONTENT_TYPE)) {
    
            //process SOAP 1.1
         }
    
         if(partContentType.toLowerCase().contains("application/octet-stream")) {
    
            // process binary part
         }
     }