summaryrefslogtreecommitdiffstats
path: root/authz-gui/src/main/java/com/att/authz/gui/Page.java
blob: a8c48e69c919ec8992a03cebc164f0ef9e6b68f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*******************************************************************************
 * Copyright (c) 2016 AT&T Intellectual Property. All rights reserved.
 *******************************************************************************/
package com.att.authz.gui;

import static com.att.xgen.html.HTMLGen.A;
import static com.att.xgen.html.HTMLGen.H1;
import static com.att.xgen.html.HTMLGen.LI;
import static com.att.xgen.html.HTMLGen.TITLE;
import static com.att.xgen.html.HTMLGen.UL;

import java.io.IOException;
import java.security.Principal;

import com.att.authz.env.AuthzEnv;
import com.att.authz.env.AuthzTrans;
import org.onap.aaf.cadi.config.Config;
import org.onap.aaf.inno.env.APIException;
import org.onap.aaf.inno.env.Slot;
import org.onap.aaf.inno.env.util.Split;
import com.att.xgen.Cache;
import com.att.xgen.CacheGen;
import com.att.xgen.Code;
import com.att.xgen.DynamicCode;
import com.att.xgen.Mark;
import com.att.xgen.html.HTMLCacheGen;
import com.att.xgen.html.HTMLGen;
import com.att.xgen.html.Imports;

/**
 * A Base "Mobile First" Page 
 * 
 *
 */
public class Page extends HTMLCacheGen {
	public static enum BROWSER {iPhone,html5,ie,ieOld};
	
	public static final int MAX_LINE=20;

	protected static final String[] NO_FIELDS = new String[0];

	private static final String ENV_CONTEXT = "envContext";
	private static final String DME_SERVICE_NAME = "DMEServiceName";
	private static final String ROUTE_OFFER = "routeOffer";
	private static final String BROWSER_TYPE = "BROWSER_TYPE";

	private final String bcName, bcUrl;
	private final String[] fields;

	public final boolean no_cache;

	public String name() {
		return bcName;
	}
	
	public String url() {
		return bcUrl;
	}
	
	public String[] fields() {
		return fields;
	}
	
	public Page(AuthzEnv env, String name, String url, String [] fields, final NamedCode ... content) throws APIException,IOException {
		this(env,name,url,1,fields,content);
	}
	
	public Page(AuthzEnv env, String name, String url, int backdots, String [] fields, final NamedCode ... content) throws APIException,IOException {
		super(CacheGen.PRETTY, new PageCode(env, backdots, content));
		bcName = name;
		bcUrl = url;
		this.fields = fields;
		// Mark which fields must be "no_cache"
		boolean no_cacheTemp=false;
		for(NamedCode nc : content) {
			if(nc.no_cache) { 
				no_cacheTemp=true;
				break;
			}
		}
		no_cache=no_cacheTemp;
	}
	
	private static class PageCode implements Code<HTMLGen> {
			private final NamedCode[] content;
			private final Slot browserSlot;
			private final int backdots;
			protected AuthzEnv env;

			public PageCode(AuthzEnv env, int backdots, final NamedCode[] content) {
				this.content = content;
				this.backdots = backdots;
				browserSlot = env.slot(BROWSER_TYPE);
				this.env = env;
			}
			
