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