Batch work and client
[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                 int maxEmail = Integer.parseInt(access.getProperty("MAX_EMAIL", "-1"));
62                 if(dryrun && maxEmail==1) {
63                         testName = "email_test";
64                 } else {
65                         testName = null;
66                 }
67         }
68
69         @Override
70         public boolean sendEmail(AuthzTrans trans, String test, List<String> toList, List<String> ccList,
71                         String subject, String body, Boolean urgent) throws OrganizationException {
72                 boolean status = false;
73                 try {
74                         Path path;
75                         if(testName==null) {
76                                 path = Files.createTempFile(dir, "email", ".hdr");
77                         } else {
78                                 path = Paths.get(dir.toString(), "emailTEST"+test+".hdr");
79                         }
80                         BufferedWriter bw = Files.newBufferedWriter(path);
81                         try {
82                                 bw.write("TO: ");
83                                 boolean first = true;
84                                 for(String to : toList) {
85                                         if(first) {
86                                                 first = false;
87                                         } else {
88                                                 bw.write(',');
89                                         }
90                                         bw.write(to);
91                                 }
92                                 bw.newLine();
93                                 
94                                 bw.write("CC: ");
95                                 first = true;
96                                 for(String cc : ccList) {
97                                         if(first) {
98                                                 first = false;
99                                         } else {
100                                                 bw.write(',');
101                                         }
102                                         bw.write(cc);
103                                 }
104                                 bw.newLine();
105                                 
106                                 bw.write("FROM: ");
107                                 bw.write(mail_from);
108                                 bw.newLine();
109                                 
110                                 bw.write("SUBJECT: ");
111                                 bw.write(subject);
112                                 bw.newLine();
113                                 
114                                 if(urgent) {
115                                         bw.write("Importance: High");  
116                                         bw.newLine();
117                                 }
118
119                         } finally {
120                                 bw.close();
121                         }
122
123                         path = Paths.get(path.toString().replaceAll(".hdr", ".html"));
124                         bw = Files.newBufferedWriter(path);
125                         try {
126                                 bw.write(body);
127                                 bw.newLine();
128                         } finally {
129                                 bw.close();
130                         }
131                         status = true;
132                 } catch ( IOException e) {
133                         throw new OrganizationException(e);
134                 }
135                 ++count;
136                 return status;
137         }
138
139         @Override
140         public String mailFrom() {
141                 return mail_from;
142         }
143
144         @Override
145         public int count() {
146                 return count;
147         }
148 }