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;
30 import java.io.IOException;
31 import java.util.HashMap;
34 import javax.servlet.http.HttpServletRequest;
36 import org.onap.aaf.auth.common.Define;
37 import org.onap.aaf.auth.env.AuthzEnv;
38 import org.onap.aaf.auth.env.AuthzTrans;
39 import org.onap.aaf.auth.rserv.CachingFileAccess;
40 import org.onap.aaf.cadi.Permission;
41 import org.onap.aaf.cadi.aaf.AAFPermission;
42 import org.onap.aaf.cadi.config.Config;
43 import org.onap.aaf.cadi.principal.TaggedPrincipal;
44 import org.onap.aaf.misc.env.APIException;
45 import org.onap.aaf.misc.env.Slot;
46 import org.onap.aaf.misc.env.StaticSlot;
47 import org.onap.aaf.misc.env.util.Split;
48 import org.onap.aaf.misc.xgen.Cache;
49 import org.onap.aaf.misc.xgen.CacheGen;
50 import org.onap.aaf.misc.xgen.Code;
51 import org.onap.aaf.misc.xgen.DynamicCode;
52 import org.onap.aaf.misc.xgen.Mark;
53 import org.onap.aaf.misc.xgen.html.HTMLCacheGen;
54 import org.onap.aaf.misc.xgen.html.HTMLGen;
55 import org.onap.aaf.misc.xgen.html.Imports;
58 * A Base "Mobile First" Page
63 public class Page extends HTMLCacheGen {
64 public static final String AAFURL_TOOLS = "aaf_url.tools";
65 public static final String AAF_URL_TOOL_DOT = "aaf_url.tool.";
66 public static final String AAF_URL_CUIGUI = "aaf_url.cuigui"; // link to help
67 public static final String AAF_URL_GUI_ONBOARD = "aaf_url.gui_onboard";
68 public static final String AAF_URL_AAF_HELP = "aaf_url.aaf_help";
69 public static final String AAF_URL_CADI_HELP = "aaf_url.cadi_help";
70 public static final String PERM_CA_TYPE = Define.ROOT_NS() + ".ca";
72 public static enum BROWSER {iPhone,html5,ie,ieOld};
74 public static final int MAX_LINE=20;
76 protected static final String[] NO_FIELDS = new String[0];
78 private static final String BROWSER_TYPE = "BROWSER_TYPE";
80 private final String bcName, bcUrl;
81 private final String[] fields;
83 public final boolean no_cache;
85 // Note: Only access is synchronized in "getPerm"
86 private final static Map<String,Map<String,Permission>> perms = new HashMap<String,Map<String,Permission>>();
88 public String name() {
96 public String[] fields() {
100 public Page(AuthzEnv env, String name, String url, Enum<?>[] en, final NamedCode ...content) throws APIException, IOException {
101 super(CacheGen.PRETTY, new PageCode(env, 1, content));
102 fields = new String[en.length];
104 for(Enum<?> p : en) {
105 fields[++i]=p.name();
110 // Mark which fields must be "no_cache"
111 boolean no_cacheTemp=false;
112 for(NamedCode nc : content) {
118 no_cache=no_cacheTemp;
120 public Page(AuthzEnv env, String name, String url, String [] fields, final NamedCode ... content) throws APIException,IOException {
121 this(env,name,url,1,fields,content);
124 public Page(AuthzEnv env, String name, String url, int backdots, String [] fields, final NamedCode ... content) throws APIException,IOException {
125 super(CacheGen.PRETTY, new PageCode(env, backdots, content));
127 this.fields = new String[0];
129 this.fields = fields;
133 // Mark which fields must be "no_cache"
134 boolean no_cacheTemp=false;
135 for(NamedCode nc : content) {
141 no_cache=no_cacheTemp;
145 private static class PageCode implements Code<HTMLGen> {
146 private static final String AAF_GUI_TITLE = "aaf_gui_title";
148 private final ContentCode[] content;
149 private final Slot browserSlot;
150 private final int backdots;
151 protected AuthzEnv env;
152 private StaticSlot sTheme;
154 public PageCode(AuthzEnv env, int backdots, final ContentCode[] content) {
155 this.content = content;
156 this.backdots = backdots;
157 browserSlot = env.slot(BROWSER_TYPE);
158 sTheme = env.staticSlot(CachingFileAccess.CFA_WEB_PATH);
163 public void code(final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
164 // Note: I found that App Storage saves everything about the page, or not. Thus, if you declare the page uncacheable, none of the
165 // Artifacts, like JPGs are stored, which makes this feature useless for Server driven elements
166 cache.dynamic(hgen, new DynamicCode<HTMLGen,AAF_GUI,AuthzTrans>() {
168 public void code(AAF_GUI state, AuthzTrans trans, final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
169 switch(browser(trans,browserSlot)) {
172 hgen.directive("!DOCTYPE html");
173 hgen.directive("meta", "http-equiv=X-UA-Compatible","content=IE=11");
179 final String title = env.getProperty(AAF_GUI_TITLE,"Authentication/Authorization Framework");
180 final String theme = env.get(sTheme);
181 Mark head = hgen.head();
182 hgen.leaf(TITLE).text(title).end();
183 hgen.imports(new Imports(backdots).css(theme + "/aaf5.css")
184 .js(theme + "/comm.js")
185 .js(theme + "/console.js")
186 .js(theme + "/common.js"));
187 cache.dynamic(hgen, new DynamicCode<HTMLGen,AAF_GUI,AuthzTrans>() {
189 public void code(AAF_GUI state, AuthzTrans trans, final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
190 switch(browser(trans,browserSlot)) {
192 hgen.imports(new Imports(backdots).css(theme + "/aaf5iPhone.css"));
196 hgen.js().text("document.createElement('header');")
197 .text("document.createElement('nav');")
200 hgen.imports(new Imports(backdots).css(theme + "/aaf5Desktop.css"));
207 Mark body = hgen.body();
208 Mark header = hgen.header();
209 cache.dynamic(hgen, new DynamicCode<HTMLGen,AAF_GUI,AuthzTrans>() {
211 public void code(AAF_GUI state, AuthzTrans trans,Cache<HTMLGen> cache, HTMLGen xgen)
212 throws APIException, IOException {
213 // Obtain Server Info, and print
215 String env = trans.getProperty(Config.AAF_ENV,"N/A");
216 xgen.leaf(H1).text(title + " on " + env).end();
217 xgen.leaf("p","id=version").text("AAF Version: " + state.deployedVersion).end();
219 // Obtain User Info, and print
220 TaggedPrincipal p = trans.getUserPrincipal();
223 user = "please choose a Login Authority";
224 secured = "NOT Secure!";
226 user = p.personalName();
229 xgen.leaf("p","id=welcome").text("Welcome, ")
233 .text("</sup>").end();
235 switch(browser(trans,browserSlot)) {
238 xgen.incr("h5").text("This app is Mobile First HTML5. Internet Explorer "
239 + " does not support all HTML5 standards. Old, non TSS-Standard versions may not function correctly.").br()
240 .text(" For best results, use a highly compliant HTML5 browser like Firefox.")
252 // If BreadCrumbs, put here
253 if(content.length>0 && content[0] instanceof BreadCrumbs) {
255 Mark ctnt = hgen.divID(nc.idattrs());
256 nc.code(cache, hgen);
265 Mark inner = hgen.divID("inner");
267 for(int i=cIdx;i<content.length;++i) {
269 Mark ctnt = hgen.divID(nc.idattrs());
270 nc.code(cache, hgen);
276 // Navigation - Using older Nav to work with decrepit IE versions
278 Mark nav = hgen.divID("nav");
279 hgen.incr("h2").text("Related Links").end();
281 String aaf_help = env.getProperty(AAF_URL_AAF_HELP,null);
283 hgen.leaf(LI).leaf(A,"href="+env.getProperty(AAF_URL_AAF_HELP),"target=_blank").text("AAF WIKI").end(2);
284 String sub = env.getProperty(AAF_URL_AAF_HELP+".sub");
286 hgen.incr(UL,"style=margin-left:5%");
287 for(String s : Split.splitTrim(',', sub)) {
288 hgen.leaf(LI).leaf(A,"href="+env.getProperty(AAF_URL_AAF_HELP+".sub."+s),"target=_blank").text(s.replace('+', ' ')).end(2);
293 aaf_help = env.getProperty(AAF_URL_CADI_HELP,null);
295 hgen.leaf(LI).leaf(A,"href="+aaf_help,"target=_blank").text("CADI WIKI").end(2);
297 String tools = env.getProperty(AAFURL_TOOLS);
300 .incr(HTMLGen.UL,"style=margin-left:5%")
301 .leaf(HTMLGen.H3).text("Related Tools").end();
303 for(String tool : Split.splitTrim(',',tools)) {
304 hgen.leaf(LI).leaf(A,"href="+env.getProperty(AAF_URL_TOOL_DOT+tool),"target=_blank").text(tool.replace('+', ' ')).end(2);
313 // Footer - Using older Footer to work with decrepit IE versions
314 Mark footer = hgen.divID("footer");
315 hgen.textCR(1, env.getProperty(AAF_GUI.AAF_GUI_COPYRIGHT))
323 public static String getBrowserType() {
330 * Use int found in "ieVersion"
333 * Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322;
334 * .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
336 * Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2;
337 * .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)
339 * IE 11 Compatibility
340 * Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; SLCC2; .NET CLR 2.0.50727;
341 * .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)
343 * IE 11 (not Compatiblity)
344 * Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727;
345 * .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)
350 public static BROWSER browser(AuthzTrans trans, Slot slot) {
351 BROWSER br = trans.get(slot, null);
353 String agent = trans.agent();
355 if(agent.contains("iPhone") /* other phones? */) {
357 } else if ((msie = agent.indexOf("MSIE"))>=0) {
359 int end = agent.indexOf(";",msie);
362 ver = Float.valueOf(agent.substring(msie,end));
363 br = ver<8f?BROWSER.ieOld:BROWSER.ie;
364 } catch (Exception e) {
376 * Get, rather than create each time, permissions for validations
378 protected static synchronized Permission getPerm(String instance, String action) {
379 Map<String,Permission> msp = perms.get(instance);
382 msp = new HashMap<String,Permission>();
383 perms.put(instance, msp);
386 p = msp.get(instance);
389 p=new AAFPermission(PERM_CA_TYPE,instance,action);
395 protected static String getSingleParam(HttpServletRequest req, String tag) {
396 String values[] = req.getParameterValues(tag);
397 return values.length<1?null:values[0];