Sonar Fixes, Formatting
[aaf/authz.git] / auth / auth-gui / src / main / java / org / onap / aaf / auth / gui / pages / PermGrantAction.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.pages;
23
24 import java.io.IOException;
25 import java.net.ConnectException;
26
27 import org.onap.aaf.auth.env.AuthzTrans;
28 import org.onap.aaf.auth.gui.AAF_GUI;
29 import org.onap.aaf.auth.gui.BreadCrumbs;
30 import org.onap.aaf.auth.gui.NamedCode;
31 import org.onap.aaf.auth.gui.Page;
32 import org.onap.aaf.cadi.CadiException;
33 import org.onap.aaf.cadi.client.Future;
34 import org.onap.aaf.cadi.client.Rcli;
35 import org.onap.aaf.cadi.client.Retryable;
36 import org.onap.aaf.misc.env.APIException;
37 import org.onap.aaf.misc.env.Env;
38 import org.onap.aaf.misc.env.Slot;
39 import org.onap.aaf.misc.env.TimeTaken;
40 import org.onap.aaf.misc.xgen.Cache;
41 import org.onap.aaf.misc.xgen.DynamicCode;
42 import org.onap.aaf.misc.xgen.html.HTMLGen;
43
44 import aaf.v2_0.Pkey;
45 import aaf.v2_0.RolePermRequest;
46
47 public class PermGrantAction extends Page {
48
49
50     public PermGrantAction(final AAF_GUI gui, final Page ... breadcrumbs) throws APIException, IOException {
51         super(gui.env,PermGrantForm.NAME, PermGrantForm.HREF, PermGrantForm.fields,
52             new BreadCrumbs(breadcrumbs),
53             new NamedCode(true,"content") {
54                 final Slot sType = gui.env.slot(PermGrantForm.NAME+'.'+PermGrantForm.fields[0]);
55                 final Slot sInstance = gui.env.slot(PermGrantForm.NAME+'.'+PermGrantForm.fields[1]);
56                 final Slot sAction = gui.env.slot(PermGrantForm.NAME+'.'+PermGrantForm.fields[2]);
57                 final Slot sRole = gui.env.slot(PermGrantForm.NAME+'.'+PermGrantForm.fields[3]);
58
59                 @Override
60                 public void code(final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
61                     cache.dynamic(hgen, new DynamicCode<HTMLGen,AAF_GUI, AuthzTrans>() {
62                         @Override
63                         public void code(final AAF_GUI gui, final AuthzTrans trans,final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
64
65                             String type = trans.get(sType,null);
66                             String instance = trans.get(sInstance,null);
67                             String action = trans.get(sAction,null);
68                             String role = trans.get(sRole,null);
69
70                             String lastPage = PermGrantForm.HREF
71                                     + "?type=" + type + "&instance=" + instance + "&action=" + action;
72
73                             // Run Validations
74                             boolean fail = true;
75
76                             TimeTaken tt = trans.start("AAF Grant Permission to Role",Env.REMOTE);
77                             try {
78
79                                 final RolePermRequest grantReq = new RolePermRequest();
80                                 Pkey pkey = new Pkey();
81                                 pkey.setType(type);
82                                 pkey.setInstance(instance);
83                                 pkey.setAction(action);
84                                 grantReq.setPerm(pkey);
85                                 grantReq.setRole(role);
86
87                                 fail = gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Boolean>() {
88                                     @Override
89                                     public Boolean code(Rcli<?> client) throws CadiException, ConnectException, APIException {
90                                         boolean fail = true;
91                                         Future<RolePermRequest> fgrant = client.create(
92                                                 "/authz/role/perm",
93                                                 gui.getDF(RolePermRequest.class),
94                                                 grantReq
95                                                 );
96
97                                         if (fgrant.get(5000)) {
98                                             hgen.p("Permission has been granted to role.");
99                                             fail = false;
100                                         } else {
101                                             if (202==fgrant.code()) {
102                                                 hgen.p("Permission Grant Request sent, but must be Approved before actualizing");
103                                                 fail = false;
104                                             } else {
105                                                 gui.writeError(trans, fgrant, hgen, 0);
106                                             }
107                                         }
108                                         return fail;
109                                     }
110                                 });
111                             } catch (Exception e) {
112                                 hgen.p("Unknown Error");
113                                 e.printStackTrace();
114                             } finally {
115                                 tt.done();
116                             }
117
118                             hgen.br();
119                             hgen.incr("a",true,"href="+lastPage);
120                             if (fail) {
121                                 hgen.text("Try again");
122                             } else {
123                                 hgen.text("Grant this Permission to Another Role");
124                             }
125                             hgen.end();
126                             hgen.js()
127                                 .text("alterLink('permgrant', '"+lastPage + "');")
128                                 .done();
129
130                         }
131                     });
132                 }
133             });
134     }
135 }