[jbpm-commits] JBoss JBPM SVN: r4487 - jbpm4/branches/email/modules/pvm/src/main/java/org/jbpm/pvm/internal/email/producer/impl.

do-not-reply at jboss.org do-not-reply at jboss.org
Tue Apr 7 15:07:17 EDT 2009


Author: bradsdavis
Date: 2009-04-07 15:07:17 -0400 (Tue, 07 Apr 2009)
New Revision: 4487

Added:
   jbpm4/branches/email/modules/pvm/src/main/java/org/jbpm/pvm/internal/email/producer/impl/HtmlScriptMailProducer.java
Log:
Added templating email producer.  Provide HTML with absolute SRC linked images [needs to be hosted images at this point] and it will embed the images into the message.

Added: jbpm4/branches/email/modules/pvm/src/main/java/org/jbpm/pvm/internal/email/producer/impl/HtmlScriptMailProducer.java
===================================================================
--- jbpm4/branches/email/modules/pvm/src/main/java/org/jbpm/pvm/internal/email/producer/impl/HtmlScriptMailProducer.java	                        (rev 0)
+++ jbpm4/branches/email/modules/pvm/src/main/java/org/jbpm/pvm/internal/email/producer/impl/HtmlScriptMailProducer.java	2009-04-07 19:07:17 UTC (rev 4487)
@@ -0,0 +1,103 @@
+package org.jbpm.pvm.internal.email.producer.impl;
+
+import java.util.Collection;
+import java.util.Date;
+import java.util.HashSet;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.commons.mail.Email;
+import org.apache.commons.mail.HtmlEmail;
+import org.jbpm.Execution;
+import org.jbpm.env.Environment;
+import org.jbpm.pvm.internal.email.producer.MailContext;
+import org.jbpm.pvm.internal.script.ScriptManager;
+
+public class HtmlScriptMailProducer extends ScriptMailProducer {
+
+	private static final Pattern imgSrcPattern = Pattern.compile("<img [.]*src=['\"]\\S+['\"]");
+	private static final Log log = LogFactory.getLog(HtmlScriptMailProducer.class);
+	
+	protected String html;
+	
+	public Collection<Email> produce(Execution exe, MailContext mailContext) throws Exception {
+
+		if(log.isTraceEnabled())
+		{
+			log.trace("Starting: "+(new Date()));
+		}
+		ScriptManager scriptManager = Environment.getFromCurrent(ScriptManager.class);
+		
+		//Apply the scripting language to the process.
+		this.body = (String)scriptManager.evaluateScript(this.body, exe, language);
+		this.subject = (String)scriptManager.evaluateScript(this.subject, exe, language);
+		this.html = (String)scriptManager.evaluate(this.html, exe, language);
+
+		HtmlEmail email = new HtmlEmail();
+		this.populateAddresses(mailContext, email);
+		email.setTextMsg(this.body);
+		email.setSubject(this.subject);
+		
+		
+		
+		Matcher imgMatcher = imgSrcPattern.matcher(html);
+		StringBuffer htmlBuffer = new StringBuffer();
+		while(imgMatcher.find())
+		{
+			String replace = embedImage(email,imgMatcher.group());
+			if(log.isDebugEnabled())
+			{
+				log.debug("Group: "+imgMatcher.group());
+				log.debug("Replacement: "+replace);
+			}
+			imgMatcher.appendReplacement(htmlBuffer, replace);
+		}
+		imgMatcher.appendTail(htmlBuffer);
+		if(log.isDebugEnabled())
+		{
+			log.debug("Embedded HTML: "+htmlBuffer.toString());
+		}
+		try {
+			email.setHtmlMsg(htmlBuffer.toString());
+		}
+		catch(Exception e)
+		{
+			
+		}
+		if(log.isTraceEnabled())
+		{
+			log.trace("Complete: "+(new Date()));
+		}
+		
+		Collection<Email> collection = new HashSet<Email>();
+		collection.add(email);
+		return collection;
+	}
+	
+
+	protected String embedImage(HtmlEmail email, String imageTag)
+	{
+		String special = null;
+		special = imageTag.contains("'") ? "'" : "\"";
+		
+		int opening = imageTag.indexOf(special,1);
+		int closing = imageTag.lastIndexOf(special);
+
+		String prefix = imageTag.substring(0,opening);
+		String src = imageTag.substring(opening+1,closing);
+		String hashName = Integer.toString(src.hashCode());
+		String cid = null; 
+		try{
+			cid = email.embed(src, hashName);
+		}
+		catch(Exception e)
+		{
+			return imageTag;
+		}
+		return prefix+special+"cid:"+cid+special;
+		
+	}
+
+}




More information about the jbpm-commits mailing list