Adding some comments and small refactoring
[aaf/sms.git] / sms-client / java / src / main / example / SmsClientExample.java
1 /*
2  * Copyright 2018 Intel Corporation, Inc
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 import java.io.FileInputStream;
18 import java.lang.Boolean;
19 import java.lang.Integer;
20 import java.net.URL;
21 import javax.net.ssl.HttpsURLConnection;
22 import javax.net.ssl.KeyManagerFactory;
23 import javax.net.ssl.SSLContext;
24 import javax.net.ssl.SSLSessionContext;
25 import javax.net.ssl.SSLSocketFactory;
26 import javax.net.ssl.TrustManagerFactory;
27 import java.security.KeyStore;
28 import java.security.Provider;
29 import java.security.SecureRandom;
30 import java.security.Security;
31 import java.util.HashMap;
32 import java.util.Map;
33 import org.onap.aaf.sms.SmsClient;
34 import org.onap.aaf.sms.SmsResponse;
35
36 /*
37  * Sample application demonstrating various operations related
38  * Secret Management Service's APIs
39  */
40
41 public class SmsClientExample {
42     public static void main(String[] args) throws Exception {
43         // Set up the Sun PKCS 11 provider
44         Provider p = Security.getProvider("SunPKCS11-pkcs11Test");
45         if (p==null) {
46             throw new RuntimeException("could not get security provider");
47         }
48
49         // Load the key store
50         char[] pin = "45789654".toCharArray();
51         KeyStore keyStore = KeyStore.getInstance("PKCS11", p);
52         keyStore.load(null, pin);
53
54         // Load the CA certificate
55         FileInputStream tst = new FileInputStream("/ca.jks");
56         KeyStore trustStore = KeyStore.getInstance("JKS");
57         trustStore.load(tst, pin);
58
59         KeyManagerFactory keyManagerFactory =
60              KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
61         //Add to keystore to key manager
62         keyManagerFactory.init(keyStore, pin);
63
64         TrustManagerFactory trustManagerFactory =
65              TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
66         trustManagerFactory.init(trustStore);
67
68         //Create the context
69         SSLContext context = SSLContext.getInstance("TLS");
70         context.init(keyManagerFactory.getKeyManagers(),
71              trustManagerFactory.getTrustManagers(), new SecureRandom());
72         //Create a socket factory
73         SSLSocketFactory ssf = context.getSocketFactory();
74         SmsClient sms = new SmsClient("onap.mydomain.com", 10443, ssf);
75         SmsResponse resp1 = sms.createDomain("onap.new.test.sms0");
76         if ( resp1.getSuccess() ) {
77             System.out.println(resp1.getResponse());
78             System.out.println(resp1.getResponseCode());
79         }
80         Map<String, Object> m1 = new HashMap<String, Object>();
81         m1.put("passwd", "gax6ChD0yft");
82         SmsResponse resp2 = sms.storeSecret("onap.new.test.sms0", "testsec",  m1);
83         if ( resp2.getSuccess() ) {
84             System.out.println(resp2.getResponse());
85             System.out.println(resp2.getResponseCode());
86         }
87         Map<String, Object> m2 = new HashMap<String, Object>();
88         m2.put("username", "dbuser");
89         m2.put("isadmin", new Boolean(true));
90         m2.put("age", new Integer(40));
91         m2.put("secretkey", "asjdhkuhioeukadfjsadnfkjhsdukfhaskdjhfasdf");
92         m2.put("token", "2139084553458973452349230849234234908234342");
93         SmsResponse resp3 = sms.storeSecret("onap.new.test.sms0","credentials", m2);
94         if ( resp3.getSuccess() ) {
95             System.out.println(resp3.getResponse());
96             System.out.println(resp3.getResponseCode());
97         }
98         SmsResponse resp4 = sms.getSecretNames("onap.new.test.sms0");
99         if ( resp4.getSuccess() ) {
100             System.out.println(resp4.getResponse());
101             System.out.println(resp4.getResponseCode());
102         }
103         SmsResponse resp5= sms.getSecret("onap.new.test.sms0", "testsec");
104         if ( resp5.getSuccess() ) {
105             System.out.println(resp5.getResponse());
106             System.out.println(resp5.getResponseCode());
107         }
108         SmsResponse resp6= sms.getSecret("onap.new.test.sms0", "credentials");
109         if ( resp6.getSuccess() ) {
110             Boolean b = (Boolean)resp6.getResponse().get("isadmin");
111             if ( b )
112                 System.out.println("Age=" + (Integer)resp6.getResponse().get("age"));
113             System.out.println(resp6.getResponse());
114             System.out.println(resp6.getResponseCode());
115         }
116         SmsResponse resp7=sms.deleteDomain("onap.new.test.sms0");
117         if ( resp7.getSuccess() ) {
118             System.out.println(resp7.getResponse());
119             System.out.println(resp7.getResponseCode());
120         }
121     }
122 }