Improve Batches
[aaf/authz.git] / auth / auth-core / src / main / java / org / onap / aaf / auth / org / FileMailer.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.org;
22
23 import java.io.BufferedWriter;
24 import java.io.IOException;
25 import java.nio.file.Files;
26 import java.nio.file.Path;
27 import java.nio.file.Paths;
28 import java.util.List;
29
30 import org.onap.aaf.auth.env.AuthzTrans;
31 import org.onap.aaf.cadi.Access;
32 import org.onap.aaf.misc.env.APIException;
33 import org.onap.aaf.misc.env.util.Chrono;
34
35 public class FileMailer implements Mailer {
36         private Path dir;
37         private String mail_from;
38         private String testName;
39         private int count;
40
41
42         public FileMailer(Access access) throws APIException {
43                 count = 0;
44                 
45                 mail_from = access.getProperty("MAIL_FROM", null);
46                 if(mail_from==null) {
47                         throw new APIException("MAIL_FROM property is required for Email Notifications");
48                 }
49                 String env = access.getProperty("CASS_ENV", "UNKNOWN");
50                 String logdir = access.getProperty(env+".LOG_DIR", "logs/"+env);
51                 dir = Paths.get(logdir+"/email/"+Chrono.dateOnlyStamp());
52                 if(!Files.exists(dir)) {
53                         try {
54                                 Files.createDirectories(dir);
55                         } catch (IOException e) {
56                                 throw new APIException("Cannot create directory: " + dir.toString(),e);
57                         }
58                 }
59                 
60                 boolean dryrun = Boolean.parseBoolean(access.getProperty("DRY_RUN","false"));
61                 String str = access.getProperty("MAX_EMAIL", null);
62                 int maxEmail = str==null || str.isEmpty()?Integer.MAX_VALUE:Integer.parseInt(str);
63                 if(dryrun && maxEmail==1) {
64                         testName = "email_test";
65                 } else {
66                         testName = null;
67                 }
68         }
69
70         @Override
71         public boolean sendEmail(AuthzTrans trans, String test, List<String> toList, List<String> ccList,
72                         String subject, String body, Boolean urgent) throws OrganizationException {
73                 boolean status = false;
74                 try {
75                         Path path;
76                         if(testName==null) {
77                                 path = Files.createTempFile(dir, "email", ".hdr");
78                         } else {
79                                 path = Paths.get(dir.toString(), "emailTEST"+test+".hdr");
80                         }
81                         BufferedWriter bw = Files.newBufferedWriter(path);
82                         try {
83                                 bw.write("TO: ");
84                                 boolean first = true;
85                                 for(String to : toList) {
86                                         if(first) {
87                                                 first = false;
88                                         } else {
89                                                 bw.write(',');
90                                         }
91                                         bw.write(to);
92                                 }
93                                 bw.newLine();
94                                 
95                                 bw.write("CC: ");
96                                 first = true;
97                                 for(String cc : ccList) {
98                                         if(first) {
99                                                 first = false;
100                                         } else {
101                                                 bw.write(',');
102                                         }
103                                         bw.write(cc);
104                                 }
105                                 bw.newLine();
106                                 
107                                 bw.write("FROM: ");
108                                 bw.write(mail_from);
109                                 bw.newLine();
110                                 
111                                 bw.write("SUBJECT: ");
112                                 bw.write(subject);
113                                 bw.newLine();
114                                 
115                                 if(urgent) {
116                                         bw.write("Importance: High");  
117                                         bw.newLine();
118                                 }
119
120                         } finally {
121                                 bw.close();
122                         }
123
124                         path = Paths.get(path.toString().replaceAll(".hdr", ".html"));
125                         bw = Files.newBufferedWriter(path);
126                         try {
127                                 bw.write(body);
128                                 bw.newLine();
129                         } finally {
130                                 bw.close();
131                         }
132                         status = true;
133                 } catch ( IOException e) {
134                         throw new OrganizationException(e);
135                 }
136                 ++count;
137                 return status;
138         }
139
140         @Override
141         public String mailFrom() {
142                 return mail_from;
143         }
144
145         @Override
146         public int count() {
147                 return count;
148         }
149 }