KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > mail > templates > MgnlEmail


1 package info.magnolia.cms.mail.templates;
2
3 import info.magnolia.cms.mail.MailException;
4
5 import java.io.BufferedReader JavaDoc;
6 import java.io.File JavaDoc;
7 import java.io.FileReader JavaDoc;
8 import java.net.URL JavaDoc;
9 import java.util.ArrayList JavaDoc;
10 import java.util.List JavaDoc;
11 import java.util.Map JavaDoc;
12 import java.util.regex.Matcher JavaDoc;
13 import java.util.regex.Pattern JavaDoc;
14
15 import javax.activation.MimetypesFileTypeMap JavaDoc;
16 import javax.mail.Address JavaDoc;
17 import javax.mail.Message JavaDoc;
18 import javax.mail.MessagingException JavaDoc;
19 import javax.mail.Session JavaDoc;
20 import javax.mail.internet.AddressException JavaDoc;
21 import javax.mail.internet.InternetAddress JavaDoc;
22 import javax.mail.internet.MimeBodyPart JavaDoc;
23 import javax.mail.internet.MimeMessage JavaDoc;
24
25 import org.apache.commons.lang.ArrayUtils;
26 import org.apache.commons.lang.StringUtils;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30
31 /**
32  * Date: Mar 30, 2006 Time: 1:01:37 PM
33  * @author <a HREF="mailto:niko@macnica.com">Nicolas Modrzyk</a>
34  */

