1 /*******************************************************************************
2 * Copyright (c) 2016 AT&T Intellectual Property. All rights reserved.
3 *******************************************************************************/
4 package com.att.authz.gui.pages;
6 import java.io.IOException;
7 import java.net.ConnectException;
8 import java.util.ArrayList;
11 import com.att.authz.env.AuthzEnv;
12 import com.att.authz.env.AuthzTrans;
13 import com.att.authz.gui.AuthGUI;
14 import com.att.authz.gui.BreadCrumbs;
15 import com.att.authz.gui.Page;
16 import com.att.authz.gui.Table;
17 import com.att.authz.gui.Table.Cells;
18 import com.att.authz.gui.table.AbsCell;
19 import com.att.authz.gui.table.RefCell;
20 import com.att.authz.gui.table.TextCell;
21 import com.att.cadi.CadiException;
22 import com.att.cadi.client.Future;
23 import com.att.cadi.client.Rcli;
24 import com.att.cadi.client.Retryable;
25 import com.att.cmd.AAFcli;
26 import com.att.inno.env.APIException;
27 import com.att.inno.env.Env;
28 import com.att.inno.env.Slot;
29 import com.att.inno.env.TimeTaken;
30 import com.att.inno.env.util.Chrono;
33 import aaf.v2_0.Nss.Ns;
35 import aaf.v2_0.Perms;
37 import aaf.v2_0.Roles;
38 import aaf.v2_0.Users;
39 import aaf.v2_0.Users.User;
41 public class NsDetail extends Page {
43 public static final String HREF = "/gui/nsdetail";
44 public static final String NAME = "NsDetail";
45 static final String WEBPHONE = "http://webphone.att.com/cgi-bin/webphones.pl?id=";
46 public static enum NS_FIELD { OWNERS, ADMINS, ROLES, PERMISSIONS, CREDS};
47 private static final String BLANK = "";
49 public NsDetail(final AuthGUI gui, Page ... breadcrumbs) throws APIException, IOException {
50 super(gui.env, NAME, HREF, new String[] {"name"},
51 new BreadCrumbs(breadcrumbs),
52 new Table<AuthGUI,AuthzTrans>("Namespace Details",gui.env.newTransNoAvg(),new Model(gui.env()),"class=detail")
57 * Implement the table content for Namespace Detail
61 private static class Model implements Table.Data<AuthGUI,AuthzTrans> {
62 private static final String[] headers = new String[0];
63 private static final String CSP_ATT_COM = "@csp.att.com";
65 public Model(AuthzEnv env) {
66 name = env.slot(NAME+".name");
70 public String[] headers() {
75 public Cells get(final AuthGUI gui, final AuthzTrans trans) {
76 final String nsName = trans.get(name, null);
80 ArrayList<AbsCell[]> rv = new ArrayList<AbsCell[]>();
81 rv.add(new AbsCell[]{new TextCell("Name:"),new TextCell(nsName)});
83 final TimeTaken tt = trans.start("AAF Namespace Details",Env.REMOTE);
85 gui.clientAsUser(trans.getUserPrincipal(),new Retryable<Void>() {
87 public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
88 Future<Nss> fn = client.read("/authz/nss/"+nsName,gui.nssDF);
90 if(fn.get(AuthGUI.TIMEOUT)) {
93 // TimeTaken tt = trans.start("Load Data", Env.SUB);
95 for(Ns n : fn.value.getNs()) {
96 String desc = (n.getDescription()!=null?n.getDescription():BLANK);
97 rv.add(new AbsCell[]{new TextCell("Description:"),new TextCell(desc)});
99 addField(trans, rv, n.getAdmin(), NS_FIELD.ADMINS);
100 addField(trans, rv, n.getResponsible(), NS_FIELD.OWNERS);
102 Future<Users> fu = client.read(
103 "/authn/creds/ns/"+nsName,
106 List<String> creds = new ArrayList<String>();
107 if(fu.get(AAFcli.timeout())) {
108 for (User u : fu.value.getUser()) {
109 StringBuilder sb = new StringBuilder(u.getId());
110 switch(u.getType()) {
111 case 1: sb.append(" (U/Pass) "); break;
112 case 10: sb.append(" (Cert) "); break;
113 case 200: sb.append(" (x509) "); break;
117 sb.append(Chrono.niceDateStamp(u.getExpires()));
118 creds.add(sb.toString());
121 addField(trans, rv, creds, NS_FIELD.CREDS);
123 Future<Roles> fr = client.read(
124 "/authz/roles/ns/"+nsName,
127 List<String> roles = new ArrayList<String>();
128 if(fr.get(AAFcli.timeout())) {
129 for (Role r : fr.value.getRole()) {
130 roles.add(r.getName());
133 addField(trans, rv, roles, NS_FIELD.ROLES);
136 Future<Perms> fp = client.read(
137 "/authz/perms/ns/"+nsName,
140 List<String> perms = new ArrayList<String>();
142 if(fp.get(AAFcli.timeout())) {
143 for (Perm p : fp.value.getPerm()) {
144 perms.add(p.getType() + "|" + p.getInstance() + "|" + p.getAction());
147 addField(trans, rv, perms, NS_FIELD.PERMISSIONS);
149 String historyLink = NsHistory.HREF
151 rv.add(new AbsCell[] {new RefCell("See History",historyLink)});
156 rv.add(new AbsCell[] {new TextCell("*** Data Unavailable ***")});
161 } catch (Exception e) {
166 return new Cells(rv,null);
169 private void addField(AuthzTrans trans, ArrayList<AbsCell[]> rv, List<String> values, NS_FIELD field) {
170 if (!values.isEmpty()) {
175 for (int i=0; i< values.size(); i++) {
176 AbsCell label = (i==0?new TextCell(sentenceCase(field)+":"):AbsCell.Null);
177 String user = values.get(i);
178 AbsCell userCell = (user.endsWith(CSP_ATT_COM)?
179 new RefCell(user,WEBPHONE + user.substring(0,user.indexOf('@'))):new TextCell(user));
180 rv.add(new AbsCell[] {
187 for (int i=0; i< values.size(); i++) {
188 AbsCell label = (i==0?new TextCell(sentenceCase(field)+":"):AbsCell.Null);
189 rv.add(new AbsCell[] {
191 new TextCell(values.get(i))
196 for (int i=0; i< values.size(); i++) {
197 AbsCell label = (i==0?new TextCell(sentenceCase(field)+":"):AbsCell.Null);
198 String perm = values.get(i);
199 String[] fields = perm.split("\\|");
200 String grantLink = PermGrantForm.HREF
201 + "?type=" + fields[0].trim()
202 + "&instance=" + fields[1].trim()
203 + "&action=" + fields[2].trim();
205 rv.add(new AbsCell[] {
208 new RefCell("Grant This Perm", grantLink)
217 private String sentenceCase(NS_FIELD field) {
218 String sField = field.toString();
219 return sField.substring(0, 1).toUpperCase() + sField.substring(1).toLowerCase();