Sonar Fixes, Formatting
[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  * ===========================================================================
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.cmd.AAFcli;
28 import org.onap.aaf.auth.env.AuthzTrans;
29 import org.onap.aaf.auth.gui.AAF_GUI;
30 import org.onap.aaf.auth.gui.BreadCrumbs;
31 import org.onap.aaf.auth.gui.Page;
32 import org.onap.aaf.auth.gui.SlotCode;
33 import org.onap.aaf.cadi.CadiException;
34 import org.onap.aaf.cadi.LocatorException;
35 import org.onap.aaf.cadi.client.Future;
36 import org.onap.aaf.cadi.client.Rcli;
37 import org.onap.aaf.cadi.client.Retryable;
38 import org.onap.aaf.misc.env.APIException;
39 import org.onap.aaf.misc.xgen.Cache;
40 import org.onap.aaf.misc.xgen.DynamicCode;
41 import org.onap.aaf.misc.xgen.html.HTMLGen;
42
43 import aaf.v2_0.CredRequest;
44
45 public class PassDeleteAction extends Page {
46     public static final String NAME = "PassDeleteAction";
47     public static final String HREF = "/gui/passdelete";
48     private static enum Params{id,date,ns,type};
49
50     public PassDeleteAction(final AAF_GUI gui, final Page ... breadcrumbs) throws APIException, IOException {
51         super(gui.env,NAME,HREF,Params.values(),
52             new BreadCrumbs(breadcrumbs),
53             new SlotCode<AuthzTrans>(true,gui.env,NAME,Params.values()) {
54                 @Override
55                 public void code(final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
56                     cache.dynamic(hgen, new DynamicCode<HTMLGen,AAF_GUI, AuthzTrans>() {
57                         @Override
58                         public void code(final AAF_GUI gui, final AuthzTrans trans,final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
59                             final CredRequest cr = new CredRequest();
60                             cr.setId(get(trans,Params.id, ""));
61                             cr.setType(Integer.parseInt(get(trans,Params.type, "0")));
62                             cr.setEntry(get(trans,Params.date,"1960-01-01"));
63                             try {
64                                 String err = gui.clientAsUser(trans.getUserPrincipal(), new Retryable<String>() {
65                                     @Override
66                                     public String code(Rcli<?> client) throws CadiException, ConnectException, APIException {
67                                         Future<CredRequest> fcr = client.delete("/authn/cred", gui.getDF(CredRequest.class),cr);
68                                         if (!fcr.get(AAFcli.timeout())) {
69                                             return gui.aafCon.readableErrMsg(fcr);
70                                         }
71                                         return null;
72                                     }
73                                 });
74                                 if (err==null) {
75                                     hgen.p("Password " + cr.getId() + ", " + cr.getEntry() + " is Deleted");
76                                 } else {
77                                     hgen.p(err);
78                                 }
79                             } catch (LocatorException | CadiException e) {
80                                 throw new APIException(e);
81                             }
82                         }
83                     });
84                 }
85             }
86         );
87     }
88 }