1 /* Copyright (c) 2019 AT&T Intellectual Property. #
\r
3 # Licensed under the Apache License, Version 2.0 (the "License"); #
\r
4 # you may not use this file except in compliance with the License. #
\r
5 # You may obtain a copy of the License at #
\r
7 # http://www.apache.org/licenses/LICENSE-2.0 #
\r
9 # Unless required by applicable law or agreed to in writing, software #
\r
10 # distributed under the License is distributed on an "AS IS" BASIS, #
\r
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
\r
12 # See the License for the specific language governing permissions and #
\r
13 # limitations under the License. #
\r
14 ##############################################################################*/
\r
17 package org.oran.otf.camunda.delegate.otf.common;
\r
19 import org.oran.otf.camunda.exception.TestExecutionException;
\r
20 import org.oran.otf.camunda.workflow.utility.WorkflowUtility;
\r
21 import org.oran.otf.common.model.TestExecution;
\r
22 import org.oran.otf.common.utility.Utility;
\r
23 import java.util.ArrayList;
\r
24 import java.util.List;
\r
25 import java.util.Map;
\r
26 import java.util.Properties;
\r
27 import javax.mail.Message;
\r
28 import javax.mail.MessagingException;
\r
29 import javax.mail.Session;
\r
30 import javax.mail.Transport;
\r
31 import javax.mail.internet.InternetAddress;
\r
32 import javax.mail.internet.MimeMessage;
\r
33 import org.assertj.core.util.Strings;
\r
34 import org.camunda.bpm.engine.delegate.DelegateExecution;
\r
35 import org.camunda.bpm.engine.delegate.JavaDelegate;
\r
36 import org.slf4j.Logger;
\r
37 import org.slf4j.LoggerFactory;
\r
38 import org.springframework.beans.factory.annotation.Autowired;
\r
39 import org.springframework.stereotype.Service;
\r
42 public class SendMailDelegate implements JavaDelegate {
\r
44 @Autowired private WorkflowUtility utility;
\r
46 private static final String logPrefix = Utility.getLoggerPrefix();
\r
47 private static final Logger log = LoggerFactory.getLogger(SendMailDelegate.class);
\r
50 public void execute(DelegateExecution execution) throws Exception {
\r
51 Map<String, Object> variables = execution.getVariables();
\r
52 Map<String, Object> testData = utility.getTestData(variables, logPrefix);
\r
54 Map<String, Object> sendMailData = null;
\r
57 (Map<String, Object>) testData.getOrDefault(execution.getCurrentActivityId(), null);
\r
58 } catch (Exception e) {
\r
59 log.error(e.getMessage());
\r
60 throw new TestExecutionException(e);
\r
63 if (sendMailData == null) {
\r
66 "%sMissing parameters for activityId, %s.",
\r
67 logPrefix, execution.getCurrentActivityId());
\r
69 throw new TestExecutionException(err);
\r
72 // Get the recipient(s)
\r
73 Object oRecipients = sendMailData.get("to");
\r
74 if (oRecipients == null) {
\r
75 String err = String.format("%sRecipients array cannot be null or empty.", logPrefix);
\r
77 throw new TestExecutionException(err);
\r
79 List<String> recipients = null;
\r
81 recipients = (ArrayList<String>) (oRecipients);
\r
82 if (recipients.size() == 0) {
\r
83 String err = String.format("%sRecipients array cannot be null or empty.", logPrefix);
\r
85 throw new TestExecutionException(err);
\r
87 } catch (Exception e) {
\r
88 throw new TestExecutionException(e);
\r
91 for (String recipient : recipients) {
\r
92 if (Strings.isNullOrEmpty(recipient)) {
\r
93 String err = String.format("%sRecipient cannot be null or empty.", logPrefix);
\r
95 throw new TestExecutionException(err);
\r
99 // Get the email subject.
\r
100 String subject = (String) sendMailData.get("subject");
\r
101 if (Strings.isNullOrEmpty(subject.trim())) {
\r
102 String err = String.format("%sSubject cannot be null or empty.", logPrefix);
\r
104 throw new TestExecutionException(err);
\r
107 // Get the body contents.
\r
108 String body = (String) sendMailData.get("body");
\r
109 if (Strings.isNullOrEmpty(body.trim())) {
\r
110 String err = String.format("%sBody cannot be null or empty.", logPrefix);
\r
112 throw new TestExecutionException(err);
\r
115 TestExecution testExecution = utility.getTestExecution(variables, logPrefix);
\r
116 String sender = testExecution.getHistoricEmail();
\r
117 String hTestInstanceId = testExecution.getHistoricTestInstance().get_id().toString();
\r
118 String processInstanceId = execution.getProcessInstanceId();
\r
120 sendMail(recipients, subject, body, sender, processInstanceId, hTestInstanceId);
\r
123 public void sendMail(
\r
124 List<String> recipients,
\r
128 String processInstanceId,
\r
129 String testInstanceId)
\r
131 // Get the system properties.
\r
132 Properties properties = System.getProperties();
\r
134 // Set the SMTP host.
\r
135 properties.setProperty("mail.smtp.host", "localhost");
\r
137 // creating session object to get properties
\r
138 Session session = Session.getDefaultInstance(properties);
\r
141 // MimeMessage object.
\r
142 MimeMessage message = new MimeMessage(session);
\r
144 // Set From Field: adding senders email to from field.
\r
145 message.setFrom(new InternetAddress("OTF_EMAIL-ALERT@localhost"));
\r
147 // Set Subject: subject of the email
\r
148 message.setSubject(subject);
\r
150 // set body of the email.
\r
151 StringBuffer sb = new StringBuffer();
\r
152 sb.append("************************OTF Alerting System************************");
\r
154 sb.append(String.format("This message was sent by %s via the Open Test Framework\n", sender));
\r
155 sb.append(String.format("processInstanceId: %s\n", processInstanceId));
\r
156 sb.append(String.format("testInstanceId: %s", testInstanceId));
\r
158 sb.append("******************************************************************");
\r
162 message.setText(sb.toString());
\r
165 Transport.send(message);
\r
166 } catch (MessagingException mex) {
\r
167 mex.printStackTrace();
\r