Post Init Service Starter
[aaf/authz.git] / auth / auth-gui / src / main / java / org / onap / aaf / auth / gui / Page.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.gui;
23
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;
29
30 import java.io.File;
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;
36 import java.util.Map;
37 import java.util.Properties;
38 import java.util.TreeMap;
39
40 import javax.servlet.http.Cookie;
41 import javax.servlet.http.HttpServletRequest;
42
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;
65
66 /**
67  * A Base "Mobile First" Page 
68  * 
69  * @author Jonathan
70  *
71  */
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();
81
82     public static enum BROWSER {iPhone,html5,ie,ieOld};
83
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";
87
88     private final String bcName, bcUrl;
89     private final String[] fields;
90
91     public final boolean no_cache;
92
93     // Note: Only access is synchronized in "getPerm"
94     private final static Map<String,Map<String,Permission>> perms = new HashMap<>();
95
96     public String name() {
97         return bcName;
98     }
99     
100     public String url() {
101         return bcUrl;
102     }
103     
104     public String[] fields() {
105         return fields;
106     }
107     
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];
111         int i=-1;
112         for (Enum<?> p : en) {
113             fields[++i]=p.name();
114         }
115
116         bcName = name;
117         bcUrl = url;
118         // Mark which fields must be "no_cache"
119         boolean no_cacheTemp=false;
120         for (NamedCode nc : content) {
121             if (nc.no_cache()) { 
122                 no_cacheTemp=true;
123                 break;
124             }
125         }
126         no_cache=no_cacheTemp;
127     }
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);
130     }
131     
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));
134         if (fields==null) {
135             this.fields = new String[0];
136         } else {
137             this.fields = fields;
138         }
139         bcName = name;
140         bcUrl = url;
141         // Mark which fields must be "no_cache"
142         boolean no_cacheTemp=false;
143         for (NamedCode nc : content) {
144             if (nc.no_cache()) { 
145                 no_cacheTemp=true;
146                 break;
147             }
148         }
149         no_cache=no_cacheTemp;
150     }
151     
152     
153     private static class PageCode implements Code<HTMLGen> {
154             private static final String AAF_GUI_TITLE = "aaf_gui_title";
155             
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;
163
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);
169                 this.env = env;
170             }
171
172             private static synchronized List<String> getThemeFiles(Env env, String theme) {
173                 if(themes==null) {
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()) {
181                                                 if(f.isFile()) {
182                                                         if(f.getName().endsWith(".props")) {
183                                                                 Properties props;
184                                                                 if(themeProps == null) {
185                                                                         themeProps = new TreeMap<>();
186                                                                         props = null;
187                                                                 } else {
188                                                                         props = themeProps.get(theme);
189                                                                 }
190                                                                 if(props==null) {
191                                                                         props = new Properties();
192                                                                         themeProps.put(theme, props);
193                                                                 }
194                                                                 
195                                                                 try {
196                                                                         FileInputStream fis = new FileInputStream(f);
197                                                                         try {
198                                                                                 props.load(fis);
199                                                                         } finally {
200                                                                                 fis.close();
201                                                                         }
202                                                                 } catch (IOException e) {
203                                                                         env.error().log(e);
204                                                                 }
205                                                         } else {
206                                                                 la.add(f.getName());
207                                                         }
208                                                 }
209                                         }
210                                                 themes.put(t.getName(),la);
211                                 }
212                         }
213                     }
214                 }
215                 return themes.get(theme);
216             }
217             
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);
221                 if(ls==null) {
222                         theme.set(defaultTheme);
223                 }
224                         String prefix = "theme/" + theme.get() + '/';
225                         for(String f : ls) {
226                         if(f.endsWith(".js")) {
227                                 imp.js(prefix + f);
228                         } else if(f.endsWith(".css")) {
229                                 if(f.endsWith("iPhone.css")) {
230                                         if(BROWSER.iPhone.equals(browser)) {
231                                                 imp.css(prefix + f);
232                                         }
233                                 } else if (f.endsWith("Desktop.css")){
234                                         if(!BROWSER.iPhone.equals(browser)) {
235                                                 imp.css(prefix + f);
236                                         }
237                                 // Make Console specific to Console page
238                                 } else if (!"console.js".equals(f)) {
239                                         imp.css(prefix + f);
240                                 }
241                         }
242                 }
243                 return imp;
244             }
245             
246             @Override
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>() {
251                     @Override
252                     public void code(AAF_GUI state, AuthzTrans trans, final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
253                         switch(browser(trans,browserSlot)) {
254                             case ieOld:
255                             case ie:
256                                 hgen.directive("!DOCTYPE html");
257                                 hgen.directive("meta", "http-equiv=X-UA-Compatible","content=IE=11");
258                             default:
259                         }
260                     }
261                 });
262                 hgen.html();
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);
266               
267                 Mark head = hgen.head();
268                     hgen.leaf(TITLE).text(title).end();
269                     cache.dynamic(hgen, new DynamicCode<HTMLGen,AAF_GUI,AuthzTrans>() {
270                         @Override
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();
274                                 if(cookies!=null) {
275                                         for(Cookie c : cookies) {
276                                                 if("aaf_theme".equals(c.getName())) {
277                                                         hTheme.set(c.getValue());
278                                                 }
279                                         }
280                                 }
281                             hgen.imports(getImports(env,hTheme,defaultTheme,backdots,browser));
282                             switch(browser) {
283                                 case ie:
284                                 case ieOld:
285                                     hgen.js().text("document.createElement('header');")
286                                             .text("document.createElement('nav');")
287                                             .done();
288                                     break;
289                                 default:
290                             }
291                             
292                         }
293                     });
294                     hgen.end(head);
295                     
296                 Mark body = hgen.body();
297                     Mark header = hgen.header();
298                     cache.dynamic(hgen, new DynamicCode<HTMLGen,AAF_GUI,AuthzTrans>() {
299                         @Override
300                         public void code(AAF_GUI state, AuthzTrans trans,Cache<HTMLGen> cache, HTMLGen xgen)
301                                 throws APIException, IOException {
302                             // Obtain Server Info, and print
303                             // AT&T Only
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();
307                             
308                             // Obtain User Info, and print
309                             TaggedPrincipal p = trans.getUserPrincipal();
310                             String user,secured;
311                             if (p==null) {
312                                 user = "please choose a Login Authority";
313                                 secured = "NOT Secure!";
314                             } else {
315                                 user = p.personalName();
316                                 secured = p.tag();
317                             }
318                             xgen.leaf("p","id=welcome").text("Welcome, ")
319                                 .text(user)
320                                 .text("<sup>")
321                                 .text(secured)
322                                 .text("</sup>").end();
323                             
324                             switch(browser(trans,browserSlot)) {
325                                 case ieOld:
326                                 case ie:
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.")
330                                         .end();
331                                     break;
332                                 default:
333                             }
334                         }
335                     });
336                     
337                     hgen.hr();
338                     
339                     int cIdx;
340                     ContentCode nc;
341                     // If BreadCrumbs, put here
342                     if (content.length>0 && content[0] instanceof BreadCrumbs) {
343                         nc = content[0];
344                         Mark ctnt = hgen.divID(nc.idattrs());
345                         nc.code(cache, hgen);
346                         hgen.end(ctnt);
347                         cIdx = 1;
348                     } else {
349                         cIdx = 0;
350                     }
351                     
352                     hgen.end(header);
353                     
354                     Mark inner = hgen.divID("inner");
355                         // Content
356                         for (int i=cIdx;i<content.length;++i) {
357                             nc = content[i];
358                             Mark ctnt = hgen.divID(nc.idattrs());
359                             nc.code(cache, hgen);
360                             hgen.end(ctnt);
361                         }
362
363                     hgen.end(inner);    
364                     
365                     // Navigation - Using older Nav to work with decrepit   IE versions
366                     
367                     Mark nav = hgen.divID("nav");
368                     cache.dynamic(hgen, new DynamicCode<HTMLGen,AAF_GUI,AuthzTrans>() {
369                         @Override
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")
380                                         .text("Home")
381                                         .end(2);
382                                 boolean noSelection = !selected;
383                                 for(String[] mi : Home.MENU_ITEMS) {
384                                         //selected = trans.path().endsWith(mi[0]);
385                                         if(noSelection) {
386                                                 selected = isSelected(trans.path(),mi[2]);
387                                                 noSelection = !selected;
388                                         } else {
389                                                 selected = false;
390                                         }
391                                         xgen.incr(HTMLGen.LI,selected?"class=selected":"")
392                                             .incr(HTMLGen.A, "href="+mi[0])
393                                             .text(mi[1])
394                                             .end(2);
395                                 }
396                                 xgen.end(mark);
397                                 }
398                         }
399
400                                                 private boolean isSelected(String path, String item) {
401                                                         if(item.equals(path)) {
402                                                                 return true;
403                                                         } else {
404                                                                 for(ContentCode c : content) {
405                                                                         if(c instanceof BreadCrumbs) {
406                                                                                 Page[] bc = ((BreadCrumbs)c).breadcrumbs;
407                                                                                 if(bc!=null) {
408                                                                                         for(int i = bc.length-1;i>0;--i) {
409                                                                                                 if(bc[i].url().equals(item)) {
410                                                                                                         return true;
411                                                                                                 }
412                                                                                         }
413                                                                                         return false;
414                                                                                 }
415                                                                         }
416                                                                 }
417                                                         }
418                                                         return false;
419                                                 }
420                     });
421                     hgen.incr("h2").text("Related Links").end();
422                     hgen.incr(UL);
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");
427                         if (sub!=null) {
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);
431                             }
432                             hgen.end();
433                         }
434                     }
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);
438                     }
439                     String tools = env.getProperty(AAFURL_TOOLS);
440                     if (tools!=null) {
441                         hgen.hr()
442                             .incr(HTMLGen.UL,"style=margin-left:5%")
443                              .leaf(HTMLGen.H3).text("Related Tools").end();
444
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);
447                         }
448                         hgen.end();
449                     }
450                     hgen.end();
451                     
452                     hgen.hr();
453                     
454                     hgen.end(nav);
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))
458                         .end(footer);
459                         
460                     hgen.end(body);
461                 hgen.endAll();
462         }
463     }
464
465     public static String getBrowserType() {
466         return BROWSER_TYPE;
467     }
468     
469     /**
470      * It's IE if int >=0
471      * 
472      * Use int found in "ieVersion"
473      * 
474      * Official IE 7
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)
477      * Official IE 8
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)
480      * 
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)
484      * 
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)
488      * 
489      * @param trans
490      * @return
491      */
492     public static BROWSER browser(AuthzTrans trans, Slot slot) {
493         BROWSER br = trans.get(slot, null);
494         if (br==null) {
495             String agent = trans.agent();
496             int msie; 
497             if (agent.contains("iPhone") /* other phones? */) {
498                 br=BROWSER.iPhone;
499             } else if ((msie = agent.indexOf("MSIE"))>=0) {
500                 msie+=5;
501                 int end = agent.indexOf(";",msie);
502                 float ver;
503                 try {
504                     ver = Float.valueOf(agent.substring(msie,end));
505                     br = ver<8f?BROWSER.ieOld:BROWSER.ie;
506                 } catch (Exception e) {
507                     br = BROWSER.ie;
508                 }
509             } else {
510                 br = BROWSER.html5;
511             }
512             trans.put(slot,br);
513         }
514         return br;
515     }
516     
517     /*
518      * Get, rather than create each time, permissions for validations
519      */
520     protected static synchronized Permission getPerm(String instance, String action) {
521         Map<String,Permission> msp = perms.get(instance);
522         Permission p;
523         if (msp==null) {
524             msp = new HashMap<>();
525             perms.put(instance, msp);
526             p=null;
527         } else {
528             p = msp.get(instance);
529         }
530         if (p==null) {
531             p=new AAFPermission(PERM_NS, PERM_CA_TYPE,instance,action);
532             msp.put(action, p);
533         }
534         return p;
535      }
536
537     protected static String getSingleParam(HttpServletRequest req, String tag) {
538         String values[] = req.getParameterValues(tag);
539         return values.length<1?null:values[0];
540     }
541
542 }
543