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.gui;
24 import static org.onap.aaf.misc.xgen.html.HTMLGen.A;
25 import static org.onap.aaf.misc.xgen.html.HTMLGen.H1;
26 import static org.onap.aaf.misc.xgen.html.HTMLGen.LI;
27 import static org.onap.aaf.misc.xgen.html.HTMLGen.TITLE;
28 import static org.onap.aaf.misc.xgen.html.HTMLGen.UL;
31 import java.io.FileInputStream;
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.List;
37 import java.util.Properties;
38 import java.util.TreeMap;
40 import javax.servlet.http.Cookie;
41 import javax.servlet.http.HttpServletRequest;
43 import org.onap.aaf.auth.common.Define;
44 import org.onap.aaf.auth.env.AuthzEnv;
45 import org.onap.aaf.auth.env.AuthzTrans;
46 import org.onap.aaf.auth.gui.pages.Home;
47 import org.onap.aaf.cadi.Permission;
48 import org.onap.aaf.cadi.aaf.AAFPermission;
49 import org.onap.aaf.cadi.client.Holder;
50 import org.onap.aaf.cadi.config.Config;
51 import org.onap.aaf.cadi.principal.TaggedPrincipal;
52 import org.onap.aaf.misc.env.APIException;
53 import org.onap.aaf.misc.env.Env;
54 import org.onap.aaf.misc.env.Slot;
55 import org.onap.aaf.misc.env.StaticSlot;
56 import org.onap.aaf.misc.env.util.Split;
57 import org.onap.aaf.misc.xgen.Cache;
58 import org.onap.aaf.misc.xgen.CacheGen;
59 import org.onap.aaf.misc.xgen.Code;
60 import org.onap.aaf.misc.xgen.DynamicCode;
61 import org.onap.aaf.misc.xgen.Mark;
62 import org.onap.aaf.misc.xgen.html.HTMLCacheGen;
63 import org.onap.aaf.misc.xgen.html.HTMLGen;
64 import org.onap.aaf.misc.xgen.html.Imports;
67 * A Base "Mobile First" Page
72 public class Page extends HTMLCacheGen {
73 public static final String AAFURL_TOOLS = "aaf_url.tools";
74 public static final String AAF_URL_TOOL_DOT = "aaf_url.tool.";
75 public static final String AAF_URL_CUIGUI = "aaf_url.cuigui"; // link to help
76 public static final String AAF_URL_GUI_ONBOARD = "aaf_url.gui_onboard";
77 public static final String AAF_URL_AAF_HELP = "aaf_url.aaf_help";
78 public static final String AAF_URL_CADI_HELP = "aaf_url.cadi_help";
79 public static final String PERM_CA_TYPE = "certman";
80 public static final String PERM_NS = Define.ROOT_NS();
82 public static enum BROWSER {iPhone,html5,ie,ieOld};
84 public static final int MAX_LINE = 20;
85 protected static final String[] NO_FIELDS = new String[0];
86 private static final String BROWSER_TYPE = "BROWSER_TYPE";
88 private final String bcName, bcUrl;
89 private final String[] fields;
91 public final boolean no_cache;
93 // Note: Only access is synchronized in "getPerm"
94 private final static Map<String,Map<String,Permission>> perms = new HashMap<>();
96 public String name() {
100 public String url() {
104 public String[] fields() {
108 public Page(AuthzEnv env, String name, String url, Enum<?>[] en, final NamedCode ...content) throws APIException, IOException {
109 super(CacheGen.PRETTY, new PageCode(env, 1, content));
110 fields = new String[en.length];
112 for (Enum<?> p : en) {
113 fields[++i]=p.name();
118 // Mark which fields must be "no_cache"
119 boolean no_cacheTemp=false;
120 for (NamedCode nc : content) {
126 no_cache=no_cacheTemp;
128 public Page(AuthzEnv env, String name, String url, String [] fields, final NamedCode ... content) throws APIException,IOException {
129 this(env,name,url,1,fields,content);
132 public Page(AuthzEnv env, String name, String url, int backdots, String [] fields, final NamedCode ... content) throws APIException,IOException {
133 super(CacheGen.PRETTY, new PageCode(env, backdots, content));
135 this.fields = new String[0];
137 this.fields = fields;
141 // Mark which fields must be "no_cache"
142 boolean no_cacheTemp=false;
143 for (NamedCode nc : content) {
149 no_cache=no_cacheTemp;
153 private static class PageCode implements Code<HTMLGen> {
154 private static final String AAF_GUI_TITLE = "aaf_gui_title";
156 private final ContentCode[] content;
157 private final Slot browserSlot;
158 private final int backdots;
159 protected AuthzEnv env;
160 private StaticSlot sTheme;
161 private static Map<String,List<String>> themes;
162 private static Map<String,Properties> themeProps;
164 public PageCode(AuthzEnv env, int backdots, final ContentCode[] content) {
165 this.content = content;
166 this.backdots = backdots;
167 browserSlot = env.slot(BROWSER_TYPE);
168 sTheme = env.staticSlot(AAF_GUI.AAF_GUI_THEME);
172 private static synchronized List<String> getThemeFiles(Env env, String theme) {
174 themes = new TreeMap<>();
175 File themeD = new File("theme");
176 if(themeD.exists() && themeD.isDirectory()) {
177 for (File t : themeD.listFiles()) {
178 if(t.isDirectory()) {
179 List<String> la = new ArrayList<>();
180 for(File f : t.listFiles()) {
182 if(f.getName().endsWith(".props")) {
184 if(themeProps == null) {
185 themeProps = new TreeMap<>();
188 props = themeProps.get(theme);
191 props = new Properties();
192 themeProps.put(theme, props);
196 FileInputStream fis = new FileInputStream(f);
202 } catch (IOException e) {
210 themes.put(t.getName(),la);
215 return themes.get(theme);
218 protected Imports getImports(Env env, Holder<String> theme, String defaultTheme, int backdots, BROWSER browser) {
219 List<String> ls = getThemeFiles(env,theme.get());
220 Imports imp = new Imports(backdots);
222 theme.set(defaultTheme);
224 String prefix = "theme/" + theme.get() + '/';
226 if(f.endsWith(".js")) {
228 } else if(f.endsWith(".css")) {
229 if(f.endsWith("iPhone.css")) {
230 if(BROWSER.iPhone.equals(browser)) {
233 } else if (f.endsWith("Desktop.css")){
234 if(!BROWSER.iPhone.equals(browser)) {
237 // Make Console specific to Console page
238 } else if (!"console.js".equals(f)) {
247 public void code(final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
248 // Note: I found that App Storage saves everything about the page, or not. Thus, if you declare the page uncacheable, none of the
249 // Artifacts, like JPGs are stored, which makes this feature useless for Server driven elements
250 cache.dynamic(hgen, new DynamicCode<HTMLGen,AAF_GUI,AuthzTrans>() {
252 public void code(AAF_GUI state, AuthzTrans trans, final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
253 switch(browser(trans,browserSlot)) {
256 hgen.directive("!DOCTYPE html");
257 hgen.directive("meta", "http-equiv=X-UA-Compatible","content=IE=11");
263 final String title = env.getProperty(AAF_GUI_TITLE,"Authentication/Authorization Framework");
264 final String defaultTheme = env.get(sTheme);
265 final Holder<String> hTheme = new Holder<>(defaultTheme);
267 Mark head = hgen.head();
268 hgen.leaf(TITLE).text(title).end();
269 cache.dynamic(hgen, new DynamicCode<HTMLGen,AAF_GUI,AuthzTrans>() {
271 public void code(AAF_GUI state, AuthzTrans trans, final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
272 BROWSER browser = browser(trans,browserSlot);
273 Cookie[] cookies = trans.hreq().getCookies();
275 for(Cookie c : cookies) {
276 if("aaf_theme".equals(c.getName())) {
277 hTheme.set(c.getValue());
281 hgen.imports(getImports(env,hTheme,defaultTheme,backdots,browser));
285 hgen.js().text("document.createElement('header');")
286 .text("document.createElement('nav');")
296 Mark body = hgen.body();
297 Mark header = hgen.header();
298 cache.dynamic(hgen, new DynamicCode<HTMLGen,AAF_GUI,AuthzTrans>() {
300 public void code(AAF_GUI state, AuthzTrans trans,Cache<HTMLGen> cache, HTMLGen xgen)
301 throws APIException, IOException {
302 // Obtain Server Info, and print
304 String env = trans.getProperty(Config.AAF_ENV,"N/A");
305 xgen.leaf(H1).text(title + " on " + env).end();
306 xgen.leaf("p","id=version").text("AAF Version: " + state.deployedVersion).end();
308 // Obtain User Info, and print
309 TaggedPrincipal p = trans.getUserPrincipal();
312 user = "please choose a Login Authority";
313 secured = "NOT Secure!";
315 user = p.personalName();
318 xgen.leaf("p","id=welcome").text("Welcome, ")
322 .text("</sup>").end();
324 switch(browser(trans,browserSlot)) {
327 xgen.incr("h5").text("This app is Mobile First HTML5. Internet Explorer "
328 + " does not support all HTML5 standards. Old, non TSS-Standard versions may not function correctly.").br()
329 .text(" For best results, use a highly compliant HTML5 browser like Firefox.")
341 // If BreadCrumbs, put here
342 if (content.length>0 && content[0] instanceof BreadCrumbs) {
344 Mark ctnt = hgen.divID(nc.idattrs());
345 nc.code(cache, hgen);
354 Mark inner = hgen.divID("inner");
356 for (int i=cIdx;i<content.length;++i) {
358 Mark ctnt = hgen.divID(nc.idattrs());
359 nc.code(cache, hgen);
365 // Navigation - Using older Nav to work with decrepit IE versions
367 Mark nav = hgen.divID("nav");
368 cache.dynamic(hgen, new DynamicCode<HTMLGen,AAF_GUI,AuthzTrans>() {
370 public void code(AAF_GUI state, AuthzTrans trans,Cache<HTMLGen> cache, HTMLGen xgen) throws APIException, IOException {
371 Properties props = themeProps.get(hTheme.get());
372 if(props!=null && "TRUE".equalsIgnoreCase(props.getProperty("main_menu_in_nav"))) {
373 xgen.incr("h2").text("Navigation").end();
374 Mark mark = new Mark();
375 boolean selected = isSelected(trans.path(),Home.HREF);
376 //trans.path().endsWith("home");
377 xgen.incr(mark,HTMLGen.UL)
378 .incr(HTMLGen.LI,selected?"class=selected":"")
379 .incr(HTMLGen.A, "href=home")
382 boolean noSelection = !selected;
383 for(String[] mi : Home.MENU_ITEMS) {
384 //selected = trans.path().endsWith(mi[0]);
386 selected = isSelected(trans.path(),mi[2]);
387 noSelection = !selected;
391 xgen.incr(HTMLGen.LI,selected?"class=selected":"")
392 .incr(HTMLGen.A, "href="+mi[0])
400 private boolean isSelected(String path, String item) {
401 if(item.equals(path)) {
404 for(ContentCode c : content) {
405 if(c instanceof BreadCrumbs) {
406 Page[] bc = ((BreadCrumbs)c).breadcrumbs;
408 for(int i = bc.length-1;i>0;--i) {
409 if(bc[i].url().equals(item)) {
421 hgen.incr("h2").text("Related Links").end();
423 String aaf_help = env.getProperty(AAF_URL_AAF_HELP,null);
424 if (aaf_help!=null) {
425 hgen.leaf(LI).leaf(A,"href="+env.getProperty(AAF_URL_AAF_HELP),"target=_blank").text("AAF WIKI").end(2);
426 String sub = env.getProperty(AAF_URL_AAF_HELP+".sub");
428 hgen.incr(UL,"style=margin-left:5%");
429 for (String s : Split.splitTrim(',', sub)) {
430 hgen.leaf(LI).leaf(A,"href="+env.getProperty(AAF_URL_AAF_HELP+".sub."+s),"target=_blank").text(s.replace('+', ' ')).end(2);
435 aaf_help = env.getProperty(AAF_URL_CADI_HELP,null);
436 if (aaf_help!=null) {
437 hgen.leaf(LI).leaf(A,"href="+aaf_help,"target=_blank").text("CADI WIKI").end(2);
439 String tools = env.getProperty(AAFURL_TOOLS);
442 .incr(HTMLGen.UL,"style=margin-left:5%")
443 .leaf(HTMLGen.H3).text("Related Tools").end();
445 for (String tool : Split.splitTrim(',',tools)) {
446 hgen.leaf(LI).leaf(A,"href="+env.getProperty(AAF_URL_TOOL_DOT+tool),"target=_blank").text(tool.replace('+', ' ')).end(2);
455 // Footer - Using older Footer to work with decrepit IE versions
456 Mark footer = hgen.divID("footer");
457 hgen.textCR(1, env.getProperty(AAF_GUI.AAF_GUI_COPYRIGHT))
465 public static String getBrowserType() {
472 * Use int found in "ieVersion"
475 * Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322;
476 * .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
478 * Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2;
479 * .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; ATT)
481 * IE 11 Compatibility
482 * Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; SLCC2; .NET CLR 2.0.50727;
483 * .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET CLR 1.1.4322; .NET4.0C; .NET4.0E; InfoPath.3; HVD; ATT)
485 * IE 11 (not Compatiblity)
486 * Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727;
487 * .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET CLR 1.1.4322; .NET4.0C; .NET4.0E; InfoPath.3; HVD; ATT)
492 public static BROWSER browser(AuthzTrans trans, Slot slot) {
493 BROWSER br = trans.get(slot, null);
495 String agent = trans.agent();
497 if (agent.contains("iPhone") /* other phones? */) {
499 } else if ((msie = agent.indexOf("MSIE"))>=0) {
501 int end = agent.indexOf(";",msie);
504 ver = Float.valueOf(agent.substring(msie,end));
505 br = ver<8f?BROWSER.ieOld:BROWSER.ie;
506 } catch (Exception e) {
518 * Get, rather than create each time, permissions for validations
520 protected static synchronized Permission getPerm(String instance, String action) {
521 Map<String,Permission> msp = perms.get(instance);
524 msp = new HashMap<>();
525 perms.put(instance, msp);
528 p = msp.get(instance);
531 p=new AAFPermission(PERM_NS, PERM_CA_TYPE,instance,action);
537 protected static String getSingleParam(HttpServletRequest req, String tag) {
538 String values[] = req.getParameterValues(tag);
539 return values.length<1?null:values[0];