a8c48e69c919ec8992a03cebc164f0ef9e6b68f6
[aaf/authz.git] / authz-gui / src / main / java / com / att / authz / gui / Page.java
1 /*******************************************************************************
2  * Copyright (c) 2016 AT&T Intellectual Property. All rights reserved.
3  *******************************************************************************/
4 package com.att.authz.gui;
5
6 import static com.att.xgen.html.HTMLGen.A;
7 import static com.att.xgen.html.HTMLGen.H1;
8 import static com.att.xgen.html.HTMLGen.LI;
9 import static com.att.xgen.html.HTMLGen.TITLE;
10 import static com.att.xgen.html.HTMLGen.UL;
11
12 import java.io.IOException;
13 import java.security.Principal;
14
15 import com.att.authz.env.AuthzEnv;
16 import com.att.authz.env.AuthzTrans;
17 import org.onap.aaf.cadi.config.Config;
18 import org.onap.aaf.inno.env.APIException;
19 import org.onap.aaf.inno.env.Slot;
20 import org.onap.aaf.inno.env.util.Split;
21 import com.att.xgen.Cache;
22 import com.att.xgen.CacheGen;
23 import com.att.xgen.Code;
24 import com.att.xgen.DynamicCode;
25 import com.att.xgen.Mark;
26 import com.att.xgen.html.HTMLCacheGen;
27 import com.att.xgen.html.HTMLGen;
28 import com.att.xgen.html.Imports;
29
30 /**
31  * A Base "Mobile First" Page 
32  * 
33  *
34  */
35 public class Page extends HTMLCacheGen {
36         public static enum BROWSER {iPhone,html5,ie,ieOld};
37         
38         public static final int MAX_LINE=20;
39
40         protected static final String[] NO_FIELDS = new String[0];
41
42         private static final String ENV_CONTEXT = "envContext";
43         private static final String DME_SERVICE_NAME = "DMEServiceName";
44         private static final String ROUTE_OFFER = "routeOffer";
45         private static final String BROWSER_TYPE = "BROWSER_TYPE";
46
47         private final String bcName, bcUrl;
48         private final String[] fields;
49
50         public final boolean no_cache;
51
52         public String name() {
53                 return bcName;
54         }
55         
56         public String url() {
57                 return bcUrl;
58         }
59         
60         public String[] fields() {
61                 return fields;
62         }
63         
64         public Page(AuthzEnv env, String name, String url, String [] fields, final NamedCode ... content) throws APIException,IOException {
65                 this(env,name,url,1,fields,content);
66         }
67         
68         public Page(AuthzEnv env, String name, String url, int backdots, String [] fields, final NamedCode ... content) throws APIException,IOException {
69                 super(CacheGen.PRETTY, new PageCode(env, backdots, content));
70                 bcName = name;
71                 bcUrl = url;
72                 this.fields = fields;
73                 // Mark which fields must be "no_cache"
74                 boolean no_cacheTemp=false;
75                 for(NamedCode nc : content) {
76                         if(nc.no_cache) { 
77                                 no_cacheTemp=true;
78                                 break;
79                         }
80                 }
81                 no_cache=no_cacheTemp;
82         }
83         
84         private static class PageCode implements Code<HTMLGen> {
85                         private final NamedCode[] content;
86                         private final Slot browserSlot;
87                         private final int backdots;
88                         protected AuthzEnv env;
89
90                         public PageCode(AuthzEnv env, int backdots, final NamedCode[] content) {
91                                 this.content = content;
92                                 this.backdots = backdots;
93                                 browserSlot = env.slot(BROWSER_TYPE);
94                                 this.env = env;
95                         }
96                         
97                         @Override
98                         public void code(Cache<HTMLGen> cache, HTMLGen hgen) throws APIException, IOException {
99                                 // Note: I found that App Storage saves everything about the page, or not.  Thus, if you declare the page uncacheable, none of the 
100                                 // Artifacts, like JPGs are stored, which makes this feature useless for Server driven elements
101                                 //hgen.html("manifest=../theme/aaf.appcache");
102                                 cache.dynamic(hgen,  new DynamicCode<HTMLGen,AuthGUI,AuthzTrans>() {
103                                         @Override
104                                         public void code(AuthGUI state, AuthzTrans trans, Cache<HTMLGen> cache, HTMLGen hgen) throws APIException, IOException {
105                                                 switch(browser(trans,browserSlot)) {
106                                                         case ieOld:
107                                                         case ie:
108                                                                 hgen.directive("!DOCTYPE html");
109                                                                 hgen.directive("meta", "http-equiv=X-UA-Compatible","content=IE=11");
110                                                         default:
111                                                 }
112                                         }
113                                 });
114                                 hgen.html();
115                                 Mark head = hgen.head();
116                                         hgen.leaf(TITLE).text("AT&amp;T Authentication/Authorization Tool").end();
117                                         hgen.imports(new Imports(backdots).css("theme/aaf5.css")
118                                                                                                 .js("theme/comm.js")
119                                                                                                 .js("theme/console.js")
120                                                                                                 .js("theme/common.js"));
121                                         cache.dynamic(hgen, new DynamicCode<HTMLGen,AuthGUI,AuthzTrans>() {
122                                                 @Override
123                                                 public void code(AuthGUI state, AuthzTrans trans, Cache<HTMLGen> cache, HTMLGen hgen) throws APIException, IOException {
124                                                         switch(browser(trans,browserSlot)) {
125                                                                 case iPhone:
126                                                                         hgen.imports(new Imports(backdots).css("theme/aaf5iPhone.css"));
127                                                                         break;
128                                                                 case ie:
129                                                                 case ieOld:
130                                                                         hgen.js().text("document.createElement('header');")
131                                                                                         .text("document.createElement('nav');")
132                                                                                         .done();
133                                                                 case html5:
134                                                                         hgen.imports(new Imports(backdots).css("theme/aaf5Desktop.css"));
135                                                                         break;
136                                                         }
137                                                 }
138                                         });
139                                         hgen.end(head);
140                                         
141                                 Mark body = hgen.body();
142                                         Mark header = hgen.header();
143                                         cache.dynamic(hgen, new DynamicCode<HTMLGen,AuthGUI,AuthzTrans>() {
144                                                 @Override
145                                                 public void code(AuthGUI state, AuthzTrans trans,Cache<HTMLGen> cache, HTMLGen xgen)
146                                                                 throws APIException, IOException {
147                                                         // Obtain Server Info, and print
148                                                         String DMEServiceName = trans.getProperty(DME_SERVICE_NAME);
149                                                         String env = DMEServiceName.substring(
150                                                                         DMEServiceName.indexOf(ENV_CONTEXT),
151                                                                         DMEServiceName.indexOf(ROUTE_OFFER) -1).split("=")[1];
152                                                         
153                                                         xgen.leaf(H1).text("AT&amp;T Auth Tool on " + env).end();
154                                                         xgen.leaf("p","id=version").text("AAF Version: " + trans.getProperty(Config.AAF_DEPLOYED_VERSION, "N/A")).end();
155                                                         
156                                                         // Obtain User Info, and print
157                                                         Principal p = trans.getUserPrincipal();
158                                                         String user;
159                                                         if(p==null) {
160                                                                 user = "please choose a Login Authority";
161                                                         } else {
162                                                                 user = p.getName();
163                                                         }
164                                                         xgen.leaf("p","id=welcome").text("Welcome, " + user).end();
165                                                         
166                                                         switch(browser(trans,browserSlot)) {
167                                                                 case ieOld:
168                                                                 case ie:
169                                                                         xgen.incr("h5").text("This app is Mobile First HTML5.  Internet Explorer " 
170                                                                                         + " does not support all HTML5 standards. Old, non TSS-Standard versions may not function correctly.").br()
171                                                                                         .text("  For best results, use a highly compliant HTML5 browser like Firefox.")
172                                                                                 .end();
173                                                                         break;
174                                                                 default:
175                                                         }
176                                                 }
177                                         });
178                                         
179                                         hgen.hr();
180                                         
181                                         int cIdx;
182                                         NamedCode nc;
183                                         // If BreadCrumbs, put here
184                                         if(content.length>0 && content[0] instanceof BreadCrumbs) {
185                                                 nc = content[0];
186                                                 Mark ctnt = hgen.divID(nc.idattrs());
187                                                 nc.code(cache, hgen);
188                                                 hgen.end(ctnt);
189                                                 cIdx = 1;
190                                         } else {
191                                                 cIdx = 0;
192                                         }
193                                         
194                                         hgen.end(header);
195                                         
196                                         Mark inner = hgen.divID("inner");
197                                                 // Content
198                                                 for(int i=cIdx;i<content.length;++i) {
199                                                         nc = content[i];
200                                                         Mark ctnt = hgen.divID(nc.idattrs());
201                                                         nc.code(cache, hgen);
202                                                         hgen.end(ctnt);
203                                                 }
204
205                                         hgen.end(inner);        
206                                         
207                                         // Navigation - Using older Nav to work with decrepit  IE versions
208                                         
209                                         Mark nav = hgen.divID("nav");
210                                         hgen.incr("h2").text("Related Links").end();
211                                         hgen.incr(UL)
212                                                  .leaf(LI).leaf(A,"href="+env.getProperty("aaf_url.aaf_help")).text("AAF WIKI").end(2)
213                                                  .leaf(LI).leaf(A,"href="+env.getProperty("aaf_url.cadi_help")).text("CADI WIKI").end(2);
214                                                 String tools = env.getProperty("aaf_tools");
215                                                 if(tools!=null) {
216                                                         hgen.hr()
217                                                                 .incr(HTMLGen.UL,"style=margin-left:5%")
218                                                                 .leaf(HTMLGen.H3).text("Related Tools").end();
219
220                                                         for(String tool : Split.splitTrim(',',tools)) {
221                                                                 hgen.leaf(LI).leaf(A,"href="+env.getProperty("aaf_url.tool."+tool)).text(tool.toUpperCase() + " Help").end(2);
222                                                         }
223                                                         hgen.end();
224                                                 }
225                                                  hgen.end();
226                                         
227                                         hgen.hr();
228                                         
229                                         hgen.end(nav);
230                                         // Footer - Using older Footer to work with decrepit IE versions
231                                         Mark footer = hgen.divID("footer");
232                                                 hgen.textCR(1, "(c) 2014-6 AT&amp;T Inc. All Rights Reserved")
233                                                 .end(footer);
234                                                 
235                                         hgen.end(body);
236                                 hgen.endAll();
237                 }
238         }
239
240         public static String getBrowserType() {
241                 return BROWSER_TYPE;
242         }
243         
244         /**
245          * It's IE if int >=0
246          * 
247          * Use int found in "ieVersion"
248          * 
249          * Official IE 7
250          *              Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; 
251          *              .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
252          * Official IE 8
253          *              Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; 
254          *              .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)
255          * 
256          * IE 11 Compatibility
257          *              Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; SLCC2; .NET CLR 2.0.50727; 
258          *              .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)
259          * 
260          * IE 11 (not Compatiblity)
261          *              Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; 
262          *              .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)
263          * 
264          * @param trans
265          * @return
266          */
267         public static BROWSER browser(AuthzTrans trans, Slot slot) {
268                 BROWSER br = trans.get(slot, null);
269                 if(br==null) {
270                         String agent = trans.agent();
271                         int msie; 
272                         if(agent.contains("iPhone") /* other phones? */) {
273                                 br=BROWSER.iPhone;
274                         } else if ((msie = agent.indexOf("MSIE"))>=0) {
275                                 msie+=5;
276                                 int end = agent.indexOf(";",msie);
277                                 float ver;
278                                 try {
279                                         ver = Float.valueOf(agent.substring(msie,end));
280                                         br = ver<8f?BROWSER.ieOld:BROWSER.ie;
281                                 } catch (Exception e) {
282                                         br = BROWSER.ie;
283                                 }
284                         } else {
285                                 br = BROWSER.html5;
286                         }
287                         trans.put(slot,br);
288                 }
289                 return br;
290         }
291 }
292