2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.auth.actions;
24 import java.io.PrintStream;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.List;
29 import org.onap.aaf.auth.env.AuthzTrans;
30 import org.onap.aaf.auth.layer.Result;
31 import org.onap.aaf.auth.org.Organization;
32 import org.onap.aaf.auth.org.OrganizationException;
33 import org.onap.aaf.auth.org.Organization.Identity;
34 import org.onap.aaf.misc.env.util.Chrono;
36 public class Email implements Action<Organization,Void, String>{
37 protected final List<String> toList;
38 protected final List<String> ccList;
39 private final String[] defaultCC;
40 protected String subject;
41 private String preamble;
44 protected String lineIndent=" ";
45 private long lastSent=0L;
48 public Email(String ... defaultCC) {
49 toList = new ArrayList<>();
50 this.defaultCC = defaultCC;
51 ccList = new ArrayList<>();
55 public Email clear() {
58 for(String s: defaultCC) {
65 public void indent(String indent) {
69 public void preamble(String format, Object ... args) {
70 preamble = String.format(format, args);
73 public Email addTo(Identity id) {
74 if(id!=null && !toList.contains(id.email())) {
75 toList.add(id.email());
80 public Email addTo(Collection<String> users) {
81 for(String u : users) {
87 public Email addTo(String email) {
88 if(!toList.contains(email)) {
94 public Email addCC(Identity id) {
95 if(id!=null && !ccList.contains(id.email())) {
96 ccList.add(id.email());
101 public Email addCC(String email) {
102 if(!ccList.contains(email)) {
109 public Email add(Identity id, boolean toSuper) throws OrganizationException {
110 Identity responsible = id.responsibleTo();
112 addTo(responsible.email());
115 addCC(responsible.email());
121 public Email subject(String format, Object ... args) {
122 if(format.contains("%s")) {
123 subject = String.format(format, args);
131 public Email signature(String format, Object ... args) {
132 sig = String.format(format, args);
136 public void msg(Message msg) {
141 public Result<Void> exec(AuthzTrans trans, Organization org, String text) {
142 StringBuilder sb = new StringBuilder();
144 sb.append(lineIndent);
150 msg.msg(sb,lineIndent);
159 long ct = System.currentTimeMillis();
160 long wait = ct-lastSent;
162 if(wait < 100) { // 10 per second
165 } catch (InterruptedException e) {
166 Thread.currentThread().interrupt();
169 return exec(trans,org,sb);
172 protected Result<Void> exec(AuthzTrans trans, Organization org, StringBuilder sb) {
181 } catch (Exception e) {
182 return Result.err(Result.ERR_ActionNotCompleted,e.getMessage());
188 public void log(PrintStream ps, String text) {
189 ps.print(Chrono.dateTime());
190 boolean first = true;
191 for(String s : toList) {
200 if(!ccList.isEmpty()) {
202 for(String s : ccList) {