Post Init Service Starter
[aaf/authz.git] / auth / auth-gui / src / main / java / org / onap / aaf / auth / cui / CUI.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Modifications Copyright (C) 2018 IBM.
8  * ============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END====================================================
21  *
22  */
23
24 package org.onap.aaf.auth.cui;
25
26 import java.io.PrintWriter;
27
28 import javax.servlet.ServletInputStream;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31
32 import org.onap.aaf.auth.cmd.AAFcli;
33 import org.onap.aaf.auth.env.AuthzTrans;
34 import org.onap.aaf.auth.env.AuthzEnv;
35 import org.onap.aaf.auth.gui.AAF_GUI;
36 import org.onap.aaf.auth.rserv.HttpCode;
37 import org.onap.aaf.cadi.aaf.v2_0.AAFConHttp;
38 import org.onap.aaf.cadi.http.HTransferSS;
39 import org.onap.aaf.cadi.principal.TaggedPrincipal;
40 import org.onap.aaf.misc.env.Env;
41 import org.onap.aaf.misc.env.TimeTaken;
42
43
44 public class CUI extends HttpCode<AuthzTrans, Void> {
45     private final AAF_GUI gui;
46
47
48     public CUI(AAF_GUI gui) {
49         super(null,"Command Line");
50         this.gui = gui;
51     }
52
53     @Override
54     public void handle(AuthzTrans trans, HttpServletRequest req,HttpServletResponse resp) throws Exception {
55         ServletInputStream isr = req.getInputStream();
56         PrintWriter pw = resp.getWriter();
57         int c;
58         StringBuilder cmd = new StringBuilder();
59
60         while ((c=isr.read())>=0) {
61             cmd.append((char)c);
62         }
63
64         TimeTaken tt = trans.start("Execute AAFCLI", Env.REMOTE);
65         try {
66             TaggedPrincipal p = trans.getUserPrincipal();
67             // Access needs to be set after overall construction.  Thus, the lazy create.
68             AAFcli aafcli;
69             AAFConHttp aafcon = gui.aafCon();
70             aafcli= new AAFcli(gui.access,gui.env, pw,
71                     aafcon.hman(),
72                     aafcon.securityInfo(),
73                     new HTransferSS(p,AAF_GUI.app,
74                             aafcon.securityInfo()));
75             aafcli.verbose(false);
76             aafcli.gui(true);
77
78             String cmdStr = cmd.toString();
79             if (cmdStr.contains("--help")) {
80                 cmdStr = cmdStr.replaceAll("--help", "help");
81             }
82             if (cmdStr.contains("--version")) {
83                 cmdStr = cmdStr.replaceAll("--version", "version");
84             }
85             try {
86                 aafcli.eval(cmdStr);
87                 pw.flush();
88             } catch (Exception e) {
89                 pw.flush();
90                 trans.error().log("Error", e.getMessage());
91             } finally {
92                 aafcli.close();
93             }
94         } finally {
95             tt.done();
96         }
97
98     }
99 }