update the testcases after the kafka 11 changes
[dmaap/messagerouter/msgrtr.git] / src / main / java / com / att / dmf / mr / utils / Emailer.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
4  *  ================================================================================
5  *  Copyright © 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
10  *        http://www.apache.org/licenses/LICENSE-2.0
11 *  
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *  ============LICENSE_END=========================================================
18  *  
19  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  *  
21  *******************************************************************************/
22 package com.att.dmf.mr.utils;
23
24 import java.io.IOException;
25 import java.util.Properties;
26 import java.util.concurrent.ExecutorService;
27 import java.util.concurrent.Executors;
28
29 import javax.mail.BodyPart;
30 import javax.mail.Message;
31 import javax.mail.Multipart;
32 import javax.mail.PasswordAuthentication;
33 import javax.mail.Session;
34 import javax.mail.Transport;
35 import javax.mail.internet.InternetAddress;
36 import javax.mail.internet.MimeBodyPart;
37 import javax.mail.internet.MimeMessage;
38 import javax.mail.internet.MimeMultipart;
39
40 //import org.slf4j.Logger;
41 //import org.slf4j.LoggerFactory;
42
43 import com.att.ajsc.filemonitor.AJSCPropertiesMap;
44 import com.att.dmf.mr.constants.CambriaConstants;
45 import com.att.eelf.configuration.EELFLogger;
46 import com.att.eelf.configuration.EELFManager;
47
48 /**
49  * Send an email from a message.
50  * 
51  * @author peter
52  */
53 public class Emailer
54 {
55         public static final String kField_To = "to";
56         public static final String kField_Subject = "subject";
57         public static final String kField_Message = "message";
58
59         public Emailer()
60         {
61                 fExec = Executors.newCachedThreadPool ();
62         //      fSettings = settings;
63         }
64
65         public void send ( String to, String subj, String body ) throws IOException
66         {
67                 final String[] addrs = to.split ( "," );
68
69                 if ( to.length () > 0 )
70                 {
71                         final MailTask mt = new MailTask ( addrs, subj, body );
72                         fExec.submit ( mt );
73                 }
74                 else
75                 {
76                         log.warn ( "At least one address is required." );
77                 }
78         }
79
80         public void close ()
81         {
82                 fExec.shutdown ();
83         }
84
85         private final ExecutorService fExec;
86         //private final rrNvReadable fSettings;
87
88         //private static final Logger log = LoggerFactory.getLogger ( Emailer.class );
89
90         private static final EELFLogger log = EELFManager.getInstance().getLogger(Emailer.class);
91         
92         public static final String kSetting_MailAuthUser = "mailLogin";
93         public static final String kSetting_MailAuthPwd = "mailPassword";
94         public static final String kSetting_MailFromEmail = "mailFromEmail";
95         public static final String kSetting_MailFromName = "mailFromName";
96         public static final String kSetting_SmtpServer = "mailSmtpServer";
97         public static final String kSetting_SmtpServerPort = "mailSmtpServerPort";
98         public static final String kSetting_SmtpServerSsl = "mailSmtpServerSsl";
99         public static final String kSetting_SmtpServerUseAuth = "mailSmtpServerUseAuth";
100
101         private class MailTask implements Runnable
102         {
103                 public MailTask ( String[] to, String subject, String msgBody )
104                 {
105                         fToAddrs = to;
106                         fSubject = subject;
107                         fBody = msgBody;
108                 }
109
110                 private String getSetting ( String settingKey, String defval )
111                 {
112                         //return fSettings.getString ( settingKey, defval );
113                         String strSet = AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop,settingKey);
114                         if(strSet==null)strSet=defval;
115                         return strSet;
116                 }
117
118                 // we need to get setting values from the evaluator but also the channel config
119                 private void makeSetting ( Properties props, String propKey, String settingKey, String defval )
120                 {
121                         props.put ( propKey, getSetting ( settingKey, defval ) );
122                 }
123
124                 private void makeSetting ( Properties props, String propKey, String settingKey, int defval )
125                 {
126                         makeSetting ( props, propKey, settingKey, "" + defval );
127                 }
128
129                 private void makeSetting ( Properties props, String propKey, String settingKey, boolean defval )
130                 {
131                         makeSetting ( props, propKey, settingKey, "" + defval );
132                 }
133
134                 @Override
135                 public void run ()
136                 {
137                         final StringBuffer tag = new StringBuffer ();
138                         final StringBuffer addrList = new StringBuffer ();
139                         tag.append ( "(" );
140                         for ( String to : fToAddrs )
141                         {
142                                 if ( addrList.length () > 0 )
143                                 {
144                                         addrList.append ( ", " );
145                                 }
146                                 addrList.append ( to );
147                         }
148                         tag.append ( addrList.toString () );
149                         tag.append ( ") \"" );
150                         tag.append ( fSubject );
151                         tag.append ( "\"" );
152                         
153                         log.info ( "sending mail to " + tag );
154
155                         try
156                         {
157                                 final Properties prop = new Properties ();
158                                 makeSetting ( prop, "mail.smtp.port", kSetting_SmtpServerPort, 587 );
159                                 prop.put ( "mail.smtp.socketFactory.fallback", "false" );
160                                 prop.put ( "mail.smtp.quitwait", "false" );
161                                 makeSetting ( prop, "mail.smtp.host", kSetting_SmtpServer, "smtp.it.att.com" );
162                                 makeSetting ( prop, "mail.smtp.auth", kSetting_SmtpServerUseAuth, true );
163                                 makeSetting ( prop, "mail.smtp.starttls.enable", kSetting_SmtpServerSsl, true );
164
165                                 final String un = getSetting ( kSetting_MailAuthUser, "" );
166                                 final String pw = getSetting ( kSetting_MailAuthPwd, "" );
167                                 final Session session = Session.getInstance ( prop,
168                                         new javax.mail.Authenticator()
169                                         {
170                                                 @Override
171                                                 protected PasswordAuthentication getPasswordAuthentication()
172                                                 {
173                                                         return new PasswordAuthentication ( un, pw );
174                                                 }
175                                         }
176                                 );
177                                 
178                                 final Message msg = new MimeMessage ( session );
179
180                                 final InternetAddress from = new InternetAddress (
181                                         getSetting ( kSetting_MailFromEmail, "team@sa2020.it.att.com" ),
182                                         getSetting ( kSetting_MailFromName, "The GFP/SA2020 Team" ) );
183                                 msg.setFrom ( from );
184                                 msg.setReplyTo ( new InternetAddress[] { from } );
185                                 msg.setSubject ( fSubject );
186
187                                 for ( String toAddr : fToAddrs )
188                                 {
189                                         final InternetAddress to = new InternetAddress ( toAddr );
190                                         msg.addRecipient ( Message.RecipientType.TO, to );
191                                 }
192
193                                 final Multipart multipart = new MimeMultipart ( "related" );
194                                 final BodyPart htmlPart = new MimeBodyPart ();
195                                 htmlPart.setContent ( fBody, "text/plain" );
196                                 multipart.addBodyPart ( htmlPart );
197                                 msg.setContent ( multipart );
198
199                                 Transport.send ( msg );
200
201                                 log.info ( "mailing " + tag + " off without error" );
202                         }
203                         catch ( Exception e )
204                         {
205                                 log.warn ( "Exception caught for " + tag, e );
206                         }
207                 }
208
209                 private final String[] fToAddrs;
210                 private final String fSubject;
211                 private final String fBody;
212         }
213 }