X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=auth%2Fauth-core%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Faaf%2Fauth%2Forg%2FFileMailer.java;h=8896ca870a4965396e74d949f161052ca036f79b;hb=HEAD;hp=1db1198522d64c44535830b8cbe38de4bce62b15;hpb=628b7105ce4d9818aac69a082e515f9275fd46fd;p=aaf%2Fauthz.git 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 index 1db11985..8896ca87 100644 --- 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 @@ -7,9 +7,9 @@ * 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. @@ -33,116 +33,120 @@ 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, String test, List toList, List 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"+test+".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; - } + 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("LOG_DIR",null); + if(logdir==null) { + 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")); + String str = access.getProperty("MAX_EMAIL", null); + int maxEmail = str==null || str.isEmpty()?Integer.MAX_VALUE:Integer.parseInt(str); + if(dryrun && maxEmail==1) { + testName = "email_test"; + } else { + testName = null; + } + } + + @Override + public boolean sendEmail(AuthzTrans trans, String test, List toList, List 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"+test+".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; + } }