1 32 33 package com.knowgate.scheduler.jobs; 34 35 import java.lang.ref.SoftReference ; 36 37 import java.util.HashMap ; 38 import java.util.Iterator ; 39 40 import java.sql.SQLException ; 41 42 import java.io.IOException ; 43 import java.io.FileNotFoundException ; 44 import java.io.FileReader ; 45 import java.io.File ; 46 import java.io.StringBufferInputStream ; 47 48 import java.net.URL ; 49 import java.net.MalformedURLException ; 50 51 import javax.activation.DataHandler ; 52 import javax.activation.DataSource ; 53 import javax.activation.FileDataSource ; 54 import javax.activation.URLDataSource ; 55 56 import javax.mail.Session ; 57 import javax.mail.Transport ; 58 import javax.mail.MessagingException ; 59 import javax.mail.NoSuchProviderException ; 60 import javax.mail.BodyPart ; 61 62 import javax.mail.internet.AddressException ; 63 import javax.mail.internet.InternetAddress ; 64 import javax.mail.internet.MimeMessage ; 65 import javax.mail.internet.MimeBodyPart ; 66 import javax.mail.internet.MimeMultipart ; 67 68 import org.htmlparser.Parser; 69 import org.htmlparser.Node; 70 import org.htmlparser.util.NodeIterator; 71 import org.htmlparser.util.ParserException; 72 import org.htmlparser.tags.ImageTag; 73 74 import org.apache.oro.text.regex.*; 75 76 import com.knowgate.debug.DebugFile; 77 import com.knowgate.jdc.JDCConnection; 78 import com.knowgate.dataobjs.DB; 79 import com.knowgate.dataxslt.FastStreamReplacer; 80 import com.knowgate.dfs.FileSystem; 81 82 import com.knowgate.scheduler.Atom; 83 import com.knowgate.scheduler.Job; 84 85 86 92 93 public class EmailSender extends Job { 94 95 private boolean bHasReplacements; 102 103 private SoftReference oFileStr; 107 108 private FastStreamReplacer oReplacer; 111 112 Session oMailSession; 114 Transport oMailTransport; 115 116 HashMap oDocumentImages; 119 120 private SoftReference oHTMLStr; 123 124 125 126 128 public EmailSender() { 129 bHasReplacements = true; 130 oFileStr = null; 131 oHTMLStr = null; 132 oReplacer = new FastStreamReplacer(); 133 oDocumentImages = new HashMap (); 134 oMailSession = null; 135 oMailTransport = null; 136 } 137 138 140 public void free() {} 141 142 144 153 public void setStatus(JDCConnection oConn, int iStatus) throws SQLException { 154 155 if (DebugFile.trace) { 156 DebugFile.writeln("Begin EmailSender.setStatus([Connection], " + String.valueOf(iStatus) + ")"); 157 DebugFile.incIdent(); 158 } 159 160 super.setStatus(oConn, iStatus); 161 162 if (Job.STATUS_RUNNING!=iStatus) { 163 164 if (oMailTransport!=null) { 165 try { 166 if (oMailTransport.isConnected()) 167 oMailTransport.close(); 168 } 169 catch (MessagingException msge) { 170 if ( DebugFile.trace) 171 DebugFile.writeln("Transport.close() MessagingException " + msge.getMessage()); 172 } 173 174 oMailTransport = null; 175 } 177 if (null!=oMailSession) oMailSession = null; 178 179 } 181 if (DebugFile.trace) { 182 DebugFile.decIdent(); 183 DebugFile.writeln("End EMailSender.setStatus()"); 184 } 185 } 187 189 private String attachFiles(String sHTMLPath) throws FileNotFoundException ,IOException { 190 String sHtml = null; 191 192 if (DebugFile.trace) { 193 DebugFile.writeln("Begin EmailSender.attachFiles(" + sHTMLPath + ")"); 194 DebugFile.incIdent(); 195 DebugFile.writeln("new File(" + sHTMLPath + ")"); 196 } 197 198 try { 199 FileSystem oFS = new FileSystem(); 200 sHtml = oFS.readfilestr(sHTMLPath, null); 201 oFS = null; 202 } 203 catch (com.enterprisedt.net.ftp.FTPException ftpe) {} 204 205 PatternMatcher oMatcher = new Perl5Matcher(); 206 PatternCompiler oCompiler = new Perl5Compiler(); 207 208 Parser parser = Parser.createParser(sHtml, null); 209 210 StringBuffer oRetVal = new StringBuffer (sHtml.length()); 211 212 try { 213 for (NodeIterator i = parser.elements(); i.hasMoreNodes(); ) { 214 Node node = i.nextNode(); 215 216 if (node instanceof ImageTag) { 217 ImageTag oImgNode = (ImageTag) node; 218 String sSrc = oImgNode.extractImageLocn(); 219 String sTag = oImgNode.getText(); 220 221 Pattern oPattern; 222 223 try { 224 oPattern = oCompiler.compile(sSrc); 225 } catch (MalformedPatternException neverthrown) { oPattern=null; } 226 227 if (!oDocumentImages.containsKey(sSrc)) { 228 int iSlash = sSrc.lastIndexOf('/'); 229 String sCid; 230 231 if (iSlash>=0) { 232 while (sSrc.charAt(iSlash)=='/') { if (++iSlash==sSrc.length()) break; } 233 sCid = sSrc.substring(iSlash); 234 } 235 else 236 sCid = sSrc; 237 238 oDocumentImages.put(sSrc, sCid); 239 } 240 241 oRetVal.append(Util.substitute(oMatcher, oPattern, 242 new Perl5Substitution("cid:"+oDocumentImages.get(sSrc), 243 Perl5Substitution.INTERPOLATE_ALL), 244 sTag, Util.SUBSTITUTE_ALL)); 245 246 } 247 else { 248 oRetVal.append(node.getText()); 249 } 250 } 251 } 252 catch (ParserException pe) { 253 if (DebugFile.trace) { 254 DebugFile.writeln("ParserException " + pe.getMessage()); 255 } 256 257 oRetVal = new StringBuffer (sHtml.length()); 258 oRetVal.append(sHtml); 259 } 260 261 if (DebugFile.trace) { 262 DebugFile.decIdent(); 263 DebugFile.writeln("End EmailSender.attachFiles()"); 264 } 265 266 return oRetVal.toString(); 267 } 269 271 301 public Object process (Atom oAtm) throws FileNotFoundException ,IOException ,MessagingException { 302 303 File oFile; FileReader oFileRead; String sPathHTML; char cBuffer[]; StringBufferInputStream oInStrm; Object oReplaced; final String Yes = "1"; 310 311 final String sSep = System.getProperty("file.separator"); 313 if (DebugFile.trace) { 314 DebugFile.writeln("Begin EMailSender.process([Job:" + getStringNull(DB.gu_job, "") + ", Atom:" + String.valueOf(oAtm.getInt(DB.pg_atom)) + "])"); 315 DebugFile.incIdent(); 316 } 317 318 if (bHasReplacements) { 320 323 sPathHTML = getProperty("workareasput"); 325 if (!sPathHTML.endsWith(sSep)) sPathHTML += sSep; 326 327 sPathHTML += getParameter("gu_workarea") + sSep + "apps" + sSep + "Mailwire" + sSep + "html" + sSep + getParameter("gu_pageset") + sSep; 329 330 sPathHTML += getParameter("nm_pageset").replace(' ', '_') + ".html"; 332 333 if (DebugFile.trace) DebugFile.writeln("PathHTML = " + sPathHTML); 334 335 338 if (Yes.equals(getParameter("bo_attachimages"))) { 339 340 if (DebugFile.trace) DebugFile.writeln("bo_attachimages=true"); 341 342 oInStrm = null; 344 345 if (null!=oHTMLStr) { 346 if (null!=oHTMLStr.get()) 347 oInStrm = new StringBufferInputStream ((String ) oHTMLStr.get()); 350 } 351 if (null==oInStrm) 352 oInStrm = new StringBufferInputStream (attachFiles(sPathHTML)); 355 356 oHTMLStr = new SoftReference (oInStrm); 357 358 oReplaced = oReplacer.replace(oInStrm, oAtm.getItemMap()); 360 } 361 362 else { 364 if (DebugFile.trace) DebugFile.writeln("bo_attachimages=false"); 365 366 oReplaced = oReplacer.replace(sPathHTML, oAtm.getItemMap()); 368 } 369 370 bHasReplacements = (oReplacer.lastReplacements() > 0); 372 } 373 374 else { 375 376 oReplaced = null; 377 378 if (null != oFileStr) 379 oReplaced = oFileStr.get(); 380 381 if (null == oReplaced) { 382 383 386 sPathHTML = getProperty("workareasput"); 388 if (!sPathHTML.endsWith(sSep)) sPathHTML += sSep; 389 390 sPathHTML += getParameter("gu_workarea") + sSep + "apps" + sSep + "Mailwire" + sSep + "html" + sSep + getParameter("gu_pageset") + sSep + getParameter("nm_pageset").replace(' ', '_') + ".html"; 391 392 if (DebugFile.trace) DebugFile.writeln("PathHTML = " + sPathHTML); 393 394 397 if (DebugFile.trace) DebugFile.writeln("new File(" + sPathHTML + ")"); 398 399 oFile = new File (sPathHTML); 400 401 cBuffer = new char[new Long (oFile.length()).intValue()]; 402 403 oFileRead = new FileReader (oFile); 404 oFileRead.read(cBuffer); 405 oFileRead.close(); 406 407 if (DebugFile.trace) DebugFile.writeln(String.valueOf(cBuffer.length) + " characters readed"); 408 409 if (Yes.equals(getParameter("bo_attachimages"))) 410 oReplaced = attachFiles(new String (cBuffer)); 411 else 412 oReplaced = new String (cBuffer); 413 414 417 oFileStr = new SoftReference (oReplaced); 418 419 } 421 } 423 426 if (null==oMailSession) { 427 if (DebugFile.trace) DebugFile.writeln("Session.getInstance(Job.getProperties(), null)"); 428 429 java.util.Properties oMailProps = getProperties(); 430 431 if (oMailProps.getProperty("mail.transport.protocol")==null) 432 oMailProps.put("mail.transport.protocol","smtp"); 433 434 if (oMailProps.getProperty("mail.host")==null) 435 oMailProps.put("mail.host","localhost"); 436 437 oMailSession = Session.getInstance(getProperties(), null); 438 439 if (null!=oMailSession) { 440 oMailTransport = oMailSession.getTransport(); 441 442 try { 443 oMailTransport.connect(); 444 } 445 catch (NoSuchProviderException nspe) { 446 if (DebugFile.trace) DebugFile.writeln("MailTransport.connect() NoSuchProviderException " + nspe.getMessage()); 447 throw new MessagingException (nspe.getMessage(), nspe); 448 } 449 } } 452 MimeMessage oMsg; 453 InternetAddress oFrom, oTo; 454 455 try { 456 if (null==getParameter("tx_sender")) 457 oFrom = new InternetAddress (getParameter("tx_from")); 458 else 459 oFrom = new InternetAddress (getParameter("tx_from"), getParameter("tx_sender")); 460 461 if (DebugFile.trace) DebugFile.writeln("to: " + oAtm.getStringNull(DB.tx_email, "ERROR Atom[" + String.valueOf(oAtm.getInt(DB.pg_atom)) + "].tx_email is null!")); 462 463 oTo = new InternetAddress (oAtm.getString(DB.tx_email), oAtm.getStringNull(DB.tx_name,"") + " " + oAtm.getStringNull(DB.tx_surname,"")); 464 } 465 catch (AddressException adre) { 466 if (DebugFile.trace) DebugFile.writeln("AddressException " + adre.getMessage() + " job " + getString(DB.gu_job) + " atom " + String.valueOf(oAtm.getInt(DB.pg_atom))); 467 468 oFrom = null; 469 oTo = null; 470 471 throw new MessagingException ("AddressException " + adre.getMessage() + " job " + getString(DB.gu_job) + " atom " + String.valueOf(oAtm.getInt(DB.pg_atom))); 472 } 473 474 if (DebugFile.trace) DebugFile.writeln("new MimeMessage([Session])"); 475 476 oMsg = new MimeMessage (oMailSession); 477 478 oMsg.setSubject(getParameter("tx_subject")); 479 480 oMsg.setFrom(oFrom); 481 482 if (DebugFile.trace) DebugFile.writeln("MimeMessage.addRecipient(MimeMessage.RecipientType.TO, " + oTo.getAddress()); 483 484 oMsg.addRecipient(MimeMessage.RecipientType.TO, oTo); 485 486 String sSrc = null, sCid = null; 487 488 try { 489 490 if (Yes.equals(getParameter("bo_attachimages"))) { 492 493 BodyPart oMsgBodyPart = new MimeBodyPart (); 494 oMsgBodyPart.setContent(oReplaced, "text/html"); 495 496 MimeMultipart oMultiPart = new MimeMultipart ("related"); 498 oMultiPart.addBodyPart(oMsgBodyPart); 499 500 Iterator oImgs = oDocumentImages.keySet().iterator(); 501 502 while (oImgs.hasNext()) { 503 BodyPart oImgBodyPart = new MimeBodyPart (); 504 505 sSrc = (String ) oImgs.next(); 506 sCid = (String ) oDocumentImages.get(sSrc); 507 508 if (sSrc.startsWith("www.")) 509 sSrc = "http://" + sSrc; 510 511 if (sSrc.startsWith("http://") || sSrc.startsWith("https://")) { 512 oImgBodyPart.setDataHandler(new DataHandler (new URL (sSrc))); 513 } 514 else { 515 oImgBodyPart.setDataHandler(new DataHandler (new FileDataSource (sSrc))); 516 } 517 518 oImgBodyPart.setHeader("Content-ID", sCid); 519 520 oMultiPart.addBodyPart(oImgBodyPart); 522 } 524 if (DebugFile.trace) DebugFile.writeln("MimeMessage.setContent([MultiPart])"); 525 526 oMsg.setContent(oMultiPart); 527 } 528 529 else { 530 531 if (DebugFile.trace) DebugFile.writeln("MimeMessage.setContent([String], \"text/html\")"); 532 533 oMsg.setContent(oReplaced, "text/html"); 534 535 } 536 537 oMsg.saveChanges(); 538 539 if (DebugFile.trace) DebugFile.writeln("Transport.sendMessage([MimeMessage], MimeMessage.getAllRecipients())"); 540 541 oMailTransport.sendMessage(oMsg, oMsg.getAllRecipients()); 542 543 iPendingAtoms--; 546 } 547 catch (MalformedURLException urle) { 548 549 if (DebugFile.trace) DebugFile.writeln("MalformedURLException " + sSrc); 550 throw new MessagingException ("MalformedURLException " + sSrc); 551 } 552 553 if (DebugFile.trace) { 554 DebugFile.writeln("End EMailSender.process([Job:" + getStringNull(DB.gu_job, "") + ", Atom:" + String.valueOf(oAtm.getInt(DB.pg_atom)) + "])"); 555 DebugFile.decIdent(); 556 } 557 558 return oReplaced; 559 560 } 562 } | Popular Tags |