KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > applications > email > MimeBodyTag


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10
11 package org.mmbase.applications.email;
12
13 import java.util.*;
14 import java.io.*;
15
16 import org.mmbase.module.core.*;
17 import org.mmbase.util.*;
18 import org.mmbase.bridge.*;
19
20 import javax.mail.MessagingException JavaDoc;
21 import javax.mail.internet.*;
22 import javax.activation.*;
23
24 import org.mmbase.util.logging.Logging;
25 import org.mmbase.util.logging.Logger;
26
27 /**
28  * @javadoc
29  * @author Daniel Ockeloen
30  */

31 public class MimeBodyTag {
32
33     private String JavaDoc type="text/plain";
34     private String JavaDoc encoding="ISO-8859-1";
35     private String JavaDoc text="";
36     private String JavaDoc id="default";
37     private String JavaDoc related;
38     private String JavaDoc alt;
39     private String JavaDoc formatter;
40     private String JavaDoc attachmentid;
41     private String JavaDoc filepath;
42     private String JavaDoc filename;
43     private Vector altnodes; // synchronized?
44
private MimeMultipart relatednodes;
45     private String JavaDoc number;
46     private String JavaDoc field;
47
48     private static final Logger log = Logging.getLoggerInstance(MimeBodyTag.class);
49
50
51     public void setFormatter(String JavaDoc formatter) {
52         this.formatter = formatter;
53     }
54
55
56     /**
57      * @javadoc
58      */

59
60     public void addAlt(MimeBodyTag sub) {
61         if (altnodes==null) {
62             //altnodes=new MimeMultipart("alternative");
63
//altnodes.addBodyPart(getMimeBodyPart());
64
altnodes = new Vector();
65         }
66         //altnodes.addBodyPart(sub.getMimeBodyPart());
67
altnodes.addElement(sub);
68     }
69
70
71
72     /**
73      * @javadoc
74      */

75
76     public void addRelated(MimeBodyTag sub) {
77         try {
78             if (relatednodes==null) {
79                 relatednodes=new MimeMultipart("related");
80                 relatednodes.addBodyPart(getMimeBodyPart());
81             }
82             relatednodes.addBodyPart(sub.getMimeBodyPart());
83         } catch(Exception JavaDoc e) {
84             log.error("Can't add related node");
85         }
86     }
87
88     public void setAlt(String JavaDoc alt) {
89         this.alt=alt;
90     }
91
92     public void setRelated(String JavaDoc related) {
93         this.related=related;
94     }
95
96     public void setId(String JavaDoc id) {
97         this.id=id;
98     }
99
100     public void setFile(String JavaDoc filepath) {
101         this.filepath=filepath;
102     }
103
104     public void setFileName(String JavaDoc filename) {
105         this.filename=filename;
106     }
107
108     public String JavaDoc getFileName() {
109         if (filename==null) {
110             // needs to be better, create a guessed name on getFile
111
return "unknown";
112         }
113         return filename;
114     }
115
116     public void setAttachment(String JavaDoc attachmentid) {
117         this.attachmentid=attachmentid;
118     }
119
120     public String JavaDoc getFormatter() {
121         return formatter;
122     }
123
124     public String JavaDoc getFile() {
125         return filepath;
126     }
127
128     public String JavaDoc getType() {
129         return type;
130     }
131
132     public String JavaDoc getEncoding() {
133         return encoding;
134     }
135
136     public String JavaDoc getNumber() {
137         return number;
138     }
139
140     public String JavaDoc getField() {
141         return field;
142     }
143
144     public String JavaDoc getId() {
145         return id;
146     }
147
148     public String JavaDoc getRelated() {
149         return related;
150     }
151
152     public String JavaDoc getAlt() {
153         return alt;
154     }
155
156     public void setType(String JavaDoc type) {
157         this.type=type;
158     }
159
160     public void setEncoding(String JavaDoc encoding) {
161         this.encoding=encoding;
162     }
163
164     public void setNumber(String JavaDoc number) {
165         this.number=number;
166     }
167     public void setField(String JavaDoc field) {
168         this.field=field;
169     }
170     public void setText(String JavaDoc text) {
171         this.text=text;
172     }
173     public String JavaDoc getText() {
174         // is there a formatter requested ??
175
if (formatter!=null) {
176             if (formatter.equals("html2plain")) {
177                 return html2plain(text);
178             }
179         }
180         return text;
181     }
182
183
184     /**
185      * convert 'html' to 'plain' text
186      * this removes the br and p tags and converts them
187      * to returns and dubble returns for email use.
188      */

189     private static String JavaDoc html2plain(String JavaDoc input) {
190         // define the result string
191
String JavaDoc result="";
192
193         // setup a tokenizer on all returns and linefeeds so
194
// we can remove them
195
StringTokenizer tok = new StringTokenizer(input,"\n\r");
196         while (tok.hasMoreTokens()) {
197             // add the content part stripped of its return/linefeed
198
result+=tok.nextToken();
199         }
200
201         // now use the html br and p tags to insert
202
// the wanted returns
203
StringObject obj=new StringObject(result);
204         obj.replace("<br/>","\n");
205         obj.replace("<br />","\n");
206         obj.replace("<BR/>","\n");
207         obj.replace("<BR />","\n");
208         obj.replace("<br>","\n");
209         obj.replace("<BR>","\n");
210         obj.replace("<p>","\n\n");
211         obj.replace("<p/>","\n\n");
212         obj.replace("<p />","\n\n");
213         obj.replace("<P>","\n\n");
214         result=obj.toString();
215
216         // return the coverted body
217
return result;
218     }
219
220     /**
221      * @javadoc
222      */

223     public MimeMultipart getMimeMultipart() {
224         try {
225             if (altnodes!=null) {
226                 MimeMultipart result=new MimeMultipart("alternative");
227
228                 MimeMultipart r=getRelatedpart();
229                 if (r==null) {
230                     result.addBodyPart(getMimeBodyPart());
231                 } else {
232                     MimeBodyPart wrapper=new MimeBodyPart();
233                     wrapper.setContent(r);
234                     result.addBodyPart(wrapper);
235                 }
236
237                 Enumeration e=altnodes.elements();
238                 while (e.hasMoreElements()) {
239                     MimeBodyTag t=(MimeBodyTag)e.nextElement();
240             
241                     r=t.getRelatedpart();
242                     if (r==null) {
243                         result.addBodyPart(t.getMimeBodyPart());
244                     } else {
245                         MimeBodyPart wrapper=new MimeBodyPart();
246                         wrapper.setContent(r);
247                         result.addBodyPart(wrapper);
248                     }
249             
250                 }
251                 return result;
252             }
253             if (relatednodes!=null) return relatednodes;
254         }
255         catch (MessagingException JavaDoc e) {
256             log.debug("Failed to get Multipart" + e.getMessage());
257         }
258         return null;
259     }
260
261     /**
262      * @javadoc
263      */

264     public MimeMultipart getRelatedpart() {
265         return relatednodes;
266     }
267     
268     /**
269      * @javadoc
270      */

271     public MimeBodyPart getMimeBodyPart() {
272         MimeBodyPart mmbp=new MimeBodyPart();
273         try {
274             DataHandler d=null;
275             if (number!=null && !number.equals("")) {
276                 if (field!=null) {
277                     d=getMMBaseObject(number,field);
278                 } else {
279                     d=getMMBaseObject(number);
280                 }
281             } else if (type.equals("text/plain")) {
282                 d=new DataHandler(text,type+";charset=\""+encoding+"\"");
283                 mmbp.setDataHandler(d);
284             } else if (type.equals("text/html")) {
285                 d=new DataHandler(text,type+";charset=\""+encoding+"\"");
286                 mmbp.setDataHandler(d);
287             } else if (type.equals("application/octet-stream")) {
288
289                 String JavaDoc filepath=MMBaseContext.getHtmlRoot()+File.separator+getFile();
290                 if (filepath.indexOf("..")==-1 && filepath.indexOf("WEB-INF")==-1) {
291                     FileDataSource fds=new FileDataSource(filepath);
292                     d=new DataHandler(fds);
293                     mmbp.setDataHandler(d);
294                     mmbp.setFileName(getFileName());
295                 } else {
296                     log.error("file from there not allowed");
297                 }
298             } else if (type.equals("image/gif") || type.equals("image/jpeg")) {
299
300                 String JavaDoc filepath=MMBaseContext.getHtmlRoot()+File.separator+getFile();
301                 if (filepath.indexOf("..")==-1 && filepath.indexOf("WEB-INF")==-1) {
302                     FileDataSource fds=new FileDataSource(filepath);
303                     d=new DataHandler(fds);
304                     mmbp.setDataHandler(d);
305                     mmbp.setHeader("Content-ID","<"+id+">");
306                     mmbp.setHeader("Content-Disposition","inline");
307                 } else {
308                     log.error("file from there not allowed");
309                 }
310             }
311
312         } catch(Exception JavaDoc e){
313             log.error("Can't add DataHandler");
314         }
315         
316         return mmbp;
317     }
318
319
320     /**
321      * @javadoc
322      */

323
324     private DataHandler getMMBaseObject(String JavaDoc number) {
325         return getMMBaseObject(number,"");
326     }
327
328
329     /**
330      * @javadoc
331      */

332
333     private DataHandler getMMBaseObject(String JavaDoc number,String JavaDoc field) {
334         Cloud cloud=LocalContext.getCloudContext().getCloud("mmbase");
335         Node node=cloud.getNode(number);
336         log.info("attached node="+node);
337         return null;
338     }
339 }
340
Popular Tags