Update Batch from Testing
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / batch / actions / Email.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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====================================================
19  *
20  */
21
22 package org.onap.aaf.auth.batch.actions;
23
24 import java.io.PrintStream;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.List;
28
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;
35
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;
42     private Message msg;
43     private String sig;
44     protected String lineIndent="  ";
45     private long lastSent=0L;
46
47     
48     public Email(String ... defaultCC) {
49         toList = new ArrayList<>();
50         this.defaultCC = defaultCC;
51         ccList = new ArrayList<>();
52         clear();
53     }
54     
55     public Email clear() {
56         toList.clear();
57         ccList.clear();
58         for (String s: defaultCC) {
59             ccList.add(s);
60         }
61         return this;
62     }
63     
64
65     public void indent(String indent) {
66         lineIndent = indent;
67     }
68     
69     public void preamble(String format, Object ... args) {
70         preamble = String.format(format, args);
71     }
72
73     public Email addTo(Identity id) {
74         if (id!=null && !toList.contains(id.email())) {
75                 toList.add(id.email());
76         }
77         return this;
78     }
79
80     public Email addTo(Collection<String> users) {
81         for (String u : users) {
82             addTo(u);
83         }
84         return this;
85     }
86
87     public Email addTo(String email) {
88         if (!toList.contains(email)) {
89             toList.add(email);
90         }
91         return this;
92     }
93
94     public Email addCC(Identity id) {
95         if (id!=null && !ccList.contains(id.email())) {
96                 ccList.add(id.email());
97         }
98         return this;
99     }
100
101     public Email addCC(String email) {
102         if (!ccList.contains(email)) {
103             ccList.add(email);
104         }
105         return this;
106     }
107
108     
109     public Email add(Identity id, boolean toSuper) throws OrganizationException {
110         Identity responsible = id.responsibleTo();
111         if (toSuper) {
112             addTo(responsible.email());
113             addCC(id.email());
114         } else {
115             addCC(responsible.email());
116             addTo(id.email());
117         }
118         return this;
119     }
120     
121     public Email subject(String format, Object ... args) {
122         if (format.contains("%s")) {
123             subject = String.format(format, args);
124         } else {
125             subject = format;
126         }
127         return this;
128     }
129     
130     
131     public Email signature(String format, Object ... args) {
132         sig = String.format(format, args);
133         return this;
134     }
135     
136     public void msg(Message msg) {
137         this.msg = msg;
138     }
139     
140     @Override
141     public Result<Void> exec(AuthzTrans trans, Organization org, String text) {
142         StringBuilder sb = new StringBuilder();
143         if (preamble!=null) {
144             sb.append(lineIndent);
145             sb.append(preamble);
146             sb.append("\n\n");
147         }
148         
149         if (msg!=null) {
150             msg.msg(sb,lineIndent);
151             sb.append("\n");
152         }
153
154         if (sig!=null) {
155             sb.append(sig);
156             sb.append("\n");
157         }
158         
159         long ct = System.currentTimeMillis();
160         long wait = ct-lastSent;
161         lastSent = ct;
162         if (wait < 100) { // 10 per second
163             try {
164                 Thread.sleep(wait);
165             } catch (InterruptedException e) {
166                  Thread.currentThread().interrupt();
167             }
168         }
169         return exec(trans,org,sb);
170     }
171
172     protected Result<Void> exec(AuthzTrans trans, Organization org, StringBuilder sb) {
173         try {
174             /* int status = */
175             org.sendEmail(trans,
176                 toList, 
177                 ccList, 
178                 subject, 
179                 sb.toString(), 
180                 false);
181         } catch (Exception e) {
182             return Result.err(Result.ERR_ActionNotCompleted,e.getMessage());
183         }
184         return Result.ok();
185
186     }
187
188     public void log(PrintStream ps, String text) {
189         ps.print(Chrono.dateTime());
190         boolean first = true;
191         for (String s : toList) {
192             if (first) {
193                 first = false;
194                 ps.print(": ");
195             } else {
196                 ps.print(", ");
197             }
198             ps.print(s);
199         }
200         if (!ccList.isEmpty()) {
201             first=true;
202             for (String s : ccList) {
203                 if (first) {
204                     first = false;
205                     ps.print(" [");
206                 } else {
207                     ps.print(", ");
208                 }
209                 ps.print(s);
210             }
211             ps.print(']');
212         }
213
214         ps.print(' ');
215         ps.println(text);
216     }
217 }