1 package org.codehaus.xfire.attachments; 2 3 import java.io.IOException; 4 import java.io.OutputStream; 5 import java.util.HashMap; 6 import java.util.Iterator; 7 import java.util.Map; 8 import java.util.Properties; 9 10 import javax.mail.MessagingException; 11 import javax.mail.Session; 12 import javax.mail.internet.MimeBodyPart; 13 import javax.mail.internet.MimeMessage; 14 import javax.mail.internet.MimeMultipart; 15 16 import org.codehaus.xfire.XFireRuntimeException; 17 18 /*** 19 * 20 * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a> 21 */ 22 public class JavaMailAttachments 23 implements Attachments 24 { 25 private static final String[] filter = new String[]{"Message-ID", "Mime-Version", "Content-Type"}; 26 27 private Map parts; 28 29 private Attachment soapMessage; 30 31 private MimeMultipart mimeMP; 32 33 public JavaMailAttachments() 34 { 35 parts = new HashMap(); 36 } 37 38 /*** 39 * Create Attachments from the MimeMultipart message. 40 * 41 * @param multipart 42 * @throws MessagingException 43 */ 44 public JavaMailAttachments(MimeMultipart multipart) 45 throws MessagingException 46 { 47 this(); 48 49 this.mimeMP = multipart; 50 51 MimeBodyPart part = (MimeBodyPart) multipart.getBodyPart(0); 52 setSoapMessage(new SimpleAttachment(part.getContentID(), part.getDataHandler())); 53 54 for ( int i = 1; i < multipart.getCount(); i++ ) 55 { 56 part = (MimeBodyPart) multipart.getBodyPart(i); 57 addPart(new SimpleAttachment(part.getContentID(), part.getDataHandler())); 58 } 59 } 60 61 /*** 62 * @return Returns the soapMessage. 63 */ 64 public Attachment getSoapMessage() 65 { 66 return soapMessage; 67 } 68 69 /*** 70 * @param soapMessage The soapMessage to set. 71 */ 72 public void setSoapMessage(Attachment soapMessage) 73 { 74 this.soapMessage = soapMessage; 75 } 76 77 public void addPart(Attachment part) 78 { 79 parts.put(part.getId(), part); 80 } 81 82 public Iterator getParts() 83 { 84 return parts.values().iterator(); 85 } 86 87 public Attachment getPart(String id) 88 { 89 return (Attachment) parts.get(id); 90 } 91 92 public int size() 93 { 94 return parts.size(); 95 } 96 97 public void write(OutputStream out) 98 throws IOException 99 { 100 Session session = Session.getDefaultInstance(new Properties(), null); 101 MimeMessage message = new MimeMessage(session); 102 103 try 104 { 105 message.setContent(getMimeMultipart()); 106 message.writeTo(out, filter); 107 } 108 catch( MessagingException e ) 109 { 110 throw new XFireRuntimeException("Couldn't create message.", e); 111 } 112 } 113 114 public MimeMultipart getMimeMultipart() 115 { 116 if ( mimeMP == null ) 117 { 118 mimeMP = new MimeMultipart("related; type=\"text/xml\";start=\"<" 119 + getSoapMessage().getId() + ">\""); 120 121 try 122 { 123 MimeBodyPart soapPart = new MimeBodyPart(); 124 soapPart.setDataHandler(soapMessage.getDataHandler()); 125 soapPart.setContentID("<"+soapMessage.getId()+">"); 126 mimeMP.addBodyPart(soapPart); 127 128 for (Iterator itr = getParts(); itr.hasNext(); ) 129 { 130 Attachment att = (Attachment) itr.next(); 131 132 MimeBodyPart part = new MimeBodyPart(); 133 part.setDataHandler(att.getDataHandler()); 134 part.setContentID("<"+att.getId()+">"); 135 mimeMP.addBodyPart(part); 136 } 137 } 138 catch( MessagingException e ) 139 { 140 throw new XFireRuntimeException("Couldn't create message.", e); 141 } 142 } 143 144 return mimeMP; 145 } 146 147 public String getContentType() 148 { 149 return getMimeMultipart().getContentType(); 150 } 151 }