View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.mail;
35  
36  import info.magnolia.module.mail.templates.MailAttachment;
37  import info.magnolia.module.mail.util.MailUtil;
38  
39  import java.util.ArrayList;
40  import java.util.HashMap;
41  import java.util.List;
42  import java.util.Map;
43  import java.util.Properties;
44  
45  import javax.mail.Authenticator;
46  import javax.mail.PasswordAuthentication;
47  import javax.mail.Session;
48  
49  import org.apache.commons.collections.CollectionUtils;
50  import org.apache.commons.lang.StringUtils;
51  
52  public class MailTemplate {
53  
54      public static String MAIL_CONTENT_TYPE = "contentType";
55  
56      public static String MAIL_FROM = "from";
57  
58      public static String MAIL_SUBJECT = "subject";
59  
60      public static String MAIL_TO = "to";
61  
62      public static String MAIL_TO_WORKFLOW = "mailTo";
63  
64      public static String MAIL_CC = "cc";
65  
66      public static String MAIL_TYPE = "type";
67  
68      public static String MAIL_PARAMETERS = "parameters";
69  
70      public static String MAIL_ATTACHMENTS = "attachments";
71  
72      public static String MAIL_BODY = "body";
73  
74      public static final String MAIL_HTML = "html";
75  
76      public static final String MAIL_TEMPLATE_FILE = "templateFile";
77  
78      private static final String MAIL_REPLY_TO = "replyTo";
79  
80      private static final String MAIL_BCC = "bcc";
81  
82      private Map<String, String> parameters = new HashMap<String, String>();
83  
84      private List<MailAttachment> attachments = new ArrayList<MailAttachment>();
85  
86      private String from;
87  
88      private String to;
89  
90      private String cc;
91  
92      private String subject;
93  
94      private String type;
95  
96      private String contentType;
97  
98      private String name;
99  
100     private String text;
101 
102     private String templateFile;
103 
104     private String bcc;
105 
106     private String replyTo;
107 
108     public MailTemplate() {
109 
110     }
111 
112     public Map<String, String> getParameters() {
113         // instance of this class will be re-used. Do not let references to internal variables escape the instance
114         return new HashMap<String, String>(parameters);
115     }
116 
117     public void setParameters(Map<String, String> parameters) {
118         // instance of this class will be re-used. Do not let references to internal variables escape the instance
119         this.parameters.clear();
120         this.parameters.putAll(parameters);
121     }
122 
123     public List<MailAttachment> getAttachments() {
124         // instance of this class will be re-used. Do not let references to internal variables escape the instance
125         return new ArrayList<MailAttachment>(attachments);
126     }
127 
128     public void setAttachments(List<MailAttachment> attachments) {
129         // instance of this class will be re-used. Do not let references to internal variables escape the instance
130         this.attachments.clear();
131         this.attachments.addAll(attachments);
132     }
133 
134     public void addAttachment(MailAttachment attachment) {
135         // instance of this class will be re-used. Do not let references to internal variables escape the instance
136         this.attachments.add(attachment);
137     }
138 
139     public String getFrom() {
140         return from;
141     }
142 
143     public void setFrom(String from) {
144         this.from = from;
145     }
146 
147     public String getTo() {
148         return to;
149     }
150 
151     public void setTo(String to) {
152         this.to = to;
153     }
154 
155     public String getSubject() {
156         return subject;
157     }
158 
159     public void setSubject(String subject) {
160         this.subject = subject;
161     }
162 
163     public String getType() {
164         return type;
165     }
166 
167     public void setType(String type) {
168         this.type = type;
169     }
170 
171     public String getContentType() {
172         return contentType;
173     }
174 
175     public void setContentType(String contentType) {
176         this.contentType = contentType;
177     }
178 
179     public String getName() {
180         return name;
181     }
182 
183     public void setName(String name) {
184         this.name = name;
185     }
186 
187     public String getText() {
188         return text;
189     }
190 
191     public void setText(String text) {
192         this.text = text;
193     }
194 
195 
196     public String getCc() {
197         return cc;
198     }
199 
200 
201     public void setCc(String cc) {
202         this.cc = cc;
203     }
204 
205     public void setValues(Map<String, String> params, List<MailAttachment> attachments) {
206 
207         if(params.containsKey(MAIL_TEMPLATE_FILE)) {
208             this.templateFile = params.get(MAIL_TEMPLATE_FILE);
209         }
210 
211         if(params.containsKey(MAIL_CONTENT_TYPE)) {
212             this.contentType = params.get(MAIL_CONTENT_TYPE);
213         }
214 
215         if(params.containsKey(MAIL_FROM)) {
216             this.from = params.get(MAIL_FROM);
217         }
218 
219         if(params.containsKey(MAIL_SUBJECT)) {
220             this.subject = params.get(MAIL_SUBJECT);
221         }
222 
223         if(params.containsKey(MAIL_TO)) {
224             this.to = params.get(MAIL_TO);
225         }
226         if(params.containsKey(MailTemplate.MAIL_TO_WORKFLOW)) {
227             this.to = params.get(MailTemplate.MAIL_TO_WORKFLOW);
228         }
229 
230         if(params.containsKey(MAIL_CC)) {
231             this.cc = params.get(MAIL_CC);
232         }
233 
234         if(params.containsKey(MAIL_TYPE)) {
235             this.type = params.get(MAIL_TYPE);
236         }
237 
238         if(params.containsKey(MAIL_BODY)) {
239             this.text = params.get(MAIL_BODY);
240         }
241 
242         if(params.containsKey(MAIL_REPLY_TO)) {
243             this.replyTo = params.get(MAIL_REPLY_TO);
244         }
245 
246         if(params.containsKey(MAIL_BCC)) {
247             this.bcc = params.get(MAIL_BCC);
248         }
249 
250         // instance of this class will be re-used. Do not let references to internal variables escape the instance
251         this.parameters.clear();
252         this.parameters.putAll(params);
253 
254         if(!CollectionUtils.isEmpty(attachments)) {
255             // instance of this class will be re-used. Do not let references to internal variables escape the instance
256             this.attachments.clear();
257             this.attachments.addAll(attachments);
258         }
259     }
260 
261     public String getTemplateFile() {
262         return templateFile;
263     }
264 
265     public void setTemplateFile(String templateFile) {
266         this.templateFile = templateFile;
267     }
268 
269     public String getBcc() {
270         return bcc;
271     }
272 
273     public void setBcc(String bcc) {
274         this.bcc = bcc;
275     }
276 
277     public String getReplyTo() {
278         return replyTo;
279     }
280 
281     public void setReplyTo(String replyTo) {
282         this.replyTo = replyTo;
283     }
284 
285     public Session initSession() {
286 
287         Map<String, String> smtp = MailModule.getInstance().getSmtp();
288         Properties props = new Properties(); // System.getProperties(); should I try to use the system properties ?
289 
290         props.put("mail.smtp.host", MailUtil.getParameter(getParameters(), MailConstants.SMTP_SERVER, smtp.get(MailConstants.SMTP_SERVER)));
291         props.put("mail.smtp.port", MailUtil.getParameter(getParameters(), MailConstants.SMTP_PORT, smtp.get(MailConstants.SMTP_PORT)));
292 
293         final String starttls = MailUtil.getParameter(getParameters(), MailConstants.SMTP_TLS, smtp.get(MailConstants.SMTP_TLS));
294         if ("true".equals(starttls)) {
295             //MAGNOLIA-2420
296             props.put("mail.smtp.starttls.enable", starttls);
297         }
298         final String ssl = MailUtil.getParameter(getParameters(), MailConstants.SMTP_SSL, smtp.get(MailConstants.SMTP_SSL));
299         if ("true".equals(ssl)) {
300             //MAGNOLIA-2420
301             props.put("mail.smtp.socketFactory.port", MailUtil.getParameter(getParameters(), MailConstants.SMTP_PORT, smtp.get(MailConstants.SMTP_PORT)));
302             props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
303             props.put("mail.smtp.socketFactory.fallback", "false");
304         }
305 
306         Authenticator auth = null;
307         final String smtpUser = MailUtil.getParameter(getParameters(), MailConstants.SMTP_USER, smtp.get(MailConstants.SMTP_USER));
308         final String smtpPassword = MailUtil.getParameter(getParameters(), MailConstants.SMTP_PASSWORD, smtp.get(MailConstants.SMTP_PASSWORD));
309         if (StringUtils.isNotBlank(smtpUser)) {
310             props.put("mail.smtp.auth", "true");
311             props.put("mail.smtp.user", smtpUser);
312             auth = new Authenticator() {
313                 protected PasswordAuthentication getPasswordAuthentication() {
314                     return new PasswordAuthentication(smtpUser, smtpPassword);
315                 }
316             };
317         }
318         props.put("mail.smtp.sendpartial", StringUtils.defaultString(smtp.get(MailConstants.SMTP_SEND_PARTIAL)));
319         return Session.getInstance(props, auth);
320     }
321 
322 }