2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 IBM.
7 * ===========================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END====================================================
22 package org.onap.aaf.auth.batch.reports.bodies;
25 import java.io.IOException;
26 import java.lang.reflect.Constructor;
27 import java.lang.reflect.InvocationTargetException;
28 import java.lang.reflect.Modifier;
29 import java.net.URISyntaxException;
31 import java.util.ArrayList;
32 import java.util.Collection;
33 import java.util.Enumeration;
34 import java.util.HashMap;
35 import java.util.HashSet;
36 import java.util.List;
39 import java.util.TreeMap;
40 import java.util.jar.JarEntry;
41 import java.util.jar.JarFile;
43 import org.onap.aaf.auth.batch.helpers.LastNotified;
44 import org.onap.aaf.auth.batch.reports.Notify;
45 import org.onap.aaf.auth.env.AuthzTrans;
46 import org.onap.aaf.cadi.Access;
47 import org.onap.aaf.misc.env.APIException;
49 public abstract class NotifyBody {
50 private static final String DUPL = "<td style=\"text-indent: 4em;\">''</td>";
51 private static final Map<String,NotifyBody> bodyMap = new HashMap<>();
53 protected Map<String,List<List<String>>> rows;
54 protected final String env;
55 protected final String gui_url;
57 private final String name;
58 private final String type;
60 private int escalation;
63 public NotifyBody(Access access, final String type, final String name) {
64 rows = new TreeMap<>();
70 env = access.getProperty("CASS_ENV","DEVL");
71 gui_url = access.getProperty("GUI_URL", "");
74 public void store(List<String> row) {
76 if("info".equals(row.get(0))) {
81 escalation = Integer.parseInt(row.get(3));
83 } else if(type.equals(row.get(0))) {
84 String user = user(row);
86 List<List<String>> lss = rows.get(user);
88 lss = new ArrayList<>();
97 public String name() {
101 public String type() {
105 public String date() {
108 public int escalation() {
112 public Set<String> users() {
113 return rows.keySet();
117 * ID must be set from Row for Email lookup
125 public abstract boolean body(AuthzTrans trans, StringBuilder sb, int indent, Notify n, String id);
128 * Return "null" if user not found in row... Code will handle.
132 protected abstract String user(List<String> row);
135 * Provide a context-sensitive Subject, which includes ENV as well as details
139 public abstract String subject();
142 * Record the fact that a particular Notification was marked as "sent" by Emailer.
148 public abstract void record(AuthzTrans trans, StringBuilder query, String id, List<String> notified, LastNotified ln);
151 * Get Notify Body based on key of
154 public static NotifyBody get(String key) {
155 return bodyMap.get(key);
159 * Return set of loaded NotifyBodies
162 public static Collection<NotifyBody> getAll() {
163 // Note: The same Notify Body is entered several times with different keys.
164 // Therefore, need a Set of Values, not all the Values.
165 Set<NotifyBody> set = new HashSet<>();
166 set.addAll(bodyMap.values());
172 * @throws URISyntaxException
175 public static void load(Access access) throws APIException, IOException {
176 // class load available NotifyBodies
177 ClassLoader cl = Thread.currentThread().getContextClassLoader();
178 Package pkg = NotifyBody.class.getPackage();
179 String path = pkg.getName().replace('.', '/');
180 URL url = cl.getResource(path);
181 List<String> classNames = new ArrayList<>();
184 urlString = url.toString();
185 if (urlString.startsWith("jar:file:")) {
186 int exclam = urlString.lastIndexOf('!');
187 JarFile jf = new JarFile(urlString.substring(9, exclam));
189 Enumeration<JarEntry> jfe = jf.entries();
190 while (jfe.hasMoreElements()) {
191 String name = jfe.nextElement().getName();
192 if (name.startsWith(path) && name.endsWith(".class")) {
193 classNames.add(name.substring(0, name.length() - 6).replace('/', '.'));
200 File dir = new File(url.getFile());
201 String[] dirs = dir.list();
203 for (String f : dirs) {
204 if (f.endsWith(".class")) {
205 classNames.add(pkg.getName() + '.' + f.substring(0, f.length() - 6));
210 for (String cls : classNames) {
212 Class<?> c = cl.loadClass(cls);
213 if ((c != null) && (!Modifier.isAbstract(c.getModifiers()))) {
214 Constructor<?> cst = c.getConstructor(Access.class);
215 NotifyBody nb = (NotifyBody) cst.newInstance(access);
216 bodyMap.put("info|" + nb.name, nb);
217 bodyMap.put(nb.type + '|' + nb.name, nb);
219 } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
226 protected void print(StringBuilder sb, int indent, Object ... objs) {
227 for(int i = 0; i < indent; ++i) {
230 for(Object o : objs) {
231 sb.append(o.toString());
235 protected void println(StringBuilder sb, int indent, Object ... objs) {
236 print(sb,indent,objs);
240 protected void printf(StringBuilder sb, int indent, String fmt, Object ... objs) {
241 print(sb,indent,String.format(fmt, objs));
244 protected String printCell(StringBuilder sb, int indent, String current, String prev) {
245 if(current.equals(prev)) {
246 println(sb,indent,DUPL);
248 printCell(sb,indent,current);
250 return current; // use to set prev...
253 protected void printCell(StringBuilder sb, int indent, String current) {
254 println(sb,indent,"<td>",current,"</td>");
257 public synchronized void inc() {