View Javadoc
1 /*** 2 * Title: S/MIME Project 3 * Description: Creating S/MIME email transport capabilities. 4 * Copyright: Copyright (c) 2001 5 * @Author Vladimir Radisic 6 * @Version 2.0.1 7 */ 8 9 package org.webdocwf.util.smime.mail; 10 11 12 import org.webdocwf.util.smime.exception.SMIMEException; 13 import org.webdocwf.util.smime.activation.StreamDataSource; 14 import javax.mail.internet.MimeBodyPart; 15 import javax.mail.internet.MimeMultipart; 16 import org.webdocwf.util.smime.util.HtmlAnalyzer; 17 import java.io.File; 18 import java.io.InputStream; 19 import java.util.Vector; 20 import javax.activation.DataHandler; 21 import javax.activation.FileDataSource; 22 23 24 /*** 25 * Methods from this class are used in creation of specific MimeMultipart objects 26 * other than default multipart/mixed. 27 */ 28 public class MultipartGenerator { 29 30 /*** 31 * Returns created MimeMultipart for text/html message with html code given 32 * through InputStream. This method creates html content withouth regarding and 33 * resolving html code resource references. Returned MimeMultipart is of 34 * content-type "multipart/alternative". 35 * @param content0 html code in form of InputStream 36 * @return MimeMultipart with Content-Type: "multipart/alternative". 37 * @exception caused by non SMIMEException which is MessagingException 38 */ 39 public static MimeMultipart getHtmlMultipart(InputStream content0) 40 throws SMIMEException { 41 42 MimeMultipart alternativePart = new MimeMultipart("alternative"); 43 44 try { 45 HtmlAnalyzer ha = new HtmlAnalyzer(content0); 46 47 MimeBodyPart textBody = new MimeBodyPart(); 48 49 textBody.setText(ha.getPlainText(), "ISO-8859-1"); 50 MimeBodyPart htmlBody = new MimeBodyPart(); 51 52 htmlBody.setContent(ha.getHtmlText(), "text/html"); 53 54 alternativePart.addBodyPart(textBody); 55 alternativePart.addBodyPart(htmlBody); 56 } catch (Exception e) { 57 throw SMIMEException.getInstance("org.webdocwf.util.smime.mail.MultipartGenerator", 58 e, "getHtmlMultipart"); 59 } 60 return alternativePart; 61 } 62 63 /*** 64 * Returns created MimeMultipart for text/html message with html code given 65 * through InputStream, path for resolving relative path of resources defined in 66 * html code and array of resources given through InputStream. This method 67 * creates html content with resolving html code resource references. Returned 68 * MimeMultipart is of content-type "multipart/related", and 69 * inside itself, this MimeMultipart convey BodyParts with embeded resources, 70 * and one BodyPart which holds MimeMultipart with Content-Type 71 * "multipart/alternative". 72 * @param content0 html code in form of InputStream 73 * @param path0 common path for resolving relative referenced resources in html 74 * code atributes of type "src" and "background". 75 * @param resources0 array of resources given as array of InputStream. For more 76 * information refer to setContent methods of classes from package: 77 * org.webdocwf.util.smime.smime 78 * @return MimeMultipart with Content-Type: "multipart/related". 79 * @exception caused by non SMIMEException which is MessagingException 80 */ 81 public static MimeMultipart getHtmlMultipart(InputStream content0, 82 String path0, 83 InputStream[] resources0) 84 throws SMIMEException { 85 try { 86 HtmlAnalyzer ha = new HtmlAnalyzer(content0, path0); 87 MimeBodyPart textBody = new MimeBodyPart(); 88 89 textBody.setText(ha.getPlainText(), "ISO-8859-1"); 90 MimeBodyPart htmlBody = new MimeBodyPart(); 91 92 htmlBody.setContent(ha.getHtmlText(), "text/html"); 93 94 MimeMultipart alternativePart = new MimeMultipart("alternative"); 95 96 alternativePart.addBodyPart(textBody); 97 alternativePart.addBodyPart(htmlBody); 98 99 MimeMultipart relatedPart = null; 100 Vector vAddress = ha.getSwappedAdresses(); 101 102 if (vAddress != null) { 103 relatedPart = new MimeMultipart("related"); 104 MimeBodyPart alternativeBody = new MimeBodyPart(); 105 106 alternativeBody.setContent(alternativePart); 107 relatedPart.addBodyPart(alternativeBody); 108 109 for (int i = 0; i < vAddress.size(); i = i + 2) { 110 try { 111 if (vAddress.elementAt(i) instanceof File) { 112 MimeBodyPart mbp = new MimeBodyPart(); 113 File fn = (File) vAddress.elementAt(i); 114 FileDataSource fd = new FileDataSource(fn); 115 116 mbp.setDataHandler(new DataHandler(fd)); 117 mbp.setFileName(fn.getName()); 118 mbp.addHeader("Content-ID", "<" + vAddress.elementAt(i + 1) + ">"); 119 relatedPart.addBodyPart(mbp); 120 } else { 121 MimeBodyPart mbp = new MimeBodyPart(); 122 int index = Integer.parseInt( 123 ((String) vAddress.elementAt(i)).substring(5, 8)); 124 String fileName = ((String) vAddress.elementAt(i)).substring(8); 125 126 mbp.setDataHandler(new DataHandler(new StreamDataSource( 127 resources0[index], fileName))); 128 mbp.setFileName(fileName); 129 mbp.addHeader("Content-ID", "<" + vAddress.elementAt(i + 1) + ">"); 130 relatedPart.addBodyPart(mbp); 131 } 132 } catch (Exception e) { 133 continue; // Forces that adding of bodyparts carry on if error happened in one of the adding 134 } 135 } 136 if (relatedPart.getCount() == 0) 137 return alternativePart; 138 return relatedPart; 139 } else 140 return alternativePart; 141 } catch (Exception e) { 142 throw SMIMEException.getInstance("org.webdocwf.util.smime.mail.MultipartGenerator", 143 e, "getHtmlMultipart"); 144 } 145 } 146 147 } 148

This page was automatically generated by Maven