PassDeleteAction.java-sonar major fixes
[aaf/authz.git] / auth / auth-gui / src / main / java / org / onap / aaf / auth / gui / pages / PassDeleteAction.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 IBM.
7  * ===========================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END====================================================
20  *
21  */
22
23 package org.onap.aaf.auth.gui.pages;
24
25 import java.io.IOException;
26 import java.net.ConnectException;
27
28 import org.onap.aaf.auth.cmd.AAFcli;
29 import org.onap.aaf.auth.env.AuthzTrans;
30 import org.onap.aaf.auth.gui.AAF_GUI;
31 import org.onap.aaf.auth.gui.BreadCrumbs;
32 import org.onap.aaf.auth.gui.Page;
33 import org.onap.aaf.auth.gui.SlotCode;
34 import org.onap.aaf.cadi.CadiException;
35 import org.onap.aaf.cadi.LocatorException;
36 import org.onap.aaf.cadi.client.Future;
37 import org.onap.aaf.cadi.client.Rcli;
38 import org.onap.aaf.cadi.client.Retryable;
39 import org.onap.aaf.misc.env.APIException;
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.CredRequest;
45
46 public class PassDeleteAction extends Page {
47     public static final String CLASS = "PassDeleteAction";
48     public static final String HREF = "/gui/passdelete";
49     private enum Params{id,date,ns,type};
50
51     public PassDeleteAction(final AAF_GUI gui, final Page ... breadcrumbs) throws APIException, IOException {
52         super(gui.env,CLASS,HREF,Params.values(),
53             new BreadCrumbs(breadcrumbs),
54             new SlotCode<AuthzTrans>(true,gui.env,CLASS,Params.values()) {
55                 @Override
56                 public void code(final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
57                     cache.dynamic(hgen, new DynamicCode<HTMLGen,AAF_GUI, AuthzTrans>() {
58                         @Override
59                         public void code(final AAF_GUI gui, final AuthzTrans trans,final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
60                             final CredRequest cr = new CredRequest();
61                             cr.setId(get(trans,Params.id, ""));
62                             cr.setType(Integer.parseInt(get(trans,Params.type, "0")));
63                             cr.setEntry(get(trans,Params.date,"1960-01-01"));
64                             try {
65                                 String err = gui.clientAsUser(trans.getUserPrincipal(), new Retryable<String>() {
66                                     @Override
67                                     public String code(Rcli<?> client) throws CadiException, ConnectException, APIException {
68                                         Future<CredRequest> fcr = client.delete("/authn/cred", gui.getDF(CredRequest.class),cr);
69                                         if (!fcr.get(AAFcli.timeout())) {
70                                             return gui.aafCon.readableErrMsg(fcr);
71                                         }
72                                         return null;
73                                     }
74                                 });
75                                 if (err==null) {
76                                     hgen.p("Password " + cr.getId() + ", " + cr.getEntry() + " is Deleted");
77                                 } else {
78                                     hgen.p(err);
79                                 }
80                             } catch (LocatorException | CadiException e) {
81                                 throw new APIException(e);
82                             }
83                         }
84                     });
85                 }
86             }
87         );
88     }
89 }