Adding some comments and small refactoring 31/48131/2
authorKiran Kamineni <kiran.k.kamineni@intel.com>
Thu, 17 May 2018 23:09:12 +0000 (16:09 -0700)
committerKiran Kamineni <kiran.k.kamineni@intel.com>
Mon, 21 May 2018 19:03:45 +0000 (12:03 -0700)
Added some comments to code
Also, refactored some things in the client

Issue-ID: AAF-92
Change-Id: I7166f8e47c31725b443662ad28e040d7ff26738d
Signed-off-by: Kiran Kamineni <kiran.k.kamineni@intel.com>
sms-client/java/pom.xml
sms-client/java/src/main/example/SmsClientExample.java
sms-client/java/src/main/java/org/onap/aaf/sms/SmsClient.java
sms-client/java/src/test/java/org/onap/aaf/sms/SmsTest.java

index e060feb..f178e55 100644 (file)
@@ -4,7 +4,7 @@
     <groupId>org.onap.aaf.sms</groupId>
     <artifactId>sms-client</artifactId>
     <packaging>jar</packaging>
-    <version>1.0.0-SNAPSHOT</version>
+    <version>2.0.0-SNAPSHOT</version>
     <name>sms-client</name>
 
     <properties>
index dc0e776..ca38adb 100644 (file)
@@ -33,6 +33,11 @@ import java.util.Map;
 import org.onap.aaf.sms.SmsClient;
 import org.onap.aaf.sms.SmsResponse;
 
+/*
+ * Sample application demonstrating various operations related
+ * Secret Management Service's APIs
+ */
+
 public class SmsClientExample {
     public static void main(String[] args) throws Exception {
         // Set up the Sun PKCS 11 provider
@@ -66,7 +71,6 @@ public class SmsClientExample {
              trustManagerFactory.getTrustManagers(), new SecureRandom());
         //Create a socket factory
         SSLSocketFactory ssf = context.getSocketFactory();
-        SSLSessionContext sessCtx = context.getServerSessionContext();
         SmsClient sms = new SmsClient("onap.mydomain.com", 10443, ssf);
         SmsResponse resp1 = sms.createDomain("onap.new.test.sms0");
         if ( resp1.getSuccess() ) {
index 17a9f16..8bc88fe 100644 (file)
@@ -98,29 +98,45 @@ public class SmsClient implements SmsInterface {
         return(jsontomap(jobj));
 
     }
-    protected SmsResponse execute(String reqtype, String t, String ins, boolean input, boolean output) {
+
+    /*
+        Inputs reqtype - type of Request, POST, GET, DELETE, PUT
+               urlstr  - url to connect to
+               body    - json encoded data being sent to SMS server
+               output  - expect a response data from SMS server
+        Return SmsResponse Object
+            success or failure
+            response code if connection succeeded, otherwise -1
+            response string if expected.
+    */
+    protected SmsResponse execute(String reqtype, String urlstr, String body,
+        boolean output) {
 
         HttpsURLConnection conn;
         int errorcode = -1;
         SmsResponse resp = new SmsResponse();
 
         try {
-            URL url = new URL(t);
+            URL url = new URL(urlstr);
             conn = (HttpsURLConnection)url.openConnection();
             conn.setSSLSocketFactory(ssf);
             conn.setRequestMethod(reqtype);
-            conn.setDoOutput(true);
-            conn.setDoInput(true);
             conn.setRequestProperty("Content-Type", "application/json");
             conn.setRequestProperty("Accept", "application/json");
 
-            if ( input ) {
+            // If we have any data in body write it out
+            if ( body != null ) {
+                conn.setDoOutput(true);
+                // Implicitly connects and writes data
                 OutputStream out = conn.getOutputStream();
                 OutputStreamWriter wr = new OutputStreamWriter(out);
-                wr.write(ins);
+                wr.write(body);
                 wr.flush();
                 wr.close();
             }
+
+            // Parse the response from Server here
+            // An implicit connection happens here
             errorcode = conn.getResponseCode();
             if ( output && errorcode > 0 ) {
                 InputStream inputstream = conn.getInputStream();
@@ -154,7 +170,7 @@ public class SmsClient implements SmsInterface {
         String t = baset + "/domain";
         String input = "{\"name\":\"" + dname + "\"}";
 
-        SmsResponse resp = execute("POST", t, input, true, true);
+        SmsResponse resp = execute("POST", t, input, true);
         int errcode = resp.getResponseCode();
 
         if ( errcode > 0 && errcode/100 == 2 )
@@ -169,7 +185,7 @@ public class SmsClient implements SmsInterface {
 
         String t = baset + "/domain/" + dname;
 
-        SmsResponse resp = execute("DELETE", t, null, false, true);
+        SmsResponse resp = execute("DELETE", t, null, true);
         int errcode = resp.getResponseCode();
 
         if ( errcode > 0 && errcode/100 == 2 )
@@ -188,7 +204,7 @@ public class SmsClient implements SmsInterface {
         cm.put("values", values);
         JSONObject jobj = new JSONObject(cm);
 
-        SmsResponse resp = execute("POST", t, jobj.toString(), true, false);
+        SmsResponse resp = execute("POST", t, jobj.toString(), false);
         int errcode = resp.getResponseCode();
 
         if ( errcode > 0 && errcode/100 == 2 )
@@ -203,7 +219,7 @@ public class SmsClient implements SmsInterface {
 
         String t = baset + "/domain/" + dname + "/secret";
 
-        SmsResponse resp = execute("GET", t, null, false, true);
+        SmsResponse resp = execute("GET", t, null, true);
         int errcode = resp.getResponseCode();
 
         if ( errcode > 0 && errcode/100 == 2 )
@@ -218,7 +234,7 @@ public class SmsClient implements SmsInterface {
 
         String t = baset + "/domain/" + dname + "/secret/" + sname;
 
-        SmsResponse resp = execute("GET", t, null, false, true);
+        SmsResponse resp = execute("GET", t, null, true);
         int errcode = resp.getResponseCode();
 
         if ( errcode > 0 && errcode/100 == 2 ) {
@@ -243,7 +259,7 @@ public class SmsClient implements SmsInterface {
 
         String t = baset + "/domain/" + dname + "/secret/" + sname;
 
-        SmsResponse resp = execute("DELETE", t, null, false, true);
+        SmsResponse resp = execute("DELETE", t, null, true);
         int errcode = resp.getResponseCode();
 
         if ( errcode > 0 && errcode/100 == 2 )
index 5277557..fad2579 100644 (file)
@@ -43,7 +43,7 @@ public class SmsTest extends SmsClient {
     public SmsTest(String host, int port, String version, SSLSocketFactory s) {
         super(host, port, version, s);
     }
-    public  SmsResponse execute(String reqtype, String t, String ins, boolean input, boolean output) {
+    public  SmsResponse execute(String reqtype, String t, String ins, boolean output) {
         Map<String, Object> m;
         SmsResponse resp = new SmsResponse();
         System.out.println(t);