Clover icon

Magnolia Mail module 5.5.2

  1. Project Clover database Mon May 6 2019 07:05:59 CEST
  2. Package info.magnolia.module.mail.app.verify

File MailVerifyPresenter.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart6.png
33% of files have more coverage

Code metrics

22
92
5
1
258
190
19
0.21
18.4
5
3.8

Classes

Class Line # Actions
MailVerifyPresenter 80 92 0% 19 51
0.571428657.1%
 

Contributing tests

This file is covered by 2 tests. .

Source view

1    /**
2    * This file Copyright (c) 2013-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.app.verify;
35   
36    import info.magnolia.context.MgnlContext;
37    import info.magnolia.dam.api.Asset;
38    import info.magnolia.dam.api.AssetProviderRegistry;
39    import info.magnolia.dam.api.ItemKey;
40    import info.magnolia.i18nsystem.SimpleTranslator;
41    import info.magnolia.jcr.util.PropertyUtil;
42    import info.magnolia.jcr.util.SessionUtil;
43    import info.magnolia.module.mail.MailModule;
44    import info.magnolia.module.mail.MailTemplate;
45    import info.magnolia.module.mail.app.MailSubAppDecriptor;
46    import info.magnolia.module.mail.templates.MailAttachment;
47    import info.magnolia.module.mail.templates.MgnlEmail;
48    import info.magnolia.module.mail.util.MailUtil;
49    import info.magnolia.objectfactory.ComponentProvider;
50    import info.magnolia.repository.RepositoryConstants;
51    import info.magnolia.ui.api.app.SubAppContext;
52    import info.magnolia.ui.api.context.UiContext;
53    import info.magnolia.ui.dialog.formdialog.FormBuilder;
54    import info.magnolia.ui.form.definition.FormDefinition;
55    import info.magnolia.ui.vaadin.form.FormViewReduced;
56    import info.magnolia.ui.vaadin.overlay.MessageStyleTypeEnum;
57   
58    import java.io.File;
59    import java.io.FileOutputStream;
60    import java.util.HashMap;
61    import java.util.Iterator;
62    import java.util.List;
63    import java.util.Map;
64   
65    import javax.inject.Inject;
66    import javax.jcr.Node;
67    import javax.mail.Message.RecipientType;
68    import javax.mail.internet.InternetAddress;
69   
70    import org.apache.commons.io.IOUtils;
71    import org.apache.commons.lang3.StringUtils;
72    import org.slf4j.Logger;
73    import org.slf4j.LoggerFactory;
74   
75    import com.vaadin.v7.data.util.PropertysetItem;
76   
77    /**
78    * Presenter for the mail verify subapp.
79    */
 
