Merge "ActionPuntDAO.java: Fixed sonar issue"
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / javax / JavaxMailer.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 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  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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====================================================
19  *
20  */
21 package org.onap.aaf.auth.javax;
22
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import javax.mail.Address;
27 import javax.mail.Message;
28 import javax.mail.MessagingException;
29 import javax.mail.Session;
30 import javax.mail.Transport;
31 import javax.mail.internet.InternetAddress;
32 import javax.mail.internet.MimeMessage;
33
34 import org.onap.aaf.auth.env.AuthzTrans;
35 import org.onap.aaf.auth.org.Mailer;
36 import org.onap.aaf.auth.org.OrganizationException;
37
38 public class JavaxMailer implements Mailer {
39     private Session session;
40
41       public JavaxMailer() {
42           
43             // Get the default Session object.
44             session = Session.getDefaultInstance(System.getProperties());
45     
46       }
47       
48       @Override
49         public int sendEmail(AuthzTrans trans, boolean testMode, String mailFrom, List<String> to, List<String> cc, String subject, String body,
50                 Boolean urgent) throws OrganizationException {
51
52             int status = 1;
53
54
55             try {
56                 // Create a default MimeMessage object.
57                 MimeMessage message = new MimeMessage(session);
58
59                 // Set From: header field of the header.
60                 message.setFrom(new InternetAddress(mailFrom));
61
62                 if (!testMode) {
63                     // Set To: header field of the header. This is a required field
64                     // and calling module should make sure that it is not null or
65                     // blank
66                     message.addRecipients(Message.RecipientType.TO,getAddresses(to));
67
68                     // Set CC: header field of the header.
69                     if ((cc != null) && (cc.size() > 0)) {
70                         message.addRecipients(Message.RecipientType.CC,getAddresses(cc));
71                     }
72
73                     // Set Subject: header field
74                     message.setSubject(subject);
75
76                     if (urgent) {
77                         message.addHeader("X-Priority", "1");
78                     }
79
80                     // Now set the actual message
81                     message.setText(body);
82                 } else {
83
84                     // override recipients
85                     message.addRecipients(Message.RecipientType.TO,
86                             InternetAddress.parse(mailFrom));
87
88                     // Set Subject: header field
89                     message.setSubject("[TESTMODE] " + subject);
90
91                     if (urgent) {
92                         message.addHeader("X-Priority", "1");
93                     }
94
95                     ArrayList<String> newBody = new ArrayList<>();
96
97                     Address temp[] = getAddresses(to);
98                     String headerString = "TO:\t" + InternetAddress.toString(temp) + "\n";
99
100                     temp = getAddresses(cc);
101                     headerString += "CC:\t" + InternetAddress.toString(temp) + "\n";
102
103                     newBody.add(headerString);
104
105                     newBody.add("Text: \n");
106
107                     newBody.add(body);
108                     String outString = "";
109                     for (String s : newBody) {
110                         outString += s + "\n";
111                     }
112
113                     message.setText(outString);
114                 }
115                 // Send message
116                 Transport.send(message);
117                 status = 0;
118
119             } catch (MessagingException mex) {
120                 System.out.println("Error messaging: "+ mex.getMessage());
121                 System.out.println("Error messaging: "+ mex.toString());
122                 throw new OrganizationException("Exception send email message "
123                         + mex.getMessage());
124             }
125
126             return status;
127         }
128
129         /**
130          * Convert the delimiter String into Internet addresses with the default
131          * delimiter of ";"
132          * @param strAddress
133          * @return
134          */
135         private Address[] getAddresses(List<String> strAddress) throws OrganizationException {
136             return this.getAddresses(strAddress,";");
137         }
138         /**
139          * Convert the delimiter String into Internet addresses with the
140          * delimiter of provided
141          * @param strAddresses
142          * @param delimiter
143          * @return
144          */
145         private Address[] getAddresses(List<String> strAddresses, String delimiter) throws OrganizationException {
146             Address[] addressArray = new Address[strAddresses.size()];
147             int count = 0;
148             for (String addr : strAddresses)
149             {
150                 try{
151                     addressArray[count] = new InternetAddress(addr);
152                     count++;
153                 } catch (Exception e){
154                     throw new OrganizationException("Failed to parse the email address "+ addr +": "+e.getMessage());
155                 }
156             }
157             return addressArray;
158         }
159
160 }