1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
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 |
|
|
79 |
|
|
|
|
| 57.1% |
Uncovered Elements: 51 (119) |
Complexity: 19 |
Complexity Density: 0.21 |
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
|
112 |
2 |
@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 |
|
|
|
|
| 0% |
Uncovered Elements: 15 (15) |
Complexity: 1 |
Complexity Density: 0.07 |
|
124 |
0 |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
144 |
1 |
private Node getMailConfigurationNode() {... |
145 |
1 |
return SessionUtil.getNode(RepositoryConstants.CONFIG, SIMPLE_CONFIGURATION_NODE_PATH); |
146 |
|
} |
147 |
|
|
|
|
| 51.2% |
Uncovered Elements: 20 (41) |
Complexity: 7 |
Complexity Density: 0.21 |
|
148 |
1 |
@Override... |
149 |
|
public void sendSimpleMail() { |
150 |
1 |
File tmp = null; |
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(); |
162 |
|
|
163 |
|
|
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 |
|
|
170 |
1 |
List<MailAttachment> attachments = MailUtil.createAttachmentList(); |
171 |
|
|
172 |
|
|
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 |
|
|
|
|
| 69.4% |
Uncovered Elements: 15 (49) |
Complexity: 9 |
Complexity Density: 0.26 |
|
202 |
1 |
@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 |
|
} |