Remove Sonar Blockers
[aaf/authz.git] / auth / auth-cmd / src / main / java / org / onap / aaf / auth / cmd / AAFcli.java
index e8069b8..2d5e172 100644 (file)
@@ -30,7 +30,6 @@ import java.io.PrintWriter;
 import java.io.Reader;
 import java.io.Writer;
 import java.net.HttpURLConnection;
-import java.net.URI;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -42,12 +41,11 @@ import org.onap.aaf.auth.cmd.user.User;
 import org.onap.aaf.auth.common.Define;
 import org.onap.aaf.auth.env.AuthzEnv;
 import org.onap.aaf.cadi.Access;
+import org.onap.aaf.cadi.Access.Level;
 import org.onap.aaf.cadi.CadiException;
-import org.onap.aaf.cadi.Locator;
 import org.onap.aaf.cadi.PropAccess;
 import org.onap.aaf.cadi.SecuritySetter;
-import org.onap.aaf.cadi.Access.Level;
-import org.onap.aaf.cadi.aaf.v2_0.AAFLocator;
+import org.onap.aaf.cadi.aaf.v2_0.AAFConHttp;
 import org.onap.aaf.cadi.client.Retryable;
 import org.onap.aaf.cadi.config.Config;
 import org.onap.aaf.cadi.config.SecurityInfoC;
