1 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 ; 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 31 public class MimeBodyTag { 32 33 private String type="text/plain"; 34 private String encoding="ISO-8859-1"; 35 private String text=""; 36 private String id="default"; 37 private String related; 38 private String alt; 39 private String formatter; 40 private String attachmentid; 41 private String filepath; 42 private String filename; 43 private Vector altnodes; private MimeMultipart relatednodes; 45 private String number; 46 private String field; 47 48 private static final Logger log = Logging.getLoggerInstance(MimeBodyTag.class); 49 50 51 public void setFormatter(String formatter) { 52 this.formatter = formatter; 53 } 54 55 56 59 60 public void addAlt(MimeBodyTag sub) { 61 if (altnodes==null) { 62 altnodes = new Vector(); 65 } 66 altnodes.addElement(sub); 68 } 69 70 71 72 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 e) { 84 log.error("Can't add related node"); 85 } 86 } 87 88 public void setAlt(String alt) { 89 this.alt=alt; 90 } 91 92 public void setRelated(String related) { 93 this.related=related; 94 } 95 96 public void setId(String id) { 97 this.id=id; 98 } 99 100 public void setFile(String filepath) { 101 this.filepath=filepath; 102 } 103 104 public void setFileName(String filename) { 105 this.filename=filename; 106 } 107 108 public String getFileName() { 109 if (filename==null) { 110 return "unknown"; 112 } 113 return filename; 114 } 115 116 public void setAttachment(String attachmentid) { 117 this.attachmentid=attachmentid; 118 } 119 120 public String getFormatter() { 121 return formatter; 122 } 123 124 public String getFile() { 125 return filepath; 126 } 127 128 public String getType() { 129 return type; 130 } 131 132 public String getEncoding() { 133 return encoding; 134 } 135 136 public String getNumber() { 137 return number; 138 } 139 140 public String getField() { 141 return field; 142 } 143 144 public String getId() { 145 return id; 146 } 147 148 public String getRelated() { 149 return related; 150 } 151 152 public String getAlt() { 153 return alt; 154 } 155 156 public void setType(String type) { 157 this.type=type; 158 } 159 160 public void setEncoding(String encoding) { 161 this.encoding=encoding; 162 } 163 164 public void setNumber(String number) { 165 this.number=number; 166 } 167 public void setField(String field) { 168 this.field=field; 169 } 170 public void setText(String text) { 171 this.text=text; 172 } 173 public String getText() { 174 if (formatter!=null) { 176 if (formatter.equals("html2plain")) { 177 return html2plain(text); 178 } 179 } 180 return text; 181 } 182 183 184 189 private static String html2plain(String input) { 190 String result=""; 192 193 StringTokenizer tok = new StringTokenizer(input,"\n\r"); 196 while (tok.hasMoreTokens()) { 197 result+=tok.nextToken(); 199 } 200 201 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 result; 218 } 219 220 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 e) { 256 log.debug("Failed to get Multipart" + e.getMessage()); 257 } 258 return null; 259 } 260 261 264 public MimeMultipart getRelatedpart() { 265 return relatednodes; 266 } 267 268 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 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 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 e){ 313 log.error("Can't add DataHandler"); 314 } 315 316 return mmbp; 317 } 318 319 320 323 324 private DataHandler getMMBaseObject(String number) { 325 return getMMBaseObject(number,""); 326 } 327 328 329 332 333 private DataHandler getMMBaseObject(String number,String 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 |