35 public abstract class MgnlEmail extends MimeMessage JavaDoc {
36
37     protected static final String JavaDoc CONTENT_TYPE = "Content-Type";
38
39     protected static final String JavaDoc TEXT_PLAIN_UTF = "text/plain; charset=UTF-8";
40
41     protected static final String JavaDoc TEXT_HTML_UTF = "text/html; charset=UTF-8";
42
43     protected static final String JavaDoc CHARSET_HEADER_STRING = "charset=";
44     
45     protected static final Pattern JavaDoc EMAIL_WITH_PERSONAL_PATTERN = Pattern.compile("\"+(.*)\"+<(.*)>");
46
47     public static Logger log = LoggerFactory.getLogger(MgnlEmail.class);
48
49     public static final MimetypesFileTypeMap JavaDoc map = new MimetypesFileTypeMap JavaDoc();
50
51     private String JavaDoc template;
52
53     private Map JavaDoc parameters;
54
55     private boolean bodyNotSetFlag; // used for threads
56

57     public boolean isBodyNotSetFlag() {
58         return this.bodyNotSetFlag;
59     }
60
61     public void setBodyNotSetFlag(boolean _bodyNotSetFlag) {
62         this.bodyNotSetFlag = _bodyNotSetFlag;
63     }
64
65     public MgnlEmail(Session JavaDoc _session) {
66         super(_session);
67     }
68
69     public abstract void setBody(String JavaDoc body, Map JavaDoc _parameters) throws Exception JavaDoc;
70     
71     public void setSubject(String JavaDoc arg0) throws MessagingException JavaDoc {
72         this.setSubject(arg0, "UTF8");
73     }
74
75     public void setTemplate(String JavaDoc _template) {
76         this.template = _template;
77     }
78
79     public Map JavaDoc getParameters() {
80         return this.parameters;
81     }
82
83     public String JavaDoc getTemplate() {
84         return this.template;
85     }
86
87     public void setParameters(Map JavaDoc _parameters) {
88         this.parameters = _parameters;
89     }
90
91     public void addParameters(Map JavaDoc params) {
92         this.parameters.putAll(params);
93     }
94
95     public void setBody() throws Exception JavaDoc {
96         if (this.template != null) {
97             this.setBody(this.template, this.parameters);
98         }
99     }
100
101     /**
102      * @noinspection MethodOverloadsMethodOfSuperclass
103      */

104     public void setFrom(String JavaDoc _from) {
105         try {
106             InternetAddress JavaDoc address;
107             Matcher JavaDoc matcher = MgnlEmail.EMAIL_WITH_PERSONAL_PATTERN.matcher(_from);
108             if(matcher.matches()){
109                 String JavaDoc email = matcher.group(2);
110                 String JavaDoc personal = matcher.group(1);
111                 address = new InternetAddress JavaDoc(email, personal, "UTF8");
112             }
113             else{
114                 address = new InternetAddress JavaDoc(_from);
115             }
116             this.setFrom(address);
117         }
118         catch (Exception JavaDoc e) {
119             log.error("Could not set from field of email:" + e.getMessage());
120         }
121     }
122
123     public void setCharsetHeader(String JavaDoc charset) throws MailException {
124         try {
125             StringBuffer JavaDoc contentType = new StringBuffer JavaDoc(this.getHeader(CONTENT_TYPE, TEXT_PLAIN_UTF));
126             int index = contentType.lastIndexOf(";");
127             if (index != -1) {
128                 contentType.substring(0, index);
129             }
130             contentType.append(CHARSET_HEADER_STRING).append(charset);
131         }
132         catch (Exception JavaDoc e) {
133             throw new MailException("Content type is not set. Set the content type before setting the charset");
134         }
135     }
136
137     public void setToList(String JavaDoc list) throws Exception JavaDoc {
138         setRecipients(Message.RecipientType.TO, createAdressList(list));
139     }
140
141     public void setCcList(String JavaDoc list) throws Exception JavaDoc {
142         setRecipients(Message.RecipientType.CC, createAdressList(list));
143     }
144
145     public void setBccList(String JavaDoc list) throws Exception JavaDoc {
146         setRecipients(Message.RecipientType.BCC, createAdressList(list));
147     }
148     
149     public void setReplyToList(String JavaDoc list) throws Exception JavaDoc {
150         setReplyTo(createAdressList(list));
151     }
152
153     private Address JavaDoc[] createAdressList(String JavaDoc adresses) throws AddressException JavaDoc {
154         if (adresses == null || adresses.equals(StringUtils.EMPTY)) {
155             return new Address JavaDoc[0];
156         }
157         String JavaDoc[] toObj = adresses.split("\n");
158         List JavaDoc atos = new ArrayList JavaDoc();
159         for (int i = 0; i < toObj.length; i++) {
160             try {
161                 atos.add(new InternetAddress JavaDoc(toObj[i]));
162             } catch (AddressException JavaDoc e) {
163                 log.warn("Error while parsing address.", e);
164             }
165         }
166         return (Address JavaDoc[]) atos.toArray(new Address JavaDoc[atos.size()]);
167     }
168
169     public void setAttachments(ArrayList JavaDoc list) throws MailException {
170         if (list == null) {
171             return;
172         }
173         if (log.isDebugEnabled()) {
174             log.debug("Set attachments [" + list.size() + "] for mail: [" + this.getClass().getName() + "]");
175         }
176         for (int i = 0; i < list.size(); i++) {
177             addAttachment((MailAttachment) list.get(i));
178         }
179     }
180
181     public MimeBodyPart JavaDoc addAttachment(MailAttachment attachment) throws MailException {
182         throw new MailException("Cannot add attachment to this email. It is not a Multimime email");
183     }
184
185     public void setBodyFromResourceFile(String JavaDoc resourceFile, Map JavaDoc map) throws Exception JavaDoc {
186         URL JavaDoc url = this.getClass().getResource("/" + resourceFile);
187         log.info("This is the url:" + url);
188         BufferedReader JavaDoc br = new BufferedReader JavaDoc(new FileReader JavaDoc(url.getFile()));
189         String JavaDoc line;
190         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
191         while ((line = br.readLine()) != null) {
192             buffer.append(line).append(File.separator);
193         }
194         this.setBody(buffer.toString(), map);
195     }
196
197 }
198
Popular Tags