diff options
author | Instrumental <jonathan.gathman@att.com> | 2019-02-10 09:54:15 -0600 |
---|---|---|
committer | Instrumental <jonathan.gathman@att.com> | 2019-02-10 10:09:21 -0600 |
commit | b24f7e097f17761a5c1b02c4dbfe6ee7d78836dd (patch) | |
tree | 74d883936a21a07a2e0c718ec6a7f456b4d18cab /auth/auth-core/src/main | |
parent | 39596f5b6d2c67d8c2b357243ecfb2dd6d746796 (diff) |
Add Cred Reporting Mailer
Issue-ID: AAF-740,AAF-753,AAF-754
Change-Id: If2efc6ffbfa9897581ea00eb148fa61d793b058e
Signed-off-by: Instrumental <jonathan.gathman@att.com>
Diffstat (limited to 'auth/auth-core/src/main')
-rw-r--r-- | auth/auth-core/src/main/java/org/onap/aaf/auth/org/FileMailer.java | 148 | ||||
-rw-r--r-- | auth/auth-core/src/main/java/org/onap/aaf/auth/org/Mailer.java | 5 |
2 files changed, 151 insertions, 2 deletions
diff --git a/auth/auth-core/src/main/java/org/onap/aaf/auth/org/FileMailer.java b/auth/auth-core/src/main/java/org/onap/aaf/auth/org/FileMailer.java new file mode 100644 index 00000000..e5fc4387 --- /dev/null +++ b/auth/auth-core/src/main/java/org/onap/aaf/auth/org/FileMailer.java @@ -0,0 +1,148 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ +package org.onap.aaf.auth.org; + +import java.io.BufferedWriter; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; + +import org.onap.aaf.auth.env.AuthzTrans; +import org.onap.aaf.cadi.Access; +import org.onap.aaf.misc.env.APIException; +import org.onap.aaf.misc.env.util.Chrono; + +public class FileMailer implements Mailer { + private Path dir; + private String mail_from; + private String testName; + private int count; + + + public FileMailer(Access access) throws APIException { + count = 0; + + mail_from = access.getProperty("MAIL_FROM", null); + if(mail_from==null) { + throw new APIException("MAIL_FROM property is required for Email Notifications"); + } + String env = access.getProperty("CASS_ENV", "UNKNOWN"); + String logdir = access.getProperty(env+".LOG_DIR", "logs/"+env); + dir = Paths.get(logdir+"/email/"+Chrono.dateOnlyStamp()); + if(!Files.exists(dir)) { + try { + Files.createDirectories(dir); + } catch (IOException e) { + throw new APIException("Cannot create directory: " + dir.toString(),e); + } + } + + boolean dryrun = Boolean.parseBoolean(access.getProperty("DRY_RUN","false")); + int maxEmail = Integer.parseInt(access.getProperty("MAX_EMAIL", "-1")); + if(dryrun && maxEmail==1) { + testName = "email_test"; + } else { + testName = null; + } + } + + @Override + public boolean sendEmail(AuthzTrans trans, boolean testMode, List<String> toList, List<String> ccList, + String subject, String body, Boolean urgent) throws OrganizationException { + boolean status = false; + try { + Path path; + if(testName==null) { + path = Files.createTempFile(dir, "email", ".hdr"); + } else { + path = Paths.get(dir.toString(), "emailTEST.hdr"); + } + BufferedWriter bw = Files.newBufferedWriter(path); + try { + bw.write("TO: "); + boolean first = true; + for(String to : toList) { + if(first) { + first = false; + } else { + bw.write(','); + } + bw.write(to); + } + bw.newLine(); + + bw.write("CC: "); + first = true; + for(String cc : ccList) { + if(first) { + first = false; + } else { + bw.write(','); + } + bw.write(cc); + } + bw.newLine(); + + bw.write("FROM: "); + bw.write(mail_from); + bw.newLine(); + + bw.write("SUBJECT: "); + bw.write(subject); + bw.newLine(); + + if(urgent) { + bw.write("Importance: High"); + bw.newLine(); + } + + } finally { + bw.close(); + } + + path = Paths.get(path.toString().replaceAll(".hdr", ".html")); + bw = Files.newBufferedWriter(path); + try { + bw.write(body); + bw.newLine(); + } finally { + bw.close(); + } + status = true; + } catch ( IOException e) { + throw new OrganizationException(e); + } + ++count; + return status; + } + + @Override + public String mailFrom() { + return mail_from; + } + + @Override + public int count() { + return count; + } +} diff --git a/auth/auth-core/src/main/java/org/onap/aaf/auth/org/Mailer.java b/auth/auth-core/src/main/java/org/onap/aaf/auth/org/Mailer.java index 1f1c28b8..f7c8b480 100644 --- a/auth/auth-core/src/main/java/org/onap/aaf/auth/org/Mailer.java +++ b/auth/auth-core/src/main/java/org/onap/aaf/auth/org/Mailer.java @@ -25,10 +25,9 @@ import java.util.List; import org.onap.aaf.auth.env.AuthzTrans; public interface Mailer { - public int sendEmail( + public boolean sendEmail( AuthzTrans trans, boolean testMode, - String mailFrom, List<String> toList, List<String> ccList, String subject, @@ -37,4 +36,6 @@ public interface Mailer { public String mailFrom(); + public int count(); + } |