KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > oyster > mail > MultipartGenerator


1 /**
2  * Title: Oyster Project
3  * Description: Creating S/MIME email transport capabilities.
4  * Copyright: Copyright (c) 2001
5  * @Author Vladimir Radisic
6  * @Version 2.1.5
7  */

8
9 package org.enhydra.oyster.mail;
10
11 import org.enhydra.oyster.exception.SMIMEException;
12 import org.enhydra.oyster.activation.StreamDataSource;
13 import javax.mail.internet.MimeBodyPart JavaDoc;
14 import javax.mail.internet.MimeMultipart JavaDoc;
15 import org.enhydra.oyster.util.HtmlAnalyzer;
16 import java.io.File JavaDoc;
17 import java.io.InputStream JavaDoc;
18 import java.util.Vector JavaDoc;
19 import javax.activation.DataHandler JavaDoc;
20 import javax.activation.FileDataSource JavaDoc;
21
22 /**
23  * Methods from this class are used in creation of specific MimeMultipart objects
24  * other than default multipart/mixed.
25  */

26 public class MultipartGenerator
27 {
28
29   /**
30    * Returns created MimeMultipart for text/html message with html code given
31    * through InputStream. This method creates html content withouth regarding and
32    * resolving html code resource references. Returned MimeMultipart has
33    * content-type "multipart/alternative". Text plain interpretation of html
34    * message, used in "multipart/alternative" MimeMultipart, is generated
35    * automatically according to given html code.
36    * @param content0 html code in form of InputStream
37    * @return MimeMultipart with Content-Type: "multipart/alternative".
38    * @exception SMIMEException caused by non SMIMEException which is MessagingException
39    */

40   public static MimeMultipart JavaDoc getHtmlMultipart(InputStream JavaDoc content0)
41       throws SMIMEException {
42
43     return getHtmlMultipart(content0, null);
44   }
45
46
47   /**
48    * Returns created MimeMultipart for text/html message with html code given
49    * through InputStream. This method creates html content withouth regarding and
50    * resolving html code resource references. Returned MimeMultipart has
51    * content-type "multipart/alternative". Text plain interpretation of html
52    * message, used in "multipart/alternative" MimeMultipart, is generated
53    * according to given external prepared text message (argument
54    * externalPlainText0) or automatically according to given html code if
55    * argument externalPlainText0 has value null.
56    * @param content0 html code in form of InputStream
57    * @param externalPlainText0 external plain text message represented as String.
58    * If this argument has value null then autogeneration of text/plain message is
59    * performed according to passed html code (content0 argument).
60    * @return MimeMultipart with Content-Type: "multipart/alternative".
61    * @exception SMIMEException caused by non SMIMEException which is MessagingException
62    */

63   public static MimeMultipart JavaDoc getHtmlMultipart(InputStream JavaDoc content0, String JavaDoc externalPlainText0)
64       throws SMIMEException {
65
66     MimeMultipart JavaDoc alternativePart = new MimeMultipart JavaDoc("alternative");
67     try {
68       HtmlAnalyzer ha = new HtmlAnalyzer(content0, externalPlainText0);
69
70       MimeBodyPart JavaDoc textBody = new MimeBodyPart JavaDoc();
71       textBody.setText(ha.getPlainText(), "ISO-8859-1");
72       MimeBodyPart JavaDoc htmlBody = new MimeBodyPart JavaDoc();
73       htmlBody.setContent(ha.getHtmlText(),"text/html");
74
75       alternativePart.addBodyPart(textBody);
76       alternativePart.addBodyPart(htmlBody);
77     }
78     catch(Exception JavaDoc e) {
79       throw SMIMEException.getInstance( "org.enhydra.oyster.mail.MultipartGenerator",
80                                         e, "getHtmlMultipart");
81     }
82     return alternativePart;
83   }
84
85
86   /**
87    * Returns created MimeMultipart for text/html message with html code given
88    * through InputStream, path for resolving relative path of resources defined in
89    * html code and array of resources given through InputStream. This method
90    * creates html content with resolving html code resource references. Returned
91    * MimeMultipart has content-type "multipart/related", and inside itself,
92    * this MimeMultipart convey BodyParts with embeded resources, and one BodyPart
93    * which holds MimeMultipart with Content-Type "multipart/alternative".
94    * text/plain interpretation of html message, used in "multipart/alternative"
95    * MimeMultipart, is generated automatically according to given html code.
96    * @param content0 html code in form of InputStream
97    * @param path0 common path for resolving relative referenced resources in html
98    * code atributes of type "src" and "background".
99    * @param resources0 array of resources given as array of InputStream. For more
100    * information refer to setContent methods of classes from package:
101    * org.enhydra.oyster.smime
102    * @return MimeMultipart with Content-Type: "multipart/related".
103    * @exception SMIMEException caused by non SMIMEException which is MessagingException
104    */

105   public static MimeMultipart JavaDoc getHtmlMultipart( InputStream JavaDoc content0,
106       String JavaDoc path0,
107       InputStream JavaDoc[] resources0 )
108       throws SMIMEException {
109     return getHtmlMultipart(content0, path0, resources0, null);
110   }
111
112
113   /**
114    * Returns created MimeMultipart for text/html message with html code given
115    * through InputStream, path for resolving relative path of resources defined in
116    * html code and array of resources given through InputStream. This method
117    * creates html content with resolving html code resource references. Returned
118    * MimeMultipart has content-type "multipart/related", and inside itself,
119    * this MimeMultipart convey BodyParts with embeded resources, and one BodyPart
120    * which holds MimeMultipart with Content-Type "multipart/alternative". Text
121    * plain interpretation of html message, used in "multipart/alternative"
122    * MimeMultipart, is generated according to given external prepared text
123    * message (argument externalPlainText0) or automatically according to given
124    * html code if argument externalPlainText0 has value null.
125    * @param content0 html code in form of InputStream
126    * @param path0 common path for resolving relative referenced resources in html
127    * code atributes of type "src" and "background".
128    * @param resources0 array of resources given as array of InputStream. For more
129    * information refer to setContent methods of classes from package:
130    * org.enhydra.oyster.smime
131    * @param externalPlainText0 external plain/text message represented as String.
132    * If this argument has value null then autogeneration of text/plain message is
133    * performed according to passed html code (content0 argument).
134    * @return MimeMultipart with Content-Type: "multipart/related".
135    * @exception SMIMEException caused by non SMIMEException which is MessagingException
136    */

137   public static MimeMultipart JavaDoc getHtmlMultipart( InputStream JavaDoc content0,
138       String JavaDoc path0,
139       InputStream JavaDoc[] resources0,
140       String JavaDoc externalPlainText0 )
141       throws SMIMEException {
142     try {
143       HtmlAnalyzer ha = new HtmlAnalyzer(content0, path0, externalPlainText0);
144       MimeBodyPart JavaDoc textBody = new MimeBodyPart JavaDoc();
145       textBody.setText(ha.getPlainText(), "ISO-8859-1");
146       MimeBodyPart JavaDoc htmlBody = new MimeBodyPart JavaDoc();
147       htmlBody.setContent(ha.getHtmlText(),"text/html");
148
149       MimeMultipart JavaDoc alternativePart = new MimeMultipart JavaDoc("alternative");
150       alternativePart.addBodyPart(textBody);
151       alternativePart.addBodyPart(htmlBody);
152
153       MimeMultipart JavaDoc relatedPart = null;
154       Vector JavaDoc vAddress = ha.getSwappedAdresses();
155       if (vAddress != null) {
156         relatedPart = new MimeMultipart JavaDoc("related");
157         MimeBodyPart JavaDoc alternativeBody = new MimeBodyPart JavaDoc();
158         alternativeBody.setContent(alternativePart);
159         relatedPart.addBodyPart( alternativeBody );
160
161         for( int i = 0; i < vAddress.size(); i=i+2 ) {
162           try {
163             if(vAddress.elementAt(i) instanceof File JavaDoc) {
164               MimeBodyPart JavaDoc mbp = new MimeBodyPart JavaDoc();
165               File JavaDoc fn = (File JavaDoc)vAddress.elementAt(i);
166               FileDataSource JavaDoc fd = new FileDataSource JavaDoc(fn);
167               mbp.setDataHandler(new DataHandler JavaDoc(fd));
168               mbp.setFileName(fn.getName());
169               mbp.addHeader("Content-ID","<" + vAddress.elementAt(i+1) + ">");
170               relatedPart.addBodyPart(mbp);
171             }
172             else {
173               MimeBodyPart JavaDoc mbp = new MimeBodyPart JavaDoc();
174               int index = Integer.parseInt(
175                   ((String JavaDoc)vAddress.elementAt(i)).substring(5,8));
176               String JavaDoc fileName = ((String JavaDoc)vAddress.elementAt(i)).substring(8);
177               mbp.setDataHandler( new DataHandler JavaDoc(new StreamDataSource(
178                   resources0[index], fileName )) );
179               mbp.setFileName(fileName);
180               mbp.addHeader("Content-ID","<" + vAddress.elementAt(i+1) + ">");
181               relatedPart.addBodyPart(mbp);
182             }
183           }
184           catch(Exception JavaDoc e) {
185             continue; // Forces that adding of bodyparts carry on if error happened in one of the adding
186
}
187         }
188         if(relatedPart.getCount() == 0)
189           return alternativePart;
190         return relatedPart;
191       }
192       else
193         return alternativePart;
194     }
195     catch(Exception JavaDoc e) {
196       throw SMIMEException.getInstance( "org.enhydra.oyster.mail.MultipartGenerator",
197                                         e, "getHtmlMultipart");
198     }
199   }
200
201 }
202
Popular Tags