Sonar Fixes
[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 import java.util.regex.Pattern;
28
29 import javax.servlet.ServletInputStream;
30 import javax.servlet.http.Cookie;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33
34 import org.onap.aaf.auth.cmd.AAFcli;
35 import org.onap.aaf.auth.env.AuthzTrans;
36 import org.onap.aaf.auth.gui.AAF_GUI;
37 import org.onap.aaf.auth.gui.Page;
38 import org.onap.aaf.auth.rserv.HttpCode;
39 import org.onap.aaf.cadi.aaf.v2_0.AAFConHttp;
40 import org.onap.aaf.cadi.http.HTransferSS;
41 import org.onap.aaf.cadi.principal.TaggedPrincipal;
42 import org.onap.aaf.misc.env.Env;
43 import org.onap.aaf.misc.env.TimeTaken;
44
45
46 public class CUI extends HttpCode<AuthzTrans, Void> {
47     private final AAF_GUI gui;
48     private static final Pattern userPerm = Pattern.compile("perm (create|delete).*@.*:id.*aaf.gui.*");
49
50
51     public CUI(AAF_GUI gui) {
52         super(null,"Command Line");
53         this.gui = gui;
54     }
55
56     @Override
57     public void handle(AuthzTrans trans, HttpServletRequest req,HttpServletResponse resp) throws Exception {
58         ServletInputStream isr = req.getInputStream();
59         PrintWriter pw = resp.getWriter();
60         int c;
61         StringBuilder cmd = new StringBuilder();
62
63         while ((c=isr.read())>=0) {
64             cmd.append((char)c);
65         }
66
67         TimeTaken tt = trans.start("Execute AAFCLI", Env.REMOTE);
68         try {
69             TaggedPrincipal p = trans.getUserPrincipal();
70             // Access needs to be set after overall construction.  Thus, the lazy create.
71             AAFcli aafcli;
72             AAFConHttp aafcon = gui.aafCon();
73             aafcli= new AAFcli(gui.access,gui.env, pw,
74                     aafcon.hman(),
75                     aafcon.securityInfo(),
76                     new HTransferSS(p,AAF_GUI.APP,
77                             aafcon.securityInfo()));
78             aafcli.verbose(false);
79             aafcli.gui(true);
80
81             String cmdStr = cmd.toString();
82             if (cmdStr.contains("--help")) {
83                 cmdStr = cmdStr.replaceAll("--help", "help");
84             }
85             if (cmdStr.contains("--version")) {
86                 cmdStr = cmdStr.replaceAll("--version", "version");
87             }
88             try {
89                 aafcli.eval(cmdStr);
90                 if(userPerm.matcher(cmdStr).matches()) {
91                     trans.clearCache();
92                     Cookie cookie = new Cookie(Page.AAF_THEME,trans.getProperty(Page.AAF_THEME));
93                     cookie.setSecure(true);
94                     cookie.setMaxAge(-1);
95                     cookie.setComment("Remove AAF GUI Theme");
96                     trans.hresp().addCookie(cookie);
97                 }
98                 pw.flush();
99             } catch (Exception e) {
100                 pw.flush();
101                 trans.error().log("Error", e.getMessage());
102             } finally {
103                 aafcli.close();
104             }
105         } finally {
106             tt.done();
107         }
108
109     }
110 }