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