e54787b976ca9a75eca7b546b466a1ed26dd12b7
[aaf/authz.git] / authz-gui / src / main / java / com / att / authz / gui / pages / UserRoleExtend.java
1 /*******************************************************************************
2  * Copyright (c) 2016 AT&T Intellectual Property. All rights reserved.
3  *******************************************************************************/
4 package com.att.authz.gui.pages;
5
6 import java.io.IOException;
7 import java.net.ConnectException;
8
9 import com.att.authz.env.AuthzTrans;
10 import com.att.authz.gui.AuthGUI;
11 import com.att.authz.gui.BreadCrumbs;
12 import com.att.authz.gui.NamedCode;
13 import com.att.authz.gui.Page;
14 import org.onap.aaf.cadi.CadiException;
15 import org.onap.aaf.cadi.client.Future;
16 import org.onap.aaf.cadi.client.Rcli;
17 import org.onap.aaf.cadi.client.Retryable;
18 import org.onap.aaf.inno.env.APIException;
19 import org.onap.aaf.inno.env.Env;
20 import org.onap.aaf.inno.env.Slot;
21 import org.onap.aaf.inno.env.TimeTaken;
22 import com.att.xgen.Cache;
23 import com.att.xgen.DynamicCode;
24 import com.att.xgen.html.HTMLGen;
25
26 public class UserRoleExtend extends Page {
27         public static final String HREF = "/gui/urExtend";
28         static final String NAME = "Extend User Role";
29         static final String fields[] = {"user","role"};
30
31         public UserRoleExtend(final AuthGUI gui, final Page ... breadcrumbs) throws APIException, IOException {
32                 super(gui.env,NAME, HREF, fields,
33                                 new BreadCrumbs(breadcrumbs),
34                                 new NamedCode(true, "content") {
35                         @Override
36                         public void code(final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
37                                 final Slot sUser = gui.env.slot(NAME+".user");
38                                 final Slot sRole = gui.env.slot(NAME+".role");
39                                 
40                                 
41                                 cache.dynamic(hgen, new DynamicCode<HTMLGen, AuthGUI, AuthzTrans>() {
42                                         @Override
43                                         public void code(AuthGUI gui, AuthzTrans trans, Cache<HTMLGen> cache, HTMLGen hgen)     throws APIException, IOException {                                              
44                                                 final String user = trans.get(sUser, "");
45                                                 final String role = trans.get(sRole, "");
46
47                                                 TimeTaken tt = trans.start("Request to extend user role",Env.REMOTE);
48                                                 try {
49                                                         gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
50                                                                 @Override
51                                                                 public Void code(Rcli<?> client)throws CadiException, ConnectException, APIException {
52                                                                         Future<Void> fv = client.setQueryParams("request=true").update("/authz/userRole/extend/"+user+"/"+role);
53                                                                         if(fv.get(5000)) {
54                                                                                 // not sure if we'll ever hit this
55                                                                                 hgen.p("Extended User ["+ user+"] in Role [" +role+"]");
56                                                                         } else {
57                                                                                 if (fv.code() == 202 ) {
58                                                                                         hgen.p("User ["+ user+"] in Role [" +role+"] Extension sent for Approval");
59                                                                                 } else {
60                                                                                         gui.writeError(trans, fv, hgen);
61                                                                                 }
62                                                                         }
63                                                                         return null;
64                                                                 }
65                                                         });
66                                                 } catch (Exception e) {
67                                                         trans.error().log(e);
68                                                         e.printStackTrace();
69                                                 } finally {
70                                                         tt.done();
71                                                 }
72                                                 
73                                                 
74                                         }
75                                 });
76                         }
77                         
78                 });
79         }
80 }
81