Skip to content

Commit

Permalink
refactor for a more unified templating system and a single MailMessage
Browse files Browse the repository at this point in the history
api
  • Loading branch information
codylerum committed Mar 4, 2011
1 parent 36aa623 commit 5c8c5b4
Show file tree
Hide file tree
Showing 37 changed files with 430 additions and 2,344 deletions.
49 changes: 49 additions & 0 deletions core/api/src/main/java/org/jboss/seam/mail/api/MailMessage.java
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.jboss.seam.mail.core;
package org.jboss.seam.mail.templating;

import java.io.InputStream;
/**
Expand All @@ -24,8 +24,8 @@
*
*/
public interface MailTemplate
{
{
public String getTemplateName();

public InputStream getInputStream();
public InputStream getInputStream();
}
@@ -0,0 +1,8 @@
package org.jboss.seam.mail.templating;

import java.util.Map;

public interface TemplateImpl
{
public String merge(Map<String, Object> context);
}
@@ -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;
Expand Down
@@ -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;
Expand Down
@@ -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;
Expand All @@ -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)
Expand Down
Expand Up @@ -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
Expand Down

This file was deleted.

52 changes: 0 additions & 52 deletions core/impl/src/main/java/org/jboss/seam/mail/core/MailContext.java

This file was deleted.

Expand Up @@ -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;
Expand All @@ -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;

/**
*
Expand All @@ -39,7 +45,13 @@
public class MailMessageImpl implements MailMessage
{
private EmailMessage emailMessage;


private TemplateImpl subjectTemplate;
private TemplateImpl textTemplate;
private TemplateImpl htmlTemplate;
private Map<String, Object> templateContext = new HashMap<String, Object>();
private boolean templatesMerged;

@Inject
private Instance<Session> session;

Expand Down Expand Up @@ -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()
{
Expand All @@ -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;
Expand Down
Expand Up @@ -21,6 +21,8 @@
import javax.inject.Inject;
import javax.mail.Session;

import org.jboss.seam.mail.util.MailUtility;

/**
*
* @author Cody Lerum
Expand Down

0 comments on commit 5c8c5b4

Please sign in to comment.