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<String>();
50 this.defaultCC = defaultCC;
51 ccList = new ArrayList<String>();
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) {
75 if(!toList.contains(id.email())) {
76 toList.add(id.email());
82 public Email addTo(Collection<String> users) {
83 for(String u : users) {
89 public Email addTo(String email) {
90 if(!toList.contains(email)) {
96 public Email addCC(Identity id) {
98 if(!ccList.contains(id.email())) {
99 ccList.add(id.email());
105 public Email addCC(String email) {
106 if(!ccList.contains(email)) {
113 public Email add(Identity id, boolean toSuper) throws OrganizationException {
114 Identity responsible = id.responsibleTo();
116 addTo(responsible.email());
119 addCC(responsible.email());
125 public Email subject(String format, Object ... args) {
126 if(format.contains("%s")) {
127 subject = String.format(format, args);
135 public Email signature(String format, Object ... args) {
136 sig = String.format(format, args);
140 public void msg(Message msg) {
145 public Result<Void> exec(AuthzTrans trans, Organization org, String text) {
146 StringBuilder sb = new StringBuilder();
148 sb.append(lineIndent);
154 msg.msg(sb,lineIndent);
163 long ct = System.currentTimeMillis();
164 long wait = ct-lastSent;
166 if(wait < 100) { // 10 per second
169 } catch (InterruptedException e) {
172 return exec(trans,org,sb);
175 protected Result<Void> exec(AuthzTrans trans, Organization org, StringBuilder sb) {
184 } catch (Exception e) {
185 return Result.err(Result.ERR_ActionNotCompleted,e.getMessage());
191 public void log(PrintStream ps, String text) {
192 ps.print(Chrono.dateTime());
193 boolean first = true;
194 for(String s : toList) {
203 if(!ccList.isEmpty()) {
205 for(String s : ccList) {