AT&T 2.0.19 Code drop, stage 3
[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  * 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.cui;
23
24 import java.io.PrintWriter;
25
26 import javax.servlet.ServletInputStream;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29
30 import org.onap.aaf.auth.cmd.AAFcli;
31 import org.onap.aaf.auth.env.AuthzTrans;
32 import org.onap.aaf.auth.gui.AAF_GUI;
33 import org.onap.aaf.auth.rserv.HttpCode;
34 import org.onap.aaf.cadi.aaf.v2_0.AAFConHttp;
35 import org.onap.aaf.cadi.http.HTransferSS;
36 import org.onap.aaf.cadi.principal.TaggedPrincipal;
37 import org.onap.aaf.misc.env.Env;
38 import org.onap.aaf.misc.env.TimeTaken;
39
40 public class CUI extends HttpCode<AuthzTrans, Void> {
41         private final AAF_GUI gui;
42         public CUI(AAF_GUI gui) {
43                 super(null,"Command Line");
44                 this.gui = gui;
45         }
46
47         @Override
48         public void handle(AuthzTrans trans, HttpServletRequest req,HttpServletResponse resp) throws Exception {
49                 ServletInputStream isr = req.getInputStream();
50                 PrintWriter pw = resp.getWriter();
51                 int c;
52                 StringBuilder cmd = new StringBuilder();
53
54                 while((c=isr.read())>=0) {
55                         cmd.append((char)c);
56                 }
57
58                 TimeTaken tt = trans.start("Execute AAFCLI", Env.REMOTE);
59                 try {
60                         TaggedPrincipal p = trans.getUserPrincipal();
61                         // Access needs to be set after overall construction.  Thus, the lazy create.
62                         AAFcli aafcli;
63                         AAFConHttp aafcon = gui.aafCon();
64                         aafcli= new AAFcli(gui.access,gui.env, pw, 
65                                         aafcon.hman(), 
66                                         aafcon.securityInfo(), 
67                                         new HTransferSS(p,AAF_GUI.app,
68                                         aafcon.securityInfo()));
69                         aafcli.verbose(false);
70                         aafcli.gui(true);
71
72                         String cmdStr = cmd.toString();
73                         if (!cmdStr.contains("--help")) {
74                                 cmdStr = cmdStr.replaceAll("help", "--help");
75                         }
76                         if (!cmdStr.contains("--version")) {
77                                 cmdStr = cmdStr.replaceAll("version", "--version");
78                         }
79                         try {
80                                 aafcli.eval(cmdStr);
81                                 pw.flush();
82                         } catch (Exception e) {
83                                 pw.flush();
84                                 pw.println(e.getMessage());
85                         } finally {
86                                 aafcli.close();
87                         }
88                 } finally {
89                         tt.done();
90                 }
91                 
92         }
93 }