Sonar Fixes, Formatting
[aaf/authz.git] / auth / auth-gui / src / main / java / org / onap / aaf / auth / gui / pages / UserRoleRemove.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 public class UserRoleRemove extends Page {
45     public static final String HREF = "/gui/urRemove";
46     static final String NAME = "Remove User Role";
47     static final String fields[] = {"user","role"};
48
49     public UserRoleRemove(final AAF_GUI gui, final Page ... breadcrumbs) throws APIException, IOException {
50         super(gui.env,NAME, HREF, fields,
51                 new BreadCrumbs(breadcrumbs),
52                 new NamedCode(true, "content") {
53             @Override
54             public void code(final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
55                 final Slot sUser = gui.env.slot(NAME+".user");
56                 final Slot sRole = gui.env.slot(NAME+".role");
57
58
59                 cache.dynamic(hgen, new DynamicCode<HTMLGen, AAF_GUI, AuthzTrans>() {
60                     @Override
61                     public void code(final AAF_GUI gui, final AuthzTrans trans,    final Cache<HTMLGen> cache, final HTMLGen hgen)    throws APIException, IOException {
62                         final String user = trans.get(sUser, "");
63                         final String role = trans.get(sRole, "");
64
65                         TimeTaken tt = trans.start("Request a user role delete",Env.REMOTE);
66                         try {
67                             gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
68                                 @Override
69                                 public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
70                                     Future<Void> fv = client.delete(
71                                                 "/authz/userRole/"+user+"/"+role+"?request=true",Void.class);
72
73                                     if (fv.get(5000)) {
74                                         // not sure if we'll ever hit this
75                                         hgen.p("User ["+ user+"] Removed from Role [" +role+"]");
76                                     } else {
77                                         if (fv.code() == 202 ) {
78                                             hgen.p("User ["+ user+"] Removal from Role [" +role+"] sent for Approval");
79                                         } else {
80                                             gui.writeError(trans, fv, hgen, 0);
81                                         }
82                                     }
83                                     return null;
84                                 }
85                             });
86                         } catch (Exception e) {
87                             e.printStackTrace();
88                         } finally {
89                             tt.done();
90                         }
91                     }
92                 });
93             }
94
95         });
96     }
97 }