2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.policy.admin;
28 import java.io.UnsupportedEncodingException;
29 import java.text.DateFormat;
30 import java.text.SimpleDateFormat;
31 import java.util.Date;
32 import java.util.List;
33 import java.util.Properties;
35 import javax.mail.MessagingException;
36 import javax.mail.internet.InternetAddress;
37 import javax.mail.internet.MimeMessage;
39 import org.openecomp.policy.controller.PolicyController;
40 import org.openecomp.policy.dao.WatchPolicyNotificationDao;
41 import org.openecomp.policy.rest.jpa.PolicyVersion;
42 import org.openecomp.policy.rest.jpa.WatchPolicyNotificationTable;
43 import org.springframework.beans.factory.annotation.Configurable;
44 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
45 import org.springframework.context.annotation.Bean;
46 import org.springframework.mail.javamail.JavaMailSenderImpl;
47 import org.springframework.mail.javamail.MimeMessageHelper;
49 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
51 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
52 import org.openecomp.policy.common.logging.flexlogger.Logger;
55 public class PolicyNotificationMail{
56 private static Logger logger = FlexLogger.getLogger(PolicyNotificationMail.class);
59 public JavaMailSenderImpl javaMailSenderImpl(){
60 JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
61 mailSender.setHost(PolicyController.smtpHost);
62 mailSender.setPort(Integer.parseInt(PolicyController.smtpPort));
63 mailSender.setUsername(PolicyController.smtpUsername);
64 mailSender.setPassword(PolicyController.smtpPassword);
65 Properties prop = mailSender.getJavaMailProperties();
66 prop.put("mail.transport.protocol", "smtp");
67 prop.put("mail.smtp.auth", "true");
68 prop.put("mail.smtp.starttls.enable", "true");
69 prop.put("mail.debug", "true");
73 @SuppressWarnings("resource")
74 public void sendMail(PolicyVersion entityItem, String policyName, String mode, WatchPolicyNotificationDao policyNotificationDao) throws MessagingException {
75 String from = PolicyController.smtpUsername;
79 DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
80 Date date = new Date();
81 if(mode.equalsIgnoreCase("EditPolicy")){
82 subject = "Policy has been Updated : "+entityItem.getPolicyName();
83 message = "The Policy Which you are watching in " + PolicyController.smtpApplicationName + " has been Updated" + '\n' + '\n' + '\n'+ "Scope + Policy Name : " + policyName + '\n' + "Active Version : " +entityItem.getActiveVersion()
84 + '\n' + '\n' + "Modified By : " +entityItem.getModifiedBy() + '\n' + "Modified Time : " +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + "Policy Notification System (please don't respond to this email)";
86 if(mode.equalsIgnoreCase("Rename")){
87 subject = "Policy has been Renamed : "+entityItem.getPolicyName();
88 message = "The Policy Which you are watching in " + PolicyController.smtpApplicationName + " has been Renamed" + '\n' + '\n' + '\n'+ "Scope + Policy Name : " + policyName + '\n' + "Active Version : " +entityItem.getActiveVersion()
89 + '\n' + '\n' + "Renamed By : " +entityItem.getModifiedBy() + '\n' + "Renamed Time : " +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + "Policy Notification System (please don't respond to this email)";
91 if(mode.equalsIgnoreCase("DeleteAll")){
92 subject = "Policy has been Deleted : "+entityItem.getPolicyName();
93 message = "The Policy Which you are watching in " + PolicyController.smtpApplicationName + " has been Deleted with All Versions" + '\n' + '\n' + '\n'+ "Scope + Policy Name : " + policyName + '\n'
94 + '\n' + '\n' + "Deleted By : " +entityItem.getModifiedBy() + '\n' + "Deleted Time : " +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + "Policy Notification System (please don't respond to this email)";
96 if(mode.equalsIgnoreCase("DeleteOne")){
97 subject = "Policy has been Deleted : "+entityItem.getPolicyName();
98 message = "The Policy Which you are watching in " + PolicyController.smtpApplicationName + " has been Deleted" + '\n' + '\n' + '\n'+ "Scope + Policy Name : " + policyName + '\n' +"Policy Version : " +entityItem.getActiveVersion()
99 + '\n' + '\n' + "Deleted By : " +entityItem.getModifiedBy() + '\n' + "Deleted Time : " +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + "Policy Notification System (please don't respond to this email)";
101 if(mode.equalsIgnoreCase("DeleteScope")){
102 subject = "Scope has been Deleted : "+entityItem.getPolicyName();
103 message = "The Scope Which you are watching in " + PolicyController.smtpApplicationName + " has been Deleted" + '\n' + '\n' + '\n'+ "Scope + Scope Name : " + policyName + '\n'
104 + '\n' + '\n' + "Deleted By : " +entityItem.getModifiedBy() + '\n' + "Deleted Time : " +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + "Policy Notification System (please don't respond to this email)";
106 if(mode.equalsIgnoreCase("SwitchVersion")){
107 subject = "Policy has been SwitchedVersion : "+entityItem.getPolicyName();
108 message = "The Policy Which you are watching in " + PolicyController.smtpApplicationName + " has been SwitchedVersion" + '\n' + '\n' + '\n'+ "Scope + Policy Name : " + policyName + '\n' + "Active Version : " +entityItem.getActiveVersion()
109 + '\n' + '\n' + "Switched By : " +entityItem.getModifiedBy() + '\n' + "Switched Time : " +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + "Policy Notification System (please don't respond to this email)";
111 if(mode.equalsIgnoreCase("Move")){
112 subject = "Policy has been Moved to Other Scope : "+entityItem.getPolicyName();
113 message = "The Policy Which you are watching in " + PolicyController.smtpApplicationName + " has been Moved to Other Scope" + '\n' + '\n' + '\n'+ "Scope + Policy Name : " + policyName + '\n' + "Active Version : " +entityItem.getActiveVersion()
114 + '\n' + '\n' + "Moved By : " +entityItem.getModifiedBy() + '\n' + "Moved Time : " +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + "Policy Notification System (please don't respond to this email)";
116 String policyFileName = entityItem.getPolicyName();
117 List<WatchPolicyNotificationTable> watchList = policyNotificationDao.getListDataByPolicyName(policyFileName);
118 if(watchList.size() > 0){
119 for(WatchPolicyNotificationTable list : watchList){
120 to = list.getLoginIds()+"@"+PolicyController.smtpEmailExtension;
122 AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
123 ctx.register(PolicyNotificationMail.class);
125 JavaMailSenderImpl mailSender = ctx.getBean(JavaMailSenderImpl.class);
126 MimeMessage mimeMessage = mailSender.createMimeMessage();
127 MimeMessageHelper mailMsg = new MimeMessageHelper(mimeMessage);
129 mailMsg.setFrom(new InternetAddress(from, "Policy Notification System"));
130 } catch (UnsupportedEncodingException e) {
131 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Exception Occured in Policy Notification" +e);
134 mailMsg.setSubject(subject);
135 mailMsg.setText(message);
136 mailSender.send(mimeMessage);