Add ONAP truststore and comment example client
[aaf/sms.git] / sms-client / java / src / main / java / org / onap / aaf / sms / SmsClient.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 package org.onap.aaf.sms;
18
19 import javax.net.ssl.SSLSocketFactory;
20 import java.net.URL;
21 import javax.net.ssl.HttpsURLConnection;
22 import org.onap.aaf.sms.SmsResponse;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.io.InputStreamReader;
26 import java.io.BufferedReader;
27 import java.io.OutputStreamWriter;
28 import java.util.Map;
29 import java.util.HashMap;
30 import java.util.Iterator;
31 import java.util.List;
32 import java.util.ArrayList;
33 import org.json.JSONArray;
34 import org.json.JSONException;
35 import org.json.JSONObject;
36
37 public class SmsClient implements SmsInterface {
38
39     private String baset;
40     private SSLSocketFactory ssf;
41
42     public SmsClient(String host, int port, SSLSocketFactory s) {
43         baset = "https://"+ host + ":" + port + "/v1/sms";
44         ssf = s;
45     }
46     public SmsClient(String host, int port, String version, SSLSocketFactory s) {
47         baset = "https://"+ host + ":" + port + "/" + version + "/sms";
48         ssf = s;
49     }
50
51     private  Map<String, Object> getSubmap(Map<String, Object> raw, String k) {
52         Object v = raw.get(k);
53         if ( v != null ) {
54             Map<String, Object> r = (Map<String, Object>)v;
55             return(r);
56         }
57         else {
58             return(null);
59         }
60     }
61
62     private List<Object> jsontolist(JSONArray a) throws JSONException {
63         List<Object> l = new ArrayList<Object>();
64         for(int i=0;i<a.length();i++) {
65             Object v = a.get(i);
66             if ( v instanceof JSONArray ) {
67                 v = jsontolist((JSONArray) v);
68             } else if (v instanceof JSONObject) {
69                 v = jsontomap((JSONObject) v);
70             }
71             l.add(v);
72         }
73         return(l);
74     }
75
76     private Map<String, Object> jsontomap(JSONObject j) throws JSONException {
77         Map<String, Object> m = new HashMap<String, Object>();
78
79         Iterator<?> ks = j.keys();
80         while( ks.hasNext() ) {
81             String k = (String)ks.next();
82             Object v = j.get(k);
83
84             if ( v instanceof JSONArray ) {
85                 v = jsontolist((JSONArray) v);
86             } else if ( v instanceof JSONObject ) {
87                 v = jsontomap((JSONObject) v);
88             }
89             m.put(k, v);
90         }
91         return(m);
92     }
93
94     protected Map<String, Object> strtomap(String r) throws JSONException {
95         JSONObject jobj = null;
96
97         jobj = new JSONObject(r);
98         return(jsontomap(jobj));
99
100     }
101
102     /*
103         Inputs reqtype - type of Request, POST, GET, DELETE, PUT
104                urlstr  - url to connect to
105                body    - json encoded data being sent to SMS server
106                output  - expect a response data from SMS server
107         Return SmsResponse Object
108             success or failure
109             response code if connection succeeded, otherwise -1
110             response string if expected.
111     */
112     protected SmsResponse execute(String reqtype, String urlstr, String body,
113         boolean output) {
114
115         HttpsURLConnection conn;
116         int errorcode = -1;
117         SmsResponse resp = new SmsResponse();
118
119         try {
120             URL url = new URL(urlstr);
121             conn = (HttpsURLConnection)url.openConnection();
122             conn.setSSLSocketFactory(ssf);
123             conn.setRequestMethod(reqtype);
124             conn.setRequestProperty("Content-Type", "application/json");
125             conn.setRequestProperty("Accept", "application/json");
126
127             // If we have any data in body write it out
128             if ( body != null ) {
129                 conn.setDoOutput(true);
130                 // Implicitly connects and writes data
131                 OutputStream out = conn.getOutputStream();
132                 OutputStreamWriter wr = new OutputStreamWriter(out);
133                 wr.write(body);
134                 wr.flush();
135                 wr.close();
136             }
137
138             // Parse the response from Server here
139             // An implicit connection happens here
140             errorcode = conn.getResponseCode();
141             if ( output && errorcode > 0 ) {
142                 InputStream inputStream;
143                 if ( errorcode/100 == 2) {
144                     inputStream = conn.getInputStream();
145                 } else {
146                     inputStream = conn.getErrorStream();
147                 }
148
149                 InputStreamReader inputstreamreader = new InputStreamReader(inputStream);
150                 BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
151
152                 String response;
153                 String save = "";
154                 while ((response = bufferedreader.readLine()) != null) {
155                     save = save + response;
156                 }
157                 if ( !save.isEmpty() ) {
158                     if ( errorcode/100 == 2) {
159                         resp.setResponse(strtomap(save));
160                     } else {
161                         resp.setErrorMessage(save);
162                     }
163                 }
164             }
165         } catch ( Exception e ) {
166             resp.setResponseCode(errorcode);
167             return(resp);
168         }
169         resp.setResponseCode(errorcode);
170         return resp;
171     }
172     @Override
173     public SmsResponse createDomain(String dname) {
174
175         String t = baset + "/domain";
176         String input = "{\"name\":\"" + dname + "\"}";
177
178         SmsResponse resp = execute("POST", t, input, true);
179         int errcode = resp.getResponseCode();
180
181         if ( errcode > 0 && errcode/100 == 2 )
182             resp.setSuccess(true);
183         else
184             resp.setSuccess(false);
185
186         return(resp);
187     }
188     @Override
189     public SmsResponse deleteDomain(String dname) {
190
191         String t = baset + "/domain/" + dname;
192
193         SmsResponse resp = execute("DELETE", t, null, true);
194         int errcode = resp.getResponseCode();
195
196         if ( errcode > 0 && errcode/100 == 2 )
197             resp.setSuccess(true);
198         else
199             resp.setSuccess(false);
200
201         return(resp);
202     }
203     @Override
204     public SmsResponse storeSecret(String dname, String sname, Map<String, Object> values) {
205
206         String t = baset + "/domain/" + dname + "/secret";
207         Map<String, Object> cm = new HashMap<String, Object>();
208         cm.put("name", sname);
209         cm.put("values", values);
210         JSONObject jobj = new JSONObject(cm);
211
212         SmsResponse resp = execute("POST", t, jobj.toString(), false);
213         int errcode = resp.getResponseCode();
214
215         if ( errcode > 0 && errcode/100 == 2 )
216             resp.setSuccess(true);
217         else
218             resp.setSuccess(false);
219
220         return(resp);
221     }
222     @Override
223     public SmsResponse getSecretNames(String dname) {
224
225         String t = baset + "/domain/" + dname + "/secret";
226
227         SmsResponse resp = execute("GET", t, null, true);
228         int errcode = resp.getResponseCode();
229
230         if ( errcode > 0 && errcode/100 == 2 )
231             resp.setSuccess(true);
232         else
233             resp.setSuccess(false);
234
235         return(resp);
236     }
237     @Override
238     public SmsResponse getSecret(String dname, String sname) {
239
240         String t = baset + "/domain/" + dname + "/secret/" + sname;
241
242         SmsResponse resp = execute("GET", t, null, true);
243         int errcode = resp.getResponseCode();
244
245         if ( errcode > 0 && errcode/100 == 2 ) {
246             Map<String, Object> m = getSubmap(resp.getResponse(), "values");
247             if ( m != null ) {
248                 resp.setSuccess(true);
249                 resp.setResponse(m);
250             }
251             else {
252                 resp.setSuccess(false);
253             }
254         }
255         else {
256             resp.setSuccess(false);
257         }
258
259         return(resp);
260
261     }
262     @Override
263     public SmsResponse deleteSecret(String dname, String sname) {
264
265         String t = baset + "/domain/" + dname + "/secret/" + sname;
266
267         SmsResponse resp = execute("DELETE", t, null, true);
268         int errcode = resp.getResponseCode();
269
270         if ( errcode > 0 && errcode/100 == 2 )
271             resp.setSuccess(true);
272         else
273             resp.setSuccess(false);
274
275         return(resp);
276     }
277 }