X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=auth%2Fauth-batch%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Faaf%2Fauth%2Fbatch%2Freports%2FNotify.java;h=7fd26747cea0f8da82c95bf807b58449ab1e7d21;hb=b24f7e097f17761a5c1b02c4dbfe6ee7d78836dd;hp=cb57497e80ec341f1920b1e12a68fcfcc14b49f2;hpb=adfe545fbfb2bf2279ed51137f8c86ca2fb35203;p=aaf%2Fauthz.git diff --git a/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/reports/Notify.java b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/reports/Notify.java index cb57497e..7fd26747 100644 --- a/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/reports/Notify.java +++ b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/reports/Notify.java @@ -19,16 +19,15 @@ * */package org.onap.aaf.auth.batch.reports; +import java.io.BufferedReader; import java.io.File; -import java.io.FileInputStream; +import java.io.FileReader; import java.io.IOException; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; -import java.util.HashMap; import java.util.HashSet; import java.util.List; -import java.util.Map; import java.util.Set; import org.onap.aaf.auth.batch.Batch; @@ -42,22 +41,29 @@ import org.onap.aaf.cadi.CadiException; import org.onap.aaf.cadi.client.Holder; import org.onap.aaf.cadi.util.CSV; import org.onap.aaf.misc.env.APIException; +import org.onap.aaf.misc.env.util.Chrono; public class Notify extends Batch { + private static final String HTML_CSS = "HTML_CSS"; private final Mailer mailer; - private final String mailFrom; private final String header; private final String footer; private List notifyFile; + public final String guiURL; + private int maxEmails; + private int indent; public Notify(AuthzTrans trans) throws APIException, IOException, OrganizationException { super(trans.env()); String mailerCls = env.getProperty("MAILER"); - mailFrom = env.getProperty("MAIL_FROM"); + String mailFrom = env.getProperty("MAIL_FROM"); String header_html = env.getProperty("HEADER_HTML"); String footer_html = env.getProperty("FOOTER_HTML"); - if(mailerCls==null || mailFrom==null || header_html==null || footer_html==null) { - throw new APIException("Notify requires MAILER, MAILER_FROM, HEADER_HTML and FOOTER_HTML properties"); + String maxEmails = env.getProperty("MAX_EMAIL"); + guiURL = env.getProperty("GUI_URL"); + this.maxEmails = maxEmails==null?1:Integer.parseInt(maxEmails); + if(mailerCls==null || mailFrom==null || guiURL==null || header_html==null || footer_html==null) { + throw new APIException("Notify requires MAILER, MAILER_FROM, GUI_URL, HEADER_HTML and FOOTER_HTML properties"); } try { Class mailc = Class.forName(mailerCls); @@ -67,34 +73,73 @@ public class Notify extends Batch { throw new APIException("Unable to construct " + mailerCls,e); } - FileInputStream fis = new FileInputStream(header_html); + String line; + StringBuilder sb = new StringBuilder(); + BufferedReader br = new BufferedReader(new FileReader(header_html)); try { - byte[] content = new byte[(int)fis.getChannel().size()]; - fis.read(content); - header = new String(content); + while((line=br.readLine())!=null) { + sb.append(line); + sb.append('\n'); + } + String html_css = env.getProperty(HTML_CSS); + int hc = sb.indexOf(HTML_CSS); + if(hc!=0 && html_css!=null) { + header = sb.replace(hc,hc+HTML_CSS.length(), html_css).toString(); + } else { + header = sb.toString(); + } } finally { - fis.close(); + br.close(); + } + + + + // Establish index from header + int lastTag = header.lastIndexOf('<'); + if(lastTag>0) { + int prevCR = header.lastIndexOf('\n',lastTag); + if(prevCR>0) { + indent = lastTag-prevCR; + } else { + indent = 6; //arbitrary + } } + - fis = new FileInputStream(footer_html); + sb.setLength(0); + br = new BufferedReader(new FileReader(footer_html)); try { - byte[] content = new byte[(int)fis.getChannel().size()]; - fis.read(content); - footer = new String(content); + while((line=br.readLine())!=null) { + sb.append(line); + sb.append('\n'); + } + footer = sb.toString(); } finally { - fis.close(); + br.close(); } // Class Load possible data NotifyBody.load(env.access()); // Create Intermediate Output - File logDir = new File(logDir()); + File logDir = logDir(); notifyFile = new ArrayList<>(); if(args().length>0) { for(int i=0;i errorSet = new HashSet<>(); try { + EMAILTYPE: for(File f : notifyFile) { - CSV csv = new CSV(f); + CSV csv = new CSV(env.access(),f); try { csv.visit(new CSV.Visitor() { @Override @@ -144,7 +190,6 @@ public class Notify extends Batch { toList.clear(); ccList.clear(); try { - String bodyS = nb.body(noAvg, notify, id); Identity identity = trans.org().getIdentity(noAvg, id); if(!identity.isPerson()) { identity = identity.responsibleTo(); @@ -159,11 +204,19 @@ public class Notify extends Batch { } } } + + StringBuilder content = new StringBuilder(); + content.append(String.format(header,version,Identity.mixedCase(identity.firstName()))); + + nb.body(noAvg, content, indent, notify, id); + content.append(footer); - mailer.sendEmail(noAvg, dryRun, mailFrom, toList, ccList, subject, - String.format(header,"2.1.9",Identity.mixedCase(identity.firstName()))+ - bodyS + - footer, urgent); + if(!mailer.sendEmail(noAvg, dryRun, toList, ccList, subject,content.toString(), urgent)) { + trans.error().log("Mailer failed to send Mail"); + } + if(maxEmails>0 && mailer.count()>=maxEmails) { + break EMAILTYPE; + } } catch (OrganizationException e) { trans.error().log(e); }