1 16 package org.apache.axis2.om; 17 18 import javax.activation.DataHandler ; 19 import javax.mail.MessagingException ; 20 import javax.mail.internet.ContentType ; 21 import javax.mail.internet.MimeBodyPart ; 22 import javax.xml.stream.XMLStreamException; 23 import java.io.IOException ; 24 import java.io.OutputStream ; 25 import java.util.Iterator ; 26 import java.util.LinkedList ; 27 28 31 public class MIMEOutputUtils { 32 33 private static byte[] CRLF = { 13, 10 }; 34 35 static String SOAP_PART_CONTENT_ID = "<http://apache.org/soappart>"; 36 37 public static void complete(OutputStream outStream, 38 OutputStream bufferedSoapOutStream, LinkedList binaryNodeList, 39 String boundary) throws XMLStreamException { 40 try { 41 startWritingMime(outStream, boundary); 42 43 DataHandler dh = new DataHandler (bufferedSoapOutStream.toString(), 44 "text/xml"); 45 MimeBodyPart rootMimeBodyPart = new MimeBodyPart (); 46 rootMimeBodyPart.setDataHandler(dh); 47 ContentType partContentType = new ContentType ("application/xop+xml"); 48 partContentType.setParameter("charset","UTF-8"); 49 partContentType.setParameter("type","application/soap+xml"); 50 rootMimeBodyPart.addHeader("Content-Type",partContentType.toString() ); 51 rootMimeBodyPart.addHeader("Content-Transfer-Encoding", "8bit"); 52 rootMimeBodyPart.addHeader("Content-ID", SOAP_PART_CONTENT_ID); 53 54 writeBodyPart(outStream, rootMimeBodyPart, boundary); 55 56 Iterator binaryNodeIterator = binaryNodeList.iterator(); 57 while (binaryNodeIterator.hasNext()) { 58 OMText binaryNode = (OMText) binaryNodeIterator.next(); 59 writeBodyPart(outStream, createMimeBodyPart(binaryNode), 60 boundary); 61 } 62 finishWritingMime(outStream); 63 } catch (IOException e) { 64 throw new OMException("Problem with the OutputStream.",e); 65 } catch (MessagingException e) { 66 throw new OMException("Problem writing Mime Parts." ,e); 67 } 68 } 69 70 protected static MimeBodyPart createMimeBodyPart(OMText node) 71 throws MessagingException { 72 MimeBodyPart mimeBodyPart = new MimeBodyPart (); 73 mimeBodyPart.setDataHandler(node.getDataHandler()); 74 mimeBodyPart.addHeader("Content-Transfer-Encoding", "binary"); 75 mimeBodyPart.addHeader("Content-ID", "<" + node.getContentID() + ">"); 76 return mimeBodyPart; 77 78 } 79 80 84 protected static void writeMimeBoundary(OutputStream outStream, 85 String boundary) throws IOException { 86 outStream.write(new byte[] { 45, 45 }); 87 outStream.write(boundary.getBytes()); 88 } 89 90 94 protected static void startWritingMime(OutputStream outStream, String boundary) 95 throws IOException { 96 writeMimeBoundary(outStream, boundary); 97 } 99 100 108 protected static void writeBodyPart(OutputStream outStream, 109 MimeBodyPart part, String boundary) throws IOException , 110 MessagingException { 111 outStream.write(CRLF); 112 part.writeTo(outStream); 113 outStream.write(CRLF); 114 writeMimeBoundary(outStream, boundary); 115 } 116 117 121 protected static void finishWritingMime(OutputStream outStream) 122 throws IOException { 123 outStream.write(new byte[] { 45, 45 }); 124 } 125 126 public static String getContentTypeForMime(String boundary) { 127 ContentType contentType = new ContentType (); 128 contentType.setPrimaryType("multipart"); 129 contentType.setSubType("related"); 130 contentType.setParameter("boundary", boundary); 131 contentType.setParameter("start", MIMEOutputUtils.SOAP_PART_CONTENT_ID); 132 contentType.setParameter("type", "application/xop+xml"); 133 contentType.setParameter("start-info", "application/soap+xml"); 136 return contentType.toString(); 137 } 138 139 } | Popular Tags |