Merge "Function.java-sonar fix"
[aaf/authz.git] / cadi / aaf / src / main / java / org / onap / aaf / cadi / aaf / client / Examples.java
index 0cf48de..308fcc8 100644 (file)
@@ -7,9 +7,9 @@
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -63,67 +63,81 @@ public class Examples {
         String className = null;
         String version = null;
         TYPE type = TYPE.JSON; // default
-        if(nameOrContentType.startsWith("application/")) {
-            for(String ct : nameOrContentType.split("\\s*,\\s*")) {
-                for(String elem : ct.split("\\s*;\\s*")) {
-                    if(elem.endsWith("+json")) {
+        if (nameOrContentType.startsWith("application/")) {
+            for (String ct : nameOrContentType.split("\\s*,\\s*")) {
+                for (String elem : ct.split("\\s*;\\s*")) {
+                    if (elem.endsWith("+json")) {
                         type = TYPE.JSON;
                         className = elem.substring(elem.indexOf('/')+1, elem.length()-5);
-                    } else if(elem.endsWith("+xml")) {
+                    } else if (elem.endsWith("+xml")) {
                         type = TYPE.XML;
                         className = elem.substring(elem.indexOf('/')+1, elem.length()-4);
-                    } else if(elem.startsWith("version=")) {
+                    } else if (elem.startsWith("version=")) {
                         version = elem.substring(8);
                     }
                 }
-                if(className!=null && version!=null)
+                if (className!=null && version!=null)
                     break;
             }
-            if(className==null) {
+            if (className==null) {
                 throw new APIException(nameOrContentType + " does not contain Class Information");
             }
         } else {
             className = nameOrContentType;
         }
-        
+
         // No Void.class in aaf.v2_0 package causing errors when trying to use a newVoidv2_0
         // method similar to others in this class. This makes it work, but is it right?
         if ("Void".equals(className))
             return "";
-                
-        if("1.1".equals(version)) {
+
+        if ("1.1".equals(version)) {
             version = "v1_0";
-        } else if(version!=null) {
+        } else if (version!=null) {
             version = "v" + version.replace('.', '_');
         } else {
             version = "v2_0";
         }
-        
-        Class<?> cls;
-        try {
-            cls = Examples.class.getClassLoader().loadClass("aaf."+version+'.'+className);
-        } catch (ClassNotFoundException e) {
-            throw new APIException(e);
+
+        Class<?> cls=null;
+        int minorIdx = version.indexOf('_');
+        if(minorIdx<0) {
+            throw new APIException("Invalid Interface Version " + version);
         }
-        
+        int minor = Integer.parseInt(version.substring(minorIdx+1));
+        String vprefix=version.substring(0, minorIdx+1);
+        while(cls==null && minor>=0) {
+            try {
+                cls = Examples.class.getClassLoader().loadClass("aaf."+vprefix+minor+'.'+className);
+            } catch (ClassNotFoundException e) {
+                if(--minor<0) {
+                    throw new APIException("No Example for Version " + version + " found.");
+                }
+            }
+        }
+
+        if(cls==null) {
+            throw new APIException("ERROR: " + "aaf."+vprefix+"X not found.");
+        }
+
         Method meth;
         try {
-            meth = Examples.class.getDeclaredMethod("new"+cls.getSimpleName()+version,boolean.class);
+            meth = Examples.class.getDeclaredMethod("new"+cls.getSimpleName()+vprefix+minor,boolean.class);
         } catch (Exception e) {
             throw new APIException("ERROR: " + cls.getName() + " does not have an Example in Code.  Request from AAF Developers");
         }
-        
+
         RosettaDF<C> df = env.newDataFactory(cls);
         df.option(Data.PRETTY);
-        
+
         Object data = meth.invoke(null,optional);
-        
+
         @SuppressWarnings("unchecked")
         String rv = df.newData().load((C)data).out(type).asString();
 //        Object obj = df.newData().in(type).load(rv).asObject();
         return rv;
     }
-    
+
     /*
      *  Set Base Class Request (easier than coding over and over)
      */
@@ -133,9 +147,9 @@ public class Examples {
         gc.add(GregorianCalendar.MONTH, 6);
         req.setEnd(Chrono.timeStamp(gc));
 //        req.setForce("false");
-        
+
     }
-    
+
     @SuppressWarnings("unused")
     private static Request newRequestv2_0(boolean optional) {
         Request r = new Request();
@@ -151,10 +165,10 @@ public class Examples {
         pkey.setAction("myAction");
         rpr.setPerm(pkey);
         rpr.setRole("org.osaaf.myns.myrole");
-        if(optional)setOptional(rpr);
+        if (optional)setOptional(rpr);
         return rpr;
     }
-    
+
     @SuppressWarnings("unused")
     private static Roles newRolesv2_0(boolean optional) {
         Role r;
@@ -192,22 +206,22 @@ public class Examples {
 
         return rs;
     }
-    
-    
+
+
     @SuppressWarnings("unused")
     private static PermRequest newPermRequestv2_0(boolean optional) {
         PermRequest pr = new PermRequest();
         pr.setType("org.osaaf.myns.myType");
         pr.setInstance("myInstance");
         pr.setAction("myAction");
-        if(optional) {
+        if (optional) {
             pr.setDescription("Short and meaningful verbiage about the Permission");
-            
+
             setOptional(pr);
         }
         return pr;
     }
-    
+
     @SuppressWarnings("unused")
     private static Perm newPermv2_0(boolean optional) {
         Perm pr = new Perm();
@@ -217,7 +231,7 @@ public class Examples {
         pr.getRoles().add("org.osaaf.aaf.myRole");
         pr.getRoles().add("org.osaaf.aaf.myRole2");
         pr.setDescription("This is my description, and I'm sticking with it");
-        if(optional) {
+        if (optional) {
             pr.setDescription("Short and meaningful verbiage about the Permission");
         }
         return pr;
@@ -232,7 +246,7 @@ public class Examples {
         pr.setAction("myAction");
         return pr;
     }
-    
+
     @SuppressWarnings("unused")
     private static Perms newPermsv2_0(boolean optional) {
         Perms perms = new Perms();
@@ -254,18 +268,18 @@ public class Examples {
         p.getRoles().add("org.osaaf.myns.myRole2");
 
         return perms;
-        
+
     }
-    
+
     @SuppressWarnings("unused")
     private static UserRoleRequest newUserRoleRequestv2_0(boolean optional) {
         UserRoleRequest urr = new UserRoleRequest();
         urr.setRole("org.osaaf.myns.myRole");
         urr.setUser("ab1234@people.osaaf.org");
-        if(optional) setOptional(urr);
+        if (optional) setOptional(urr);
         return urr;
     }
-    
+
     @SuppressWarnings("unused")
     private static NsRequest newNsRequestv2_0(boolean optional) {
         NsRequest nr = new NsRequest();
@@ -273,43 +287,44 @@ public class Examples {
         nr.getResponsible().add("ab1234@people.osaaf.org");
         nr.getResponsible().add("cd5678@people.osaaf.org");
         nr.getAdmin().add("zy9876@people.osaaf.org");
-        nr.getAdmin().add("xw5432@people.osaaf.org");        
-        if(optional) {
+        nr.getAdmin().add("xw5432@people.osaaf.org");
+        if (optional) {
             nr.setDescription("This is my Namespace to set up");
             nr.setType("APP");
             setOptional(nr);
         }
         return nr;
     }
-    
-    
+
+
     @SuppressWarnings("unused")
     private static Nss newNssv2_0(boolean optional) {
         Ns ns;
-        
+
         Nss nss = new Nss();
-        nss.getNs().add(ns = new Nss.Ns());
+        ns = new Nss.Ns();
+        nss.getNs().add(ns);
         ns.setName("org.osaaf.myns");
         ns.getResponsible().add("ab1234@people.osaaf.org");
         ns.getResponsible().add("cd5678@people.osaaf.org");
         ns.getAdmin().add("zy9876@people.osaaf.org");
         ns.getAdmin().add("xw5432@people.osaaf.org");
         ns.setDescription("This is my Namespace to set up");
-        
-        nss.getNs().add(ns = new Nss.Ns());
+        ns = new Nss.Ns();
+        nss.getNs().add(ns);
         ns.setName("org.osaaf.myOtherNs");
         ns.getResponsible().add("ab1234@people.osaaf.org");
         ns.getResponsible().add("cd5678@people.osaaf.org");
         ns.getAdmin().add("zy9876@people.osaaf.org");
-        ns.getAdmin().add("xw5432@people.osaaf.org");        
-            
+        ns.getAdmin().add("xw5432@people.osaaf.org");
+
         return nss;
     }
     @SuppressWarnings("unused")
     private static RoleRequest newRoleRequestv2_0(boolean optional) {
         RoleRequest rr = new RoleRequest();
         rr.setName("org.osaaf.myns.myRole");
-        if(optional) {
+        if (optional) {
             rr.setDescription("This is my Role");
             setOptional(rr);
         }
@@ -320,7 +335,7 @@ public class Examples {
     private static CredRequest newCredRequestv2_0(boolean optional) {
         CredRequest cr = new CredRequest();
         cr.setId("myID@fully.qualified.domain");
-        if(optional) {
+        if (optional) {
             cr.setType(2);
             cr.setEntry("0x125AB256344CE");
         } else {
@@ -329,23 +344,23 @@ public class Examples {
 
         return cr;
     }
-    
+
     @SuppressWarnings("unused")
     private static Users newUsersv2_0(boolean optional) {
         User user;
-    
+
         Users users = new Users();
     user = new Users.User();
         users.getUser().add(user);
-        user.setId("ab1234@people.osaaf.org");    
+        user.setId("ab1234@people.osaaf.org");
         GregorianCalendar gc = new GregorianCalendar();
         user.setExpires(Chrono.timeStamp(gc));
 
     user = new Users.User();
         users.getUser().add(user);
-        user.setId("zy9876@people.osaaf.org");    
-        user.setExpires(Chrono.timeStamp(gc));    
-            
+        user.setId("zy9876@people.osaaf.org");
+        user.setExpires(Chrono.timeStamp(gc));
+
         return users;
     }
 
@@ -386,7 +401,7 @@ public class Examples {
         ur.setRole("com.test.myapp.myRole");
         ur.setExpires(Chrono.timeStamp());
         urs.getUserRole().add(ur);
-        
+
         ur = new UserRole();
         ur.setUser("yx4321");
         ur.setRole("com.test.yourapp.yourRole");
@@ -439,7 +454,7 @@ public class Examples {
         return a;
     }
 
-    
+
 
     @SuppressWarnings("unused")
     private static aaf.v2_0.Error newErrorv2_0(boolean optional) {