			@Override
			public void code(Cache<HTMLGen> cache, HTMLGen hgen) throws APIException, IOException {
				// Note: I found that App Storage saves everything about the page, or not.  Thus, if you declare the page uncacheable, none of the 
				// Artifacts, like JPGs are stored, which makes this feature useless for Server driven elements
				//hgen.html("manifest=../theme/aaf.appcache");
				cache.dynamic(hgen,  new DynamicCode<HTMLGen,AuthGUI,AuthzTrans>() {
					@Override
					public void code(AuthGUI state, AuthzTrans trans, Cache<HTMLGen> cache, HTMLGen hgen) throws APIException, IOException {
						switch(browser(trans,browserSlot)) {
							case ieOld:
							case ie:
								hgen.directive("!DOCTYPE html");
								hgen.directive("meta", "http-equiv=X-UA-Compatible","content=IE=11");
							default:
						}
					}
				});
				hgen.html();
				Mark head = hgen.head();
					hgen.leaf(TITLE).text("AT&amp;T Authentication/Authorization Tool").end();
					hgen.imports(new Imports(backdots).css("theme/aaf5.css")
								 			   	.js("theme/comm.js")
												.js("theme/console.js")
												.js("theme/common.js"));
					cache.dynamic(hgen, new DynamicCode<HTMLGen,AuthGUI,AuthzTrans>() {
						@Override
						public void code(AuthGUI state, AuthzTrans trans, Cache<HTMLGen> cache, HTMLGen hgen) throws APIException, IOException {
							switch(browser(trans,browserSlot)) {
								case iPhone:
									hgen.imports(new Imports(backdots).css("theme/aaf5iPhone.css"));
									break;
								case ie:
								case ieOld:
									hgen.js().text("document.createElement('header');")
											.text("document.createElement('nav');")
											.done();
								case html5:
									hgen.imports(new Imports(backdots).css("theme/aaf5Desktop.css"));
									break;
							}
						}
					});
					hgen.end(head);
					
				Mark body = hgen.body();
					Mark header = hgen.header();
					cache.dynamic(hgen, new DynamicCode<HTMLGen,AuthGUI,AuthzTrans>() {
						@Override
						public void code(AuthGUI state, AuthzTrans trans,Cache<HTMLGen> cache, HTMLGen xgen)
								throws APIException, IOException {
							// Obtain Server Info, and print
							String DMEServiceName = trans.getProperty(DME_SERVICE_NAME);
							String env = DMEServiceName.substring(
									DMEServiceName.indexOf(ENV_CONTEXT),
									DMEServiceName.indexOf(ROUTE_OFFER) -1).split("=")[1];
							
							xgen.leaf(H1).text("AT&amp;T Auth Tool on " + env).end();
							xgen.leaf("p","id=version").text("AAF Version: " + trans.getProperty(Config.AAF_DEPLOYED_VERSION, "N/A")).end();
							
							// Obtain User Info, and print
							Principal p = trans.getUserPrincipal();
							String user;
							if(p==null) {
								user = "please choose a Login Authority";
							} else {
								user = p.getName();
							}
							xgen.leaf("p","id=welcome").text("Welcome, " + user).end();
							
							switch(browser(trans,browserSlot)) {
								case ieOld:
								case ie:
									xgen.incr("h5").text("This app is Mobile First HTML5.  Internet Explorer " 
											+ " does not support all HTML5 standards. Old, non TSS-Standard versions may not function correctly.").br()
											.text("  For best results, use a highly compliant HTML5 browser like Firefox.")
										.end();
									break;
								default:
							}
						}
					});
					
					hgen.hr();
					
					int cIdx;
					NamedCode nc;
					// If BreadCrumbs, put here
					if(content.length>0 && content[0] instanceof BreadCrumbs) {
						nc = content[0];
						Mark ctnt = hgen.divID(nc.idattrs());
						nc.code(cache, hgen);
						hgen.end(ctnt);
						cIdx = 1;
					} else {
						cIdx = 0;
					}
					
					hgen.end(header);
					
					Mark inner = hgen.divID("inner");
						// Content
						for(int i=cIdx;i<content.length;++i) {
							nc = content[i];
							Mark ctnt = hgen.divID(nc.idattrs());
							nc.code(cache, hgen);
							hgen.end(ctnt);
						}

					hgen.end(inner);	
					
					// Navigation - Using older Nav to work with decrepit  IE versions
					
					Mark nav = hgen.divID("nav");
					hgen.incr("h2").text("Related Links").end();
					hgen.incr(UL)
						 .leaf(LI).leaf(A,"href="+env.getProperty("aaf_url.aaf_help")).text("AAF WIKI").end(2)
						 .leaf(LI).leaf(A,"href="+env.getProperty("aaf_url.cadi_help")).text("CADI WIKI").end(2);
						String tools = env.getProperty("aaf_tools");
						if(tools!=null) {
							hgen.hr()
								.incr(HTMLGen.UL,"style=margin-left:5%")
							 	.leaf(HTMLGen.H3).text("Related Tools").end();

							for(String tool : Split.splitTrim(',',tools)) {
								hgen.leaf(LI).leaf(A,"href="+env.getProperty("aaf_url.tool."+tool)).text(tool.toUpperCase() + " Help").end(2);
							}
							hgen.end();
						}
						 hgen.end();
					
					hgen.hr();
					
					hgen.end(nav);
					// Footer - Using older Footer to work with decrepit IE versions
					Mark footer = hgen.divID("footer");
						hgen.textCR(1, "(c) 2014-6 AT&amp;T Inc. All Rights Reserved")
						.end(footer);
						
					hgen.end(body);
				hgen.endAll();
		}
	}

	public static String getBrowserType() {
		return BROWSER_TYPE;
	}
	
	/**
	 * It's IE if int >=0
	 * 
	 * Use int found in "ieVersion"
	 * 
	 * Official IE 7
	 * 		Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; 
	 * 		.NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
	 * Official IE 8
	 * 		Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; 
	 * 		.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)
	 * 
	 * IE 11 Compatibility
	 * 		Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; SLCC2; .NET CLR 2.0.50727; 
	 * 		.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)
	 * 
	 * IE 11 (not Compatiblity)
	 * 		Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; 
	 * 		.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)
	 * 
	 * @param trans
	 * @return
	 */
	public static BROWSER browser(AuthzTrans trans, Slot slot) {
		BROWSER br = trans.get(slot, null);
		if(br==null) {
			String agent = trans.agent();
			int msie; 
			if(agent.contains("iPhone") /* other phones? */) {
				br=BROWSER.iPhone;
			} else if ((msie = agent.indexOf("MSIE"))>=0) {
				msie+=5;
				int end = agent.indexOf(";",msie);
				float ver;
				try {
					ver = Float.valueOf(agent.substring(msie,end));
					br = ver<8f?BROWSER.ieOld:BROWSER.ie;
				} catch (Exception e) {
					br = BROWSER.ie;
				}
			} else {
				br = BROWSER.html5;
			}
			trans.put(slot,br);
		}
		return br;
	}
}