added svcapi ui and camunda code
[it/otf.git] / otf-camunda / src / main / java / org / oran / otf / camunda / delegate / otf / common / SendMailDelegate.java
1 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
2 #                                                                              #\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
6 #                                                                              #\r
7 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
8 #                                                                              #\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
15 \r
16 \r
17 package org.oran.otf.camunda.delegate.otf.common;\r
18 \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
40 \r
41 @Service\r
42 public class SendMailDelegate implements JavaDelegate {\r
43 \r
44   @Autowired private WorkflowUtility utility;\r
45 \r
46   private static final String logPrefix = Utility.getLoggerPrefix();\r
47   private static final Logger log = LoggerFactory.getLogger(SendMailDelegate.class);\r
48 \r
49   @Override\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
53 \r
54     Map<String, Object> sendMailData = null;\r
55     try {\r
56       sendMailData =\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
61     }\r
62 \r
63     if (sendMailData == null) {\r
64       String err =\r
65           String.format(\r
66               "%sMissing parameters for activityId, %s.",\r
67               logPrefix, execution.getCurrentActivityId());\r
68       log.error(err);\r
69       throw new TestExecutionException(err);\r
70     }\r
71 \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
76       log.error(err);\r
77       throw new TestExecutionException(err);\r
78     }\r
79     List<String> recipients = null;\r
80     try {\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
84         log.error(err);\r
85         throw new TestExecutionException(err);\r
86       }\r
87     } catch (Exception e) {\r
88       throw new TestExecutionException(e);\r
89     }\r
90 \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
94         log.error(err);\r
95         throw new TestExecutionException(err);\r
96       }\r
97     }\r
98 \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
103       log.error(err);\r
104       throw new TestExecutionException(err);\r
105     }\r
106 \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
111       log.error(err);\r
112       throw new TestExecutionException(err);\r
113     }\r
114 \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
119 \r
120     sendMail(recipients, subject, body, sender, processInstanceId, hTestInstanceId);\r
121   }\r
122 \r
123   public void sendMail(\r
124       List<String> recipients,\r
125       String subject,\r
126       String body,\r
127       String sender,\r
128       String processInstanceId,\r
129       String testInstanceId)\r
130       throws Exception {\r
131     // Get the system properties.\r
132     Properties properties = System.getProperties();\r
133 \r
134     // Set the SMTP host.\r
135     properties.setProperty("mail.smtp.host", "localhost");\r
136 \r
137     // creating session object to get properties\r
138     Session session = Session.getDefaultInstance(properties);\r
139 \r
140     try {\r
141       // MimeMessage object.\r
142       MimeMessage message = new MimeMessage(session);\r
143 \r
144       // Set From Field: adding senders email to from field.\r
145       message.setFrom(new InternetAddress("OTF_EMAIL-ALERT@localhost"));\r
146 \r
147       // Set Subject: subject of the email\r
148       message.setSubject(subject);\r
149 \r
150       // set body of the email.\r
151       StringBuffer sb = new StringBuffer();\r
152       sb.append("************************OTF Alerting System************************");\r
153       sb.append("\n\n");\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
157       sb.append("\n\n");\r
158       sb.append("******************************************************************");\r
159       sb.append("\n\n");\r
160       sb.append(body);\r
161 \r
162       message.setText(sb.toString());\r
163 \r
164       // Send email.\r
165       Transport.send(message);\r
166     } catch (MessagingException mex) {\r
167       mex.printStackTrace();\r
168     }\r
169   }\r
170 }\r