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