@@ -59,7 +57,6 @@ import org.onap.aaf.misc.env.APIException;
 import jline.console.ConsoleReader;
 
 public class AAFcli {
-       private static final String HTTPS = "https://";
        protected static PrintWriter pw;
        protected HMangr hman;
        // Storage for last reused client. We can do this
@@ -72,7 +69,7 @@ public class AAFcli {
        private List<Cmd> cmds;
 
        // Lex State
-       private ArrayList<Integer> expect = new ArrayList<Integer>();
+       private ArrayList<Integer> expect = new ArrayList<>();
        private boolean verbose = true;
        private int delay;
        private SecurityInfoC<HttpURLConnection> si;
@@ -95,11 +92,11 @@ public class AAFcli {
        }
 
        // Create when only have Access
-       public AAFcli(Access access, Writer wtr, HMangr hman, SecurityInfoC<HttpURLConnection> si, SecuritySetter<HttpURLConnection> ss) throws APIException {
+       public AAFcli(Access access, Writer wtr, HMangr hman, SecurityInfoC<HttpURLConnection> si, SecuritySetter<HttpURLConnection> ss) throws APIException, CadiException {
                this(access,new AuthzEnv(access.getProperties()),wtr,hman, si,ss);
        }
 
-       public AAFcli(Access access, AuthzEnv env, Writer wtr, HMangr hman, SecurityInfoC<HttpURLConnection> si, SecuritySetter<HttpURLConnection> ss) throws APIException {
+       public AAFcli(Access access, AuthzEnv env, Writer wtr, HMangr hman, SecurityInfoC<HttpURLConnection> si, SecuritySetter<HttpURLConnection> ss) throws APIException, CadiException {
                this.env = env;
                this.access = access;
                this.ss = ss;
@@ -113,11 +110,10 @@ public class AAFcli {
                        close = true;
                }
 
-
                /*
                 * Create Cmd Tree
                 */
-               cmds = new ArrayList<Cmd>();
+               cmds = new ArrayList<>();
 
                Role role = new Role(this);
                cmds.add(new Help(this, cmds));
@@ -134,10 +130,10 @@ public class AAFcli {
        }
 
        public void close() {
-               if (hman != null) {
-                       hman.close();
-                       hman = null;
-               }
+//             if (hman != null) {
+//                     hman.close();
+//                     hman = null;
+//             }
                if (close) {
                        pw.close();
                }
@@ -202,7 +198,7 @@ public class AAFcli {
                                                if (pass != null) {
                                                        pass = access.decrypt(pass, false);
                                                        access.getProperties().put(user, pass);
-                                                       ss = new HBasicAuthSS(si, user, pass);
+                                                       ss=new HBasicAuthSS(si, user, pass);
                                                        pw.println("as " + user);
                                                } else { // get Pass from System Properties, under name of
                                                        // Tag
@@ -247,7 +243,10 @@ public class AAFcli {
                                } else if ("pause".equalsIgnoreCase(largs[idx])) {
                                        pw.println("Press <Return> to continue...");
                                        ++idx;
-                                       new BufferedReader(new InputStreamReader(System.in)).readLine();
+                                       // Sonar insists we do something with the string, though it's only a pause.  Not very helpful...
+                                       String sonar = new BufferedReader(new InputStreamReader(System.in)).readLine();
+                                       sonar=""; // this useless code brought to you by Sonar.
+                                       pw.print(sonar);
                                        continue;
                                } else if ("exit".equalsIgnoreCase(largs[idx])) {
                                        pw.println("Exiting...");
@@ -324,7 +323,7 @@ public class AAFcli {
                                        try {
                                                ret = c.exec(++idx, largs);
                                                if (delay+globalDelay > 0) {
-                                                       Thread.sleep(delay+globalDelay);
+                                                       Thread.sleep((long)(delay+globalDelay));
                                                }
                                        } catch (Exception e) {
                                                if (expect.contains(-1)) {
@@ -359,7 +358,7 @@ public class AAFcli {
 
        private String[] argEval(String line) {
                StringBuilder sb = new StringBuilder();
-               ArrayList<String> arr = new ArrayList<String>();
+               ArrayList<String> arr = new ArrayList<>();
                boolean start = true;
                char quote = 0;
                char last = 0;
@@ -434,181 +433,179 @@ public class AAFcli {
                
                try {
                        AAFSSO aafsso = new AAFSSO(args);
+                       String noexit = aafsso.access().getProperty("no_exit");
                        try {
                                PropAccess access = aafsso.access();
-                               Define.set(access);
-                               AuthzEnv env = new AuthzEnv(access);
-                               
-                               StringBuilder err = aafsso.err();
-                               String noexit = access.getProperty("no_exit");
-                               if (err != null) {
-                                       err.append("to continue...");
-                                       System.err.println(err);
-                                       if(noexit!=null) {
-                                               System.exit(1);
-                                       }
-                               }
-       
-                               Reader rdr = null;
-                               boolean exitOnFailure = true;
-                               /*
-                                * Check for "-" options anywhere in command line
-                                */
-                               StringBuilder sb = new StringBuilder();
-                               for (int i = 0; i < args.length; ++i) {
-                                       if ("-i".equalsIgnoreCase(args[i])) {
-                                               rdr = new InputStreamReader(System.in);
-                                               // } else if("-o".equalsIgnoreCase(args[i])) {
-                                               // // shall we do something different? Output stream is
-                                               // already done...
-                                       } else if ("-f".equalsIgnoreCase(args[i])) {
-                                               if (args.length > i + 1) {
-                                                       rdr = new FileReader(args[++i]);
-                                               }
-                                       } else if ("-a".equalsIgnoreCase(args[i])) {
-                                               exitOnFailure = false;
-                                       } else if ("-c".equalsIgnoreCase(args[i])) {
-                                               isConsole = true;
-                                       } else if ("-s".equalsIgnoreCase(args[i]) && args.length > i + 1) {
-                                               access.setProperty(Cmd.STARTDATE, args[++i]);
-                                       } else if ("-e".equalsIgnoreCase(args[i]) && args.length > i + 1) {
-                                               access.setProperty(Cmd.ENDDATE, args[++i]);
-                                       } else if ("-t".equalsIgnoreCase(args[i])) {
-                                               isTest = true;
-                                       } else if ("-d".equalsIgnoreCase(args[i])) {
-                                               showDetails = true;
-                                       } else if ("-n".equalsIgnoreCase(args[i])) {
-                                               ignoreDelay = true;
-                                       } else {
-                                               if (sb.length() > 0) {
-                                                       sb.append(' ');
-                                               }
-                                               sb.append(args[i]);
-                                       }
-                               }
-       
-                               SecurityInfoC<HttpURLConnection> si = SecurityInfoC.instance(access, HttpURLConnection.class);
-                               Locator<URI> loc;
-                               String aafUrl = access.getProperty(Config.AAF_URL);
-                               if(aafUrl==null) {
-                                       aafsso.setLogDefault();
-                                       aafsso.setStdErrDefault();
-                                       aafUrl=AAFSSO.cons.readLine("aaf_url=%s", HTTPS);
-                                       if(aafUrl.length()==0) {
-                                               System.exit(0);
-                                       } else if(!aafUrl.startsWith(HTTPS)) {
-                                               aafUrl=HTTPS+aafUrl;
-                                       }
-                                       aafsso.addProp(Config.AAF_URL, aafUrl);
-                               } 
-                               // Note, with AAF Locator, this may not longer be necessary 3/2018 Jonathan
-                               if(!aafsso.loginOnly()) {
-                                       try {
-                                               loc = new AAFLocator(si,new URI(aafUrl));
-                                       } catch (Throwable t) {
-                                               aafsso.setStdErrDefault();
-                                               throw t;
-                                       } finally {
-                                               // Other Access is done writing to StdOut and StdErr, reset Std out
-                                               aafsso.setLogDefault();
-                                       }
 
-                                       TIMEOUT = Integer.parseInt(access.getProperty(Config.AAF_CONN_TIMEOUT, Config.AAF_CONN_TIMEOUT_DEF));
-                                       HMangr hman = new HMangr(access, loc).readTimeout(TIMEOUT).apiVersion("2.0");
+                               if(aafsso.ok()) {
+                                       Define.set(access);
+                                       AuthzEnv env = new AuthzEnv(access);
                                        
-                                       if(access.getProperty(Config.AAF_DEFAULT_REALM)==null) {
-                                               access.log(Level.ERROR, Config.AAF_DEFAULT_REALM,"is required");
+                                       Reader rdr = null;
+                                       boolean exitOnFailure = true;
+                                       /*
+                                        * Check for "-" options anywhere in command line
+                                        */
+                                       StringBuilder sb = new StringBuilder();
+                                       for (int i = 0; i < args.length; ++i) {
+                                               if ("-i".equalsIgnoreCase(args[i])) {
+                                                       rdr = new InputStreamReader(System.in);
+                                                       // } else if("-o".equalsIgnoreCase(args[i])) {
+                                                       // // shall we do something different? Output stream is
+                                                       // already done...
+                                               } else if ("-f".equalsIgnoreCase(args[i])) {
+                                                       if (args.length > i + 1) {
+                                                               rdr = new FileReader(args[++i]);
+                                                       }
+                                               } else if ("-a".equalsIgnoreCase(args[i])) {
+                                                       exitOnFailure = false;
+                                               } else if ("-c".equalsIgnoreCase(args[i])) {
+                                                       isConsole = true;
+                                               } else if ("-s".equalsIgnoreCase(args[i]) && args.length > i + 1) {
+                                                       access.setProperty(Cmd.STARTDATE, args[++i]);
+                                               } else if ("-e".equalsIgnoreCase(args[i]) && args.length > i + 1) {
+                                                       access.setProperty(Cmd.ENDDATE, args[++i]);
+                                               } else if ("-t".equalsIgnoreCase(args[i])) {
+                                                       isTest = true;
+                                               } else if ("-d".equalsIgnoreCase(args[i])) {
+                                                       showDetails = true;
+                                               } else if ("-n".equalsIgnoreCase(args[i])) {
+                                                       ignoreDelay = true;
+                                               } else {
+                                                       if (sb.length() > 0) {
+                                                               sb.append(' ');
+                                                       }
+                                                       sb.append(args[i]);
+                                               }
                                        }
-               
                                        
-                                       AAFcli aafcli = new AAFcli(access,env, new OutputStreamWriter(System.out), hman, si, 
-                                               new HBasicAuthSS(si,aafsso.user(), access.decrypt(aafsso.enc_pass(),false)));
-                                       if(!ignoreDelay) {
-                                               File delay = new File("aafcli.delay");
-                                               if(delay.exists()) {
-                                                       BufferedReader br = new BufferedReader(new FileReader(delay));
-                                                       try {
-                                                               globalDelay = Integer.parseInt(br.readLine());
-                                                       } catch(Exception e) {
-                                                               access.log(Level.DEBUG,e);
-                                                       } finally {
-                                                               br.close();
+                                       AAFConHttp aafcon = new AAFConHttp(access);
+//                                     
+//                                     SecurityInfoC<?> si = aafcon.securityInfo();
+//                                     Locator<URI> loc;
+                                       
+                                       aafsso.setLogDefault();
+                                       aafsso.setStdErrDefault();
+       
+                                       // Note, with AAF Locator, this may not longer be necessary 3/2018 Jonathan
+                                       if(!aafsso.loginOnly()) {
+//                                             try {
+//                                                     loc = new AAFLocator(si,new URI(access.getProperty(Config.AAF_URL)));
+//                                             } catch (Throwable t) {
+//                                                     aafsso.setStdErrDefault();
+//                                                     throw t;
+//                                             } finally {
+//                                                     // Other Access is done writing to StdOut and StdErr, reset Std out
+//                                                     aafsso.setLogDefault();
+//                                             }
+       
+                                               TIMEOUT = Integer.parseInt(access.getProperty(Config.AAF_CONN_TIMEOUT, Config.AAF_CONN_TIMEOUT_DEF));
+//                                             HMangr hman = new HMangr(access, loc).readTimeout(TIMEOUT).apiVersion(Config.AAF_DEFAULT_VERSION);
+                                               
+                                               if(access.getProperty(Config.AAF_DEFAULT_REALM)==null) {
+                                                       access.setProperty(Config.AAF_DEFAULT_REALM, "people.osaaf.org");
+                                                       aafsso.addProp(Config.AAF_DEFAULT_REALM, "people.osaaf.org");
+                                               }
+                       
+                                               AAFcli aafcli = new AAFcli(access,env, new OutputStreamWriter(System.out),  
+                                                               aafcon.hman(), aafcon.securityInfo(), aafcon.securityInfo().defSS);
+//                                                     new HBasicAuthSS(si,aafsso.user(), access.decrypt(aafsso.enc_pass(),false)));
+//                                             }
+                                               if(!ignoreDelay) {
+                                                       File delay = new File("aafcli.delay");
+                                                       if(delay.exists()) {
+                                                               BufferedReader br = new BufferedReader(new FileReader(delay));
+                                                               try {
+                                                                       globalDelay = Integer.parseInt(br.readLine());
+                                                               } catch(Exception e) {
+                                                                       access.log(Level.DEBUG,e);
+                                                               } finally {
+                                                                       br.close();
+                                                               }
                                                        }
                                                }
-                                       }
-                                       try {
-                                               if (isConsole) {
-                                                       System.out.println("Type 'help' for short help or 'help -d' for detailed help with aafcli commands");
-                                                       System.out.println("Type '?' for help with command line editing");
-                                                       System.out.println("Type 'q', 'quit', or 'exit' to quit aafcli\n");
-               
-                                                       ConsoleReader reader = new ConsoleReader();
-                                                       try {
-                                                               reader.setPrompt("aafcli > ");
+                                               try {
+                                                       if (isConsole) {
+                                                               System.out.println("Type 'help' for short help or 'help -d' for detailed help with aafcli commands");
+                                                               System.out.println("Type '?' for help with command line editing");
+                                                               System.out.println("Type 'q', 'quit', or 'exit' to quit aafcli\n");
                        
+                                                               ConsoleReader reader = new ConsoleReader();
+                                                               try {
+                                                                       reader.setPrompt("aafcli > ");
+                               
+                                                                       String line;
+                                                                       while ((line = reader.readLine()) != null) {
+                                                                               showDetails = (line.contains("-d"))?true:false;
+                               
+                                                                               if (line.equalsIgnoreCase("quit") || line.equalsIgnoreCase("q") || line.equalsIgnoreCase("exit")) {
+                                                                                       break;
+                                                                               } else if (line.equalsIgnoreCase("--help -d") || line.equalsIgnoreCase("help -d") 
+                                                                                               || line.equalsIgnoreCase("help")) {
+                                                                                       line = "--help";
+                                                                               } else if (line.equalsIgnoreCase("cls")) {
+                                                                                       reader.clearScreen();
+                                                                                       continue;
+                                                                               } else if (line.equalsIgnoreCase("?")) {
+                                                                                       keyboardHelp();
+                                                                                       continue;
+                                                                               }
+                                                                               try {
+                                                                                       aafcli.eval(line);
+                                                                                       pw.flush();
+                                                                               } catch (Exception e) {
+                                                                                       pw.println(e.getMessage());
+                                                                                       pw.flush();
+                                                                               }
+                                                                       }
+                                                               } finally {
+                                                                       reader.close();
+                                                               }
+                                                       } else if (rdr != null) {
+                                                               BufferedReader br = new BufferedReader(rdr);
                                                                String line;
-                                                               while ((line = reader.readLine()) != null) {
-                                                                       showDetails = (line.contains("-d"))?true:false;
-                       
-                                                                       if (line.equalsIgnoreCase("quit") || line.equalsIgnoreCase("q") || line.equalsIgnoreCase("exit")) {
+                                                               while ((line = br.readLine()) != null) {
+                                                                       if (!aafcli.eval(line) && exitOnFailure) {
+                                                                               rv = 1;
                                                                                break;
-                                                                       } else if (line.equalsIgnoreCase("--help -d") || line.equalsIgnoreCase("help -d") 
-                                                                                       || line.equalsIgnoreCase("help")) {
-                                                                               line = "--help";
-                                                                       } else if (line.equalsIgnoreCase("cls")) {
-                                                                               reader.clearScreen();
-                                                                               continue;
-                                                                       } else if (line.equalsIgnoreCase("?")) {
-                                                                               keyboardHelp();
-                                                                               continue;
-                                                                       }
-                                                                       try {
-                                                                               aafcli.eval(line);
-                                                                               pw.flush();
-                                                                       } catch (Exception e) {
-                                                                               pw.println(e.getMessage());
-                                                                               pw.flush();
                                                                        }
                                                                }
-                                                       } finally {
-                                                               reader.close();
-                                                       }
-                                               } else if (rdr != null) {
-                                                       BufferedReader br = new BufferedReader(rdr);
-                                                       String line;
-                                                       while ((line = br.readLine()) != null) {
-                                                               if (!aafcli.eval(line) && exitOnFailure) {
-                                                                       rv = 1;
-                                                                       break;
+                                                       } else { // just run the command line
+                                                               aafcli.verbose(false);
+                                                               if (sb.length() == 0) {
+                                                                       sb.append("--help");
                                                                }
+                                                               rv = aafcli.eval(sb.toString()) ? 0 : 1;
                                                        }
-                                               } else { // just run the command line
-                                                       aafcli.verbose(false);
-                                                       if (sb.length() == 0) {
-                                                               sb.append("--help");
+                                                       
+                                               } finally {
+                                                       aafcli.close();
+                       
+                                                       // Don't close if No Reader, or it's a Reader of Standard In
+                                                       if (rdr != null && !(rdr instanceof InputStreamReader)) {
+                                                               rdr.close();
                                                        }
-                                                       rv = aafcli.eval(sb.toString()) ? 0 : 1;
-                                               }
-                                               
-                                       } finally {
-                                               aafcli.close();
-               
-                                               // Don't close if No Reader, or it's a Reader of Standard In
-                                               if (rdr != null && !(rdr instanceof InputStreamReader)) {
-                                                       rdr.close();
                                                }
                                        }
                                }
-                               aafsso.writeFiles();
                        } finally {
                                aafsso.close();
+                               StringBuilder err = aafsso.err();
+                               if (err != null) {
+                                       err.append("to continue...");
+                                       System.err.println(err);
+                               }
                        }
-                       
+                       if(noexit==null) {
+                               return;
+                       }
+
+
                } catch (MessageException e) {
                        System.out.println("MessageException caught");
 
                        System.err.println(e.getMessage());
-               } catch (Throwable e) {
+               } catch (Exception e) {
                        e.printStackTrace(System.err);
                }
                System.exit(rv);
@@ -623,7 +620,7 @@ public class AAFcli {
        }
 
        public String typeString(Class<?> cls, boolean json) {
-               return "application/" + cls.getSimpleName() + "+" + (json ? "json" : "xml") + ";version=" + hman.apiVersion();
+               return "application/" + cls.getSimpleName() + "+" + (json ? "json" : "xml");//+ ";version=" + hman.apiVersion();
        }
 
        public String forceString() {