80    public class MailVerifyPresenter implements MailVerifyView.Listener {
81   
82    private MailVerifyView view;
83    private ComponentProvider componentProvider;
84    private MailModule mailModule;
85    private SubAppContext subAppContext;
86    private UiContext context;
87    private SimpleTranslator translator;
88    private FormBuilder builder;
89    protected Map<String, PropertysetItem> items = new HashMap<>();
90   
91    private static final String PATH_SEPARATOR = "/";
92    private static final String SIMPLE_RENDERER = "simple";
93   
94    private static final String SIMPLE_MAIL_FORM_DEFINITION = "simple";
95    private static final String TEMPLATE_MAIL_FORM_DEFINITION = "template";
96    private static final String SIMPLE_CONFIGURATION_NODE_PATH = "/modules/mail/config/simpleConfiguration";
97   
98    private static final String PROPERTY_CONTENT_TYPE = "contentType";
99    private static final String PROPERTY_ATTACHMENT = "attachment";
100    private static final String PROPERTY_EMAIL = "email";
101    private static final String PROPERTY_TEMPLATE = "template";
102    private static final String PROPERTY_DATA = "data";
103   
104    private static final String BUTTON_SIMPLE_MAIL_SUCCESS = "mail.app.verify.button.simple.success";
105    private static final String BUTTON_SIMPLE_MAIL_ERROR = "mail.app.verify.button.simple.error";
106    private static final String BUTTON_TEMPLATE_MAIL_SUCCESS = "mail.app.verify.button.template.success";
107    private static final String BUTTON_TEMPLATE_MAIL_ERROR = "mail.app.verify.button.template.error";
108   
109    private static final Logger log = LoggerFactory.getLogger(MailVerifyPresenter.class);
110    private final AssetProviderRegistry assetRegistry;
111   
 
112  2 toggle @Inject
113    public MailVerifyPresenter(MailVerifyView view, FormBuilder builder, ComponentProvider componentProvider, MailModule mailModule, SubAppContext subAppContext, UiContext context, SimpleTranslator translator, AssetProviderRegistry assetRegistry) {
114  2 this.view = view;
115  2 this.builder = builder;
116  2 this.componentProvider = componentProvider;
117  2 this.mailModule = mailModule;
118  2 this.subAppContext = subAppContext;
119  2 this.context = context;
120  2 this.translator = translator;
121  2 this.assetRegistry = assetRegistry;
122    }
123   
 
124  0 toggle public MailVerifyView start() {
125  0 FormDefinition formDefinition = ((MailSubAppDecriptor) subAppContext.getSubAppDescriptor()).getFormDefinitions().get(SIMPLE_MAIL_FORM_DEFINITION);
126  0 FormViewReduced formView = componentProvider.getComponent(FormViewReduced.class);
127  0 PropertysetItem item = new PropertysetItem();
128  0 items.put(SIMPLE_MAIL_FORM_DEFINITION, item);
129  0 builder.buildReducedForm(formDefinition, formView, item, null);
130  0 view.addFormViewReduced(SIMPLE_MAIL_FORM_DEFINITION, formView);
131   
132  0 formDefinition = ((MailSubAppDecriptor) subAppContext.getSubAppDescriptor()).getFormDefinitions().get(TEMPLATE_MAIL_FORM_DEFINITION);
133  0 formView = componentProvider.getComponent(FormViewReduced.class);
134  0 item = new PropertysetItem();
135  0 items.put(TEMPLATE_MAIL_FORM_DEFINITION, item);
136  0 builder.buildReducedForm(formDefinition, formView, item, null);
137  0 view.addFormViewReduced(TEMPLATE_MAIL_FORM_DEFINITION, formView);
138   
139  0 view.setListner(this);
140  0 view.build();
141  0 return view;
142    }
143   
 
144  1 toggle private Node getMailConfigurationNode() {
145  1 return SessionUtil.getNode(RepositoryConstants.CONFIG, SIMPLE_CONFIGURATION_NODE_PATH);
146    }
147   
 
148  1 toggle @Override
149    public void sendSimpleMail() {
150  1 File tmp = null; // temporary file used for attachment
151  1 try {
152  1 Node mailNode = getMailConfigurationNode();
153  1 Map<String, Object> params = new HashMap<>();
154   
155  1 String userEmailAddress = PropertyUtil.getString(SessionUtil.getNodeByIdentifier(RepositoryConstants.USERS, MgnlContext.getUser().getIdentifier()), PROPERTY_EMAIL);
156  1 if (StringUtils.isBlank(userEmailAddress)) {
157  0 context.openNotification(MessageStyleTypeEnum.ERROR, true, translator.translate("mail.app.verify.button.simple.error.no.email", MgnlContext.getUser().getName()));
158  0 return;
159    }
160  1 String contentType = (String) items.get(SIMPLE_MAIL_FORM_DEFINITION).getItemProperty(PROPERTY_CONTENT_TYPE).getValue();
161  1 String itemKeyAsString = (String) items.get(SIMPLE_MAIL_FORM_DEFINITION).getItemProperty(PROPERTY_ATTACHMENT).getValue(); // asset id to attachment
162   
163    // set mail parameters
164  1 params.put(MailTemplate.MAIL_TO, userEmailAddress);
165  1 params.put(MailTemplate.MAIL_FROM, PropertyUtil.getString(mailNode, MailTemplate.MAIL_FROM));
166  1 params.put(MailTemplate.MAIL_SUBJECT, PropertyUtil.getString(mailNode, MailTemplate.MAIL_SUBJECT));
167  1 params.put(MailTemplate.MAIL_BODY, PropertyUtil.getString(mailNode, MailTemplate.MAIL_BODY));
168   
169    // create attachment list
170  1 List<MailAttachment> attachments = MailUtil.createAttachmentList();
171   
172    // get asset and create temporary file to be send as attachment
173  1 if (StringUtils.isNotEmpty(itemKeyAsString) && ItemKey.isValid(itemKeyAsString)) {
174  0 ItemKey itemKey = ItemKey.from(itemKeyAsString);
175  0 Asset asset = assetRegistry.getProviderFor(itemKey).getAsset(itemKey);
176  0 if (asset != null) {
177  0 tmp = new File(asset.getFileName());
178  0 FileOutputStream fileOutputStream = new FileOutputStream(tmp);
179  0 try {
180  0 fileOutputStream.write(IOUtils.toByteArray(asset.getContentStream()));
181    } finally {
182  0 IOUtils.closeQuietly(fileOutputStream);
183    }
184  0 MailAttachment a = new MailAttachment(tmp, asset.getFileName(), asset.getDescription(), MailAttachment.DISPOSITION_ATTACHMENT);
185  0 attachments.add(a);
186    }
187    }
188   
189  1 MgnlEmail email = mailModule.getFactory().getEmailFromType(params, SIMPLE_RENDERER, contentType, attachments);
190  1 mailModule.getHandler().prepareAndSendMail(email);
191  1 context.openNotification(MessageStyleTypeEnum.INFO, true, translator.translate(BUTTON_SIMPLE_MAIL_SUCCESS, userEmailAddress));
192    } catch (Exception e) {
193  0 log.error("Unable to send email.", e);
194  0 context.openNotification(MessageStyleTypeEnum.ERROR, true, translator.translate(BUTTON_SIMPLE_MAIL_ERROR, e.getMessage()));
195    } finally {
196  1 if (tmp != null) {
197  0 tmp.delete();
198    }
199    }
200    }
201   
 
202  1 toggle @Override
203    public void sendTemplateMail() {
204  1 try {
205  1 String templateName = StringUtils.substringAfterLast((String) items.get(TEMPLATE_MAIL_FORM_DEFINITION).getItemProperty(PROPERTY_TEMPLATE).getValue(), PATH_SEPARATOR);
206  1 String paramStr = (String) items.get(TEMPLATE_MAIL_FORM_DEFINITION).getItemProperty(PROPERTY_DATA).getValue();
207   
208  1 Iterator<MailTemplate> iter = mailModule.getTemplatesConfiguration().iterator();
209  1 MailTemplate template = null;
210  1 while (iter.hasNext()) {
211  1 template = iter.next();
212  1 if (!template.getName().equals(templateName)) {
213  0 template = null;
214    } else {
215  1 break;
216    }
217    }
218  1 if (template != null) {
219  1 Map<String, Object> params = new HashMap<>();
220   
221  1 String userEmailAddress = PropertyUtil.getString(SessionUtil.getNodeByIdentifier(RepositoryConstants.USERS, MgnlContext.getUser().getIdentifier()), PROPERTY_EMAIL);
222  1 if (StringUtils.isBlank(userEmailAddress)) {
223  0 context.openNotification(MessageStyleTypeEnum.ERROR, true, translator.translate("mail.app.verify.button.simple.error.no.email", MgnlContext.getUser().getName()));
224  0 return;
225    }
226  1 params.put(MailTemplate.MAIL_TO, userEmailAddress);
227  1 params.putAll(template.getParameters());
228   
229  1 if (StringUtils.isNotEmpty(paramStr)) {
230  1 String[] parameters = StringUtils.split(paramStr, "\n");
231  1 for (String parameter : parameters) {
232  1 String[] value = StringUtils.split(parameter, "=");
233  1 if (value.length == 2) {
234  1 params.put(value[0], value[1]);
235    } else {
236  0 log.warn("Template mail parameter {} has no value specified.", value[0]);
237  0 params.put(value[0], "");
238    }
239    }
240    }
241   
242  1 MgnlEmail email = mailModule.getFactory().getEmailFromTemplate(template.getName(), template.getAttachments(), params);
243  1 email.addRecipient(RecipientType.TO, new InternetAddress(userEmailAddress));
244  1 if (template.getTemplateFile() == null) {
245  0 email.setBody();
246    } else {
247  1 email.setBodyFromResourceFile();
248    }
249  1 mailModule.getHandler().sendMail(email);
250  1 context.openNotification(MessageStyleTypeEnum.INFO, true, translator.translate(BUTTON_TEMPLATE_MAIL_SUCCESS, template.getName(), userEmailAddress));
251    }
252    } catch (Exception e) {
253  0 log.error("Unable to send email.", e);
254  0 context.openNotification(MessageStyleTypeEnum.ERROR, true, translator.translate(BUTTON_TEMPLATE_MAIL_ERROR, e.getMessage()));
255    }
256    }
257   
258    }