From 5c8c5b48922e3e91d5c6728722b2ceb171b05fb4 Mon Sep 17 00:00:00 2001 From: Cody Lerum Date: Thu, 3 Mar 2011 22:41:12 -0700 Subject: [PATCH] refactor for a more unified templating system and a single MailMessage api --- .../org/jboss/seam/mail/api/MailMessage.java | 49 ++ .../{core => templating}/MailTemplate.java | 6 +- .../seam/mail/templating/TemplateImpl.java | 8 + .../seam/mail/attachments/FileAttachment.java | 17 + .../attachments/InputStreamAttachment.java | 17 + .../seam/mail/attachments/URLAttachment.java | 23 +- .../jboss/seam/mail/core/AttachmentPart.java | 0 .../jboss/seam/mail/core/BaseMailMessage.java | 1 + .../seam/mail/core/FileEmailAttachment.java | 44 -- .../org/jboss/seam/mail/core/MailContext.java | 52 --- .../jboss/seam/mail/core/MailMessageImpl.java | 74 ++- .../seam/mail/core/MailSessionProducer.java | 2 + .../jboss/seam/mail/core/MailTestUtil.java | 55 --- .../org/jboss/seam/mail/core/MailUtility.java | 247 ---------- .../seam/mail/templating/FileTemplate.java | 3 +- .../mail/templating/InputStreamTemplate.java | 1 - .../seam/mail/templating/TextTemplate.java | 1 - .../mail/example/GmailSessionProducer.java | 2 +- .../org/jboss/seam/mail/example/SendMail.java | 32 +- .../seam/mail/FreeMarkerMailMessageTest.java | 35 +- .../org/jboss/seam/mail/MailMessageTest.java | 4 +- .../seam/mail/VelocityMailMessageTest.java | 39 +- freemarker/api/pom.xml | 46 -- .../templating/FreeMarkerMailMessage.java | 409 ----------------- .../api/src/main/resources/META-INF/beans.xml | 27 -- freemarker/impl/pom.xml | 18 +- .../freemarker/FreeMarkerMailMessageImpl.java | 416 ----------------- .../freemarker/FreeMarkerTemplate.java | 75 ++++ pom.xml | 16 - velocity/api/pom.xml | 46 -- .../mail/templating/VelocityMailMessage.java | 409 ----------------- .../api/src/main/resources/META-INF/beans.xml | 27 -- velocity/impl/pom.xml | 5 - ...tyContext.java => CDIVelocityContext.java} | 5 +- .../velocity/SeamBaseVelocityContext.java | 51 --- .../velocity/VelocityMailMessageImpl.java | 420 ------------------ .../templating/velocity/VelocityTemplate.java | 92 ++++ 37 files changed, 430 insertions(+), 2344 deletions(-) rename core/api/src/main/java/org/jboss/seam/mail/{core => templating}/MailTemplate.java (88%) create mode 100644 core/api/src/main/java/org/jboss/seam/mail/templating/TemplateImpl.java rename core/{api => impl}/src/main/java/org/jboss/seam/mail/core/AttachmentPart.java (100%) delete mode 100644 core/impl/src/main/java/org/jboss/seam/mail/core/FileEmailAttachment.java delete mode 100644 core/impl/src/main/java/org/jboss/seam/mail/core/MailContext.java delete mode 100644 core/impl/src/main/java/org/jboss/seam/mail/core/MailTestUtil.java delete mode 100644 core/impl/src/main/java/org/jboss/seam/mail/core/MailUtility.java delete mode 100644 freemarker/api/pom.xml delete mode 100644 freemarker/api/src/main/java/org/jboss/seam/mail/templating/FreeMarkerMailMessage.java delete mode 100644 freemarker/api/src/main/resources/META-INF/beans.xml delete mode 100644 freemarker/impl/src/main/java/org/jboss/seam/mail/templating/freemarker/FreeMarkerMailMessageImpl.java create mode 100644 freemarker/impl/src/main/java/org/jboss/seam/mail/templating/freemarker/FreeMarkerTemplate.java delete mode 100644 velocity/api/pom.xml delete mode 100644 velocity/api/src/main/java/org/jboss/seam/mail/templating/VelocityMailMessage.java delete mode 100644 velocity/api/src/main/resources/META-INF/beans.xml rename velocity/impl/src/main/java/org/jboss/seam/mail/templating/velocity/{SeamCDIVelocityContext.java => CDIVelocityContext.java} (91%) delete mode 100644 velocity/impl/src/main/java/org/jboss/seam/mail/templating/velocity/SeamBaseVelocityContext.java delete mode 100644 velocity/impl/src/main/java/org/jboss/seam/mail/templating/velocity/VelocityMailMessageImpl.java create mode 100644 velocity/impl/src/main/java/org/jboss/seam/mail/templating/velocity/VelocityTemplate.java diff --git a/core/api/src/main/java/org/jboss/seam/mail/api/MailMessage.java b/core/api/src/main/java/org/jboss/seam/mail/api/MailMessage.java index 9c46d20..74611f2 100644 --- a/core/api/src/main/java/org/jboss/seam/mail/api/MailMessage.java +++ b/core/api/src/main/java/org/jboss/seam/mail/api/MailMessage.java @@ -28,6 +28,8 @@ import org.jboss.seam.mail.core.InvalidAddressException; import org.jboss.seam.mail.core.SendFailedException; import org.jboss.seam.mail.core.enumurations.MessagePriority; +import org.jboss.seam.mail.templating.MailTemplate; +import org.jboss.seam.mail.templating.TemplateImpl; /** * Base interface for creating email messages. @@ -371,4 +373,51 @@ public interface MailMessage */ public EmailMessage send(); + + //Templating Specific + + /** + * Set the template to be used for the message subject + * + * @param subject {@link MailTemplate} to use + * @throws TemplatingException + */ + public MailMessage subject(TemplateImpl subject); + + /** + * Sets the text body of the message to the plain text output of the given + * template + * + * @param textBody {@link MailTemplate} to use + * @throws TemplatingException + */ + public MailMessage bodyText(TemplateImpl textbody); + + /** + * Sets the HTML body of the message to the HTML output of the given template + * + * @param htmlBody {@link MailTemplate} to use + * @throws TemplatingException + */ + public MailMessage bodyHtml(TemplateImpl htmlBody); + + /** + * Sets the body of the message to a HTML body with a plain text alternative + * output of the given templates + * + * @param htmlBody {@link MailTemplate} to use for HTML portion of + * message + * @param textBody {@link MailTemplate} to use for Text alternative + * portion of message + * @throws TemplatingException + */ + public MailMessage bodyHtmlTextAlt(TemplateImpl htmlBody, TemplateImpl textbody); + + /** + * Places a variable in the templating engines context + * + * @param name Reference name of the object + * @param value the Object being placed in the context + */ + public MailMessage put(String name, Object value); } diff --git a/core/api/src/main/java/org/jboss/seam/mail/core/MailTemplate.java b/core/api/src/main/java/org/jboss/seam/mail/templating/MailTemplate.java similarity index 88% rename from core/api/src/main/java/org/jboss/seam/mail/core/MailTemplate.java rename to core/api/src/main/java/org/jboss/seam/mail/templating/MailTemplate.java index 69767bd..6866b31 100644 --- a/core/api/src/main/java/org/jboss/seam/mail/core/MailTemplate.java +++ b/core/api/src/main/java/org/jboss/seam/mail/templating/MailTemplate.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.jboss.seam.mail.core; +package org.jboss.seam.mail.templating; import java.io.InputStream; /** @@ -24,8 +24,8 @@ * */ public interface MailTemplate -{ +{ public String getTemplateName(); - public InputStream getInputStream(); + public InputStream getInputStream(); } diff --git a/core/api/src/main/java/org/jboss/seam/mail/templating/TemplateImpl.java b/core/api/src/main/java/org/jboss/seam/mail/templating/TemplateImpl.java new file mode 100644 index 0000000..e637e9d --- /dev/null +++ b/core/api/src/main/java/org/jboss/seam/mail/templating/TemplateImpl.java @@ -0,0 +1,8 @@ +package org.jboss.seam.mail.templating; + +import java.util.Map; + +public interface TemplateImpl +{ + public String merge(Map context); +} diff --git a/core/impl/src/main/java/org/jboss/seam/mail/attachments/FileAttachment.java b/core/impl/src/main/java/org/jboss/seam/mail/attachments/FileAttachment.java index 1d0256e..9e21536 100644 --- a/core/impl/src/main/java/org/jboss/seam/mail/attachments/FileAttachment.java +++ b/core/impl/src/main/java/org/jboss/seam/mail/attachments/FileAttachment.java @@ -1,3 +1,20 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2011, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.jboss.seam.mail.attachments; import java.io.File; diff --git a/core/impl/src/main/java/org/jboss/seam/mail/attachments/InputStreamAttachment.java b/core/impl/src/main/java/org/jboss/seam/mail/attachments/InputStreamAttachment.java index 09eef46..2989c7d 100644 --- a/core/impl/src/main/java/org/jboss/seam/mail/attachments/InputStreamAttachment.java +++ b/core/impl/src/main/java/org/jboss/seam/mail/attachments/InputStreamAttachment.java @@ -1,3 +1,20 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2011, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.jboss.seam.mail.attachments; import java.io.IOException; diff --git a/core/impl/src/main/java/org/jboss/seam/mail/attachments/URLAttachment.java b/core/impl/src/main/java/org/jboss/seam/mail/attachments/URLAttachment.java index 960f7d1..2068e48 100644 --- a/core/impl/src/main/java/org/jboss/seam/mail/attachments/URLAttachment.java +++ b/core/impl/src/main/java/org/jboss/seam/mail/attachments/URLAttachment.java @@ -1,3 +1,20 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2011, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.jboss.seam.mail.attachments; import java.io.IOException; @@ -9,7 +26,11 @@ import org.jboss.seam.mail.core.AttachmentException; import org.jboss.seam.mail.core.Header; import org.jboss.seam.mail.core.enumurations.ContentDisposition; - +/** + * + * @author Cody Lerum + * + */ public class URLAttachment extends BaseAttachment { public URLAttachment(String url, String fileName, ContentDisposition contentDisposition) diff --git a/core/api/src/main/java/org/jboss/seam/mail/core/AttachmentPart.java b/core/impl/src/main/java/org/jboss/seam/mail/core/AttachmentPart.java similarity index 100% rename from core/api/src/main/java/org/jboss/seam/mail/core/AttachmentPart.java rename to core/impl/src/main/java/org/jboss/seam/mail/core/AttachmentPart.java diff --git a/core/impl/src/main/java/org/jboss/seam/mail/core/BaseMailMessage.java b/core/impl/src/main/java/org/jboss/seam/mail/core/BaseMailMessage.java index 791904b..c64ac22 100644 --- a/core/impl/src/main/java/org/jboss/seam/mail/core/BaseMailMessage.java +++ b/core/impl/src/main/java/org/jboss/seam/mail/core/BaseMailMessage.java @@ -39,6 +39,7 @@ import org.jboss.seam.mail.core.enumurations.MailHeader; import org.jboss.seam.mail.core.enumurations.MessagePriority; import org.jboss.seam.mail.core.enumurations.RecipientType; +import org.jboss.seam.mail.util.MailUtility; /** * * @author Cody Lerum diff --git a/core/impl/src/main/java/org/jboss/seam/mail/core/FileEmailAttachment.java b/core/impl/src/main/java/org/jboss/seam/mail/core/FileEmailAttachment.java deleted file mode 100644 index 38ffeca..0000000 --- a/core/impl/src/main/java/org/jboss/seam/mail/core/FileEmailAttachment.java +++ /dev/null @@ -1,44 +0,0 @@ -package org.jboss.seam.mail.core; - -import java.io.File; -import java.io.IOException; - -import javax.activation.FileDataSource; - -import org.jboss.seam.mail.attachments.BaseAttachment; -import org.jboss.seam.mail.core.enumurations.ContentDisposition; - -import com.google.common.io.Files; - -/** - * - * @author Cody Lerum - * - */ -public class FileEmailAttachment extends BaseAttachment -{ - public FileEmailAttachment(File file, ContentDisposition contentDisposition) - { - super(); - - FileDataSource fds = new FileDataSource(file); - - try - { - super.setFileName(fds.getName()); - super.setMimeType(fds.getContentType()); - super.setContentDisposition(contentDisposition); - super.setBytes(Files.toByteArray(file)); - } - catch (IOException e) - { - throw new AttachmentException("Wasn't able to create email attachment from File: " + file.getName(), e); - } - } - - public FileEmailAttachment(File file, ContentDisposition contentDisposition, String contentClass) - { - this(file, contentDisposition); - super.addHeader(new Header("Content-Class", contentClass)); - } -} diff --git a/core/impl/src/main/java/org/jboss/seam/mail/core/MailContext.java b/core/impl/src/main/java/org/jboss/seam/mail/core/MailContext.java deleted file mode 100644 index 347f6e4..0000000 --- a/core/impl/src/main/java/org/jboss/seam/mail/core/MailContext.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * JBoss, Home of Professional Open Source - * Copyright 2011, Red Hat, Inc., and individual contributors - * by the @authors tag. See the copyright.txt in the distribution for a - * full listing of individual contributors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * http://www.apache.org/licenses/LICENSE-2.0 - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jboss.seam.mail.core; - -import java.util.Map; - -/** - * - * @author Cody Lerum - * - */ -public class MailContext -{ - - private Map attachments; - - public MailContext(Map attachments) - { - this.attachments = attachments; - } - - public String insert(String fileName) - { - EmailAttachment attachment = null; - - attachment = attachments.get(fileName); - - if (attachment == null) - { - throw new RuntimeException("Unable to find attachment: " + fileName); - } - else - { - return "cid:" + attachment.getContentId(); - } - } -} diff --git a/core/impl/src/main/java/org/jboss/seam/mail/core/MailMessageImpl.java b/core/impl/src/main/java/org/jboss/seam/mail/core/MailMessageImpl.java index ad514e6..0323a16 100644 --- a/core/impl/src/main/java/org/jboss/seam/mail/core/MailMessageImpl.java +++ b/core/impl/src/main/java/org/jboss/seam/mail/core/MailMessageImpl.java @@ -18,6 +18,8 @@ package org.jboss.seam.mail.core; import java.util.Collection; +import java.util.HashMap; +import java.util.Map; import javax.enterprise.inject.Instance; import javax.inject.Inject; @@ -30,6 +32,10 @@ import org.jboss.seam.mail.core.enumurations.ContentType; import org.jboss.seam.mail.core.enumurations.EmailMessageType; import org.jboss.seam.mail.core.enumurations.MessagePriority; +import org.jboss.seam.mail.templating.MailContext; +import org.jboss.seam.mail.templating.TemplateImpl; +import org.jboss.seam.mail.util.EmailAttachmentUtil; +import org.jboss.seam.mail.util.MailUtility; /** * @@ -39,7 +45,13 @@ public class MailMessageImpl implements MailMessage { private EmailMessage emailMessage; - + + private TemplateImpl subjectTemplate; + private TemplateImpl textTemplate; + private TemplateImpl htmlTemplate; + private Map templateContext = new HashMap(); + private boolean templatesMerged; + @Inject private Instance session; @@ -283,6 +295,37 @@ public MailMessage iCal(String html, byte[] bytes) } // End Calendar + + public MailMessage subject(TemplateImpl subject) + { + subjectTemplate = subject; + return this; + } + + public MailMessage bodyText(TemplateImpl textBody) + { + textTemplate = textBody; + return this; + } + + public MailMessage bodyHtml(TemplateImpl htmlBody) + { + htmlTemplate = htmlBody; + return this; + } + + public MailMessage bodyHtmlTextAlt(TemplateImpl htmlBody, TemplateImpl textBody) + { + bodyHtml(htmlBody); + bodyText(textBody); + return this; + } + + public MailMessage put(String key, Object value) + { + templateContext.put(key, value); + return this; + } public EmailMessage getEmailMessage() { @@ -293,9 +336,38 @@ public void setEmailMessage(EmailMessage emailMessage) { this.emailMessage = emailMessage; } + + public MailMessage mergeTemplates() + { + put("mailContext", new MailContext(EmailAttachmentUtil.getEmailAttachmentMap(emailMessage.getAttachments()))); + + if (subjectTemplate != null) + { + emailMessage.setSubject(subjectTemplate.merge(templateContext)); + } + + if (textTemplate != null) + { + emailMessage.setTextBody(textTemplate.merge(templateContext)); + } + + if (htmlTemplate != null) + { + emailMessage.setHtmlBody(htmlTemplate.merge(templateContext)); + } + + templatesMerged = true; + + return this; + } public EmailMessage send(Session session) throws SendFailedException { + if(!templatesMerged) + { + mergeTemplates(); + } + MailUtility.send(emailMessage, session); return emailMessage; diff --git a/core/impl/src/main/java/org/jboss/seam/mail/core/MailSessionProducer.java b/core/impl/src/main/java/org/jboss/seam/mail/core/MailSessionProducer.java index 841e128..5674972 100644 --- a/core/impl/src/main/java/org/jboss/seam/mail/core/MailSessionProducer.java +++ b/core/impl/src/main/java/org/jboss/seam/mail/core/MailSessionProducer.java @@ -21,6 +21,8 @@ import javax.inject.Inject; import javax.mail.Session; +import org.jboss.seam.mail.util.MailUtility; + /** * * @author Cody Lerum diff --git a/core/impl/src/main/java/org/jboss/seam/mail/core/MailTestUtil.java b/core/impl/src/main/java/org/jboss/seam/mail/core/MailTestUtil.java deleted file mode 100644 index 271198d..0000000 --- a/core/impl/src/main/java/org/jboss/seam/mail/core/MailTestUtil.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * JBoss, Home of Professional Open Source - * Copyright 2011, Red Hat, Inc., and individual contributors - * by the @authors tag. See the copyright.txt in the distribution for a - * full listing of individual contributors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * http://www.apache.org/licenses/LICENSE-2.0 - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jboss.seam.mail.core; - -import java.io.IOException; -import java.io.InputStreamReader; - -import javax.mail.BodyPart; -import javax.mail.MessagingException; -import javax.mail.internet.MimeMultipart; - -import com.google.common.io.CharStreams; - -/** - * - * @author Cody Lerum - * - */ -public class MailTestUtil -{ - public static String getAddressHeader(String address) - { - return address; - } - - public static String getAddressHeader(String name, String address) - { - return name + " <" + address + ">"; - } - - public static String getStringContent(MimeMultipart mmp, int index) throws IOException, MessagingException - { - return getStringContent(mmp.getBodyPart(index)); - } - - public static String getStringContent(BodyPart bodyPart) throws IOException, MessagingException - { - return CharStreams.toString(new InputStreamReader(bodyPart.getInputStream())); - } -} diff --git a/core/impl/src/main/java/org/jboss/seam/mail/core/MailUtility.java b/core/impl/src/main/java/org/jboss/seam/mail/core/MailUtility.java deleted file mode 100644 index 84b2d88..0000000 --- a/core/impl/src/main/java/org/jboss/seam/mail/core/MailUtility.java +++ /dev/null @@ -1,247 +0,0 @@ -/* - * JBoss, Home of Professional Open Source - * Copyright 2011, Red Hat, Inc., and individual contributors - * by the @authors tag. See the copyright.txt in the distribution for a - * full listing of individual contributors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * http://www.apache.org/licenses/LICENSE-2.0 - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jboss.seam.mail.core; - -import java.io.UnsupportedEncodingException; -import java.net.UnknownHostException; -import java.util.Collection; -import java.util.HashSet; -import java.util.Properties; -import java.util.Set; - -import javax.mail.MessagingException; -import javax.mail.Session; -import javax.mail.internet.AddressException; -import javax.mail.internet.InternetAddress; - -import org.jboss.seam.mail.core.enumurations.EmailMessageType; -import org.jboss.seam.mail.core.enumurations.RecipientType; -import org.jboss.seam.mail.util.Strings; - -/** - * - * @author Cody Lerum - * - */ -public class MailUtility -{ - public static InternetAddress internetAddress(String address) throws InvalidAddressException - { - try - { - return new InternetAddress(address); - } - catch (AddressException e) - { - throw new InvalidAddressException(e); - } - } - - public static InternetAddress internetAddress(String address, String name) throws InvalidAddressException - { - InternetAddress internetAddress; - try - { - internetAddress = new InternetAddress(address); - internetAddress.setPersonal(name); - return internetAddress; - } - catch (AddressException e) - { - throw new InvalidAddressException(e); - } - catch (UnsupportedEncodingException e) - { - throw new InvalidAddressException(e); - } - } - - public static InternetAddress internetAddress(EmailContact emailContact) throws InvalidAddressException - { - if (Strings.isNullOrBlank(emailContact.getName())) - { - return MailUtility.internetAddress(emailContact.getAddress()); - } - else - { - return MailUtility.internetAddress(emailContact.getAddress(), emailContact.getName()); - } - } - - public static Collection internetAddress(Collection emailContacts) throws InvalidAddressException - { - Set internetAddresses = new HashSet(); - - for (EmailContact ec : emailContacts) - { - internetAddresses.add(MailUtility.internetAddress(ec)); - } - - return internetAddresses; - } - - public static InternetAddress[] getInternetAddressses(InternetAddress emaiAddress) - { - InternetAddress[] internetAddresses = { emaiAddress }; - - return internetAddresses; - } - - public static InternetAddress[] getInternetAddressses(Collection recipients) - { - InternetAddress[] result = new InternetAddress[recipients.size()]; - recipients.toArray(result); - return result; - } - - public static String getHostName() - { - try - { - java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost(); - return localMachine.getHostName(); - } - catch (UnknownHostException e) - { - return "localhost"; - } - } - - public static Session buildMailSession(MailConfig mailConfig) - { - Session session; - - Properties props = new Properties(); - - if (mailConfig.isValid()) - { - props.put("mail.smtp.host", mailConfig.getServerHost()); - props.put("mail.smtp.port", mailConfig.getServerPort()); - props.put("mail.smtp.starttls.enable", mailConfig.isEnableTls()); - props.put("mail.smtp.starttls.required", mailConfig.isRequireTls()); - props.put("mail.smtp.ssl.enable", mailConfig.isEnableSsl()); - props.put("mail.smtp.auth", mailConfig.isAuth()); - } - else - { - throw new RuntimeException("ServerHost and ServerPort must be set in MailConfig"); - } - - if (!Strings.isNullOrBlank(mailConfig.getDomainName())) - { - props.put("mail.seam.domainName", mailConfig.getDomainName()); - } - - if (mailConfig.getUsername() != null && mailConfig.getUsername().length() != 0 && mailConfig.getPassword() != null && mailConfig.getPassword().length() != 0) - { - MailSessionAuthenticator authenticator = new MailSessionAuthenticator(mailConfig.getUsername(), mailConfig.getPassword()); - - session = Session.getInstance(props, authenticator); - } - else - { - session = Session.getInstance(props, null); - } - return session; - } - - public static String headerStripper(String header) - { - if (!Strings.isNullOrBlank(header)) - { - String s = header.trim(); - - if (s.matches("^<.*>$")) - { - return header.substring(1, header.length() - 1); - } - else - { - return header; - } - } - else - { - return header; - } - } - - public static void send(EmailMessage e, Session session) throws SendFailedException - { - BaseMailMessage b = new BaseMailMessage(session, e.getRootContentType()); - - if (!Strings.isNullOrBlank(e.getMessageId())) - { - b.setMessageID(e.getMessageId()); - } - - b.setFrom(e.getFromAddresses()); - b.addRecipients(RecipientType.TO, e.getToAddresses()); - b.addRecipients(RecipientType.CC, e.getCcAddresses()); - b.addRecipients(RecipientType.BCC, e.getBccAddresses()); - b.setReplyTo(e.getReplyToAddresses()); - b.addDeliveryRecieptAddresses(e.getDeliveryReceiptAddresses()); - b.addReadRecieptAddresses(e.getReadReceiptAddresses()); - b.setImportance(e.getImportance()); - b.addHeaders(e.getHeaders()); - - if (e.getSubject() != null) - { - b.setSubject(e.getSubject()); - } - - if (e.getType() == EmailMessageType.STANDARD) - { - - if (e.getHtmlBody() != null && e.getTextBody() != null) - { - b.setHTMLTextAlt(e.getHtmlBody(), e.getTextBody()); - } - else if (e.getTextBody() != null) - { - b.setText(e.getTextBody()); - } - else if (e.getHtmlBody() != null) - { - b.setHTML(e.getHtmlBody()); - } - - b.addAttachments(e.getAttachments()); - } - else if (e.getType() == EmailMessageType.INVITE_ICAL) - { - b.setHTMLNotRelated(e.getHtmlBody()); - b.addAttachments(e.getAttachments()); - } - else - { - throw new RuntimeException("Unsupported Message Type: " + e.getType()); - } - b.send(); - - try - { - e.setMessageId(null); - e.setLastMessageId(MailUtility.headerStripper(b.getFinalizedMessage().getMessageID())); - } - catch (MessagingException e1) - { - throw new RuntimeException("Unable to read Message-ID from sent message"); - } - } -} diff --git a/core/impl/src/main/java/org/jboss/seam/mail/templating/FileTemplate.java b/core/impl/src/main/java/org/jboss/seam/mail/templating/FileTemplate.java index 2f53882..554a2c2 100644 --- a/core/impl/src/main/java/org/jboss/seam/mail/templating/FileTemplate.java +++ b/core/impl/src/main/java/org/jboss/seam/mail/templating/FileTemplate.java @@ -22,7 +22,6 @@ import java.io.FileNotFoundException; import java.io.InputStream; -import org.jboss.seam.mail.core.MailTemplate; import org.jboss.seam.mail.templating.TemplatingException; /** @@ -32,7 +31,7 @@ */ public class FileTemplate implements MailTemplate { - private String templateName; + private String templateName; private File file; public FileTemplate(File file) diff --git a/core/impl/src/main/java/org/jboss/seam/mail/templating/InputStreamTemplate.java b/core/impl/src/main/java/org/jboss/seam/mail/templating/InputStreamTemplate.java index b7fea2f..a02a2df 100644 --- a/core/impl/src/main/java/org/jboss/seam/mail/templating/InputStreamTemplate.java +++ b/core/impl/src/main/java/org/jboss/seam/mail/templating/InputStreamTemplate.java @@ -19,7 +19,6 @@ import java.io.InputStream; -import org.jboss.seam.mail.core.MailTemplate; /** * diff --git a/core/impl/src/main/java/org/jboss/seam/mail/templating/TextTemplate.java b/core/impl/src/main/java/org/jboss/seam/mail/templating/TextTemplate.java index d5c83ac..bf3536c 100644 --- a/core/impl/src/main/java/org/jboss/seam/mail/templating/TextTemplate.java +++ b/core/impl/src/main/java/org/jboss/seam/mail/templating/TextTemplate.java @@ -21,7 +21,6 @@ import java.io.InputStream; import java.io.UnsupportedEncodingException; -import org.jboss.seam.mail.core.MailTemplate; import org.jboss.seam.mail.templating.TemplatingException; /** diff --git a/examples/sendmail/src/main/java/org/jboss/seam/mail/example/GmailSessionProducer.java b/examples/sendmail/src/main/java/org/jboss/seam/mail/example/GmailSessionProducer.java index 0d2b34f..8575227 100644 --- a/examples/sendmail/src/main/java/org/jboss/seam/mail/example/GmailSessionProducer.java +++ b/examples/sendmail/src/main/java/org/jboss/seam/mail/example/GmailSessionProducer.java @@ -23,7 +23,7 @@ import org.jboss.logging.Logger; import org.jboss.seam.mail.core.MailConfig; -import org.jboss.seam.mail.core.MailUtility; +import org.jboss.seam.mail.util.MailUtility; /** * diff --git a/examples/sendmail/src/main/java/org/jboss/seam/mail/example/SendMail.java b/examples/sendmail/src/main/java/org/jboss/seam/mail/example/SendMail.java index 7240239..843ebb6 100644 --- a/examples/sendmail/src/main/java/org/jboss/seam/mail/example/SendMail.java +++ b/examples/sendmail/src/main/java/org/jboss/seam/mail/example/SendMail.java @@ -29,9 +29,10 @@ import org.jboss.seam.mail.attachments.URLAttachment; import org.jboss.seam.mail.core.enumurations.ContentDisposition; import org.jboss.seam.mail.core.enumurations.MessagePriority; -import org.jboss.seam.mail.templating.FreeMarkerMailMessage; import org.jboss.seam.mail.templating.InputStreamTemplate; -import org.jboss.seam.mail.templating.VelocityMailMessage; +import org.jboss.seam.mail.templating.freemarker.FreeMarkerTemplate; +import org.jboss.seam.mail.templating.velocity.CDIVelocityContext; +import org.jboss.seam.mail.templating.velocity.VelocityTemplate; import org.jboss.seam.solder.resourceLoader.ResourceProvider; /** * @@ -47,11 +48,8 @@ public class SendMail private Instance mailMessage; @Inject - private Instance velocityMailMessage; - - @Inject - private Instance freeMarkerMailMessage; - + private Instance cDIVelocityContext; + @Inject private ResourceProvider resourceProvider; @@ -73,11 +71,11 @@ public void sendText() public void sendHTMLFreeMarker() throws MalformedURLException { - freeMarkerMailMessage.get() + mailMessage.get() .from("seam@test.test", "Seam Framework") .to(person) .subject("HTML Message from Seam Mail - " + java.util.UUID.randomUUID().toString()) - .bodyHtml(new InputStreamTemplate(resourceProvider.loadResourceStream("template.html.freemarker"))) + .bodyHtml(new FreeMarkerTemplate(new InputStreamTemplate(resourceProvider.loadResourceStream("template.html.freemarker")))) .put("person", person) .put("version", "Seam 3") .importance(MessagePriority.HIGH) @@ -87,13 +85,15 @@ public void sendHTMLFreeMarker() throws MalformedURLException public void sendHTMLwithAlternativeFreeMarker() throws MalformedURLException { - freeMarkerMailMessage.get() + mailMessage.get() .from("seam@test.test", "Seam Framework") .to(person.getEmail(), person.getName()) .subject("HTML+Text Message from Seam Mail - " + java.util.UUID.randomUUID().toString()) .put("person", person) .put("version", "Seam 3") - .bodyHtmlTextAlt(new InputStreamTemplate(resourceProvider.loadResourceStream("template.html.freemarker")), new InputStreamTemplate(resourceProvider.loadResourceStream("template.text.freemarker"))) + .bodyHtmlTextAlt( + new FreeMarkerTemplate(new InputStreamTemplate(resourceProvider.loadResourceStream("template.html.freemarker"))), + new FreeMarkerTemplate(new InputStreamTemplate(resourceProvider.loadResourceStream("template.text.freemarker")))) .importance(MessagePriority.LOW) .deliveryReceipt("seam@jboss.org") .readReceipt("seam@jboss.org") @@ -104,11 +104,11 @@ public void sendHTMLwithAlternativeFreeMarker() throws MalformedURLException public void sendHTMLVelocity() throws MalformedURLException { - velocityMailMessage.get() + mailMessage.get() .from("seam@test.test", "Seam Framework") .to(person) .subject("HTML Message from Seam Mail - " + java.util.UUID.randomUUID().toString()) - .bodyHtml(new InputStreamTemplate(resourceProvider.loadResourceStream("template.html.velocity"))) + .bodyHtml(new VelocityTemplate(new InputStreamTemplate(resourceProvider.loadResourceStream("template.html.velocity")), cDIVelocityContext.get())) .put("version", "Seam 3") .importance(MessagePriority.HIGH) .addAttachment(new URLAttachment("http://www.seamframework.org/themes/sfwkorg/img/seam_icon_large.png", "seamLogo.png", ContentDisposition.INLINE)) @@ -117,12 +117,14 @@ public void sendHTMLVelocity() throws MalformedURLException public void sendHTMLwithAlternativeVelocity() throws MalformedURLException { - velocityMailMessage.get() + mailMessage.get() .from("seam@test.test", "Seam Framework") .to(person.getEmail(), person.getName()) .subject("HTML+Text Message from Seam Mail - " + java.util.UUID.randomUUID().toString()) .put("version", "Seam 3") - .bodyHtmlTextAlt(new InputStreamTemplate(resourceProvider.loadResourceStream("template.html.velocity")), new InputStreamTemplate(resourceProvider.loadResourceStream("template.text.velocity"))) + .bodyHtmlTextAlt( + new VelocityTemplate(new InputStreamTemplate(resourceProvider.loadResourceStream("template.html.velocity")), cDIVelocityContext.get()), + new VelocityTemplate(new InputStreamTemplate(resourceProvider.loadResourceStream("template.text.velocity")), cDIVelocityContext.get())) .importance(MessagePriority.LOW) .deliveryReceipt("seam@jboss.org") .readReceipt("seam@jboss.org") diff --git a/examples/sendmail/src/test/java/org/jboss/seam/mail/FreeMarkerMailMessageTest.java b/examples/sendmail/src/test/java/org/jboss/seam/mail/FreeMarkerMailMessageTest.java index 2406676..a6a86df 100644 --- a/examples/sendmail/src/test/java/org/jboss/seam/mail/FreeMarkerMailMessageTest.java +++ b/examples/sendmail/src/test/java/org/jboss/seam/mail/FreeMarkerMailMessageTest.java @@ -33,20 +33,21 @@ import org.jboss.arquillian.api.Deployment; import org.jboss.arquillian.junit.Arquillian; +import org.jboss.seam.mail.api.MailMessage; import org.jboss.seam.mail.attachments.InputStreamAttachment; import org.jboss.seam.mail.attachments.URLAttachment; import org.jboss.seam.mail.core.EmailMessage; import org.jboss.seam.mail.core.MailConfig; -import org.jboss.seam.mail.core.MailTestUtil; import org.jboss.seam.mail.core.SendFailedException; import org.jboss.seam.mail.core.enumurations.ContentDisposition; import org.jboss.seam.mail.core.enumurations.MessagePriority; import org.jboss.seam.mail.example.Gmail; import org.jboss.seam.mail.example.Person; -import org.jboss.seam.mail.templating.FreeMarkerMailMessage; import org.jboss.seam.mail.templating.InputStreamTemplate; import org.jboss.seam.mail.templating.TextTemplate; +import org.jboss.seam.mail.templating.freemarker.FreeMarkerTemplate; import org.jboss.seam.mail.util.EmailAttachmentUtil; +import org.jboss.seam.mail.util.MailTestUtil; import org.jboss.seam.mail.util.MavenArtifactResolver; import org.jboss.seam.mail.util.SMTPAuthenticator; import org.jboss.seam.solder.resourceLoader.ResourceProvider; @@ -82,7 +83,7 @@ public static Archive createTestArchive() } @Inject - private Instance freeMarkerMailMessage; + private Instance mailMessage; @Inject private ResourceProvider resourceProvider; @@ -127,12 +128,12 @@ public void testFreeMarkerTextMailMessage() throws MessagingException, IOExcepti person.setName(toName); person.setEmail(toAddress); - freeMarkerMailMessage.get() + mailMessage.get() .from(fromAddress, fromName) .replyTo(replyToAddress) .to(toAddress, toName) - .subject(new TextTemplate(subject)) - .bodyText(new InputStreamTemplate(resourceProvider.loadResourceStream("template.text.freemarker"))) + .subject(new FreeMarkerTemplate(new TextTemplate(subject))) + .bodyText(new FreeMarkerTemplate(new InputStreamTemplate(resourceProvider.loadResourceStream("template.text.freemarker")))) .put("person", person) .put("version", version) .importance(MessagePriority.HIGH) @@ -184,12 +185,12 @@ public void testFreeMarkerHTMLMailMessage() throws MessagingException, IOExcepti person.setName(toName); person.setEmail(toAddress); - emailMessage = freeMarkerMailMessage.get() + emailMessage = mailMessage.get() .from(fromAddress, fromName) .replyTo(replyToAddress, replyToName) .to(person) .subject(subject) - .bodyHtml(new InputStreamTemplate(resourceProvider.loadResourceStream("template.html.freemarker"))) + .bodyHtml(new FreeMarkerTemplate(new InputStreamTemplate(resourceProvider.loadResourceStream("template.html.freemarker")))) .put("person", person) .put("version", version) .importance(MessagePriority.HIGH) @@ -250,13 +251,15 @@ public void testFreeMarkerHTMLTextAltMailMessage() throws MessagingException, IO person.setName(toName); person.setEmail(toAddress); - emailMessage = freeMarkerMailMessage.get() + emailMessage = mailMessage.get() .from(fromAddress, fromName) .to(person.getEmail(), person.getName()) .subject(subject) .put("person", person) .put("version", version) - .bodyHtmlTextAlt(new InputStreamTemplate(resourceProvider.loadResourceStream("template.html.freemarker")), new InputStreamTemplate(resourceProvider.loadResourceStream("template.text.freemarker"))) + .bodyHtmlTextAlt( + new FreeMarkerTemplate(new InputStreamTemplate(resourceProvider.loadResourceStream("template.html.freemarker"))), + new FreeMarkerTemplate(new InputStreamTemplate(resourceProvider.loadResourceStream("template.text.freemarker")))) .importance(MessagePriority.LOW) .deliveryReceipt(fromAddress) .readReceipt("seam.test") @@ -327,13 +330,15 @@ public void testSMTPSessionAuthentication() throws MessagingException, Malformed person.setName(toName); person.setEmail(toAddress); - freeMarkerMailMessage.get() + mailMessage.get() .from(fromAddress, fromName) .to(person.getEmail(), person.getName()) .subject(subject) .put("person", person) .put("version", "Seam 3") - .bodyHtmlTextAlt(new InputStreamTemplate(resourceProvider.loadResourceStream("template.html.freemarker")), new InputStreamTemplate(resourceProvider.loadResourceStream("template.text.freemarker"))) + .bodyHtmlTextAlt( + new FreeMarkerTemplate(new InputStreamTemplate(resourceProvider.loadResourceStream("template.html.freemarker"))), + new FreeMarkerTemplate(new InputStreamTemplate(resourceProvider.loadResourceStream("template.text.freemarker")))) .importance(MessagePriority.LOW) .deliveryReceipt(fromAddress) .readReceipt("seam.test") @@ -372,12 +377,12 @@ public void testFreeMarkerTextMailMessageSendFailed() person.setName(toName); person.setEmail(toAddress); - freeMarkerMailMessage.get() + mailMessage.get() .from(fromAddress, fromName) .replyTo(replyToAddress) .to(toAddress, toName) - .subject(new TextTemplate(subject)) - .bodyText(new InputStreamTemplate(resourceProvider.loadResourceStream("template.text.freemarker"))) + .subject(new FreeMarkerTemplate(new TextTemplate(subject))) + .bodyText(new FreeMarkerTemplate(new InputStreamTemplate(resourceProvider.loadResourceStream("template.text.freemarker")))) .put("person", person) .put("version", version) .importance(MessagePriority.HIGH) diff --git a/examples/sendmail/src/test/java/org/jboss/seam/mail/MailMessageTest.java b/examples/sendmail/src/test/java/org/jboss/seam/mail/MailMessageTest.java index 3b8893f..17f1e52 100644 --- a/examples/sendmail/src/test/java/org/jboss/seam/mail/MailMessageTest.java +++ b/examples/sendmail/src/test/java/org/jboss/seam/mail/MailMessageTest.java @@ -38,12 +38,12 @@ import org.jboss.seam.mail.core.EmailMessage; import org.jboss.seam.mail.core.InvalidAddressException; import org.jboss.seam.mail.core.MailConfig; -import org.jboss.seam.mail.core.MailTestUtil; -import org.jboss.seam.mail.core.MailUtility; import org.jboss.seam.mail.core.SendFailedException; import org.jboss.seam.mail.core.enumurations.ContentDisposition; import org.jboss.seam.mail.core.enumurations.MessagePriority; import org.jboss.seam.mail.example.Person; +import org.jboss.seam.mail.util.MailTestUtil; +import org.jboss.seam.mail.util.MailUtility; import org.jboss.seam.mail.util.MavenArtifactResolver; import org.jboss.seam.solder.resourceLoader.ResourceProvider; import org.jboss.shrinkwrap.api.Archive; diff --git a/examples/sendmail/src/test/java/org/jboss/seam/mail/VelocityMailMessageTest.java b/examples/sendmail/src/test/java/org/jboss/seam/mail/VelocityMailMessageTest.java index ada8692..ffc0991 100644 --- a/examples/sendmail/src/test/java/org/jboss/seam/mail/VelocityMailMessageTest.java +++ b/examples/sendmail/src/test/java/org/jboss/seam/mail/VelocityMailMessageTest.java @@ -33,11 +33,11 @@ import org.jboss.arquillian.api.Deployment; import org.jboss.arquillian.junit.Arquillian; +import org.jboss.seam.mail.api.MailMessage; import org.jboss.seam.mail.attachments.InputStreamAttachment; import org.jboss.seam.mail.attachments.URLAttachment; import org.jboss.seam.mail.core.EmailMessage; import org.jboss.seam.mail.core.MailConfig; -import org.jboss.seam.mail.core.MailTestUtil; import org.jboss.seam.mail.core.SendFailedException; import org.jboss.seam.mail.core.enumurations.ContentDisposition; import org.jboss.seam.mail.core.enumurations.MessagePriority; @@ -45,8 +45,10 @@ import org.jboss.seam.mail.example.Person; import org.jboss.seam.mail.templating.InputStreamTemplate; import org.jboss.seam.mail.templating.TextTemplate; -import org.jboss.seam.mail.templating.VelocityMailMessage; +import org.jboss.seam.mail.templating.velocity.CDIVelocityContext; +import org.jboss.seam.mail.templating.velocity.VelocityTemplate; import org.jboss.seam.mail.util.EmailAttachmentUtil; +import org.jboss.seam.mail.util.MailTestUtil; import org.jboss.seam.mail.util.MavenArtifactResolver; import org.jboss.seam.mail.util.SMTPAuthenticator; import org.jboss.seam.solder.resourceLoader.ResourceProvider; @@ -83,11 +85,14 @@ public static Archive createTestArchive() } @Inject - private Instance velocityMailMessage; + private Instance mailMessage; @Inject private ResourceProvider resourceProvider; + @Inject + private Instance cDIVelocityContext; + @Inject private MailConfig mailConfig; @@ -128,12 +133,12 @@ public void testVelocityTextMailMessage() throws MessagingException, IOException person.setName(toName); person.setEmail(toAddress); - velocityMailMessage.get() + mailMessage.get() .from(fromAddress, fromName) .replyTo(replyToAddress) .to(toAddress, toName) - .subject(new TextTemplate(subject)) - .bodyText(new InputStreamTemplate(resourceProvider.loadResourceStream("template.text.velocity"))) + .subject(new VelocityTemplate(new TextTemplate(subject), cDIVelocityContext.get())) + .bodyText(new VelocityTemplate(new InputStreamTemplate(resourceProvider.loadResourceStream("template.text.velocity")), cDIVelocityContext.get())) .put("version", version) .importance(MessagePriority.HIGH) .send(session.get()); @@ -184,12 +189,12 @@ public void testVelocityHTMLMailMessage() throws MessagingException, IOException person.setName(toName); person.setEmail(toAddress); - emailMessage = velocityMailMessage.get() + emailMessage = mailMessage.get() .from(fromAddress, fromName) .replyTo(replyToAddress, replyToName) .to(person) .subject(subject) - .bodyHtml(new InputStreamTemplate(resourceProvider.loadResourceStream("template.html.velocity"))) + .bodyHtml(new VelocityTemplate(new InputStreamTemplate(resourceProvider.loadResourceStream("template.html.velocity")), cDIVelocityContext.get())) .put("version", version) .importance(MessagePriority.HIGH) .addAttachment(new URLAttachment("http://www.seamframework.org/themes/sfwkorg/img/seam_icon_large.png", "seamLogo.png", ContentDisposition.INLINE)) @@ -249,12 +254,14 @@ public void testVelocityHTMLTextAltMailMessage() throws MessagingException, IOEx person.setName(toName); person.setEmail(toAddress); - emailMessage = velocityMailMessage.get() + emailMessage = mailMessage.get() .from(fromAddress, fromName) .to(person.getEmail(), person.getName()) .subject(subject) .put("version", version) - .bodyHtmlTextAlt(new InputStreamTemplate(resourceProvider.loadResourceStream("template.html.velocity")), new InputStreamTemplate(resourceProvider.loadResourceStream("template.text.velocity"))) + .bodyHtmlTextAlt( + new VelocityTemplate(new InputStreamTemplate(resourceProvider.loadResourceStream("template.html.velocity")), cDIVelocityContext.get()), + new VelocityTemplate(new InputStreamTemplate(resourceProvider.loadResourceStream("template.text.velocity")), cDIVelocityContext.get())) .importance(MessagePriority.LOW) .deliveryReceipt(fromAddress) .readReceipt("seam.test") @@ -325,12 +332,14 @@ public void testSMTPSessionAuthentication() throws MessagingException, Malformed person.setName(toName); person.setEmail(toAddress); - velocityMailMessage.get() + mailMessage.get() .from(fromAddress, fromName) .to(person.getEmail(), person.getName()) .subject(subject) .put("version", "Seam 3") - .bodyHtmlTextAlt(new InputStreamTemplate(resourceProvider.loadResourceStream("template.html.velocity")), new InputStreamTemplate(resourceProvider.loadResourceStream("template.text.velocity"))) + .bodyHtmlTextAlt( + new VelocityTemplate(new InputStreamTemplate(resourceProvider.loadResourceStream("template.html.velocity")), cDIVelocityContext.get()), + new VelocityTemplate(new InputStreamTemplate(resourceProvider.loadResourceStream("template.text.velocity")), cDIVelocityContext.get())) .importance(MessagePriority.LOW) .deliveryReceipt(fromAddress) .readReceipt("seam.test") @@ -369,12 +378,12 @@ public void testVelocityTextMailMessageSendFailed() person.setName(toName); person.setEmail(toAddress); - velocityMailMessage.get() + mailMessage.get() .from(fromAddress, fromName) .replyTo(replyToAddress) .to(toAddress, toName) - .subject(new TextTemplate(subject)) - .bodyText(new InputStreamTemplate(resourceProvider.loadResourceStream("template.text.velocity"))) + .subject(new VelocityTemplate(new TextTemplate(subject), cDIVelocityContext.get())) + .bodyText(new VelocityTemplate(new InputStreamTemplate(resourceProvider.loadResourceStream("template.text.velocity")), cDIVelocityContext.get())) .put("version", version) .importance(MessagePriority.HIGH) .send(session.get()); diff --git a/freemarker/api/pom.xml b/freemarker/api/pom.xml deleted file mode 100644 index ec0c88a..0000000 --- a/freemarker/api/pom.xml +++ /dev/null @@ -1,46 +0,0 @@ - - 4.0.0 - - - seam-mail-parent - org.jboss.seam.mail - 3.0.0-SNAPSHOT - ../../pom.xml - - - org.jboss.seam.mail - seam-mail-freemarker-api - 3.0.0-SNAPSHOT - jar - Seam Mail Module Freemarker API - - - - org.jboss.seam.mail - seam-mail-core-api - - - - - - code-coverage - - - - org.codehaus.mojo - emma-maven-plugin - - - org.apache.maven.plugins - maven-surefire-plugin - - - org.sonatype.maven.plugin - emma4it-maven-plugin - - - - - - - diff --git a/freemarker/api/src/main/java/org/jboss/seam/mail/templating/FreeMarkerMailMessage.java b/freemarker/api/src/main/java/org/jboss/seam/mail/templating/FreeMarkerMailMessage.java deleted file mode 100644 index feaa401..0000000 --- a/freemarker/api/src/main/java/org/jboss/seam/mail/templating/FreeMarkerMailMessage.java +++ /dev/null @@ -1,409 +0,0 @@ -/* - * JBoss, Home of Professional Open Source - * Copyright 2011, Red Hat, Inc., and individual contributors - * by the @authors tag. See the copyright.txt in the distribution for a - * full listing of individual contributors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * http://www.apache.org/licenses/LICENSE-2.0 - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jboss.seam.mail.templating; - -import java.util.Collection; - -import javax.mail.Session; -import javax.mail.internet.InternetAddress; - -import org.jboss.seam.mail.api.MailMessage; -import org.jboss.seam.mail.core.EmailAttachment; -import org.jboss.seam.mail.core.EmailContact; -import org.jboss.seam.mail.core.EmailMessage; -import org.jboss.seam.mail.core.InvalidAddressException; -import org.jboss.seam.mail.core.MailTemplate; -import org.jboss.seam.mail.core.SendFailedException; -import org.jboss.seam.mail.core.enumurations.MessagePriority; - -/** - * Interface for creating email messages using a templating engine. - * - * @author Cody Lerum - */ -public interface FreeMarkerMailMessage -{ - - // Begin Recipients - - /** - * Convenience method to add a FROM address - * - * @param address Email address of the recipient eq "john.doe@example.com" - * @throws InvalidAddressException if address is in invalid format - */ - public FreeMarkerMailMessage from(String address); - - /** - * Convenience method to add a FROM address - * - * @param name Personal name of the recipient eg "John Doe" - * @param address Email address of the recipient eg "john.doe@example.com" - * @throws InvalidAddressException if address is in invalid format - */ - public FreeMarkerMailMessage from(String address, String name); - - /** - * Adds a From Address - * - * @param emailAddress {@link InternetAddress} of the address to be added - */ - public FreeMarkerMailMessage from(InternetAddress emailAddress); - - /** - * Adds a From Address - * - * @param emailContact {@link EmailContact} of the address to be added - */ - public FreeMarkerMailMessage from(EmailContact emailContact); - - /** - * Adds a Collection of {@link EmailContact} as FROM addresses - * - * @param emailContacts Collection of {@link EmailContact} to be added - */ - public FreeMarkerMailMessage from(Collection emailContacts); - - /** - * Convenience method to add a REPLY-TO address - * - * @param address Email address of the recipient eq "john.doe@example.com - * @throws InvalidAddressException if address is in invalid format" - */ - public FreeMarkerMailMessage replyTo(String address); - - /** - * Convenience method to add a REPLY-TO name and address - * - * @param name Personal name of the recipient eg "John Doe" - * @param address Email address of the recipient eg "john.doe@example.com" - * @throws InvalidAddressException if address is in invalid format - */ - public FreeMarkerMailMessage replyTo(String address, String name); - - /** - * Adds a REPLY-TO Address - * - * @param emailAddress {@link InternetAddress} of the address to be added - */ - public FreeMarkerMailMessage replyTo(InternetAddress emailAddress); - - /** - * Adds a REPLY-TO Address - * - * @param emailContact {@link EmailContact} of the address to be added - */ - public FreeMarkerMailMessage replyTo(EmailContact emailContact); - - /** - * Adds a Collection of {@link EmailContact} as REPLY-TO addresses - * - * @param emailContacts Collection of {@link EmailContact} to be added - */ - public FreeMarkerMailMessage replyTo(Collection emailContacts); - - /** - * Convenience method to add a TO address - * - * @param address Email address of the recipient eq "john.doe@example.com" - * @throws InvalidAddressException if address is in invalid format - */ - public FreeMarkerMailMessage to(String address); - - /** - * Convenience method to add a TO recipient - * - * @param name Personal name of the recipient eg "John Doe" - * @param address Email address of the recipient eg "john.doe@example.com" - * @throws InvalidAddressException if address is in invalid format - */ - public FreeMarkerMailMessage to(String address, String name); - - /** - * Add TO recipient - * - * @param emailAddress {@link InternetAddress} of the address to be added - */ - public FreeMarkerMailMessage to(InternetAddress emailAddress); - - /** - * Add TO recipient - * - * @param emailContact {@link EmailContact} of the address to be added - */ - public FreeMarkerMailMessage to(EmailContact emailContact); - - /** - * Convenience method to add a TO recipients - * - * @param emailContacts Collection of {@link EmailContact} to be added - */ - public FreeMarkerMailMessage to(Collection emailContacts); - - /** - * Convenience method to add a CC (Carbon Copy) recipient - * - * @param address Email address of the recipient eg "john.doe@example.com" - * @throws InvalidAddressException if address is in invalid format - * - */ - public FreeMarkerMailMessage cc(String address); - - /** - * Convenience method to add a CC (Carbon Copy) recipient - * - * @param name Personal name of the recipient eg "John Doe" - * @param address Email address of the recipient eg "john.doe@example.com" - * @throws InvalidAddressException if address is in invalid format - */ - public FreeMarkerMailMessage cc(String address, String name); - - /** - * Add CC (Carbon Copy) recipient - * - * @param emailAddress {@link InternetAddress} of the address to be added - */ - public FreeMarkerMailMessage cc(InternetAddress emailAddress); - - /** - * Add CC recipient - * - * @param emailContact {@link EmailContact} of the address to be added - */ - public FreeMarkerMailMessage cc(EmailContact emailContact); - - /** - * Add collection of CC (Carbon Copy) recipients - * - * @param emailContacts Collection of {@link EmailContact} to be added - */ - public FreeMarkerMailMessage cc(Collection emailContacts); - - /** - * Convenience method to add a BCC (Blind Carbon Copy) recipient - * - * @param address Email address of the recipient eg "john.doe@example.com" - * @throws InvalidAddressException if address is in invalid format - */ - public FreeMarkerMailMessage bcc(String address); - - /** - * Convenience method to add a BCC (Blind Carbon Copy) recipient - * - * @param name Personal name of the recipient eg "John Doe" - * @param address Email address of the recipient eg "john.doe@example.com" - * @throws InvalidAddressException if address is in invalid format - */ - public FreeMarkerMailMessage bcc(String address, String name); - - /** - * Add BCC (Blind Carbon Copy) recipient - * - * @param emailAddress {@link InternetAddress} of the address to be added - */ - public FreeMarkerMailMessage bcc(InternetAddress emailAddress); - - /** - * Add BCC recipient - * - * @param emailContact {@link EmailContact} of the address to be added - */ - public FreeMarkerMailMessage bcc(EmailContact emailContact); - - /** - * Add collection of BCC (Blind Carbon Copy) recipients - * - * @param emailContacts Collection of {@link EmailContact} to be added - */ - public FreeMarkerMailMessage bcc(Collection emailContacts); - - // End Recipients - - // Begin Attachments - - /** - * Adds Attachment to the message - * - * @param attachment {@link EmailAttachment} to be added - */ - public FreeMarkerMailMessage addAttachment(EmailAttachment attachment); - - // End Attachements - - // Begin Flags - - /** - * Sets the importance level of the message with a given - * {@link MessagePriority} - * - * @param messagePriority The priority level of the message. - * - */ - public FreeMarkerMailMessage importance(MessagePriority messagePriority); - - /** - * Request a delivery receipt "Return-Receipt-To" to the given address - * - * @param address Email address the receipt should be sent to - * @throws InvalidAddressException if address is in invalid format - */ - public FreeMarkerMailMessage deliveryReceipt(String address); - - /** - * Request a read receipt "Disposition-Notification-To" to a given address - * - * @param address Email address the receipt should be sent to - * @throws InvalidAddressException if address is in invalid format - */ - public FreeMarkerMailMessage readReceipt(String address); - - /** - * Set the Message-ID for the message. - * - * @param messageId - */ - public FreeMarkerMailMessage messageId(String messageId); - - // End Flags - - // Begin Calendar - - /** - * Used for creating iCal Calendar Invites. - * - * @param htmlSummary Summary of the invite to be displayed in the body of - * the email messages. - * @param bytes iCal data which will be attached to the message - * - */ - public FreeMarkerMailMessage iCal(String htmlSummary, byte[] bytes); - - // End Calendar - - // Begin Core - - /** - * Set the subject on the message - * - * @param value Subject of the message - * - */ - public FreeMarkerMailMessage subject(String value); - - /** - * Sets the body of the message a plan text body represented by the supplied - * string - * - * @param text Plain text body - * - */ - public FreeMarkerMailMessage bodyText(String text); - - /** - * Sets the body of the message a HTML body represented by the supplied - * string - * - * @param html HTML body - * - */ - public FreeMarkerMailMessage bodyHtml(String html); - - /** - * Sets the body of the message to a HTML body with a plain text alternative - * - * @param html HTML body - * @param text Plain text body - * - */ - public FreeMarkerMailMessage bodyHtmlTextAlt(String html, String text); - - // End Core - - /** - * Get the {@link EmailMessage} representing this {@link FreeMarkerMailMessage} - * - * @return {@link EmailMessage} representing this {@link FreeMarkerMailMessage} - */ - public EmailMessage getEmailMessage(); - - /** - * Send the Message - * - * @return {@link EmailMessage} which represents the - * {@link FreeMarkerMailMessage} as sent - * @throws TemplatingException - If errors occur during template processing - * @throws SendFailedException If the messages fails to be sent. - */ - public EmailMessage send(Session session); - - /** - * Send the Message - * - * @return {@link EmailMessage} which represents the {@link MailMessage} as - * sent - * @throws SendFailedException If the messages fails to be sent. - */ - public EmailMessage send(); - - // Begin Template Specific - - /** - * Set the template to be used for the message subject - * - * @param subject {@link MailTemplate} to use - * @throws TemplatingException - */ - public FreeMarkerMailMessage subject(MailTemplate subject); - - /** - * Sets the text body of the message to the plain text output of the given - * template - * - * @param textBody {@link MailTemplate} to use - * @throws TemplatingException - */ - public FreeMarkerMailMessage bodyText(MailTemplate textbody); - - /** - * Sets the HTML body of the message to the HTML output of the given template - * - * @param htmlBody {@link MailTemplate} to use - * @throws TemplatingException - */ - public FreeMarkerMailMessage bodyHtml(MailTemplate htmlBody); - - /** - * Sets the body of the message to a HTML body with a plain text alternative - * output of the given templates - * - * @param htmlBody {@link MailTemplate} to use for HTML portion of - * message - * @param textBody {@link MailTemplate} to use for Text alternative - * portion of message - * @throws TemplatingException - */ - public FreeMarkerMailMessage bodyHtmlTextAlt(MailTemplate htmlBody, MailTemplate textbody); - - /** - * Places a variable in the templating engines context - * - * @param name Reference name of the object - * @param value the Object being placed in the context - */ - public FreeMarkerMailMessage put(String name, Object value); -} diff --git a/freemarker/api/src/main/resources/META-INF/beans.xml b/freemarker/api/src/main/resources/META-INF/beans.xml deleted file mode 100644 index d7977cf..0000000 --- a/freemarker/api/src/main/resources/META-INF/beans.xml +++ /dev/null @@ -1,27 +0,0 @@ - - - - diff --git a/freemarker/impl/pom.xml b/freemarker/impl/pom.xml index 0c4af30..7c871db 100644 --- a/freemarker/impl/pom.xml +++ b/freemarker/impl/pom.xml @@ -19,18 +19,13 @@ org.jboss.seam.mail seam-mail-core-impl - compile + compile - org.jboss.seam.mail - seam-mail-freemarker-api - - - - org.freemarker - freemarker - 2.3.16 + org.freemarker + freemarker + 2.3.16 @@ -39,11 +34,6 @@ provided - - org.jboss.seam.solder - seam-solder - - org.jboss.arquillian arquillian-junit diff --git a/freemarker/impl/src/main/java/org/jboss/seam/mail/templating/freemarker/FreeMarkerMailMessageImpl.java b/freemarker/impl/src/main/java/org/jboss/seam/mail/templating/freemarker/FreeMarkerMailMessageImpl.java deleted file mode 100644 index b6eba9d..0000000 --- a/freemarker/impl/src/main/java/org/jboss/seam/mail/templating/freemarker/FreeMarkerMailMessageImpl.java +++ /dev/null @@ -1,416 +0,0 @@ -/* - * JBoss, Home of Professional Open Source - * Copyright 2011, Red Hat, Inc., and individual contributors - * by the @authors tag. See the copyright.txt in the distribution for a - * full listing of individual contributors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * http://www.apache.org/licenses/LICENSE-2.0 - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jboss.seam.mail.templating.freemarker; - -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.StringWriter; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; - -import javax.enterprise.inject.Instance; -import javax.inject.Inject; -import javax.mail.Session; -import javax.mail.internet.InternetAddress; - -import org.jboss.seam.mail.attachments.BaseAttachment; -import org.jboss.seam.mail.core.EmailAttachment; -import org.jboss.seam.mail.core.EmailContact; -import org.jboss.seam.mail.core.EmailMessage; -import org.jboss.seam.mail.core.MailContext; -import org.jboss.seam.mail.core.MailTemplate; -import org.jboss.seam.mail.core.MailUtility; -import org.jboss.seam.mail.core.SendFailedException; -import org.jboss.seam.mail.core.enumurations.ContentDisposition; -import org.jboss.seam.mail.core.enumurations.EmailMessageType; -import org.jboss.seam.mail.core.enumurations.MessagePriority; -import org.jboss.seam.mail.templating.TemplatingException; -import org.jboss.seam.mail.templating.FreeMarkerMailMessage; -import org.jboss.seam.mail.util.EmailAttachmentUtil; - -import freemarker.template.Configuration; -import freemarker.template.DefaultObjectWrapper; -import freemarker.template.Template; -import freemarker.template.TemplateException; - -/** - * - * @author Cody Lerum - * - */ -public class FreeMarkerMailMessageImpl implements FreeMarkerMailMessage -{ - private EmailMessage emailMessage; - - private MailTemplate subjectTemplate; - private MailTemplate textTemplate; - private MailTemplate htmlTemplate; - - private Configuration configuration; - private Map rootMap = new HashMap(); - - private boolean templatesMerged = false; - - @Inject - private Instance session; - - - public FreeMarkerMailMessageImpl() - { - emailMessage = new EmailMessage(); - configuration = new Configuration(); - configuration.setObjectWrapper(new DefaultObjectWrapper()); - } - - // Begin Addressing - - public FreeMarkerMailMessage from(String address) - { - emailMessage.addFromAddress(MailUtility.internetAddress(address)); - return this; - } - - public FreeMarkerMailMessage from(String address, String name) - { - emailMessage.addFromAddress(MailUtility.internetAddress(address, name)); - return this; - } - - public FreeMarkerMailMessage from(InternetAddress emailAddress) - { - emailMessage.addFromAddress(emailAddress); - return this; - } - - public FreeMarkerMailMessage from(EmailContact emailContact) - { - emailMessage.addFromAddress(MailUtility.internetAddress(emailContact)); - return this; - } - - public FreeMarkerMailMessage from(Collection emailContacts) - { - emailMessage.addFromAddresses(MailUtility.internetAddress(emailContacts)); - return this; - } - - public FreeMarkerMailMessage replyTo(String address) - { - emailMessage.addReplyToAddress(MailUtility.internetAddress(address)); - return this; - } - - public FreeMarkerMailMessage replyTo(String address, String name) - { - emailMessage.addReplyToAddress(MailUtility.internetAddress(address, name)); - return this; - } - - public FreeMarkerMailMessage replyTo(InternetAddress emailAddress) - { - emailMessage.addReplyToAddress(emailAddress); - return this; - } - - public FreeMarkerMailMessage replyTo(EmailContact emailContact) - { - emailMessage.addReplyToAddress(MailUtility.internetAddress(emailContact)); - return this; - } - - public FreeMarkerMailMessage replyTo(Collection emailContacts) - { - emailMessage.addReplyToAddresses(MailUtility.internetAddress(emailContacts)); - return this; - } - - public FreeMarkerMailMessage to(String address) - { - emailMessage.addToAddress(MailUtility.internetAddress(address)); - return this; - } - - public FreeMarkerMailMessage to(String address, String name) - { - emailMessage.addToAddress(MailUtility.internetAddress(address, name)); - return this; - } - - public FreeMarkerMailMessage to(InternetAddress emailAddress) - { - emailMessage.addToAddress(emailAddress); - return this; - } - - public FreeMarkerMailMessage to(EmailContact emailContact) - { - emailMessage.addToAddress(MailUtility.internetAddress(emailContact)); - return this; - } - - public FreeMarkerMailMessage to(Collection emailContacts) - { - emailMessage.addToAddresses(MailUtility.internetAddress(emailContacts)); - return this; - } - - public FreeMarkerMailMessage cc(String address) - { - emailMessage.addCcAddress(MailUtility.internetAddress(address)); - return this; - } - - public FreeMarkerMailMessage cc(String address, String name) - { - emailMessage.addCcAddress(MailUtility.internetAddress(address, name)); - return this; - } - - public FreeMarkerMailMessage cc(InternetAddress emailAddress) - { - emailMessage.addCcAddress(emailAddress); - return this; - } - - public FreeMarkerMailMessage cc(EmailContact emailContact) - { - emailMessage.addCcAddress(MailUtility.internetAddress(emailContact)); - return this; - } - - public FreeMarkerMailMessage cc(Collection emailContacts) - { - emailMessage.addCcAddresses(MailUtility.internetAddress(emailContacts)); - return this; - } - - public FreeMarkerMailMessage bcc(String address) - { - emailMessage.addBccAddress(MailUtility.internetAddress(address)); - return this; - } - - public FreeMarkerMailMessage bcc(String address, String name) - { - emailMessage.addBccAddress(MailUtility.internetAddress(address, name)); - return this; - } - - public FreeMarkerMailMessage bcc(InternetAddress emailAddress) - { - emailMessage.addBccAddress(emailAddress); - return this; - } - - public FreeMarkerMailMessage bcc(EmailContact emailContact) - { - emailMessage.addBccAddress(MailUtility.internetAddress(emailContact)); - return this; - } - - public FreeMarkerMailMessage bcc(Collection emailContacts) - { - emailMessage.addBccAddresses(MailUtility.internetAddress(emailContacts)); - return this; - } - - // End Addressing - - public FreeMarkerMailMessage subject(String value) - { - emailMessage.setSubject(value); - return this; - } - - public FreeMarkerMailMessage deliveryReceipt(String address) - { - emailMessage.addDeliveryReceiptAddress(MailUtility.internetAddress(address)); - return this; - } - - public FreeMarkerMailMessage readReceipt(String address) - { - emailMessage.addReadReceiptAddress(MailUtility.internetAddress(address)); - return this; - } - - public FreeMarkerMailMessage importance(MessagePriority messagePriority) - { - emailMessage.setImportance(messagePriority); - return this; - } - - public FreeMarkerMailMessage messageId(String messageId) - { - emailMessage.setMessageId(messageId); - return this; - } - - public FreeMarkerMailMessage bodyText(String text) - { - emailMessage.setTextBody(text); - return this; - } - - public FreeMarkerMailMessage bodyHtml(String html) - { - emailMessage.setHtmlBody(html); - return this; - } - - public FreeMarkerMailMessage bodyHtmlTextAlt(String html, String text) - { - emailMessage.setTextBody(text); - emailMessage.setHtmlBody(html); - return this; - } - - // Begin Attachments - - public FreeMarkerMailMessage addAttachment(EmailAttachment attachment) - { - emailMessage.addAttachment(attachment); - return this; - } - - public FreeMarkerMailMessage addAttachment(Collection attachments) - { - emailMessage.addAttachments(attachments); - return this; - } - - // End Attachments - - // Begin Calendar - - public FreeMarkerMailMessage iCal(String html, byte[] bytes) - { - emailMessage.setType(EmailMessageType.INVITE_ICAL); - emailMessage.setHtmlBody(html); - emailMessage.addAttachment(new BaseAttachment(null, "text/calendar;method=CANCEL", ContentDisposition.INLINE, bytes, "urn:content-classes:calendarmessage")); - return this; - } - - // End Calendar - - public FreeMarkerMailMessage subject(MailTemplate subject) - { - subjectTemplate = subject; - return this; - } - - public FreeMarkerMailMessageImpl bodyText(MailTemplate textBody) - { - textTemplate = textBody; - return this; - } - - public FreeMarkerMailMessageImpl bodyHtml(MailTemplate htmlBody) - { - htmlTemplate = htmlBody; - return this; - } - - public FreeMarkerMailMessageImpl bodyHtmlTextAlt(MailTemplate htmlBody, MailTemplate textBody) - { - bodyHtml(htmlBody); - bodyText(textBody); - return this; - } - - private String mergeTemplate(MailTemplate mailTemplate) - { - StringWriter writer = new StringWriter(); - - try - { - Template template = new Template(mailTemplate.getTemplateName() , new InputStreamReader(mailTemplate.getInputStream()), configuration); - template.process(rootMap, writer); - } - catch (IOException e) - { - throw new TemplatingException("Error creating template", e); - } - catch (TemplateException e) - { - throw new TemplatingException("Error rendering output", e); - } - - return writer.toString(); - } - - public FreeMarkerMailMessageImpl put(String key, Object value) - { - rootMap.put(key, value); - return this; - } - - public EmailMessage getEmailMessage() - { - return emailMessage; - } - - private FreeMarkerMailMessageImpl mergeTemplates() - { - if (!templatesMerged) - { - put("mailContext", new MailContext(EmailAttachmentUtil.getEmailAttachmentMap(emailMessage.getAttachments()))); - - if (subjectTemplate != null) - { - emailMessage.setSubject(mergeTemplate(subjectTemplate)); - } - - if (textTemplate != null) - { - emailMessage.setTextBody(mergeTemplate(textTemplate)); - } - - if (htmlTemplate != null) - { - emailMessage.setHtmlBody(mergeTemplate(htmlTemplate)); - } - - templatesMerged = true; - - return this; - } - else - { - throw new TemplatingException("Email Templates Already Merged"); - } - } - - public EmailMessage send(Session session) throws SendFailedException, TemplatingException - { - if (!templatesMerged) - { - mergeTemplates(); - } - - MailUtility.send(emailMessage, session); - - return emailMessage; - } - - public EmailMessage send() throws SendFailedException, TemplatingException - { - return this.send(session.get()); - } -} diff --git a/freemarker/impl/src/main/java/org/jboss/seam/mail/templating/freemarker/FreeMarkerTemplate.java b/freemarker/impl/src/main/java/org/jboss/seam/mail/templating/freemarker/FreeMarkerTemplate.java new file mode 100644 index 0000000..49072d1 --- /dev/null +++ b/freemarker/impl/src/main/java/org/jboss/seam/mail/templating/freemarker/FreeMarkerTemplate.java @@ -0,0 +1,75 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2011, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jboss.seam.mail.templating.freemarker; + +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.StringWriter; +import java.util.HashMap; +import java.util.Map; + +import org.jboss.seam.mail.templating.MailTemplate; +import org.jboss.seam.mail.templating.TemplateImpl; +import org.jboss.seam.mail.templating.TemplatingException; + +import freemarker.template.Configuration; +import freemarker.template.DefaultObjectWrapper; +import freemarker.template.Template; +import freemarker.template.TemplateException; + +/** + * + * @author Cody Lerum + * + */ +public class FreeMarkerTemplate implements TemplateImpl +{ + private Configuration configuration; + private Map rootMap = new HashMap(); + private MailTemplate mailTemplate; + + public FreeMarkerTemplate(MailTemplate mailTemplate) + { + this.mailTemplate = mailTemplate; + configuration = new Configuration(); + configuration.setObjectWrapper(new DefaultObjectWrapper()); + } + + public String merge(Map context) + { + rootMap.putAll(context); + + StringWriter writer = new StringWriter(); + + try + { + Template template = new Template(mailTemplate.getTemplateName() , new InputStreamReader(mailTemplate.getInputStream()), configuration); + template.process(rootMap, writer); + } + catch (IOException e) + { + throw new TemplatingException("Error creating template", e); + } + catch (TemplateException e) + { + throw new TemplatingException("Error rendering output", e); + } + + return writer.toString(); + } +} diff --git a/pom.xml b/pom.xml index 7bda409..a72a903 100644 --- a/pom.xml +++ b/pom.xml @@ -20,9 +20,7 @@ core/api core/impl - freemarker/api freemarker/impl - velocity/api velocity/impl examples/sendmail @@ -45,21 +43,7 @@ ${project.version} compile - - - org.jboss.seam.mail - seam-mail-freemarker-api - ${project.version} - compile - - - org.jboss.seam.mail - seam-mail-velocity-api - ${project.version} - compile - - org.jboss.seam.mail seam-mail-core-impl diff --git a/velocity/api/pom.xml b/velocity/api/pom.xml deleted file mode 100644 index 9361f56..0000000 --- a/velocity/api/pom.xml +++ /dev/null @@ -1,46 +0,0 @@ - - 4.0.0 - - - seam-mail-parent - org.jboss.seam.mail - 3.0.0-SNAPSHOT - ../../pom.xml - - - org.jboss.seam.mail - seam-mail-velocity-api - 3.0.0-SNAPSHOT - jar - Seam Mail Module Velocity API - - - - org.jboss.seam.mail - seam-mail-core-api - - - - - - code-coverage - - - - org.codehaus.mojo - emma-maven-plugin - - - org.apache.maven.plugins - maven-surefire-plugin - - - org.sonatype.maven.plugin - emma4it-maven-plugin - - - - - - - diff --git a/velocity/api/src/main/java/org/jboss/seam/mail/templating/VelocityMailMessage.java b/velocity/api/src/main/java/org/jboss/seam/mail/templating/VelocityMailMessage.java deleted file mode 100644 index ba63865..0000000 --- a/velocity/api/src/main/java/org/jboss/seam/mail/templating/VelocityMailMessage.java +++ /dev/null @@ -1,409 +0,0 @@ -/* - * JBoss, Home of Professional Open Source - * Copyright 2011, Red Hat, Inc., and individual contributors - * by the @authors tag. See the copyright.txt in the distribution for a - * full listing of individual contributors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * http://www.apache.org/licenses/LICENSE-2.0 - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jboss.seam.mail.templating; - -import java.util.Collection; - -import javax.mail.Session; -import javax.mail.internet.InternetAddress; - -import org.jboss.seam.mail.api.MailMessage; -import org.jboss.seam.mail.core.EmailAttachment; -import org.jboss.seam.mail.core.EmailContact; -import org.jboss.seam.mail.core.EmailMessage; -import org.jboss.seam.mail.core.InvalidAddressException; -import org.jboss.seam.mail.core.MailTemplate; -import org.jboss.seam.mail.core.SendFailedException; -import org.jboss.seam.mail.core.enumurations.MessagePriority; - -/** - * Interface for creating email messages using a templating engine. - * - * @author Cody Lerum - */ -public interface VelocityMailMessage -{ - - // Begin Recipients - - /** - * Convenience method to add a FROM address - * - * @param address Email address of the recipient eq "john.doe@example.com" - * @throws InvalidAddressException if address is in invalid format - */ - public VelocityMailMessage from(String address); - - /** - * Convenience method to add a FROM address - * - * @param name Personal name of the recipient eg "John Doe" - * @param address Email address of the recipient eg "john.doe@example.com" - * @throws InvalidAddressException if address is in invalid format - */ - public VelocityMailMessage from(String address, String name); - - /** - * Adds a From Address - * - * @param emailAddress {@link InternetAddress} of the address to be added - */ - public VelocityMailMessage from(InternetAddress emailAddress); - - /** - * Adds a From Address - * - * @param emailContact {@link EmailContact} of the address to be added - */ - public VelocityMailMessage from(EmailContact emailContact); - - /** - * Adds a Collection of {@link EmailContact} as FROM addresses - * - * @param emailContacts Collection of {@link EmailContact} to be added - */ - public VelocityMailMessage from(Collection emailContacts); - - /** - * Convenience method to add a REPLY-TO address - * - * @param address Email address of the recipient eq "john.doe@example.com - * @throws InvalidAddressException if address is in invalid format" - */ - public VelocityMailMessage replyTo(String address); - - /** - * Convenience method to add a REPLY-TO name and address - * - * @param name Personal name of the recipient eg "John Doe" - * @param address Email address of the recipient eg "john.doe@example.com" - * @throws InvalidAddressException if address is in invalid format - */ - public VelocityMailMessage replyTo(String address, String name); - - /** - * Adds a REPLY-TO Address - * - * @param emailAddress {@link InternetAddress} of the address to be added - */ - public VelocityMailMessage replyTo(InternetAddress emailAddress); - - /** - * Adds a REPLY-TO Address - * - * @param emailContact {@link EmailContact} of the address to be added - */ - public VelocityMailMessage replyTo(EmailContact emailContact); - - /** - * Adds a Collection of {@link EmailContact} as REPLY-TO addresses - * - * @param emailContacts Collection of {@link EmailContact} to be added - */ - public VelocityMailMessage replyTo(Collection emailContacts); - - /** - * Convenience method to add a TO address - * - * @param address Email address of the recipient eq "john.doe@example.com" - * @throws InvalidAddressException if address is in invalid format - */ - public VelocityMailMessage to(String address); - - /** - * Convenience method to add a TO recipient - * - * @param name Personal name of the recipient eg "John Doe" - * @param address Email address of the recipient eg "john.doe@example.com" - * @throws InvalidAddressException if address is in invalid format - */ - public VelocityMailMessage to(String address, String name); - - /** - * Add TO recipient - * - * @param emailAddress {@link InternetAddress} of the address to be added - */ - public VelocityMailMessage to(InternetAddress emailAddress); - - /** - * Add TO recipient - * - * @param emailContact {@link EmailContact} of the address to be added - */ - public VelocityMailMessage to(EmailContact emailContact); - - /** - * Convenience method to add a TO recipients - * - * @param emailContacts Collection of {@link EmailContact} to be added - */ - public VelocityMailMessage to(Collection emailContacts); - - /** - * Convenience method to add a CC (Carbon Copy) recipient - * - * @param address Email address of the recipient eg "john.doe@example.com" - * @throws InvalidAddressException if address is in invalid format - * - */ - public VelocityMailMessage cc(String address); - - /** - * Convenience method to add a CC (Carbon Copy) recipient - * - * @param name Personal name of the recipient eg "John Doe" - * @param address Email address of the recipient eg "john.doe@example.com" - * @throws InvalidAddressException if address is in invalid format - */ - public VelocityMailMessage cc(String address, String name); - - /** - * Add CC (Carbon Copy) recipient - * - * @param emailAddress {@link InternetAddress} of the address to be added - */ - public VelocityMailMessage cc(InternetAddress emailAddress); - - /** - * Add CC recipient - * - * @param emailContact {@link EmailContact} of the address to be added - */ - public VelocityMailMessage cc(EmailContact emailContact); - - /** - * Add collection of CC (Carbon Copy) recipients - * - * @param emailContacts Collection of {@link EmailContact} to be added - */ - public VelocityMailMessage cc(Collection emailContacts); - - /** - * Convenience method to add a BCC (Blind Carbon Copy) recipient - * - * @param address Email address of the recipient eg "john.doe@example.com" - * @throws InvalidAddressException if address is in invalid format - */ - public VelocityMailMessage bcc(String address); - - /** - * Convenience method to add a BCC (Blind Carbon Copy) recipient - * - * @param name Personal name of the recipient eg "John Doe" - * @param address Email address of the recipient eg "john.doe@example.com" - * @throws InvalidAddressException if address is in invalid format - */ - public VelocityMailMessage bcc(String address, String name); - - /** - * Add BCC (Blind Carbon Copy) recipient - * - * @param emailAddress {@link InternetAddress} of the address to be added - */ - public VelocityMailMessage bcc(InternetAddress emailAddress); - - /** - * Add BCC recipient - * - * @param emailContact {@link EmailContact} of the address to be added - */ - public VelocityMailMessage bcc(EmailContact emailContact); - - /** - * Add collection of BCC (Blind Carbon Copy) recipients - * - * @param emailContacts Collection of {@link EmailContact} to be added - */ - public VelocityMailMessage bcc(Collection emailContacts); - - // End Recipients - - // Begin Attachments - - /** - * Adds Attachment to the message - * - * @param attachment {@link EmailAttachment} to be added - */ - public VelocityMailMessage addAttachment(EmailAttachment attachment); - - // End Attachements - - // Begin Flags - - /** - * Sets the importance level of the message with a given - * {@link MessagePriority} - * - * @param messagePriority The priority level of the message. - * - */ - public VelocityMailMessage importance(MessagePriority messagePriority); - - /** - * Request a delivery receipt "Return-Receipt-To" to the given address - * - * @param address Email address the receipt should be sent to - * @throws InvalidAddressException if address is in invalid format - */ - public VelocityMailMessage deliveryReceipt(String address); - - /** - * Request a read receipt "Disposition-Notification-To" to a given address - * - * @param address Email address the receipt should be sent to - * @throws InvalidAddressException if address is in invalid format - */ - public VelocityMailMessage readReceipt(String address); - - /** - * Set the Message-ID for the message. - * - * @param messageId - */ - public VelocityMailMessage messageId(String messageId); - - // End Flags - - // Begin Calendar - - /** - * Used for creating iCal Calendar Invites. - * - * @param htmlSummary Summary of the invite to be displayed in the body of - * the email messages. - * @param bytes iCal data which will be attached to the message - * - */ - public VelocityMailMessage iCal(String htmlSummary, byte[] bytes); - - // End Calendar - - // Begin Core - - /** - * Set the subject on the message - * - * @param value Subject of the message - * - */ - public VelocityMailMessage subject(String value); - - /** - * Sets the body of the message a plan text body represented by the supplied - * string - * - * @param text Plain text body - * - */ - public VelocityMailMessage bodyText(String text); - - /** - * Sets the body of the message a HTML body represented by the supplied - * string - * - * @param html HTML body - * - */ - public VelocityMailMessage bodyHtml(String html); - - /** - * Sets the body of the message to a HTML body with a plain text alternative - * - * @param html HTML body - * @param text Plain text body - * - */ - public VelocityMailMessage bodyHtmlTextAlt(String html, String text); - - // End Core - - /** - * Get the {@link EmailMessage} representing this {@link VelocityMailMessage} - * - * @return {@link EmailMessage} representing this {@link VelocityMailMessage} - */ - public EmailMessage getEmailMessage(); - - /** - * Send the Message - * - * @return {@link EmailMessage} which represents the - * {@link VelocityMailMessage} as sent - * @throws TemplatingException - If errors occur during template processing - * @throws SendFailedException If the messages fails to be sent. - */ - public EmailMessage send(Session session); - - /** - * Send the Message - * - * @return {@link EmailMessage} which represents the {@link MailMessage} as - * sent - * @throws SendFailedException If the messages fails to be sent. - */ - public EmailMessage send(); - - // Begin Velocity Specific - - /** - * Set the template to be used for the message subject - * - * @param subject {@link MailTemplate} to use - * @throws TemplatingException - */ - public VelocityMailMessage subject(MailTemplate subject); - - /** - * Sets the text body of the message to the plain text output of the given - * template - * - * @param textBody {@link MailTemplate} to use - * @throws TemplatingException - */ - public VelocityMailMessage bodyText(MailTemplate textbody); - - /** - * Sets the HTML body of the message to the HTML output of the given template - * - * @param htmlBody {@link MailTemplate} to use - * @throws TemplatingException - */ - public VelocityMailMessage bodyHtml(MailTemplate htmlBody); - - /** - * Sets the body of the message to a HTML body with a plain text alternative - * output of the given templates - * - * @param htmlBody {@link MailTemplate} to use for HTML portion of - * message - * @param textBody {@link MailTemplate} to use for Text alternative - * portion of message - * @throws TemplatingException - */ - public VelocityMailMessage bodyHtmlTextAlt(MailTemplate htmlBody, MailTemplate textbody); - - /** - * Places a variable in the templating engines context - * - * @param name Reference name of the object - * @param value the Object being placed in the context - */ - public VelocityMailMessage put(String name, Object value); -} diff --git a/velocity/api/src/main/resources/META-INF/beans.xml b/velocity/api/src/main/resources/META-INF/beans.xml deleted file mode 100644 index d7977cf..0000000 --- a/velocity/api/src/main/resources/META-INF/beans.xml +++ /dev/null @@ -1,27 +0,0 @@ - - - - diff --git a/velocity/impl/pom.xml b/velocity/impl/pom.xml index 1be56d8..fe7eea4 100644 --- a/velocity/impl/pom.xml +++ b/velocity/impl/pom.xml @@ -22,11 +22,6 @@ compile - - org.jboss.seam.mail - seam-mail-velocity-api - - org.apache.velocity velocity diff --git a/velocity/impl/src/main/java/org/jboss/seam/mail/templating/velocity/SeamCDIVelocityContext.java b/velocity/impl/src/main/java/org/jboss/seam/mail/templating/velocity/CDIVelocityContext.java similarity index 91% rename from velocity/impl/src/main/java/org/jboss/seam/mail/templating/velocity/SeamCDIVelocityContext.java rename to velocity/impl/src/main/java/org/jboss/seam/mail/templating/velocity/CDIVelocityContext.java index dd26a22..ff51198 100644 --- a/velocity/impl/src/main/java/org/jboss/seam/mail/templating/velocity/SeamCDIVelocityContext.java +++ b/velocity/impl/src/main/java/org/jboss/seam/mail/templating/velocity/CDIVelocityContext.java @@ -31,7 +31,7 @@ * @author Cody Lerum * */ -public class SeamCDIVelocityContext extends AbstractContext +public class CDIVelocityContext extends AbstractContext { @Inject private Logger log; @@ -39,8 +39,7 @@ public class SeamCDIVelocityContext extends AbstractContext @Inject private BeanManager beanManager; - @Inject - public SeamCDIVelocityContext() + public CDIVelocityContext() { super(); } diff --git a/velocity/impl/src/main/java/org/jboss/seam/mail/templating/velocity/SeamBaseVelocityContext.java b/velocity/impl/src/main/java/org/jboss/seam/mail/templating/velocity/SeamBaseVelocityContext.java deleted file mode 100644 index 672d92a..0000000 --- a/velocity/impl/src/main/java/org/jboss/seam/mail/templating/velocity/SeamBaseVelocityContext.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * JBoss, Home of Professional Open Source - * Copyright 2011, Red Hat, Inc., and individual contributors - * by the @authors tag. See the copyright.txt in the distribution for a - * full listing of individual contributors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * http://www.apache.org/licenses/LICENSE-2.0 - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jboss.seam.mail.templating.velocity; - -import org.apache.velocity.VelocityContext; - -/** - * - * @author Cody Lerum - * - */ -public class SeamBaseVelocityContext extends VelocityContext -{ - private VelocityMailMessageImpl mailMessage; - - public SeamBaseVelocityContext(VelocityMailMessageImpl velocityMailMessageImpl, SeamCDIVelocityContext seamCDIVelocityContext) - { - super(seamCDIVelocityContext); - this.mailMessage = velocityMailMessageImpl; - } - - public void to(String name, String address) - { - mailMessage.to(name, address); - } - - public void from(String name, String address) - { - mailMessage.from(name, address); - } - - public void subject(String subject) - { - mailMessage.subject(subject); - } -} diff --git a/velocity/impl/src/main/java/org/jboss/seam/mail/templating/velocity/VelocityMailMessageImpl.java b/velocity/impl/src/main/java/org/jboss/seam/mail/templating/velocity/VelocityMailMessageImpl.java deleted file mode 100644 index 43e5923..0000000 --- a/velocity/impl/src/main/java/org/jboss/seam/mail/templating/velocity/VelocityMailMessageImpl.java +++ /dev/null @@ -1,420 +0,0 @@ -/* - * JBoss, Home of Professional Open Source - * Copyright 2011, Red Hat, Inc., and individual contributors - * by the @authors tag. See the copyright.txt in the distribution for a - * full listing of individual contributors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * http://www.apache.org/licenses/LICENSE-2.0 - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jboss.seam.mail.templating.velocity; - -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.StringWriter; -import java.util.Collection; - -import javax.enterprise.inject.Instance; -import javax.inject.Inject; -import javax.mail.Session; -import javax.mail.internet.InternetAddress; - -import org.apache.velocity.app.VelocityEngine; -import org.apache.velocity.exception.MethodInvocationException; -import org.apache.velocity.exception.ParseErrorException; -import org.apache.velocity.exception.ResourceNotFoundException; -import org.jboss.seam.mail.attachments.BaseAttachment; -import org.jboss.seam.mail.core.EmailAttachment; -import org.jboss.seam.mail.core.EmailContact; -import org.jboss.seam.mail.core.EmailMessage; -import org.jboss.seam.mail.core.MailContext; -import org.jboss.seam.mail.core.MailTemplate; -import org.jboss.seam.mail.core.MailUtility; -import org.jboss.seam.mail.core.SendFailedException; -import org.jboss.seam.mail.core.enumurations.ContentDisposition; -import org.jboss.seam.mail.core.enumurations.EmailMessageType; -import org.jboss.seam.mail.core.enumurations.MessagePriority; -import org.jboss.seam.mail.templating.TemplatingException; -import org.jboss.seam.mail.templating.VelocityMailMessage; -import org.jboss.seam.mail.util.EmailAttachmentUtil; - -/** - * - * @author Cody Lerum - * - */ -public class VelocityMailMessageImpl implements VelocityMailMessage -{ - private EmailMessage emailMessage; - private VelocityEngine velocityEngine; - private SeamBaseVelocityContext context; - - private MailTemplate subjectTemplate; - private MailTemplate textTemplate; - private MailTemplate htmlTemplate; - - private boolean templatesMerged = false; - - @Inject - private Instance session; - - @Inject - public VelocityMailMessageImpl(SeamCDIVelocityContext seamCDIVelocityContext) - { - emailMessage = new EmailMessage(); - velocityEngine = new VelocityEngine(); - velocityEngine.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.SimpleLog4JLogSystem"); - context = new SeamBaseVelocityContext(this, seamCDIVelocityContext); - } - - // Begin Addressing - - public VelocityMailMessage from(String address) - { - emailMessage.addFromAddress(MailUtility.internetAddress(address)); - return this; - } - - public VelocityMailMessage from(String address, String name) - { - emailMessage.addFromAddress(MailUtility.internetAddress(address, name)); - return this; - } - - public VelocityMailMessage from(InternetAddress emailAddress) - { - emailMessage.addFromAddress(emailAddress); - return this; - } - - public VelocityMailMessage from(EmailContact emailContact) - { - emailMessage.addFromAddress(MailUtility.internetAddress(emailContact)); - return this; - } - - public VelocityMailMessage from(Collection emailContacts) - { - emailMessage.addFromAddresses(MailUtility.internetAddress(emailContacts)); - return this; - } - - public VelocityMailMessage replyTo(String address) - { - emailMessage.addReplyToAddress(MailUtility.internetAddress(address)); - return this; - } - - public VelocityMailMessage replyTo(String address, String name) - { - emailMessage.addReplyToAddress(MailUtility.internetAddress(address, name)); - return this; - } - - public VelocityMailMessage replyTo(InternetAddress emailAddress) - { - emailMessage.addReplyToAddress(emailAddress); - return this; - } - - public VelocityMailMessage replyTo(EmailContact emailContact) - { - emailMessage.addReplyToAddress(MailUtility.internetAddress(emailContact)); - return this; - } - - public VelocityMailMessage replyTo(Collection emailContacts) - { - emailMessage.addReplyToAddresses(MailUtility.internetAddress(emailContacts)); - return this; - } - - public VelocityMailMessage to(String address) - { - emailMessage.addToAddress(MailUtility.internetAddress(address)); - return this; - } - - public VelocityMailMessage to(String address, String name) - { - emailMessage.addToAddress(MailUtility.internetAddress(address, name)); - return this; - } - - public VelocityMailMessage to(InternetAddress emailAddress) - { - emailMessage.addToAddress(emailAddress); - return this; - } - - public VelocityMailMessage to(EmailContact emailContact) - { - emailMessage.addToAddress(MailUtility.internetAddress(emailContact)); - return this; - } - - public VelocityMailMessage to(Collection emailContacts) - { - emailMessage.addToAddresses(MailUtility.internetAddress(emailContacts)); - return this; - } - - public VelocityMailMessage cc(String address) - { - emailMessage.addCcAddress(MailUtility.internetAddress(address)); - return this; - } - - public VelocityMailMessage cc(String address, String name) - { - emailMessage.addCcAddress(MailUtility.internetAddress(address, name)); - return this; - } - - public VelocityMailMessage cc(InternetAddress emailAddress) - { - emailMessage.addCcAddress(emailAddress); - return this; - } - - public VelocityMailMessage cc(EmailContact emailContact) - { - emailMessage.addCcAddress(MailUtility.internetAddress(emailContact)); - return this; - } - - public VelocityMailMessage cc(Collection emailContacts) - { - emailMessage.addCcAddresses(MailUtility.internetAddress(emailContacts)); - return this; - } - - public VelocityMailMessage bcc(String address) - { - emailMessage.addBccAddress(MailUtility.internetAddress(address)); - return this; - } - - public VelocityMailMessage bcc(String address, String name) - { - emailMessage.addBccAddress(MailUtility.internetAddress(address, name)); - return this; - } - - public VelocityMailMessage bcc(InternetAddress emailAddress) - { - emailMessage.addBccAddress(emailAddress); - return this; - } - - public VelocityMailMessage bcc(EmailContact emailContact) - { - emailMessage.addBccAddress(MailUtility.internetAddress(emailContact)); - return this; - } - - public VelocityMailMessage bcc(Collection emailContacts) - { - emailMessage.addBccAddresses(MailUtility.internetAddress(emailContacts)); - return this; - } - - // End Addressing - - public VelocityMailMessage subject(String value) - { - emailMessage.setSubject(value); - return this; - } - - public VelocityMailMessage deliveryReceipt(String address) - { - emailMessage.addDeliveryReceiptAddress(MailUtility.internetAddress(address)); - return this; - } - - public VelocityMailMessage readReceipt(String address) - { - emailMessage.addReadReceiptAddress(MailUtility.internetAddress(address)); - return this; - } - - public VelocityMailMessage importance(MessagePriority messagePriority) - { - emailMessage.setImportance(messagePriority); - return this; - } - - public VelocityMailMessage messageId(String messageId) - { - emailMessage.setMessageId(messageId); - return this; - } - - public VelocityMailMessage bodyText(String text) - { - emailMessage.setTextBody(text); - return this; - } - - public VelocityMailMessage bodyHtml(String html) - { - emailMessage.setHtmlBody(html); - return this; - } - - public VelocityMailMessage bodyHtmlTextAlt(String html, String text) - { - emailMessage.setTextBody(text); - emailMessage.setHtmlBody(html); - return this; - } - - // Begin Attachments - - public VelocityMailMessage addAttachment(EmailAttachment attachment) - { - emailMessage.addAttachment(attachment); - return this; - } - - public VelocityMailMessage addAttachment(Collection attachments) - { - emailMessage.addAttachments(attachments); - return this; - } - - // End Attachments - - // Begin Calendar - - public VelocityMailMessage iCal(String html, byte[] bytes) - { - emailMessage.setType(EmailMessageType.INVITE_ICAL); - emailMessage.setHtmlBody(html); - emailMessage.addAttachment(new BaseAttachment(null, "text/calendar;method=CANCEL", ContentDisposition.INLINE, bytes, "urn:content-classes:calendarmessage")); - return this; - } - - // End Calendar - - public VelocityMailMessage subject(MailTemplate subject) - { - subjectTemplate = subject; - return this; - } - - public VelocityMailMessageImpl bodyText(MailTemplate textBody) - { - textTemplate = textBody; - return this; - } - - public VelocityMailMessageImpl bodyHtml(MailTemplate htmlBody) - { - htmlTemplate = htmlBody; - return this; - } - - public VelocityMailMessageImpl bodyHtmlTextAlt(MailTemplate htmlBody, MailTemplate textBody) - { - bodyHtml(htmlBody); - bodyText(textBody); - return this; - } - - private String mergeTemplate(MailTemplate template) - { - StringWriter writer = new StringWriter(); - - try - { - velocityEngine.evaluate(context, writer, template.getTemplateName(), new InputStreamReader(template.getInputStream())); - } - catch (ResourceNotFoundException e) - { - throw new TemplatingException("Unable to find template", e); - } - catch (ParseErrorException e) - { - throw new TemplatingException("Unable to find template", e); - } - catch (MethodInvocationException e) - { - throw new TemplatingException("Error processing method referenced in context", e); - } - catch (IOException e) - { - throw new TemplatingException("Error rendering output", e); - } - - return writer.toString(); - } - - public VelocityMailMessageImpl put(String key, Object value) - { - context.put(key, value); - return this; - } - - public EmailMessage getEmailMessage() - { - return emailMessage; - } - - private VelocityMailMessageImpl mergeTemplates() - { - if (!templatesMerged) - { - put("mailContext", new MailContext(EmailAttachmentUtil.getEmailAttachmentMap(emailMessage.getAttachments()))); - - if (subjectTemplate != null) - { - emailMessage.setSubject(mergeTemplate(subjectTemplate)); - } - - if (textTemplate != null) - { - emailMessage.setTextBody(mergeTemplate(textTemplate)); - } - - if (htmlTemplate != null) - { - emailMessage.setHtmlBody(mergeTemplate(htmlTemplate)); - } - - templatesMerged = true; - - return this; - } - else - { - throw new TemplatingException("Email Templates Already Merged"); - } - } - - public EmailMessage send(Session session) throws SendFailedException, TemplatingException - { - if (!templatesMerged) - { - mergeTemplates(); - } - - MailUtility.send(emailMessage, session); - - return emailMessage; - } - - public EmailMessage send() throws SendFailedException, TemplatingException - { - return this.send(session.get()); - } -} diff --git a/velocity/impl/src/main/java/org/jboss/seam/mail/templating/velocity/VelocityTemplate.java b/velocity/impl/src/main/java/org/jboss/seam/mail/templating/velocity/VelocityTemplate.java new file mode 100644 index 0000000..09e603a --- /dev/null +++ b/velocity/impl/src/main/java/org/jboss/seam/mail/templating/velocity/VelocityTemplate.java @@ -0,0 +1,92 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2011, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jboss.seam.mail.templating.velocity; + +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.StringWriter; +import java.util.Map; + +import org.apache.velocity.VelocityContext; +import org.apache.velocity.app.VelocityEngine; +import org.apache.velocity.exception.MethodInvocationException; +import org.apache.velocity.exception.ParseErrorException; +import org.apache.velocity.exception.ResourceNotFoundException; +import org.jboss.seam.mail.templating.MailTemplate; +import org.jboss.seam.mail.templating.TemplateImpl; +import org.jboss.seam.mail.templating.TemplatingException; + +/** + * + * @author Cody Lerum + * + */ +public class VelocityTemplate implements TemplateImpl +{ + private VelocityEngine velocityEngine; + private VelocityContext velocityContext; + private CDIVelocityContext cdiContext; + + private MailTemplate mailTemplate; + + public VelocityTemplate(MailTemplate mailTemplate) + { + velocityEngine = new VelocityEngine(); + velocityEngine.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.SimpleLog4JLogSystem"); + this.mailTemplate = mailTemplate; + } + + public VelocityTemplate(MailTemplate mailTemplate, CDIVelocityContext cdiContext) + { + this(mailTemplate); + this.cdiContext = cdiContext; + } + + public String merge(Map context) + { + StringWriter writer = new StringWriter(); + + if(cdiContext != null) + { + velocityContext = new VelocityContext(context, cdiContext); + } + + try + { + velocityEngine.evaluate(velocityContext, writer, mailTemplate.getTemplateName(), new InputStreamReader(mailTemplate.getInputStream())); + } + catch (ResourceNotFoundException e) + { + throw new TemplatingException("Unable to find template", e); + } + catch (ParseErrorException e) + { + throw new TemplatingException("Unable to find template", e); + } + catch (MethodInvocationException e) + { + throw new TemplatingException("Error processing method referenced in context", e); + } + catch (IOException e) + { + throw new TemplatingException("Error rendering output", e); + } + + return writer.toString(); + } +}