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====================================================
21 package org.onap.aaf.auth.batch.reports.bodies;
24 import java.io.IOException;
25 import java.lang.reflect.Constructor;
26 import java.lang.reflect.InvocationTargetException;
27 import java.lang.reflect.Modifier;
28 import java.net.URISyntaxException;
30 import java.util.ArrayList;
31 import java.util.Collection;
32 import java.util.Enumeration;
33 import java.util.HashMap;
34 import java.util.HashSet;
35 import java.util.List;
38 import java.util.TreeMap;
39 import java.util.jar.JarEntry;
40 import java.util.jar.JarFile;
42 import org.onap.aaf.auth.batch.reports.Notify;
43 import org.onap.aaf.auth.env.AuthzTrans;
44 import org.onap.aaf.cadi.Access;
45 import org.onap.aaf.misc.env.APIException;
47 public abstract class NotifyBody {
48 private static final String DUPL = "<td style=\"text-indent: 4em;\">''</td>";
49 private static final Map<String,NotifyBody> bodyMap = new HashMap<>();
51 protected Map<String,List<List<String>>> rows;
52 protected final String env;
53 protected final String gui_url;
55 private final String name;
56 private final String type;
58 private int escalation;
61 public NotifyBody(Access access, final String type, final String name) {
62 rows = new TreeMap<>();
68 env = access.getProperty("CASS_ENV","DEVL");
69 gui_url = access.getProperty("GUI_URL", "");
72 public void store(List<String> row) {
74 if("info".equals(row.get(0))) {
79 escalation = Integer.parseInt(row.get(3));
82 } else if(type.equals(row.get(0))) {
83 String user = user(row);
85 List<List<String>> lss = rows.get(user);
87 lss = new ArrayList<>();
96 public String name() {
100 public String type() {
104 public String date() {
107 public int escalation() {
111 public Set<String> users() {
112 return rows.keySet();
116 * ID must be set from Row for Email lookup
124 public abstract boolean body(AuthzTrans trans, StringBuilder sb, int indent, Notify n, String id);
127 * Return "null" if user not found in row... Code will handle.
131 protected abstract String user(List<String> row);
133 public abstract String subject();
136 * Get Notify Body based on key of
139 public static NotifyBody get(String key) {
140 return bodyMap.get(key);
144 * Return set of loaded NotifyBodies
147 public static Collection<NotifyBody> getAll() {
148 // Note: The same Notify Body is entered several times with different keys.
149 // Therefore, need a Set of Values, not all the Values.
150 Set<NotifyBody> set = new HashSet<>();
151 set.addAll(bodyMap.values());
157 * @throws URISyntaxException
160 public static void load(Access access) throws APIException, IOException {
161 // class load available NotifyBodies
162 ClassLoader cl = Thread.currentThread().getContextClassLoader();
163 Package pkg = NotifyBody.class.getPackage();
164 String path = pkg.getName().replace('.', '/');
165 URL url = cl.getResource(path);
166 List<String> classNames = new ArrayList<>();
167 String urlString = url.toString();
168 if(urlString.startsWith("jar:file:")) {
169 int exclam = urlString.lastIndexOf('!');
170 JarFile jf = new JarFile(urlString.substring(9,exclam));
172 Enumeration<JarEntry> jfe = jf.entries();
173 while(jfe.hasMoreElements()) {
174 String name = jfe.nextElement().getName();
175 if(name.startsWith(path) && name.endsWith(".class")) {
176 classNames.add(name.substring(0,name.length()-6).replace('/', '.'));
183 File dir = new File(url.getFile());
184 for( String f : dir.list()) {
185 if(f.endsWith(".class")) {
186 classNames.add(pkg.getName()+'.'+f.substring(0,f.length()-6));
190 for(String cls : classNames) {
192 Class<?> c = cl.loadClass(cls);
194 if(!Modifier.isAbstract(c.getModifiers())) {
195 Constructor<?> cst = c.getConstructor(Access.class);
196 NotifyBody nb = (NotifyBody)cst.newInstance(access);
198 bodyMap.put("info|"+nb.name, nb);
199 bodyMap.put(nb.type+'|'+nb.name, nb);
203 } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
209 protected void print(StringBuilder sb, int indent, Object ... objs) {
210 for(int i=0;i<indent;++i) {
213 for(Object o : objs) {
214 sb.append(o.toString());
218 protected void println(StringBuilder sb, int indent, Object ... objs) {
219 print(sb,indent,objs);
223 protected void printf(StringBuilder sb, int indent, String fmt, Object ... objs) {
224 print(sb,indent,String.format(fmt, objs));
227 protected String printCell(StringBuilder sb, int indent, String current, String prev) {
228 if(current.equals(prev)) {
229 println(sb,indent,DUPL);
231 printCell(sb,indent,current);
233 return current; // use to set prev...
236 protected void printCell(StringBuilder sb, int indent, String current) {
237 println(sb,indent,"<td>",current,"</td>");
240 public synchronized void inc() {