DMAAP-MR - Merge MR repos
[dmaap/messagerouter/messageservice.git] / src / main / java / org / onap / dmaap / 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 org.onap.dmaap.dmf.mr.utils;
23
24 import com.att.ajsc.filemonitor.AJSCPropertiesMap;
25 import com.att.eelf.configuration.EELFLogger;
26 import com.att.eelf.configuration.EELFManager;
27 import org.onap.dmaap.dmf.mr.constants.CambriaConstants;
28
29 import javax.mail.*;
30 import javax.mail.internet.InternetAddress;
31 import javax.mail.internet.MimeBodyPart;
32 import javax.mail.internet.MimeMessage;
33 import javax.mail.internet.MimeMultipart;
34 import java.io.IOException;
35 import java.util.Properties;
36 import java.util.concurrent.ExecutorService;
37 import java.util.concurrent.Executors;
38
39 /**
40  * Send an email from a message.
41  * 
42  * @author peter
43  */
44 public class Emailer
45 {
46         public static final String kField_To = "to";
47         public static final String kField_Subject = "subject";
48         public static final String kField_Message = "message";
49
50         public Emailer()
51         {
52                 fExec = Executors.newCachedThreadPool ();
53         
54         }
55
56         public void send ( String to, String subj, String body ) throws IOException
57         {
58                 final String[] addrs = to.split ( "," );
59
60                 if ( to.length () > 0 )
61                 {
62                         final MailTask mt = new MailTask ( addrs, subj, body );
63                         fExec.submit ( mt );
64                 }
65                 else
66                 {
67                         log.warn ( "At least one address is required." );
68                 }
69         }
70
71         public void close ()
72         {
73                 fExec.shutdown ();
74         }
75
76         private final ExecutorService fExec;
77         
78
79         
80
81         private static final EELFLogger log = EELFManager.getInstance().getLogger(Emailer.class);
82         
83         public static final String kSetting_MailAuthUser = "mailLogin";
84         public static final String kSetting_MailFromEmail = "mailFromEmail";
85         public static final String kSetting_MailFromName = "mailFromName";
86         public static final String kSetting_SmtpServer = "mailSmtpServer";
87         public static final String kSetting_SmtpServerPort = "mailSmtpServerPort";
88         public static final String kSetting_SmtpServerSsl = "mailSmtpServerSsl";
89         public static final String kSetting_SmtpServerUseAuth = "mailSmtpServerUseAuth";
90
91         private class MailTask implements Runnable
92         {
93                 public MailTask ( String[] to, String subject, String msgBody )
94                 {
95                         fToAddrs = to;
96                         fSubject = subject;
97                         fBody = msgBody;
98                 }
99
100                 private String getSetting ( String settingKey, String defval )
101                 {
102                         
103                         String strSet = AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop,settingKey);
104                         if(strSet==null)strSet=defval;
105                         return strSet;
106                 }
107
108                 // we need to get setting values from the evaluator but also the channel config
109                 private void makeSetting ( Properties props, String propKey, String settingKey, String defval )
110                 {
111                         props.put ( propKey, getSetting ( settingKey, defval ) );
112                 }
113
114                 private void makeSetting ( Properties props, String propKey, String settingKey, int defval )
115                 {
116                         makeSetting ( props, propKey, settingKey, "" + defval );
117                 }
118
119                 private void makeSetting ( Properties props, String propKey, String settingKey, boolean defval )
120                 {
121                         makeSetting ( props, propKey, settingKey, "" + defval );
122                 }
123
124                 @Override
125                 public void run ()
126                 {
127                         final StringBuffer tag = new StringBuffer ();
128                         final StringBuffer addrList = new StringBuffer ();
129                         tag.append ( "(" );
130                         for ( String to : fToAddrs )
131                         {
132                                 if ( addrList.length () > 0 )
133                                 {
134                                         addrList.append ( ", " );
135                                 }
136                                 addrList.append ( to );
137                         }
138                         tag.append ( addrList.toString () );
139                         tag.append ( ") \"" );
140                         tag.append ( fSubject );
141                         tag.append ( "\"" );
142                         
143                         log.info ( "sending mail to " + tag );
144
145                         try
146                         {
147                                 final Properties prop = new Properties ();
148                                 makeSetting ( prop, "mail.smtp.port", kSetting_SmtpServerPort, 587 );
149                                 prop.put ( "mail.smtp.socketFactory.fallback", "false" );
150                                 prop.put ( "mail.smtp.quitwait", "false" );
151                                 makeSetting ( prop, "mail.smtp.host", kSetting_SmtpServer, "smtp.it.onap.com" );
152                                 makeSetting ( prop, "mail.smtp.auth", kSetting_SmtpServerUseAuth, true );
153                                 makeSetting ( prop, "mail.smtp.starttls.enable", kSetting_SmtpServerSsl, true );
154
155                                 final String un = getSetting ( kSetting_MailAuthUser, "" );
156                                 final String value=(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop,"mailPassword")!=null)?AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop,"mailPassword"):"";
157                                 final Session session = Session.getInstance ( prop,
158                                         new javax.mail.Authenticator()
159                                         {
160                                                 @Override
161                                                 protected PasswordAuthentication getPasswordAuthentication()
162                                                 {
163                                                         return new PasswordAuthentication ( un, value );
164                                                 }
165                                         }
166                                 );
167                                 
168                                 final Message msg = new MimeMessage ( session );
169
170                                 final InternetAddress from = new InternetAddress (
171                                         getSetting ( kSetting_MailFromEmail, "team@dmaap.mr.onap.com" ),
172                                         getSetting ( kSetting_MailFromName, "The GFP/SA2020 Team" ) );
173                                 msg.setFrom ( from );
174                                 msg.setReplyTo ( new InternetAddress[] { from } );
175                                 msg.setSubject ( fSubject );
176
177                                 for ( String toAddr : fToAddrs )
178                                 {
179                                         final InternetAddress to = new InternetAddress ( toAddr );
180                                         msg.addRecipient ( Message.RecipientType.TO, to );
181                                 }
182
183                                 final Multipart multipart = new MimeMultipart ( "related" );
184                                 final BodyPart htmlPart = new MimeBodyPart ();
185                                 htmlPart.setContent ( fBody, "text/plain" );
186                                 multipart.addBodyPart ( htmlPart );
187                                 msg.setContent ( multipart );
188
189                                 Transport.send ( msg );
190
191                                 log.info ( "mailing " + tag + " off without error" );
192                         }
193                         catch ( Exception e )
194                         {
195                                 log.warn ( "Exception caught for " + tag, e );
196                         }
197                 }
198
199                 private final String[] fToAddrs;
200                 private final String fSubject;
201                 private final String fBody;
202         }
203 }