View Javadoc
1   /**
2    * This file Copyright (c) 2003-2018 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.ModuleRegistry;
37  import info.magnolia.module.mail.handlers.MgnlMailHandler;
38  import info.magnolia.module.mail.templates.MailAttachment;
39  import info.magnolia.module.mail.templates.MgnlEmail;
40  import info.magnolia.module.mail.templates.impl.SimpleEmail;
41  import info.magnolia.objectfactory.Classes;
42  import info.magnolia.objectfactory.Components;
43  
44  import java.util.ArrayList;
45  import java.util.HashMap;
46  import java.util.List;
47  import java.util.Map;
48  
49  import javax.inject.Inject;
50  
51  import org.apache.commons.beanutils.BeanUtils;
52  import org.apache.commons.collections4.CollectionUtils;
53  import org.apache.commons.lang3.StringUtils;
54  import org.slf4j.Logger;
55  import org.slf4j.LoggerFactory;
56  
57  
58  /**
59   * This reads the repository to know what kind of email to instantiate.
60   *
61   * @author <a href="mailto:niko@macnica.com">Nicolas Modrzyk</a>
62   */
63  public class MgnlMailFactory {
64      private final static Logger log = LoggerFactory.getLogger(MgnlMailFactory.class);
65  
66      private Map<String, String> renderers = new HashMap<>();
67  
68      private final MailModule mailModule;
69  
70      @Inject
71      public MgnlMailFactory(MailModule mailModule) {
72          this.mailModule = mailModule;
73      }
74  
75      /**
76       * @deprecated since 5.5. Please, use {@link #MgnlMailFactory(info.magnolia.module.mail.MailModule mailModule)} instead.
77       */
78      public MgnlMailFactory(ModuleRegistry moduleRegistry) {
79          this(Components.getComponent(MailModule.class));
80      }
81  
82      public MgnlMailHandler getEmailHandler() {
83          return mailModule.getHandler();
84      }
85  
86      /**
87       * Creates email with no attachments.
88       */
89      public MgnlEmail getEmail(Map<String, Object> params) {
90          return getEmail(params, null);
91      }
92  
93      /**
94       * Creates email with no attachments.
95       */
96      public MgnlEmail getEmailFromType(Map<String, Object> params, String type) {
97          return getEmailFromType(params, type, null);
98      }
99  
100     public MgnlEmail getEmailFromType(Map<String, Object> params, String type, String contentType, List<MailAttachment> attachments) {
101         Map<String, Object> newParams = new HashMap<>();
102         newParams.putAll(params);
103         if (!StringUtils.isEmpty(type)) {
104             newParams.put(MailTemplate.MAIL_TYPE, type);
105         }
106         if (!StringUtils.isEmpty(contentType)) {
107             newParams.put(MailTemplate.MAIL_CONTENT_TYPE, contentType);
108         }
109         return getEmail(newParams, attachments);
110     }
111 
112     /**
113      * Creates email with attachments.
114      */
115     public MgnlEmail getEmailFromType(Map<String, Object> params, String type, List<MailAttachment> attachments) {
116         return getEmailFromType(params, type, null, attachments);
117     }
118 
119     /**
120      * Creates email with attachments.
121      */
122     public MgnlEmail getEmail(Map<String, Object> params, List<MailAttachment> attachments) {
123         MailTemplatete.html#MailTemplate">MailTemplate template = new MailTemplate();
124         return getEmail(params, attachments, template);
125     }
126 
127     /**
128      * Creates email using predefined template.
129      */
130     public MgnlEmail getEmailFromTemplate(String id, List<MailAttachment> attachments, Map<String, Object> params) throws Exception {
131         MailTemplate template = getTemplate(id);
132         if (template == null) {
133             log.error("Template {} can't be found", id);
134             return null;
135         }
136         if (!CollectionUtils.isEmpty(attachments)) {
137             // add to attachments also attachments from template configuration
138             attachments.addAll(template.getAttachments());
139         }
140         return getEmail(params, attachments, template);
141     }
142 
143     /**
144      * Creates email using predefined template with no attachments.
145      */
146     public MgnlEmail getEmailFromTemplate(String id, Map<String, Object> params) throws Exception {
147         return getEmailFromTemplate(id, null, params);
148     }
149 
150     protected MgnlEmail getEmail(Map<String, Object> params, List<MailAttachment> attachments, MailTemplate template) {
151         template.setValues(params, attachments);
152 
153         try {
154             return getEmailFromType(template);
155 
156         } catch (Exception e) {
157             log.error("Couln't instantiate email type: " + template.getType(), e);
158             return null;
159         }
160     }
161 
162     protected MgnlEmail getEmailFromType(MailTemplate template) throws Exception {
163         MgnlEmail mail;
164         if (StringUtils.isEmpty(template.getType()) || !renderers.containsKey(template.getType().toLowerCase())) {
165             mail = new SimpleEmail(template);
166 
167         } else {
168             String rendererClass = renderers.get(template.getType().toLowerCase());
169             mail = Classes.newInstance(rendererClass, template);
170         }
171 
172         //set all mail parameters
173         if (!StringUtils.isEmpty(template.getFrom())) {
174             mail.setFrom(template.getFrom());
175         }
176         if (!StringUtils.isEmpty(template.getTo())) {
177             mail.setToList(template.getTo());
178         }
179         if (!StringUtils.isEmpty(template.getCc())) {
180             mail.setCcList(template.getCc());
181         }
182         if (!StringUtils.isEmpty(template.getBcc())) {
183             mail.setBccList(template.getBcc());
184         }
185         if (!StringUtils.isEmpty(template.getReplyTo())) {
186             mail.setReplyToList(template.getReplyTo());
187         }
188         if (!StringUtils.isEmpty(template.getSubject())) {
189             mail.setSubject(template.getSubject());
190         }
191 
192         return mail;
193     }
194 
195 
196     protected MailTemplate getTemplate(String templateName) throws Exception {
197         final List<MailTemplate> configuration = mailModule.getTemplatesConfiguration();
198         for (MailTemplate mailTemplate : configuration) {
199             if (StringUtils.equals(mailTemplate.getName(), templateName)) {
200                 MailTemplate../../info/magnolia/module/mail/MailTemplate.html#MailTemplate">MailTemplate cloned = (MailTemplate) BeanUtils.cloneBean(mailTemplate);
201                 cloned.setParameters(new HashMap<>(mailTemplate.getParameters()));
202                 cloned.setAttachments(new ArrayList<>(mailTemplate.getAttachments()));
203                 return cloned;
204             }
205         }
206         return null;
207     }
208 
209 
210     public Map<String, String> getRenderers() {
211         return renderers;
212     }
213 
214     public void setRenderers(Map<String, String> renderers) {
215         this.renderers = renderers;
216     }
217 
218 }